Fix master leader election when grpc ports change (#8272)
* Fix master leader detection when grpc ports change * Canonicalize self peer entry to avoid raft self-alias panic * Normalize and deduplicate master peer addresses
This commit is contained in:
@@ -4,9 +4,11 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -338,8 +340,16 @@ func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers strin
|
||||
}
|
||||
|
||||
peers = strings.TrimSpace(peers)
|
||||
|
||||
cleanedPeers = pb.ServerAddresses(peers).ToAddresses()
|
||||
seenPeers := make(map[string]struct{})
|
||||
for _, peer := range pb.ServerAddresses(peers).ToAddresses() {
|
||||
normalizedPeer := normalizeMasterPeerAddress(peer, masterAddress)
|
||||
key := string(normalizedPeer)
|
||||
if _, found := seenPeers[key]; found {
|
||||
continue
|
||||
}
|
||||
seenPeers[key] = struct{}{}
|
||||
cleanedPeers = append(cleanedPeers, normalizedPeer)
|
||||
}
|
||||
|
||||
hasSelf := false
|
||||
for _, peer := range cleanedPeers {
|
||||
@@ -358,14 +368,31 @@ func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers strin
|
||||
return
|
||||
}
|
||||
|
||||
func normalizeMasterPeerAddress(peer pb.ServerAddress, self pb.ServerAddress) pb.ServerAddress {
|
||||
if peer.ToHttpAddress() == self.ToHttpAddress() {
|
||||
return self
|
||||
}
|
||||
|
||||
_, grpcPort, err := net.SplitHostPort(peer.ToGrpcAddress())
|
||||
if err != nil {
|
||||
return peer
|
||||
}
|
||||
grpcPortValue, err := strconv.Atoi(grpcPort)
|
||||
if err != nil {
|
||||
return peer
|
||||
}
|
||||
|
||||
return pb.NewServerAddressWithGrpcPort(peer.ToHttpAddress(), grpcPortValue)
|
||||
}
|
||||
|
||||
func isTheFirstOne(self pb.ServerAddress, peers []pb.ServerAddress) bool {
|
||||
slices.SortFunc(peers, func(a, b pb.ServerAddress) int {
|
||||
return strings.Compare(string(a), string(b))
|
||||
return strings.Compare(a.ToHttpAddress(), b.ToHttpAddress())
|
||||
})
|
||||
if len(peers) <= 0 {
|
||||
return true
|
||||
}
|
||||
return self == peers[0]
|
||||
return self.ToHttpAddress() == peers[0].ToHttpAddress()
|
||||
}
|
||||
|
||||
func (m *MasterOptions) toMasterOption(whiteList []string) *weed_server.MasterOption {
|
||||
|
||||
Reference in New Issue
Block a user