diff --git a/go/vt/vtorc/logic/tablet_discovery.go b/go/vt/vtorc/logic/tablet_discovery.go index ba29dec564c..fb2382e8493 100644 --- a/go/vt/vtorc/logic/tablet_discovery.go +++ b/go/vt/vtorc/logic/tablet_discovery.go @@ -99,10 +99,10 @@ func hasShardInKeyRanges(shard string, keyRanges []*topodatapb.KeyRange) (bool, // getKeyspaceShardsToWatch converts the input clustersToWatch into a list of individual keyspace/shards. func getKeyspaceShardsToWatch() ([]*topo.KeyspaceShard, error) { var keyspaceShards []*topo.KeyspaceShard - keyspaceWatchKeyRanges := make(map[string][]*topodatapb.KeyRange) + keyspaceWatchKeyRanges := make(map[string][]*topodatapb.KeyRange, 0) for _, clusterToWatch := range clustersToWatch { var err error - var keyRange *topodatapb.KeyRange + keyRange := &topodatapb.KeyRange{} keyspace := clusterToWatch if strings.Contains(clusterToWatch, "/") { var shard string @@ -143,11 +143,11 @@ func getKeyspaceShardsToWatch() ([]*topo.KeyspaceShard, error) { log.Errorf("Failed to parse key ranges for shard %q: %+v", s, err) } else if found { keyspaceShardsMu.Lock() - defer keyspaceShardsMu.Unlock() keyspaceShards = append(keyspaceShards, &topo.KeyspaceShard{ Keyspace: keyspace, Shard: s, }) + keyspaceShardsMu.Unlock() } } return nil @@ -157,10 +157,6 @@ func getKeyspaceShardsToWatch() ([]*topo.KeyspaceShard, error) { return nil, err } - slices.SortStableFunc(keyspaceShards, func(a, b *topo.KeyspaceShard) int { - return strings.Compare(a.String(), b.String()) - }) - return keyspaceShards, nil } diff --git a/go/vt/vtorc/logic/tablet_discovery_test.go b/go/vt/vtorc/logic/tablet_discovery_test.go index c4aef5ba0eb..fce721aac58 100644 --- a/go/vt/vtorc/logic/tablet_discovery_test.go +++ b/go/vt/vtorc/logic/tablet_discovery_test.go @@ -19,6 +19,8 @@ package logic import ( "context" "fmt" + "slices" + "strings" "sync/atomic" "testing" "time" @@ -396,6 +398,13 @@ func TestGetKeyspaceShardsToWatch(t *testing.T) { clustersToWatch = testcase.clusters res, err := getKeyspaceShardsToWatch() + slices.SortStableFunc(res, func(a, b *topo.KeyspaceShard) int { + return strings.Compare(a.String(), b.String()) + }) + slices.SortStableFunc(testcase.expected, func(a, b *topo.KeyspaceShard) int { + return strings.Compare(a.String(), b.String()) + }) + assert.NoError(t, err) assert.ElementsMatch(t, testcase.expected, res) })