From 24d138319797b602848b0d387dcb91c736770d44 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Wed, 13 Oct 2021 13:47:27 +0800 Subject: [PATCH] *: fix race in hot region config (#4167) (#4170) * fix race in hot region config Signed-off-by: Ryan Leung * address the comment Signed-off-by: Ryan Leung Co-authored-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 08dbe5fa947..a0daacb0bb1 100644 --- a/server/schedulers/hot_region_config.go +++ b/server/schedulers/hot_region_config.go @@ -242,8 +242,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 } @@ -309,7 +309,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") } @@ -333,7 +333,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 @@ -343,6 +343,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),