From 0e5873cf2172d53f5ff28337ae9732beaa8f6f41 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Tue, 4 Sep 2018 15:24:37 +0200 Subject: [PATCH 1/2] Accept an address change in RECOVERY in more cases. --- service/service.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/service/service.go b/service/service.go index 8d55d2b1..3561460c 100644 --- a/service/service.go +++ b/service/service.go @@ -900,15 +900,30 @@ func (s *Service) HandleHello(ownAddress, remoteAddress string, req *HelloReques // ID already found, update peer data for i, p := range s.myPeers.AllPeers { if p.ID == req.SlaveID { - s.myPeers.AllPeers[i].Port = req.SlavePort if s.cfg.AllPortOffsetsUnique { s.myPeers.AllPeers[i].Address = slaveAddr } else { - // Slave address may not change - if p.Address != slaveAddr { + // Need to check if this particular address does appear in + // another peer, if so, we forbid to change the address: + addrFound := false + for j, pp := range s.myPeers.AllPeers { + if j != i && pp.Address == slaveAddr { + addrFound = true + break + } + } + // Slave address may not change in this case + if addrFound && p.Address != slaveAddr { return ClusterConfig{}, maskAny(client.NewBadRequestError("Cannot change slave address while using an existing ID.")) } + // We accept the new address (it might be the old one): + s.myPeers.AllPeers[i].Address = slaveAddr + // However, since we also accept the port, we must set the + // port ofset of that replaced peer to 0 such that the AllPeers + // information actually contains the right port. + s.myPeers.AllPeers[i].PortOffset = 0 } + s.myPeers.AllPeers[i].Port = req.SlavePort s.myPeers.AllPeers[i].DataDir = req.DataDir } } From 56e872d3cc96f9c9af17de7715ca63499d3cf7d5 Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Tue, 4 Sep 2018 16:00:55 +0200 Subject: [PATCH 2/2] Improve readability of code. --- service/service.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/service/service.go b/service/service.go index 3561460c..2b3f3ec8 100644 --- a/service/service.go +++ b/service/service.go @@ -905,15 +905,15 @@ func (s *Service) HandleHello(ownAddress, remoteAddress string, req *HelloReques } else { // Need to check if this particular address does appear in // another peer, if so, we forbid to change the address: - addrFound := false - for j, pp := range s.myPeers.AllPeers { - if j != i && pp.Address == slaveAddr { - addrFound = true + addrFoundInOtherPeer := false + for _, pp := range s.myPeers.AllPeers { + if pp.ID != req.SlaveID && pp.Address == slaveAddr { + addrFoundInOtherPeer = true break } } // Slave address may not change in this case - if addrFound && p.Address != slaveAddr { + if addrFoundInOtherPeer && p.Address != slaveAddr { return ClusterConfig{}, maskAny(client.NewBadRequestError("Cannot change slave address while using an existing ID.")) } // We accept the new address (it might be the old one):