@@ -26,7 +26,6 @@ import (
2626 "strconv"
2727 "strings"
2828 "sync"
29- "sync/atomic"
3029 "time"
3130
3231 "github.com/patrickmn/go-cache"
@@ -54,31 +53,37 @@ var (
5453 instanceWriteSem = semaphore .NewWeighted (config .GetBackendWriteConcurrency ())
5554)
5655
57- var forgetAliases * cache.Cache
56+ var (
57+ forgetAliases * cache.Cache
58+ forgetAliasesOnce sync.Once
59+ )
5860
5961var (
6062 readTopologyInstanceCounter = stats .NewCounter ("InstanceReadTopology" , "Number of times an instance was read from the topology" )
6163 readInstanceCounter = stats .NewCounter ("InstanceRead" , "Number of times an instance was read" )
6264 currentErrantGTIDCount = stats .NewGaugesWithSingleLabel ("CurrentErrantGTIDCount" , "Number of errant GTIDs a vttablet currently has" , "TabletAlias" )
6365)
6466
65- var (
66- emptyQuotesRegexp = regexp .MustCompile (`^""$` )
67- cacheInitializationCompleted atomic.Bool
68- )
67+ var emptyQuotesRegexp = regexp .MustCompile (`^""$` )
6968
7069func init () {
7170 go initializeInstanceDao ()
7271}
7372
7473func initializeInstanceDao () {
7574 config .WaitForConfigurationToBeLoaded ()
76- InitializeForgetAliasesCache ()
75+ initForgetAliasesCache ()
76+ }
77+
78+ func initForgetAliasesCache () {
79+ forgetAliasesOnce .Do (func () {
80+ forgetAliases = cache .New (config .GetInstancePollTime ()* 3 , time .Second )
81+ })
7782}
7883
84+ // InitializeForgetAliasesCache ensures the forgetAliases cache is initialized.
7985func InitializeForgetAliasesCache () {
80- forgetAliases = cache .New (config .GetInstancePollTime ()* 3 , time .Second )
81- cacheInitializationCompleted .Store (true )
86+ initForgetAliasesCache ()
8287}
8388
8489// ExecDBWriteFunc chooses how to execute a write onto the database: whether synchronously or not
@@ -1105,6 +1110,7 @@ func UpdateInstanceLastAttemptedCheck(tabletAlias *topodatapb.TabletAlias) error
11051110
11061111// InstanceIsForgotten returns true if an instance was forgotten.
11071112func InstanceIsForgotten (tabletAlias * topodatapb.TabletAlias ) bool {
1113+ initForgetAliasesCache ()
11081114 tabletAliasString := topoproto .TabletAliasString (tabletAlias )
11091115 _ , found := forgetAliases .Get (tabletAliasString )
11101116 return found
@@ -1118,6 +1124,7 @@ func ForgetInstance(tabletAlias *topodatapb.TabletAlias) error {
11181124 log .Error (errMsg )
11191125 return errors .New (errMsg )
11201126 }
1127+ initForgetAliasesCache ()
11211128 tabletAliasString := topoproto .TabletAliasString (tabletAlias )
11221129 forgetAliases .Set (tabletAliasString , true , cache .DefaultExpiration )
11231130 log .Info (fmt .Sprintf ("Forgetting: %v" , tabletAliasString ))
0 commit comments