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,16 +340,22 @@ 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 i, peer := range cleanedPeers {
|
||||
for _, peer := range cleanedPeers {
|
||||
if peer.ToHttpAddress() == masterAddress.ToHttpAddress() {
|
||||
// Canonicalize self to avoid adding a second logical self peer
|
||||
// when -peers uses host:port and local name is host:port.grpcPort.
|
||||
cleanedPeers[i] = masterAddress
|
||||
hasSelf = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,6 +368,23 @@ 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(a.ToHttpAddress(), b.ToHttpAddress())
|
||||
|
||||
@@ -43,3 +43,24 @@ func TestCheckPeersCanonicalizesSelfEntry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPeersDeduplicatesAliasPeers(t *testing.T) {
|
||||
_, peers := checkPeers("127.0.0.1", 9000, 19000, "127.0.0.1:9002,127.0.0.1:9002.19002,127.0.0.1:9003")
|
||||
|
||||
if len(peers) != 3 {
|
||||
t.Fatalf("expected 3 unique peers after normalization, got %d: %+v", len(peers), peers)
|
||||
}
|
||||
|
||||
count9002 := 0
|
||||
for _, peer := range peers {
|
||||
if peer.ToHttpAddress() == "127.0.0.1:9002" {
|
||||
count9002++
|
||||
if string(peer) != "127.0.0.1:9002.19002" {
|
||||
t.Fatalf("expected canonical peer 127.0.0.1:9002.19002, got %q", peer)
|
||||
}
|
||||
}
|
||||
}
|
||||
if count9002 != 1 {
|
||||
t.Fatalf("expected one peer for 127.0.0.1:9002, got %d in %+v", count9002, peers)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user