Skip to content

Commit f1c0106

Browse files
feat: Improve shard chunking (#2163)
1 parent 76be175 commit f1c0106

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

scheduler/scheduler.go

Lines changed: 6 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,16 @@ 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+
chunkSize := int(math.Ceil(float64(len(tableClients)) / float64(total)))
314+
314315
chunks := lo.Chunk(tableClients, chunkSize)
315316
if num > len(chunks) {
316317
return nil
317318
}
318-
if len(chunks) > total && num == total {
319-
return append(chunks[num-1], chunks[num]...)
320-
}
319+
321320
return chunks[num-1]
322321
}

scheduler/scheduler_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ func Test_shardTableClients(t *testing.T) {
559559
expected: []tableClient{
560560
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
561561
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
562+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
562563
},
563564
},
564565
{
@@ -571,9 +572,52 @@ func Test_shardTableClients(t *testing.T) {
571572
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
572573
},
573574
shard: &shard{num: 2, total: 2},
575+
expected: []tableClient{
576+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
577+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
578+
},
579+
},
580+
{
581+
name: "uneven split 1 of 3",
582+
tableClients: []tableClient{
583+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
584+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
585+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
586+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
587+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
588+
},
589+
shard: &shard{num: 1, total: 3},
590+
expected: []tableClient{
591+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
592+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
593+
},
594+
},
595+
{
596+
name: "uneven split 2 of 3",
597+
tableClients: []tableClient{
598+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
599+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
600+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
601+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
602+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
603+
},
604+
shard: &shard{num: 2, total: 3},
574605
expected: []tableClient{
575606
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
576607
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
608+
},
609+
},
610+
{
611+
name: "uneven split 3 of 3",
612+
tableClients: []tableClient{
613+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_1"}},
614+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_2"}},
615+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_3"}},
616+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_4"}},
617+
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
618+
},
619+
shard: &shard{num: 3, total: 3},
620+
expected: []tableClient{
577621
{client: &testExecutionClient{}, table: &schema.Table{Name: "table_5"}},
578622
},
579623
},

0 commit comments

Comments
 (0)