Skip to content

Commit f0e52a8

Browse files
committed
feat: improve shard chunking
1 parent 76be175 commit f0e52a8

2 files changed

Lines changed: 63 additions & 7 deletions

File tree

scheduler/scheduler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"math"
78
"sync"
89
"time"
910

@@ -305,18 +306,17 @@ func shardTableClients(tableClients []tableClient, shard *shard) []tableClient {
305306
if shard == nil || len(tableClients) == 0 {
306307
return tableClients
307308
}
309+
308310
num := int(shard.num)
309311
total := int(shard.total)
310-
chunkSize := len(tableClients) / total
311-
if chunkSize == 0 {
312-
chunkSize = 1
313-
}
312+
313+
var chunkSize int
314+
chunkSize = int(math.Ceil(float64(len(tableClients)) / float64(total)))
315+
314316
chunks := lo.Chunk(tableClients, chunkSize)
315317
if num > len(chunks) {
316318
return nil
317319
}
318-
if len(chunks) > total && num == total {
319-
return append(chunks[num-1], chunks[num]...)
320-
}
320+
321321
return chunks[num-1]
322322
}

scheduler/scheduler_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,18 @@ func Test_shardTableClients(t *testing.T) {
518518
shard: &shard{num: 1, total: 2},
519519
expected: []tableClient{},
520520
},
521+
{
522+
name: "invalid total number of shards",
523+
tableClients: []tableClient{
524+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
525+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
526+
},
527+
shard: &shard{num: 1, total: 0},
528+
expected: []tableClient{
529+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
530+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
531+
},
532+
},
521533
{
522534
name: "even shard 1 of 2",
523535
tableClients: []tableClient{
@@ -559,6 +571,7 @@ func Test_shardTableClients(t *testing.T) {
559571
expected: []tableClient{
560572
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
561573
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
574+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
562575
},
563576
},
564577
{
@@ -572,10 +585,53 @@ func Test_shardTableClients(t *testing.T) {
572585
},
573586
shard: &shard{num: 2, total: 2},
574587
expected: []tableClient{
588+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
589+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
590+
},
591+
},
592+
{
593+
name: "uneven split 1 of 3",
594+
tableClients: []tableClient{
595+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
596+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
597+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
598+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
599+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
600+
},
601+
shard: &shard{num: 1, total: 3},
602+
expected: []tableClient{
603+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
604+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
605+
},
606+
},
607+
{
608+
name: "uneven split 2 of 3",
609+
tableClients: []tableClient{
610+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
611+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
612+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
613+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
614+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
615+
},
616+
shard: &shard{num: 2, total: 3},
617+
expected: []tableClient{
618+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
619+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
620+
},
621+
},
622+
{
623+
name: "uneven split 3 of 3",
624+
tableClients: []tableClient{
625+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
626+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
575627
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
576628
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
577629
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
578630
},
631+
shard: &shard{num: 3, total: 3},
632+
expected: []tableClient{
633+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
634+
},
579635
},
580636
{
581637
name: "more shards than table clients",

0 commit comments

Comments
 (0)