mirror of https://github.com/status-im/consul.git
Mutate copies of serverCfg.servers, not original
Removing any ambiguity re: ownership of the mutated server lists is a win for maintenance and debugging.
This commit is contained in:
parent
e6c27325d9
commit
e53704b032
|
@ -105,9 +105,14 @@ func (sm *ServerManager) AddServer(server *server_details.ServerDetails) {
|
||||||
found := false
|
found := false
|
||||||
for idx, existing := range serverCfg.servers {
|
for idx, existing := range serverCfg.servers {
|
||||||
if existing.Name == server.Name {
|
if existing.Name == server.Name {
|
||||||
// Overwrite the existing server parts in order to
|
newServers := make([]*server_details.ServerDetails, len(serverCfg.servers))
|
||||||
// possibly update metadata (i.e. server version)
|
copy(newServers, serverCfg.servers)
|
||||||
serverCfg.servers[idx] = server
|
|
||||||
|
// Overwrite the existing server details in order to
|
||||||
|
// possibly update metadata (e.g. server version)
|
||||||
|
newServers[idx] = server
|
||||||
|
|
||||||
|
serverCfg.servers = newServers
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -240,13 +245,17 @@ func (sm *ServerManager) RebalanceServers() {
|
||||||
defer sm.serverConfigLock.Unlock()
|
defer sm.serverConfigLock.Unlock()
|
||||||
serverCfg := sm.getServerConfig()
|
serverCfg := sm.getServerConfig()
|
||||||
|
|
||||||
|
newServers := make([]*server_details.ServerDetails, len(serverCfg.servers)+1)
|
||||||
|
copy(newServers, serverCfg.servers)
|
||||||
|
|
||||||
// Shuffle the server list on server join. Servers are selected from
|
// Shuffle the server list on server join. Servers are selected from
|
||||||
// the head of the list and are moved to the end of the list on
|
// the head of the list and are moved to the end of the list on
|
||||||
// failure.
|
// failure.
|
||||||
for i := len(serverCfg.servers) - 1; i > 0; i-- {
|
for i := len(serverCfg.servers) - 1; i > 0; i-- {
|
||||||
j := rand.Int31n(int32(i + 1))
|
j := rand.Int31n(int32(i + 1))
|
||||||
serverCfg.servers[i], serverCfg.servers[j] = serverCfg.servers[j], serverCfg.servers[i]
|
newServers[i], newServers[j] = newServers[j], newServers[i]
|
||||||
}
|
}
|
||||||
|
serverCfg.servers = newServers
|
||||||
|
|
||||||
serverCfg.resetRebalanceTimer(sm)
|
serverCfg.resetRebalanceTimer(sm)
|
||||||
sm.serverConfigValue.Store(serverCfg)
|
sm.serverConfigValue.Store(serverCfg)
|
||||||
|
@ -267,13 +276,17 @@ func (sm *ServerManager) RemoveServer(server *server_details.ServerDetails) {
|
||||||
n := len(serverCfg.servers)
|
n := len(serverCfg.servers)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
if serverCfg.servers[i].Name == server.Name {
|
if serverCfg.servers[i].Name == server.Name {
|
||||||
serverCfg.servers[i], serverCfg.servers[n-1] = serverCfg.servers[n-1], nil
|
newServers := make([]*server_details.ServerDetails, len(serverCfg.servers)-1)
|
||||||
serverCfg.servers = serverCfg.servers[:n-1]
|
copy(newServers, serverCfg.servers)
|
||||||
break
|
|
||||||
}
|
newServers[i], newServers[n-1] = newServers[n-1], nil
|
||||||
}
|
newServers = newServers[:n-1]
|
||||||
|
serverCfg.servers = newServers
|
||||||
|
|
||||||
sm.serverConfigValue.Store(serverCfg)
|
sm.serverConfigValue.Store(serverCfg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// resetRebalanceTimer assumes:
|
// resetRebalanceTimer assumes:
|
||||||
|
|
Loading…
Reference in New Issue