From 7c76c98da8161ccd125d74430757d1fda9571b98 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Mon, 27 Sep 2021 16:38:47 +0800 Subject: [PATCH] *: fix race in hot region config (#4167) * fix race in hot region config Signed-off-by: Ryan Leung * address the comment Signed-off-by: Ryan Leung --- server/schedulers/hot_region_config.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/schedulers/hot_region_config.go b/server/schedulers/hot_region_config.go index c8309cac995..cd43a38636b 100644 --- a/server/schedulers/hot_region_config.go +++ b/server/schedulers/hot_region_config.go @@ -243,8 +243,8 @@ func (conf *hotRegionSchedulerConfig) GetEnableForTiFlash() bool { } func (conf *hotRegionSchedulerConfig) SetEnableForTiFlash(enable bool) { - conf.RLock() - defer conf.RUnlock() + conf.Lock() + defer conf.Unlock() conf.EnableForTiFlash = enable } @@ -310,7 +310,7 @@ func (conf *hotRegionSchedulerConfig) handleSetConfig(w http.ResponseWriter, r * } newc, _ := json.Marshal(conf) if !bytes.Equal(oldc, newc) { - conf.persist() + conf.persistLocked() rd.Text(w, http.StatusOK, "success") } @@ -334,7 +334,7 @@ func (conf *hotRegionSchedulerConfig) handleSetConfig(w http.ResponseWriter, r * rd.Text(w, http.StatusBadRequest, "config item not found") } -func (conf *hotRegionSchedulerConfig) persist() error { +func (conf *hotRegionSchedulerConfig) persistLocked() error { data, err := schedule.EncodeConfig(conf) if err != nil { return err @@ -344,6 +344,8 @@ func (conf *hotRegionSchedulerConfig) persist() error { func (conf *hotRegionSchedulerConfig) checkQuerySupport(cluster opt.Cluster) bool { querySupport := cluster.IsFeatureSupported(versioninfo.HotScheduleWithQuery) + conf.Lock() + defer conf.Unlock() if querySupport != conf.lastQuerySupported { log.Info("query supported changed", zap.Bool("last-query-support", conf.lastQuerySupported),