From 2be07e4762c0edea518ffa7df1dc973d8b1d1b0e Mon Sep 17 00:00:00 2001 From: Erik Serating Date: Mon, 20 Jun 2022 09:50:40 -0400 Subject: [PATCH 1/2] Issue #642 - Expose config and INFO response for slave_priority --- internal/server/config.go | 32 ++++++++++++++++++++++++++------ internal/server/stats.go | 3 +++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/internal/server/config.go b/internal/server/config.go index b7355900..e17aa438 100644 --- a/internal/server/config.go +++ b/internal/server/config.go @@ -25,6 +25,7 @@ const ( FollowPort = "follow_port" FollowID = "follow_id" FollowPos = "follow_pos" + SlavePriority = "slave_priority" ServerID = "server_id" ReadOnly = "read_only" RequirePass = "requirepass" @@ -44,12 +45,13 @@ type Config struct { mu sync.RWMutex - _followHost string - _followPort int64 - _followID string - _followPos int64 - _serverID string - _readOnly bool + _followHost string + _followPort int64 + _followID string + _followPos int64 + _slavePriority int64 + _serverID string + _readOnly bool _requirePassP string _requirePass string @@ -99,6 +101,15 @@ func loadConfig(path string) (*Config, error) { config._serverID = randomKey(16) } + // Need to be sure we look for existence vs not zero because zero is an intentional setting + // anything less than zero will be considered default and will result in no slave_priority + // being output when INFO is called. + if gjson.Get(json, SlavePriority).Exists() { + config._slavePriority = gjson.Get(json, SlavePriority).Int() + } else { + config._slavePriority = -1 + } + // load properties if err := config.setProperty(RequirePass, config._requirePassP, true); err != nil { return nil, err @@ -167,6 +178,9 @@ func (config *Config) write(writeProperties bool) { if config._followPos != 0 { m[FollowPos] = config._followPos } + if config._slavePriority >= 0 { + m[SlavePriority] = config._slavePriority + } if config._serverID != "" { m[ServerID] = config._serverID } @@ -426,6 +440,12 @@ func (config *Config) followPort() int { config.mu.RUnlock() return int(v) } +func (config *Config) slavePriority() int { + config.mu.RLock() + v := config._slavePriority + config.mu.RUnlock() + return int(v) +} func (config *Config) serverID() string { config.mu.RLock() v := config._serverID diff --git a/internal/server/stats.go b/internal/server/stats.go index 503beafe..975665c2 100644 --- a/internal/server/stats.go +++ b/internal/server/stats.go @@ -408,6 +408,9 @@ func (s *Server) writeInfoReplication(w *bytes.Buffer) { fmt.Fprintf(w, "role:slave\r\n") fmt.Fprintf(w, "master_host:%s\r\n", s.config.followHost()) fmt.Fprintf(w, "master_port:%v\r\n", s.config.followPort()) + if s.config.slavePriority() >= 0 { + fmt.Fprintf(w, "slave_priority:%v\r\n", s.config.slavePriority()) + } } else { fmt.Fprintf(w, "role:master\r\n") var i int From b256d4752bd1a04ea60c224e86b291cb2f94ae04 Mon Sep 17 00:00:00 2001 From: Erik Serating Date: Wed, 22 Jun 2022 12:50:43 -0400 Subject: [PATCH 2/2] Issue #642 - Renamed config property to replica-priority and added config set/get functionality --- internal/server/config.go | 71 +++++++++++++++++++++++---------------- internal/server/stats.go | 4 +-- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/internal/server/config.go b/internal/server/config.go index e17aa438..31aca1d0 100644 --- a/internal/server/config.go +++ b/internal/server/config.go @@ -21,23 +21,23 @@ const ( // Config keys const ( - FollowHost = "follow_host" - FollowPort = "follow_port" - FollowID = "follow_id" - FollowPos = "follow_pos" - SlavePriority = "slave_priority" - ServerID = "server_id" - ReadOnly = "read_only" - RequirePass = "requirepass" - LeaderAuth = "leaderauth" - ProtectedMode = "protected-mode" - MaxMemory = "maxmemory" - AutoGC = "autogc" - KeepAlive = "keepalive" - LogConfig = "logconfig" + FollowHost = "follow_host" + FollowPort = "follow_port" + FollowID = "follow_id" + FollowPos = "follow_pos" + ReplicaPriority = "replica-priority" + ServerID = "server_id" + ReadOnly = "read_only" + RequirePass = "requirepass" + LeaderAuth = "leaderauth" + ProtectedMode = "protected-mode" + MaxMemory = "maxmemory" + AutoGC = "autogc" + KeepAlive = "keepalive" + LogConfig = "logconfig" ) -var validProperties = []string{RequirePass, LeaderAuth, ProtectedMode, MaxMemory, AutoGC, KeepAlive, LogConfig} +var validProperties = []string{RequirePass, LeaderAuth, ProtectedMode, MaxMemory, AutoGC, KeepAlive, LogConfig, ReplicaPriority} // Config is a tile38 config type Config struct { @@ -45,13 +45,13 @@ type Config struct { mu sync.RWMutex - _followHost string - _followPort int64 - _followID string - _followPos int64 - _slavePriority int64 - _serverID string - _readOnly bool + _followHost string + _followPort int64 + _followID string + _followPos int64 + _replicaPriority int64 + _serverID string + _readOnly bool _requirePassP string _requirePass string @@ -104,10 +104,10 @@ func loadConfig(path string) (*Config, error) { // Need to be sure we look for existence vs not zero because zero is an intentional setting // anything less than zero will be considered default and will result in no slave_priority // being output when INFO is called. - if gjson.Get(json, SlavePriority).Exists() { - config._slavePriority = gjson.Get(json, SlavePriority).Int() + if gjson.Get(json, ReplicaPriority).Exists() { + config._replicaPriority = gjson.Get(json, ReplicaPriority).Int() } else { - config._slavePriority = -1 + config._replicaPriority = -1 } // load properties @@ -178,8 +178,8 @@ func (config *Config) write(writeProperties bool) { if config._followPos != 0 { m[FollowPos] = config._followPos } - if config._slavePriority >= 0 { - m[SlavePriority] = config._slavePriority + if config._replicaPriority >= 0 { + m[ReplicaPriority] = config._replicaPriority } if config._serverID != "" { m[ServerID] = config._serverID @@ -326,6 +326,13 @@ func (config *Config) setProperty(name, value string, fromLoad bool) error { } else { config._logConfig = value } + case ReplicaPriority: + replicaPriority, err := strconv.ParseUint(value, 10, 64) + if err != nil || replicaPriority < 0 { + invalid = true + } else { + config._replicaPriority = int64(replicaPriority) + } } if invalid { @@ -365,6 +372,12 @@ func (config *Config) getProperty(name string) string { return strconv.FormatUint(uint64(config._keepAlive), 10) case LogConfig: return config._logConfig + case ReplicaPriority: + if config._replicaPriority < 0 { + return "" + } else { + return strconv.FormatUint(uint64(config._replicaPriority), 10) + } } } @@ -440,9 +453,9 @@ func (config *Config) followPort() int { config.mu.RUnlock() return int(v) } -func (config *Config) slavePriority() int { +func (config *Config) replicaPriority() int { config.mu.RLock() - v := config._slavePriority + v := config._replicaPriority config.mu.RUnlock() return int(v) } diff --git a/internal/server/stats.go b/internal/server/stats.go index 975665c2..b89c274e 100644 --- a/internal/server/stats.go +++ b/internal/server/stats.go @@ -408,8 +408,8 @@ func (s *Server) writeInfoReplication(w *bytes.Buffer) { fmt.Fprintf(w, "role:slave\r\n") fmt.Fprintf(w, "master_host:%s\r\n", s.config.followHost()) fmt.Fprintf(w, "master_port:%v\r\n", s.config.followPort()) - if s.config.slavePriority() >= 0 { - fmt.Fprintf(w, "slave_priority:%v\r\n", s.config.slavePriority()) + if s.config.replicaPriority() >= 0 { + fmt.Fprintf(w, "slave_priority:%v\r\n", s.config.replicaPriority()) } } else { fmt.Fprintf(w, "role:master\r\n")