Skip to content

Commit

Permalink
Merge pull request wind-c#104 from ohkinozomu/derived-ports
Browse files Browse the repository at this point in the history
Fix to always use the derived port for memberlist
  • Loading branch information
wind-c authored Jul 31, 2024
2 parents 928da23 + d1e021d commit 633231b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cluster/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func NewAgent(conf *config.Cluster) *Agent {

func (a *Agent) Start() (err error) {
// setup raft
if a.Config.RaftPort == 0 {
a.Config.RaftPort = 8946
if a.Config.RaftPort == 0 || a.Config.DiscoveryWay == config.DiscoveryWayMemberlist {
a.Config.RaftPort = mlist.GetRaftPortFromBindPort(a.Config.BindPort)
}
if a.Config.RaftDir == "" {
a.Config.RaftDir = path.Join("data", a.Config.NodeName)
Expand Down Expand Up @@ -208,7 +208,7 @@ func getRaftPeerAddr(member *discovery.Member) string {
}

// using memberlist
return net.JoinHostPort(member.Addr, strconv.Itoa(member.Port+1000))
return net.JoinHostPort(member.Addr, strconv.Itoa(mlist.GetRaftPortFromBindPort(member.Port)))
}

func getGrpcAddr(member *discovery.Member) string {
Expand All @@ -218,7 +218,7 @@ func getGrpcAddr(member *discovery.Member) string {
}

// using memberlist
return net.JoinHostPort(member.Addr, strconv.Itoa(member.Port+10000))
return net.JoinHostPort(member.Addr, strconv.Itoa(mlist.GetGRPCPortFromBindPort(member.Port)))
}

func (a *Agent) Stat() map[string]int64 {
Expand Down
12 changes: 12 additions & 0 deletions cluster/discovery/mlist/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mlist

// serf.Member has Tags, but memberlist.Node does not, so the Raft and gRPC ports cannot be relayed through events.
// Ports are derived based on the bind port instead.

func GetRaftPortFromBindPort(bindPort int) int {
return bindPort + 1000
}

func GetGRPCPortFromBindPort(bindPort int) int {
return bindPort + 10000
}

0 comments on commit 633231b

Please sign in to comment.