Skip to content

Commit 8ed1123

Browse files
Dynamic modification of configuration files&&Slow log (#2103)
* Dynamic modification of configuration files.Check the correctness of the functionality. * Modified some specification issues. * Dynamically modify Codis configuration file and add slow query logs.
1 parent 000cb24 commit 8ed1123

8 files changed

Lines changed: 821 additions & 143 deletions

File tree

codis/cmd/proxy/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Options:
112112
if err := config.LoadFromFile(s); err != nil {
113113
log.PanicErrorf(err, "load config %s failed", s)
114114
}
115+
config.ConfigFileName = s
116+
log.Warnf("option --config = %s", s)
115117
}
116118
models.SetMaxSlotNum(config.MaxSlotNum)
117119
if s, ok := utils.Argument(d, "--host-admin"); ok {

codis/config/proxy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ session_keepalive_period = "75s"
9999
# Set session to be sensitive to failures. Default is false, instead of closing socket, proxy will send an error response to client.
100100
session_break_on_failure = false
101101

102+
# Slowlog-log-slower-than(us), from receive command to send response, 0 is allways print slow log
103+
slowlog_log_slower_than = 100000
104+
105+
# set the number of slowlog in memory, max len is 10000000. (0 to disable)
106+
slowlog_max_len = 128000
107+
102108
# Set metrics server (such as http://localhost:28000), proxy will report json formatted metrics to specified server in a predefined period.
103109
metrics_report_server = ""
104110
metrics_report_period = "1s"

codis/pkg/proxy/config.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ session_keepalive_period = "75s"
115115
# Set session to be sensitive to failures. Default is false, instead of closing socket, proxy will send an error response to client.
116116
session_break_on_failure = false
117117
118+
# Slowlog-log-slower-than(us), from receive command to send response, 0 is allways print slow log
119+
slowlog_log_slower_than = 100000
120+
121+
# set the number of slowlog in memory, max len is 10000000. (0 to disable)
122+
slowlog_max_len = 128000
123+
118124
# Set metrics server (such as http://localhost:28000), proxy will report json formatted metrics to specified server in a predefined period.
119125
metrics_report_server = ""
120126
metrics_report_period = "1s"
@@ -176,6 +182,9 @@ type Config struct {
176182
SessionKeepAlivePeriod timesize.Duration `toml:"session_keepalive_period" json:"session_keepalive_period"`
177183
SessionBreakOnFailure bool `toml:"session_break_on_failure" json:"session_break_on_failure"`
178184

185+
SlowlogLogSlowerThan int64 `toml:"slowlog_log_slower_than" json:"slowlog_log_slower_than"`
186+
SlowlogMaxLen int64 `toml:"slowlog_max_len" json:"slowlog_max_len"`
187+
179188
MetricsReportServer string `toml:"metrics_report_server" json:"metrics_report_server"`
180189
MetricsReportPeriod timesize.Duration `toml:"metrics_report_period" json:"metrics_report_period"`
181190
MetricsReportInfluxdbServer string `toml:"metrics_report_influxdb_server" json:"metrics_report_influxdb_server"`
@@ -186,6 +195,7 @@ type Config struct {
186195
MetricsReportStatsdServer string `toml:"metrics_report_statsd_server" json:"metrics_report_statsd_server"`
187196
MetricsReportStatsdPeriod timesize.Duration `toml:"metrics_report_statsd_period" json:"metrics_report_statsd_period"`
188197
MetricsReportStatsdPrefix string `toml:"metrics_report_statsd_prefix" json:"metrics_report_statsd_prefix"`
198+
ConfigFileName string `toml:"-" json:"config_file_name"`
189199
}
190200

191201
func NewDefaultConfig() *Config {
@@ -302,6 +312,13 @@ func (c *Config) Validate() error {
302312
return errors.New("invalid session_keepalive_period")
303313
}
304314

315+
if c.SlowlogLogSlowerThan < 0 {
316+
return errors.New("invalid slowlog_log_slower_than")
317+
}
318+
if c.SlowlogMaxLen < 0 {
319+
return errors.New("invalid slowlog_max_len")
320+
}
321+
305322
if c.MetricsReportPeriod < 0 {
306323
return errors.New("invalid metrics_report_period")
307324
}
@@ -311,5 +328,6 @@ func (c *Config) Validate() error {
311328
if c.MetricsReportStatsdPeriod < 0 {
312329
return errors.New("invalid metrics_report_statsd_period")
313330
}
331+
314332
return nil
315333
}

codis/pkg/proxy/mapper.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package proxy
66
import (
77
"bytes"
88
"hash/crc32"
9+
"strconv"
910
"strings"
1011

1112
"pika/codis/v2/pkg/proxy/redis"
@@ -228,6 +229,8 @@ func init() {
228229
{"SUNION", 0},
229230
{"SUNIONSTORE", FlagWrite},
230231
{"SYNC", FlagNotAllow},
232+
{"PCONFIG", 0},
233+
{"PSLOWLOG", 0},
231234
{"TIME", FlagNotAllow},
232235
{"TOUCH", FlagWrite},
233236
{"TTL", 0},
@@ -318,3 +321,30 @@ func getHashKey(multi []*redis.Resp, opstr string) []byte {
318321
}
319322
return nil
320323
}
324+
325+
func getWholeCmd(multi []*redis.Resp, cmd []byte) int {
326+
var (
327+
index = 0
328+
bytes = 0
329+
)
330+
for i := 0; i < len(multi); i++ {
331+
if index < len(cmd) {
332+
index += copy(cmd[index:], multi[i].Value)
333+
if i < len(multi)-i {
334+
index += copy(cmd[index:], []byte(" "))
335+
}
336+
}
337+
bytes += len(multi[i].Value)
338+
339+
if i == len(multi)-1 && index == len(cmd) {
340+
more := []byte("... " + strconv.Itoa(len(multi)) + " elements " + strconv.Itoa(bytes) + " bytes.")
341+
index = len(cmd) - len(more)
342+
if index < 0 {
343+
index = 0
344+
}
345+
index += copy(cmd[index:], more)
346+
break
347+
}
348+
}
349+
return index
350+
}

0 commit comments

Comments
 (0)