Skip to content

Commit

Permalink
Dynamic modification of configuration files&&Slow log (#2103)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dingxiaoshuai123 authored Nov 8, 2023
1 parent 000cb24 commit 8ed1123
Show file tree
Hide file tree
Showing 8 changed files with 821 additions and 143 deletions.
2 changes: 2 additions & 0 deletions codis/cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Options:
if err := config.LoadFromFile(s); err != nil {
log.PanicErrorf(err, "load config %s failed", s)
}
config.ConfigFileName = s
log.Warnf("option --config = %s", s)
}
models.SetMaxSlotNum(config.MaxSlotNum)
if s, ok := utils.Argument(d, "--host-admin"); ok {
Expand Down
6 changes: 6 additions & 0 deletions codis/config/proxy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ session_keepalive_period = "75s"
# Set session to be sensitive to failures. Default is false, instead of closing socket, proxy will send an error response to client.
session_break_on_failure = false

# Slowlog-log-slower-than(us), from receive command to send response, 0 is allways print slow log
slowlog_log_slower_than = 100000

# set the number of slowlog in memory, max len is 10000000. (0 to disable)
slowlog_max_len = 128000

# Set metrics server (such as http://localhost:28000), proxy will report json formatted metrics to specified server in a predefined period.
metrics_report_server = ""
metrics_report_period = "1s"
Expand Down
18 changes: 18 additions & 0 deletions codis/pkg/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ session_keepalive_period = "75s"
# Set session to be sensitive to failures. Default is false, instead of closing socket, proxy will send an error response to client.
session_break_on_failure = false
# Slowlog-log-slower-than(us), from receive command to send response, 0 is allways print slow log
slowlog_log_slower_than = 100000
# set the number of slowlog in memory, max len is 10000000. (0 to disable)
slowlog_max_len = 128000
# Set metrics server (such as http://localhost:28000), proxy will report json formatted metrics to specified server in a predefined period.
metrics_report_server = ""
metrics_report_period = "1s"
Expand Down Expand Up @@ -176,6 +182,9 @@ type Config struct {
SessionKeepAlivePeriod timesize.Duration `toml:"session_keepalive_period" json:"session_keepalive_period"`
SessionBreakOnFailure bool `toml:"session_break_on_failure" json:"session_break_on_failure"`

SlowlogLogSlowerThan int64 `toml:"slowlog_log_slower_than" json:"slowlog_log_slower_than"`
SlowlogMaxLen int64 `toml:"slowlog_max_len" json:"slowlog_max_len"`

MetricsReportServer string `toml:"metrics_report_server" json:"metrics_report_server"`
MetricsReportPeriod timesize.Duration `toml:"metrics_report_period" json:"metrics_report_period"`
MetricsReportInfluxdbServer string `toml:"metrics_report_influxdb_server" json:"metrics_report_influxdb_server"`
Expand All @@ -186,6 +195,7 @@ type Config struct {
MetricsReportStatsdServer string `toml:"metrics_report_statsd_server" json:"metrics_report_statsd_server"`
MetricsReportStatsdPeriod timesize.Duration `toml:"metrics_report_statsd_period" json:"metrics_report_statsd_period"`
MetricsReportStatsdPrefix string `toml:"metrics_report_statsd_prefix" json:"metrics_report_statsd_prefix"`
ConfigFileName string `toml:"-" json:"config_file_name"`
}

func NewDefaultConfig() *Config {
Expand Down Expand Up @@ -302,6 +312,13 @@ func (c *Config) Validate() error {
return errors.New("invalid session_keepalive_period")
}

if c.SlowlogLogSlowerThan < 0 {
return errors.New("invalid slowlog_log_slower_than")
}
if c.SlowlogMaxLen < 0 {
return errors.New("invalid slowlog_max_len")
}

if c.MetricsReportPeriod < 0 {
return errors.New("invalid metrics_report_period")
}
Expand All @@ -311,5 +328,6 @@ func (c *Config) Validate() error {
if c.MetricsReportStatsdPeriod < 0 {
return errors.New("invalid metrics_report_statsd_period")
}

return nil
}
30 changes: 30 additions & 0 deletions codis/pkg/proxy/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proxy
import (
"bytes"
"hash/crc32"
"strconv"
"strings"

"pika/codis/v2/pkg/proxy/redis"
Expand Down Expand Up @@ -228,6 +229,8 @@ func init() {
{"SUNION", 0},
{"SUNIONSTORE", FlagWrite},
{"SYNC", FlagNotAllow},
{"PCONFIG", 0},
{"PSLOWLOG", 0},
{"TIME", FlagNotAllow},
{"TOUCH", FlagWrite},
{"TTL", 0},
Expand Down Expand Up @@ -318,3 +321,30 @@ func getHashKey(multi []*redis.Resp, opstr string) []byte {
}
return nil
}

func getWholeCmd(multi []*redis.Resp, cmd []byte) int {
var (
index = 0
bytes = 0
)
for i := 0; i < len(multi); i++ {
if index < len(cmd) {
index += copy(cmd[index:], multi[i].Value)
if i < len(multi)-i {
index += copy(cmd[index:], []byte(" "))
}
}
bytes += len(multi[i].Value)

if i == len(multi)-1 && index == len(cmd) {
more := []byte("... " + strconv.Itoa(len(multi)) + " elements " + strconv.Itoa(bytes) + " bytes.")
index = len(cmd) - len(more)
if index < 0 {
index = 0
}
index += copy(cmd[index:], more)
break
}
}
return index
}
Loading

0 comments on commit 8ed1123

Please sign in to comment.