Class SwimGossipProtocol
java.lang.Object
com.loomcache.server.cluster.SwimGossipProtocol
- All Implemented Interfaces:
AutoCloseable
Scalable Weakly-consistent Infection-style Membership (SWIM) protocol detector.
Implements the SWIM gossip protocol as described in "SWIM: Scalable Weakly-consistent Infection-style Process Group Membership Protocol" (Das et al., 2002).
Key features:
- Scalable membership detection with O(log N) load per node
- Infection-style dissemination of membership updates
- Suspicion mechanism with timeout-based confirmation
- Incarnation numbers for refuting false suspicions
- Protocol period with ping, ping-req, suspect, alive, and dead states
- Thread-safe with virtual thread support via locked operations
The protocol operates in rounds: each round, the local node selects a random member and sends a ping. If no ack is received within the timeout, it sends ping-req messages to k random members. After the suspicion timeout, a suspected member transitions to dead.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic final recordGossip message with dissemination tracking.static enumGossip message types for the SWIM protocol.static interfacestatic final recordInformation about a cluster member.static interfaceSealed interface for member state in the protocol.static classMetrics holder for SWIM protocol statistics. -
Constructor Summary
ConstructorsConstructorDescriptionSwimGossipProtocol(String localNodeId, String localAddress, int localPort) Create a SWIM gossip protocol detector with default configuration.SwimGossipProtocol(String localNodeId, String localAddress, int localPort, long protocolPeriodMs, long suspicionTimeoutMs, int pingReqFanout, int initialDisseminationCount, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Create a SWIM gossip protocol detector with custom configuration.SwimGossipProtocol(String localNodeId, String localAddress, int localPort, io.micrometer.core.instrument.MeterRegistry meterRegistry) Create a SWIM gossip protocol detector with metrics support. -
Method Summary
Modifier and TypeMethodDescriptionAdd or update a member in the cluster.voidvoidvoidclose()Close and stop the protocol.protected voidExecute one protocol round. 1.Get all alive members.Get all members.Get all dead members.longGet the local incarnation number.@Nullable SwimGossipProtocol.MemberInfoGet a member by node ID.longGet the protocol round count.Get all suspected members.voidhandleAlive(String nodeId, String address, int port, long incarnation) Handle an alive notification (refutes suspicion).voidhandleDead(String nodeId, long incarnation) Handle a dead notification.voidhandlePing(String fromNodeId, String fromAddress, int fromPort, long fromIncarnation) Handle a ping message from another node (membership update).voidhandleSuspect(String suspectedNodeId, long incarnation, long sourcePriority) Handle a suspected member notification (from gossip).voidIncrement local incarnation number (refute suspicions against self).booleanremoveMember(String nodeId) Remove a member from the cluster.protected SwimGossipProtocol.MemberInfoselectProbeTarget(List<SwimGossipProtocol.MemberInfo> aliveMembers) protected List<SwimGossipProtocol.MemberInfo> selectRandomAliveMembers(int fanout, @Nullable String exclude) Select k random alive members (excluding self and optionally another member).voidsetDirectPingProbe(SwimGossipProtocol.DirectPingProbe directPingProbe) voidsetIndirectPingProbe(SwimGossipProtocol.IndirectPingProbe indirectPingProbe) voidstart()Start the protocol ticker thread.voidstop()Stop the protocol ticker thread.toString()
-
Constructor Details
-
SwimGossipProtocol
public SwimGossipProtocol(String localNodeId, String localAddress, int localPort, long protocolPeriodMs, long suspicionTimeoutMs, int pingReqFanout, int initialDisseminationCount, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Create a SWIM gossip protocol detector with custom configuration.- Parameters:
localNodeId- unique identifier for this nodelocalAddress- network address of this nodelocalPort- network port for SWIM protocolprotocolPeriodMs- protocol round duration in millisecondssuspicionTimeoutMs- suspicion timeout in millisecondspingReqFanout- number of members to send ping-req toinitialDisseminationCount- initial dissemination count for messagesmeterRegistry- optional MeterRegistry for metrics
-
SwimGossipProtocol
-
SwimGossipProtocol
public SwimGossipProtocol(String localNodeId, String localAddress, int localPort, io.micrometer.core.instrument.MeterRegistry meterRegistry) Create a SWIM gossip protocol detector with metrics support.- Parameters:
localNodeId- unique identifier for this nodelocalAddress- network address of this nodelocalPort- network port for SWIM protocolmeterRegistry- MeterRegistry for metrics
-
-
Method Details
-
addMember
Add or update a member in the cluster.- Parameters:
nodeId- unique identifier for the memberaddress- network address of the memberport- network port for SWIM protocol- Returns:
- the new MemberInfo
-
getMember
Get a member by node ID.- Parameters:
nodeId- the member's node ID- Returns:
- the MemberInfo, or null if not found
-
getAllMembers
Get all members.- Returns:
- collection of all members
-
getAliveMembers
Get all alive members.- Returns:
- collection of alive members
-
getSuspectedMembers
Get all suspected members.- Returns:
- collection of suspected members
-
getDeadMembers
Get all dead members.- Returns:
- collection of dead members
-
removeMember
Remove a member from the cluster.- Parameters:
nodeId- the member's node ID- Returns:
- true if the member was removed, false if not found
-
handlePing
Handle a ping message from another node (membership update).- Parameters:
fromNodeId- the sender's node IDfromAddress- the sender's addressfromPort- the sender's portfromIncarnation- the sender's incarnation number
-
handleSuspect
Handle a suspected member notification (from gossip).- Parameters:
suspectedNodeId- the suspected member's node IDincarnation- the incarnation numbersourcePriority- incarnation number of the suspecting member (for conflict resolution)
-
handleAlive
-
handleDead
Handle a dead notification.- Parameters:
nodeId- the dead member's node IDincarnation- the incarnation number
-
incrementIncarnation
public void incrementIncarnation()Increment local incarnation number (refute suspicions against self). -
getIncarnationNumber
public long getIncarnationNumber()Get the local incarnation number.- Returns:
- the current local incarnation
-
selectRandomAliveMembers
protected List<SwimGossipProtocol.MemberInfo> selectRandomAliveMembers(int fanout, @Nullable String exclude) Select k random alive members (excluding self and optionally another member).- Parameters:
fanout- number of members to selectexclude- optional member ID to exclude- Returns:
- list of selected members
-
start
public void start()Start the protocol ticker thread. This should be called before the protocol is used. -
stop
public void stop()Stop the protocol ticker thread. -
executeProtocolRound
protected void executeProtocolRound()Execute one protocol round. 1. Select a random alive member 2. Check for suspected members whose timeout has expired -
selectProbeTarget
protected SwimGossipProtocol.MemberInfo selectProbeTarget(List<SwimGossipProtocol.MemberInfo> aliveMembers) -
getProtocolRoundCount
public long getProtocolRoundCount()Get the protocol round count.- Returns:
- number of protocol rounds executed
-
setDirectPingProbe
-
clearDirectPingProbe
public void clearDirectPingProbe() -
setIndirectPingProbe
-
clearIndirectPingProbe
public void clearIndirectPingProbe() -
close
-
toString
-