@@ -805,21 +805,26 @@ func (s *Service) HandleHello(ownAddress, remoteAddress string, req *HelloReques
805
805
break
806
806
}
807
807
}
808
- // Slave address may not change in this case
809
- if addrFoundInOtherPeer && p .Address != slaveAddr {
810
- s .log .Warn ().Msgf ("Cannot change slave address while using an existing ID. Remote address: %s" , remoteAddress )
811
- return ClusterConfig {}, maskAny (client .NewBadRequestError ("Cannot change slave address while using an existing ID." ))
808
+ if addrFoundInOtherPeer && isLocalAddress (slaveAddr ) && isLocalAddress (p .Address ) && s .runtimeClusterManager .myPeers .IsPortOffsetInUse () {
809
+ // This is a default configuration, where host and port are not set. Keep offset.
810
+ s .log .Warn ().Msgf ("Updating slave with local address (%s). Offset (%d) will be kept. Peer id: %s" ,
811
+ p .Address , p .PortOffset , p .ID )
812
+ } else if addrFoundInOtherPeer && p .Address != slaveAddr {
813
+ msg := fmt .Sprintf ("Cannot change slave address (%s) to an address that is already in use by another peer (id: %s)" , slaveAddr , p .ID )
814
+ s .log .Warn ().Msgf (msg )
815
+ return ClusterConfig {}, maskAny (client .NewBadRequestError (msg ))
816
+ } else {
817
+ // We accept the new address (it might be the old one):
818
+ peer .Address = slaveAddr
819
+ // However, since we also accept the port, we must set the
820
+ // port offset of that replaced peer to 0 such that the AllPeers
821
+ // information actually contains the right port.
822
+ peer .PortOffset = 0
812
823
}
813
- // We accept the new address (it might be the old one):
814
- peer .Address = slaveAddr
815
- // However, since we also accept the port, we must set the
816
- // port ofset of that replaced peer to 0 such that the AllPeers
817
- // information actually contains the right port.
818
- peer .PortOffset = 0
824
+
819
825
}
820
826
peer .Port = req .SlavePort
821
827
peer .DataDir = req .DataDir
822
-
823
828
peer .HasAgentFlag = boolFromRef (req .Agent , peer .HasAgentFlag )
824
829
peer .HasCoordinatorFlag = utils .NotNilDefault (req .Coordinator , peer .HasCoordinatorFlag )
825
830
peer .HasDBServerFlag = utils .NotNilDefault (req .DBServer , peer .HasDBServerFlag )
@@ -867,6 +872,10 @@ func (s *Service) HandleHello(ownAddress, remoteAddress string, req *HelloReques
867
872
return s .runtimeClusterManager .myPeers , nil
868
873
}
869
874
875
+ func isLocalAddress (addr string ) bool {
876
+ return addr == "127.0.0.1" || addr == "localhost"
877
+ }
878
+
870
879
// ChangeState alters the current state of the service
871
880
func (s * Service ) ChangeState (newState State ) {
872
881
s .mutex .Lock ()
@@ -989,8 +998,8 @@ func (s *Service) UpdateClusterConfig(newConfig ClusterConfig) error {
989
998
// Only update when changed
990
999
if ! reflect .DeepEqual (s .runtimeClusterManager .myPeers , newConfig ) {
991
1000
s .runtimeClusterManager .myPeers = newConfig
992
- s .saveSetup ( )
993
- s . log . Debug (). Msg ( "Updated cluster config" )
1001
+ s .log . Debug (). Msgf ( "Updating cluster config - %v" , newConfig )
1002
+ return s . saveSetup ( )
994
1003
} else {
995
1004
s .log .Debug ().Msg ("Updating cluster config is not needed" )
996
1005
}
@@ -1030,7 +1039,7 @@ func (s *Service) GetHTTPServerPort() (containerPort, hostPort int, err error) {
1030
1039
if myPeer , ok := s .runtimeClusterManager .myPeers .PeerByID (s .id ); ok {
1031
1040
containerPort += myPeer .PortOffset
1032
1041
} else {
1033
- return 0 , 0 , maskAny (fmt .Errorf ("No peer information found for ID '%s'" , s .id ))
1042
+ return 0 , 0 , maskAny (fmt .Errorf ("no peer information found for ID '%s'" , s .id ))
1034
1043
}
1035
1044
}
1036
1045
if s .isNetHost {
@@ -1050,6 +1059,7 @@ func (s *Service) createHTTPServer(config Config) (srv *httpServer, containerPor
1050
1059
hostAddr = net .JoinHostPort (config .OwnAddress , strconv .Itoa (hostPort ))
1051
1060
1052
1061
// Create HTTP server
1062
+ s .log .Debug ().Msgf ("Creating HTTP server on %s" , hostAddr )
1053
1063
return newHTTPServer (s .log , s , & s .runtimeServerManager , config , s .id ), containerPort , hostAddr , containerAddr , nil
1054
1064
}
1055
1065
@@ -1095,7 +1105,7 @@ func (s *Service) startRunning(runner Runner, config Config, bsCfg BootstrapConf
1095
1105
go func () {
1096
1106
defer wg .Done ()
1097
1107
1098
- s .runtimeClusterManager .Run (s .stopPeer .ctx , s .log , s , BuildHelloRequest ( s . id , 0 , s . IsSecure (), config , bsCfg ) )
1108
+ s .runtimeClusterManager .Run (s .stopPeer .ctx , s .log , s )
1099
1109
}()
1100
1110
1101
1111
// Start the upgrade manager
@@ -1228,7 +1238,7 @@ func (s *Service) adjustClusterConfigForRelaunch(bsCfg BootstrapConfig) {
1228
1238
1229
1239
s .runtimeClusterManager .myPeers .ForEachPeer (func (p Peer ) Peer {
1230
1240
if bsCfg .ID == p .ID {
1231
- s .log .Debug ().Msgf ("Adjusting current memeber cluster config after restart (port: %d)" , p .Port )
1241
+ s .log .Debug ().Msgf ("Adjusting current memeber cluster config (locally) after restart (port: %d)" , p .Port )
1232
1242
p .peerServers = preparePeerServers (s .mode , bsCfg , & p )
1233
1243
}
1234
1244
return p
0 commit comments