@@ -27,7 +27,6 @@ import (
2727 "time"
2828
2929 "github.com/spf13/pflag"
30-
3130 "google.golang.org/protobuf/encoding/prototext"
3231 "google.golang.org/protobuf/proto"
3332
@@ -70,30 +69,36 @@ func OpenTabletDiscovery() <-chan time.Time {
7069 if _ , err := db .ExecVTOrc ("delete from vitess_tablet" ); err != nil {
7170 log .Error (err )
7271 }
72+ // We refresh all information from the topo once before we start the ticks to do
73+ // it on a timer.
74+ ctx , cancel := context .WithTimeout (context .Background (), topo .RemoteOperationTimeout )
75+ defer cancel ()
76+ if err := refreshAllInformation (ctx ); err != nil {
77+ log .Errorf ("failed to initialize topo information: %+v" , err )
78+ }
7379 return time .Tick (time .Second * time .Duration (config .Config .TopoInformationRefreshSeconds )) //nolint SA1015: using time.Tick leaks the underlying ticker
7480}
7581
7682// refreshAllTablets reloads the tablets from topo and discovers the ones which haven't been refreshed in a while
77- func refreshAllTablets () {
78- refreshTabletsUsing (func (tabletAlias string ) {
83+ func refreshAllTablets (ctx context. Context ) error {
84+ return refreshTabletsUsing (ctx , func (tabletAlias string ) {
7985 DiscoverInstance (tabletAlias , false /* forceDiscovery */ )
8086 }, false /* forceRefresh */ )
8187}
8288
83- func refreshTabletsUsing (loader func (tabletAlias string ), forceRefresh bool ) {
89+ func refreshTabletsUsing (ctx context. Context , loader func (tabletAlias string ), forceRefresh bool ) error {
8490 if ! IsLeaderOrActive () {
85- return
91+ return nil
8692 }
8793 if len (clustersToWatch ) == 0 { // all known clusters
88- ctx , cancel := context .WithTimeout (context . Background () , topo .RemoteOperationTimeout )
94+ ctx , cancel := context .WithTimeout (ctx , topo .RemoteOperationTimeout )
8995 defer cancel ()
9096 cells , err := ts .GetKnownCells (ctx )
9197 if err != nil {
92- log .Error (err )
93- return
98+ return err
9499 }
95100
96- refreshCtx , refreshCancel := context .WithTimeout (context . Background () , topo .RemoteOperationTimeout )
101+ refreshCtx , refreshCancel := context .WithTimeout (ctx , topo .RemoteOperationTimeout )
97102 defer refreshCancel ()
98103 var wg sync.WaitGroup
99104 for _ , cell := range cells {
@@ -114,7 +119,7 @@ func refreshTabletsUsing(loader func(tabletAlias string), forceRefresh bool) {
114119 keyspaceShards = append (keyspaceShards , & topo.KeyspaceShard {Keyspace : input [0 ], Shard : input [1 ]})
115120 } else {
116121 // Assume this is a keyspace and find all shards in keyspace
117- ctx , cancel := context .WithTimeout (context . Background () , topo .RemoteOperationTimeout )
122+ ctx , cancel := context .WithTimeout (ctx , topo .RemoteOperationTimeout )
118123 defer cancel ()
119124 shards , err := ts .GetShardNames (ctx , ks )
120125 if err != nil {
@@ -133,9 +138,9 @@ func refreshTabletsUsing(loader func(tabletAlias string), forceRefresh bool) {
133138 }
134139 if len (keyspaceShards ) == 0 {
135140 log .Errorf ("Found no keyspaceShards for input: %+v" , clustersToWatch )
136- return
141+ return nil
137142 }
138- refreshCtx , refreshCancel := context .WithTimeout (context . Background () , topo .RemoteOperationTimeout )
143+ refreshCtx , refreshCancel := context .WithTimeout (ctx , topo .RemoteOperationTimeout )
139144 defer refreshCancel ()
140145 var wg sync.WaitGroup
141146 for _ , ks := range keyspaceShards {
@@ -147,6 +152,7 @@ func refreshTabletsUsing(loader func(tabletAlias string), forceRefresh bool) {
147152 }
148153 wg .Wait ()
149154 }
155+ return nil
150156}
151157
152158func refreshTabletsInCell (ctx context.Context , cell string , loader func (tabletAlias string ), forceRefresh bool ) {
0 commit comments