Skip to content

Commit e26e887

Browse files
authored
Do not log config when the config is not changed (#694)
1 parent 26c66bc commit e26e887

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

lib/config/proxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package config
55

66
import (
77
"bytes"
8+
"maps"
89
"net"
910
"os"
1011
"path/filepath"
@@ -168,6 +169,7 @@ func NewConfig() *Config {
168169

169170
func (cfg *Config) Clone() *Config {
170171
newCfg := *cfg
172+
newCfg.Labels = maps.Clone(cfg.Labels)
171173
return &newCfg
172174
}
173175

lib/config/proxy_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,12 @@ func TestGetIPPort(t *testing.T) {
170170
require.Equal(t, cas.port, statusPort)
171171
}
172172
}
173+
174+
func TestCloneConfig(t *testing.T) {
175+
cfg := testProxyConfig
176+
cfg.Labels = map[string]string{"a": "b"}
177+
clone := cfg.Clone()
178+
require.Equal(t, cfg, *clone)
179+
cfg.Labels["c"] = "d"
180+
require.NotContains(t, clone.Labels, "c")
181+
}

pkg/manager/config/config.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ func (e *ConfigManager) reloadConfigFile(file string) error {
3434
// that are specified by users.
3535
func (e *ConfigManager) SetTOMLConfig(data []byte) (err error) {
3636
e.sts.Lock()
37-
defer func() {
38-
if err == nil {
39-
e.logger.Info("current config", zap.Any("cfg", e.sts.current))
40-
}
41-
e.sts.Unlock()
42-
}()
37+
defer e.sts.Unlock()
4338

4439
base := e.sts.current
4540
if base == nil {
@@ -62,16 +57,22 @@ func (e *ConfigManager) SetTOMLConfig(data []byte) (err error) {
6257
}
6358

6459
e.sts.current = base
60+
originalData := e.sts.data
6561
var buf bytes.Buffer
6662
if err = toml.NewEncoder(&buf).Encode(base); err != nil {
6763
return errors.WithStack(err)
6864
}
69-
e.sts.checksum = crc32.ChecksumIEEE(buf.Bytes())
70-
71-
for _, list := range e.sts.listeners {
72-
list <- base.Clone()
65+
newData := buf.Bytes()
66+
67+
// TiDB-Operator may set the same labels by the HTTP API periodically, don't notify the listeners every time.
68+
if originalData == nil || !bytes.Equal(originalData, newData) {
69+
e.sts.checksum = crc32.ChecksumIEEE(newData)
70+
e.sts.data = newData
71+
e.logger.Info("current config", zap.Any("cfg", e.sts.current))
72+
for _, list := range e.sts.listeners {
73+
list <- base.Clone()
74+
}
7375
}
74-
7576
return
7677
}
7778

pkg/manager/config/config_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"strings"
1011
"testing"
1112
"time"
1213

@@ -142,7 +143,7 @@ func TestConfigRemove(t *testing.T) {
142143
require.NoError(t, os.WriteFile(tmpcfg, []byte(`proxy.addr = "gg"`), 0644))
143144

144145
// check that re-watch still works
145-
require.Eventually(t, func() bool { return cfgmgr.GetConfig().Proxy.Addr == "gg" }, 3*time.Second, 100*time.Millisecond)
146+
require.Eventually(t, func() bool { return cfgmgr.GetConfig() != nil && cfgmgr.GetConfig().Proxy.Addr == "gg" }, 3*time.Second, 100*time.Millisecond)
146147

147148
// remove again but with a long sleep
148149
require.NoError(t, os.Remove(tmpcfg))
@@ -298,13 +299,19 @@ func TestFilePath(t *testing.T) {
298299
}
299300

300301
func TestChecksum(t *testing.T) {
301-
cfgmgr, _, _ := testConfigManager(t, "", "")
302+
cfgmgr, text, _ := testConfigManager(t, "", "")
303+
require.Equal(t, 1, strings.Count(text.String(), "current config"))
302304
c1 := cfgmgr.GetConfigChecksum()
303305
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`)))
306+
require.Equal(t, 2, strings.Count(text.String(), "current config"))
307+
// same config, shouldn't log it again
308+
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "gg"`)))
309+
require.Equal(t, 2, strings.Count(text.String(), "current config"))
304310
c2 := cfgmgr.GetConfigChecksum()
305311
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr = "vv"`)))
306312
c3 := cfgmgr.GetConfigChecksum()
307313
require.NoError(t, cfgmgr.SetTOMLConfig([]byte(`proxy.addr="gg"`)))
314+
require.Equal(t, 4, strings.Count(text.String(), "current config"))
308315
c4 := cfgmgr.GetConfigChecksum()
309316
require.Equal(t, c2, c4)
310317
require.NotEqual(t, c1, c2)

pkg/manager/config/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ type ConfigManager struct {
4242
kv *btree.BTreeG[KVValue]
4343

4444
checkFileInterval time.Duration
45-
fileContent []byte
45+
fileContent []byte // used to compare whether the config file has changed
4646
sts struct {
4747
sync.Mutex
4848
listeners []chan<- *config.Config
4949
current *config.Config
50+
data []byte // used to strictly compare whether the config has changed
5051
checksum uint32 // checksum of the unmarshalled toml
5152
}
5253
}

pkg/server/api/server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,8 @@ func (h *Server) attachLogger(c *gin.Context) {
174174
switch {
175175
case len(c.Errors) > 0:
176176
h.lg.Warn(path, fields...)
177-
case strings.Contains(path, "/debug"), strings.Contains(path, "/metrics"):
178-
h.lg.Debug(path, fields...)
179177
default:
180-
h.lg.Info(path, fields...)
178+
h.lg.Debug(path, fields...)
181179
}
182180
}
183181

0 commit comments

Comments
 (0)