Issue #642 - Renamed config property to replica-priority and added config set/get functionality

This commit is contained in:
Erik Serating 2022-06-22 12:50:43 -04:00
parent 2be07e4762
commit b256d4752b
2 changed files with 44 additions and 31 deletions

View File

@ -25,7 +25,7 @@ const (
FollowPort = "follow_port" FollowPort = "follow_port"
FollowID = "follow_id" FollowID = "follow_id"
FollowPos = "follow_pos" FollowPos = "follow_pos"
SlavePriority = "slave_priority" ReplicaPriority = "replica-priority"
ServerID = "server_id" ServerID = "server_id"
ReadOnly = "read_only" ReadOnly = "read_only"
RequirePass = "requirepass" RequirePass = "requirepass"
@ -37,7 +37,7 @@ const (
LogConfig = "logconfig" 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 // Config is a tile38 config
type Config struct { type Config struct {
@ -49,7 +49,7 @@ type Config struct {
_followPort int64 _followPort int64
_followID string _followID string
_followPos int64 _followPos int64
_slavePriority int64 _replicaPriority int64
_serverID string _serverID string
_readOnly bool _readOnly bool
@ -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 // 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 // anything less than zero will be considered default and will result in no slave_priority
// being output when INFO is called. // being output when INFO is called.
if gjson.Get(json, SlavePriority).Exists() { if gjson.Get(json, ReplicaPriority).Exists() {
config._slavePriority = gjson.Get(json, SlavePriority).Int() config._replicaPriority = gjson.Get(json, ReplicaPriority).Int()
} else { } else {
config._slavePriority = -1 config._replicaPriority = -1
} }
// load properties // load properties
@ -178,8 +178,8 @@ func (config *Config) write(writeProperties bool) {
if config._followPos != 0 { if config._followPos != 0 {
m[FollowPos] = config._followPos m[FollowPos] = config._followPos
} }
if config._slavePriority >= 0 { if config._replicaPriority >= 0 {
m[SlavePriority] = config._slavePriority m[ReplicaPriority] = config._replicaPriority
} }
if config._serverID != "" { if config._serverID != "" {
m[ServerID] = config._serverID m[ServerID] = config._serverID
@ -326,6 +326,13 @@ func (config *Config) setProperty(name, value string, fromLoad bool) error {
} else { } else {
config._logConfig = value 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 { if invalid {
@ -365,6 +372,12 @@ func (config *Config) getProperty(name string) string {
return strconv.FormatUint(uint64(config._keepAlive), 10) return strconv.FormatUint(uint64(config._keepAlive), 10)
case LogConfig: case LogConfig:
return config._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() config.mu.RUnlock()
return int(v) return int(v)
} }
func (config *Config) slavePriority() int { func (config *Config) replicaPriority() int {
config.mu.RLock() config.mu.RLock()
v := config._slavePriority v := config._replicaPriority
config.mu.RUnlock() config.mu.RUnlock()
return int(v) return int(v)
} }

View File

@ -408,8 +408,8 @@ func (s *Server) writeInfoReplication(w *bytes.Buffer) {
fmt.Fprintf(w, "role:slave\r\n") fmt.Fprintf(w, "role:slave\r\n")
fmt.Fprintf(w, "master_host:%s\r\n", s.config.followHost()) fmt.Fprintf(w, "master_host:%s\r\n", s.config.followHost())
fmt.Fprintf(w, "master_port:%v\r\n", s.config.followPort()) fmt.Fprintf(w, "master_port:%v\r\n", s.config.followPort())
if s.config.slavePriority() >= 0 { if s.config.replicaPriority() >= 0 {
fmt.Fprintf(w, "slave_priority:%v\r\n", s.config.slavePriority()) fmt.Fprintf(w, "slave_priority:%v\r\n", s.config.replicaPriority())
} }
} else { } else {
fmt.Fprintf(w, "role:master\r\n") fmt.Fprintf(w, "role:master\r\n")