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

Fix multiple values custom config #121

Merged
merged 2 commits into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions example/redisfailover/custom-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ spec:
- "maxclients 100"
- "hz 50"
- "timeout 60"
- "tcp-keepalive 60"
- "client-output-buffer-limit normal 0 0 0"
- "client-output-buffer-limit slave 1000000000 1000000000 0"
- "client-output-buffer-limit pubsub 33554432 8388608 60"
44 changes: 25 additions & 19 deletions service/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const (
redisPort = "6379"
sentinelPort = "26379"
masterName = "mymaster"
sentinelSetCommand = "SENTINEL set %s %s"
redisSetCommand = "CONFIG set %s"
)

var (
Expand Down Expand Up @@ -177,7 +175,7 @@ func (c *client) MonitorRedis(ip string, monitor string, quorum string) error {
defer rClient.Close()
cmd := rediscli.NewBoolCmd("SENTINEL", "REMOVE", masterName)
rClient.Process(cmd)
// We'll continue even if it fails, the priotity is to have the redises monitored
// We'll continue even if it fails, the priority is to have the redises monitored
cmd = rediscli.NewBoolCmd("SENTINEL", "MONITOR", masterName, monitor, redisPort, quorum)
rClient.Process(cmd)
_, err := cmd.Result()
Expand Down Expand Up @@ -243,8 +241,11 @@ func (c *client) SetCustomSentinelConfig(ip string, configs []string) error {
defer rClient.Close()

for _, config := range configs {
setCommand := fmt.Sprintf(sentinelSetCommand, masterName, config)
if err := c.applyConfig(setCommand, rClient); err != nil {
param, value, err := c.getConfigParameters(config)
if err != nil {
return err
}
if err := c.applySentinelConfig(param, value, rClient); err != nil {
return err
}
}
Expand All @@ -261,27 +262,32 @@ func (c *client) SetCustomRedisConfig(ip string, configs []string) error {
defer rClient.Close()

for _, config := range configs {
setCommand := fmt.Sprintf(redisSetCommand, config)
if err := c.applyConfig(setCommand, rClient); err != nil {
param, value, err := c.getConfigParameters(config)
if err != nil {
return err
}
if err := c.applyRedisConfig(param, value, rClient); err != nil {
return err
}
}
return nil
}

func (c *client) applyConfig(command string, rClient *rediscli.Client) error {
sc := strings.Split(command, " ")
// Required conversion due to language specifications
// https://golang.org/doc/faq#convert_slice_of_interface
s := make([]interface{}, len(sc))
for i, v := range sc {
s[i] = v
}
func (c *client) applyRedisConfig(parameter string, value string, rClient *rediscli.Client) error {
jchanam marked this conversation as resolved.
Show resolved Hide resolved
result := rClient.ConfigSet(parameter, value)
return result.Err()
}

cmd := rediscli.NewBoolCmd(s...)
func (c *client) applySentinelConfig(parameter string, value string, rClient *rediscli.Client) error {
jchanam marked this conversation as resolved.
Show resolved Hide resolved
cmd := rediscli.NewStatusCmd("SENTINEL", "set", masterName, parameter, value)
rClient.Process(cmd)
if _, err := cmd.Result(); err != nil {
return err
return cmd.Err()
}

func (c *client) getConfigParameters(config string) (parameter string, value string, err error) {
jchanam marked this conversation as resolved.
Show resolved Hide resolved
s := strings.Split(config, " ")
if len(s) < 2 {
return "", "", fmt.Errorf("configuration '%s' malformed", config)
}
return nil
return s[0], strings.Join(s[1:], " "), nil
}