From 36954bc1244fc36fbd1ece52a28174b88546fe41 Mon Sep 17 00:00:00 2001 From: djshow832 <873581766@qq.com> Date: Tue, 31 Jan 2023 15:25:54 +0800 Subject: [PATCH 1/2] add checksum --- lib/config/health.go | 2 +- pkg/manager/config/config.go | 25 +++++++++++++++++++++++-- pkg/manager/config/config_test.go | 15 +++++++++++++++ pkg/manager/config/manager.go | 1 + pkg/server/api/debug.go | 2 +- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/config/health.go b/lib/config/health.go index 5e76b0a0..31fe30df 100644 --- a/lib/config/health.go +++ b/lib/config/health.go @@ -15,5 +15,5 @@ package config type HealthInfo struct { - ConfigVersion uint32 `json:"config_version"` + ConfigChecksum uint32 `json:"config_checksum"` } diff --git a/pkg/manager/config/config.go b/pkg/manager/config/config.go index ca3318aa..de05da4e 100644 --- a/pkg/manager/config/config.go +++ b/pkg/manager/config/config.go @@ -15,11 +15,13 @@ package config import ( + "hash/crc32" "os" "time" "github.com/BurntSushi/toml" "github.com/fsnotify/fsnotify" + gotoml "github.com/pelletier/go-toml/v2" "github.com/pingcap/TiProxy/lib/config" "go.uber.org/zap" ) @@ -74,8 +76,9 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) error { return err } - e.sts.current = base - e.sts.version++ + if err := e.setCurrentConfig(base); err != nil { + return err + } for _, list := range e.sts.listeners { list <- base.Clone() @@ -84,6 +87,17 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) error { return nil } +func (e *ConfigManager) setCurrentConfig(cfg *config.Config) error { + e.sts.current = cfg + e.sts.version++ + bytes, err := gotoml.Marshal(cfg) + if err != nil { + return err + } + e.sts.checksum = crc32.ChecksumIEEE(bytes) + return nil +} + func (e *ConfigManager) GetConfig() *config.Config { e.sts.Lock() v := e.sts.current @@ -98,6 +112,13 @@ func (e *ConfigManager) GetConfigVersion() uint32 { return v } +func (e *ConfigManager) GetConfigChecksum() uint32 { + e.sts.Lock() + c := e.sts.checksum + e.sts.Unlock() + return c +} + func (e *ConfigManager) WatchConfig() <-chan *config.Config { ch := make(chan *config.Config) e.sts.Lock() diff --git a/pkg/manager/config/config_test.go b/pkg/manager/config/config_test.go index 421110ab..eb6395cf 100644 --- a/pkg/manager/config/config_test.go +++ b/pkg/manager/config/config_test.go @@ -163,3 +163,18 @@ func TestConfigRemove(t *testing.T) { require.NoError(t, os.WriteFile(tmpcfg, []byte(`proxy.addr = "vv"`), 0644)) require.Eventually(t, func() bool { return cfgmgr.GetConfig().Proxy.Addr == "vv" }, time.Second, 100*time.Millisecond) } + +func TestChecksum(t *testing.T) { + cfgmgr, _ := testConfigManager(t, "") + c1 := cfgmgr.GetConfigChecksum() + require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`))) + c2 := cfgmgr.GetConfigChecksum() + require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "vv"`))) + c3 := cfgmgr.GetConfigChecksum() + require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr="gg"`))) + c4 := cfgmgr.GetConfigChecksum() + require.Equal(t, c2, c4) + require.NotEqual(t, c1, c2) + require.NotEqual(t, c1, c3) + require.NotEqual(t, c2, c3) +} diff --git a/pkg/manager/config/manager.go b/pkg/manager/config/manager.go index 79804948..b077e9a1 100644 --- a/pkg/manager/config/manager.go +++ b/pkg/manager/config/manager.go @@ -57,6 +57,7 @@ type ConfigManager struct { listeners []chan<- *config.Config current *config.Config version uint32 + checksum uint32 } } diff --git a/pkg/server/api/debug.go b/pkg/server/api/debug.go index 463e57cd..46eefb4e 100644 --- a/pkg/server/api/debug.go +++ b/pkg/server/api/debug.go @@ -24,7 +24,7 @@ import ( func (h *HTTPServer) DebugHealth(c *gin.Context) { c.JSON(http.StatusOK, config.HealthInfo{ - ConfigVersion: h.mgr.cfg.GetConfigVersion(), + ConfigChecksum: h.mgr.cfg.GetConfigChecksum(), }) } From 0fda5944023ddcdc35e3974ae35921f4a7118594 Mon Sep 17 00:00:00 2001 From: djshow832 <873581766@qq.com> Date: Tue, 31 Jan 2023 21:51:21 +0800 Subject: [PATCH 2/2] address comments --- pkg/manager/config/config.go | 16 ++++------------ pkg/manager/config/manager.go | 1 - 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/pkg/manager/config/config.go b/pkg/manager/config/config.go index de05da4e..bb5e4c01 100644 --- a/pkg/manager/config/config.go +++ b/pkg/manager/config/config.go @@ -15,13 +15,13 @@ package config import ( + "bytes" "hash/crc32" "os" "time" "github.com/BurntSushi/toml" "github.com/fsnotify/fsnotify" - gotoml "github.com/pelletier/go-toml/v2" "github.com/pingcap/TiProxy/lib/config" "go.uber.org/zap" ) @@ -89,12 +89,11 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) error { func (e *ConfigManager) setCurrentConfig(cfg *config.Config) error { e.sts.current = cfg - e.sts.version++ - bytes, err := gotoml.Marshal(cfg) - if err != nil { + var buf bytes.Buffer + if err := toml.NewEncoder(&buf).Encode(cfg); err != nil { return err } - e.sts.checksum = crc32.ChecksumIEEE(bytes) + e.sts.checksum = crc32.ChecksumIEEE(buf.Bytes()) return nil } @@ -105,13 +104,6 @@ func (e *ConfigManager) GetConfig() *config.Config { return v } -func (e *ConfigManager) GetConfigVersion() uint32 { - e.sts.Lock() - v := e.sts.version - e.sts.Unlock() - return v -} - func (e *ConfigManager) GetConfigChecksum() uint32 { e.sts.Lock() c := e.sts.checksum diff --git a/pkg/manager/config/manager.go b/pkg/manager/config/manager.go index b077e9a1..27d4bab3 100644 --- a/pkg/manager/config/manager.go +++ b/pkg/manager/config/manager.go @@ -56,7 +56,6 @@ type ConfigManager struct { sync.Mutex listeners []chan<- *config.Config current *config.Config - version uint32 checksum uint32 } }