Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config, api: add API for getting the config checksum #203

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/config/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
package config

type HealthInfo struct {
ConfigVersion uint32 `json:"config_version"`
ConfigChecksum uint32 `json:"config_checksum"`
}
25 changes: 23 additions & 2 deletions pkg/manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
djshow832 marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions pkg/manager/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions pkg/manager/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ConfigManager struct {
listeners []chan<- *config.Config
current *config.Config
version uint32
djshow832 marked this conversation as resolved.
Show resolved Hide resolved
checksum uint32
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down