From 24983bac820bdea89cdcf3c7bc124428827c83b4 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Wed, 22 Nov 2023 11:49:11 +0800 Subject: [PATCH] fix race Signed-off-by: Ryan Leung --- pkg/mcs/scheduling/server/config/config.go | 4 ++-- pkg/mcs/scheduling/server/config/watcher.go | 4 ---- pkg/mcs/scheduling/server/server.go | 3 ++- tests/integrations/mcs/scheduling/config_test.go | 5 +++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/mcs/scheduling/server/config/config.go b/pkg/mcs/scheduling/server/config/config.go index bb8c35e0b53a..6c38f66dc686 100644 --- a/pkg/mcs/scheduling/server/config/config.go +++ b/pkg/mcs/scheduling/server/config/config.go @@ -234,7 +234,7 @@ type PersistConfig struct { } // NewPersistConfig creates a new PersistConfig instance. -func NewPersistConfig(cfg *Config) *PersistConfig { +func NewPersistConfig(cfg *Config, ttl *cache.TTLString) *PersistConfig { o := &PersistConfig{} o.SetClusterVersion(&cfg.ClusterVersion) o.schedule.Store(&cfg.Schedule) @@ -242,7 +242,7 @@ func NewPersistConfig(cfg *Config) *PersistConfig { // storeConfig will be fetched from TiKV by PD API server, // so we just set an empty value here first. o.storeConfig.Store(&sc.StoreConfig{}) - o.ttl = nil + o.ttl = ttl return o } diff --git a/pkg/mcs/scheduling/server/config/watcher.go b/pkg/mcs/scheduling/server/config/watcher.go index e4d03d10edf1..4ded93ceb1b3 100644 --- a/pkg/mcs/scheduling/server/config/watcher.go +++ b/pkg/mcs/scheduling/server/config/watcher.go @@ -24,7 +24,6 @@ import ( "github.com/coreos/go-semver/semver" "github.com/pingcap/log" - "github.com/tikv/pd/pkg/cache" sc "github.com/tikv/pd/pkg/schedule/config" "github.com/tikv/pd/pkg/schedule/schedulers" "github.com/tikv/pd/pkg/storage" @@ -155,9 +154,6 @@ func (cw *Watcher) initializeConfigWatcher() error { func (cw *Watcher) initializeTTLConfigWatcher() error { putFn := func(kv *mvccpb.KeyValue) error { - if cw.ttl == nil { - cw.ttl = cache.NewStringTTL(cw.ctx, time.Second*5, time.Minute*5) - } key := string(kv.Key)[len(sc.TTLConfigPrefix)+1:] value := string(kv.Value) leaseID := kv.Lease diff --git a/pkg/mcs/scheduling/server/server.go b/pkg/mcs/scheduling/server/server.go index c5b73dea5fc7..52fed8add90f 100644 --- a/pkg/mcs/scheduling/server/server.go +++ b/pkg/mcs/scheduling/server/server.go @@ -36,6 +36,7 @@ import ( "github.com/pingcap/sysutil" "github.com/spf13/cobra" bs "github.com/tikv/pd/pkg/basicserver" + "github.com/tikv/pd/pkg/cache" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/errs" "github.com/tikv/pd/pkg/mcs/discovery" @@ -534,7 +535,7 @@ func CreateServer(ctx context.Context, cfg *config.Config) *Server { BaseServer: server.NewBaseServer(ctx), DiagnosticsServer: sysutil.NewDiagnosticsServer(cfg.Log.File.Filename), cfg: cfg, - persistConfig: config.NewPersistConfig(cfg), + persistConfig: config.NewPersistConfig(cfg, cache.NewStringTTL(ctx, time.Second*5, time.Minute*5)), checkMembershipCh: make(chan struct{}, 1), } return svr diff --git a/tests/integrations/mcs/scheduling/config_test.go b/tests/integrations/mcs/scheduling/config_test.go index ccf7cdaf48cd..cf1528b47a33 100644 --- a/tests/integrations/mcs/scheduling/config_test.go +++ b/tests/integrations/mcs/scheduling/config_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/kvproto/pkg/metapb" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/tikv/pd/pkg/cache" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/mcs/scheduling/server/config" sc "github.com/tikv/pd/pkg/schedule/config" @@ -84,7 +85,7 @@ func (suite *configTestSuite) TestConfigWatch() { suite.ctx, suite.pdLeaderServer.GetEtcdClient(), suite.cluster.GetCluster().GetId(), - config.NewPersistConfig(config.NewConfig()), + config.NewPersistConfig(config.NewConfig(), cache.NewStringTTL(suite.ctx, time.Second*5, time.Minute*5)), endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil), ) re.NoError(err) @@ -144,7 +145,7 @@ func (suite *configTestSuite) TestSchedulerConfigWatch() { suite.ctx, suite.pdLeaderServer.GetEtcdClient(), suite.cluster.GetCluster().GetId(), - config.NewPersistConfig(config.NewConfig()), + config.NewPersistConfig(config.NewConfig(), cache.NewStringTTL(suite.ctx, time.Second*5, time.Minute*5)), storage, ) re.NoError(err)