Fix master leader detection when grpc ports change

This commit is contained in:
Chris Lu
2026-02-09 18:00:09 -08:00
parent 30812b85f3
commit 02dac23119
2 changed files with 37 additions and 2 deletions

View File

@@ -360,12 +360,12 @@ func checkPeers(masterIp string, masterPort int, masterGrpcPort int, peers strin
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 {

View File

@@ -0,0 +1,35 @@
package command
import (
"testing"
"github.com/seaweedfs/seaweedfs/weed/pb"
)
func TestIsTheFirstOneIgnoresGrpcPort(t *testing.T) {
self := pb.ServerAddress("127.0.0.1:9000.19000")
peers := []pb.ServerAddress{
"127.0.0.1:9000",
"127.0.0.1:9002.19002",
"127.0.0.1:9003.19003",
}
if !isTheFirstOne(self, peers) {
t.Fatalf("expected first peer match by HTTP address between %q and %+v", self, peers)
}
}
func TestCheckPeersAddsSelfWhenGrpcPortMismatches(t *testing.T) {
self, peers := checkPeers("127.0.0.1", 9000, 19000, "127.0.0.1:9002,127.0.0.1:9003")
found := false
for _, peer := range peers {
if peer.ToHttpAddress() == self.ToHttpAddress() {
found = true
break
}
}
if !found {
t.Fatalf("expected peers %+v to contain self %s by HTTP address", peers, self)
}
}