@@ -27,20 +27,20 @@ import (
27
27
"sync"
28
28
"time"
29
29
30
- "vitess.io/vitess/go/constants/sidecar"
31
- "vitess.io/vitess/go/protoutil"
32
- "vitess.io/vitess/go/vt/proto/vtrpc"
33
- "vitess.io/vitess/go/vt/vterrors"
30
+ "golang.org/x/sync/semaphore"
34
31
32
+ "vitess.io/vitess/go/constants/sidecar"
35
33
"vitess.io/vitess/go/event"
34
+ "vitess.io/vitess/go/protoutil"
36
35
"vitess.io/vitess/go/trace"
37
36
"vitess.io/vitess/go/vt/concurrency"
38
37
"vitess.io/vitess/go/vt/key"
39
38
"vitess.io/vitess/go/vt/log"
39
+ topodatapb "vitess.io/vitess/go/vt/proto/topodata"
40
+ "vitess.io/vitess/go/vt/proto/vtrpc"
40
41
"vitess.io/vitess/go/vt/topo/events"
41
42
"vitess.io/vitess/go/vt/topo/topoproto"
42
-
43
- topodatapb "vitess.io/vitess/go/vt/proto/topodata"
43
+ "vitess.io/vitess/go/vt/vterrors"
44
44
)
45
45
46
46
const (
@@ -670,16 +670,30 @@ func (ts *Server) GetTabletsByShardCell(ctx context.Context, keyspace, shard str
670
670
}
671
671
}
672
672
673
+ // divide the concurrency limit by the number of cells. if there are more
674
+ // cells than the limit, default to concurrency of 1. A semaphore ensures
675
+ // the limit is not exceeded in this scenario.
676
+ sem := semaphore .NewWeighted (int64 (DefaultConcurrency ))
677
+ getConcurrency := DefaultConcurrency / len (cells )
678
+ if getConcurrency == 0 {
679
+ getConcurrency = 1
680
+ }
681
+
673
682
wg := sync.WaitGroup {}
674
683
mutex := sync.Mutex {}
675
684
rec := concurrency.AllErrorRecorder {}
676
685
tablets := make ([]* TabletInfo , 0 )
677
- concurrency := DefaultConcurrency / len (cells )
678
686
for _ , cell := range cells {
679
687
wg .Add (1 )
680
688
go func () {
689
+ if err := sem .Acquire (ctx , 1 ); err != nil {
690
+ rec .RecordError (vterrors .Wrap (err , fmt .Sprintf ("GetTabletsByCell for %v failed." , cell )))
691
+ return
692
+ }
693
+ defer sem .Release (1 )
694
+
681
695
t , err := ts .GetTabletsByCell (ctx , cell , & GetTabletsByCellOptions {
682
- Concurrency : concurrency ,
696
+ Concurrency : getConcurrency ,
683
697
Keyspace : keyspace ,
684
698
Shard : shard ,
685
699
})
0 commit comments