From 12643913eac46d5fa646af2d5032cf88e01b31ef Mon Sep 17 00:00:00 2001 From: Matt Lord Date: Fri, 26 Jan 2024 10:54:31 -0500 Subject: [PATCH] Make GetServingShards concurrent Signed-off-by: Matt Lord --- go/vt/topo/keyspace.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/go/vt/topo/keyspace.go b/go/vt/topo/keyspace.go index 4ff53e24204..b5e7b6b2274 100755 --- a/go/vt/topo/keyspace.go +++ b/go/vt/topo/keyspace.go @@ -19,11 +19,13 @@ package topo import ( "context" "path" + "sort" "sync" "golang.org/x/sync/errgroup" "vitess.io/vitess/go/constants/sidecar" + "vitess.io/vitess/go/vt/key" "vitess.io/vitess/go/vt/vterrors" "vitess.io/vitess/go/event" @@ -245,25 +247,26 @@ func (ts *Server) FindAllShardsInKeyspace(ctx context.Context, keyspace string, // GetServingShards returns all shards where the primary is serving. func (ts *Server) GetServingShards(ctx context.Context, keyspace string) ([]*ShardInfo, error) { - shards, err := ts.GetShardNames(ctx, keyspace) + shards, err := ts.FindAllShardsInKeyspace(ctx, keyspace, nil) if err != nil { return nil, vterrors.Wrapf(err, "failed to get list of shards for keyspace '%v'", keyspace) } result := make([]*ShardInfo, 0, len(shards)) for _, shard := range shards { - si, err := ts.GetShard(ctx, keyspace, shard) - if err != nil { - return nil, vterrors.Wrapf(err, "GetShard(%v, %v) failed", keyspace, shard) - } - if !si.IsPrimaryServing { + if !shard.IsPrimaryServing { continue } - result = append(result, si) + result = append(result, shard) } if len(result) == 0 { return nil, vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "%v has no serving shards", keyspace) } + // Sort the shards by KeyRange for deterministic results. + sort.Slice(result, func(i, j int) bool { + return key.KeyRangeLess(result[i].KeyRange, result[j].KeyRange) + }) + return result, nil }