Class NonBlockingCoordinator
- All Implemented Interfaces:
- ChannelInterceptor,- Heartbeat,- MembershipListener
Title: Auto merging leader election algorithm
Description: Implementation of a simple coordinator algorithm that not only selects a coordinator, it also merges groups automatically when members are discovered that weren't part of the
This algorithm is non blocking meaning it allows for transactions while the coordination phase is going on
This implementation is based on a home brewed algorithm that uses the AbsoluteOrder of a membership
 to pass a token ring of the current membership.
 This is not the same as just using AbsoluteOrder! Consider the following scenario:
 Nodes, A,B,C,D,E on a network, in that priority. AbsoluteOrder will only work if all
 nodes are receiving pings from all the other nodes.
 meaning, that node{i} receives pings from node{all}-node{i}
 but the following could happen if a multicast problem occurs.
 A has members {B,C,D}
 B has members {A,C}
 C has members {D,E}
 D has members {A,B,C,E}
 E has members {A,C,D}
 Because the default Tribes membership implementation, relies on the multicast packets to
 arrive at all nodes correctly, there is nothing guaranteeing that it will.
 
 To best explain how this algorithm works, lets take the above example:
 For simplicity we assume that a send operation is O(1) for all nodes, although this algorithm will work
 where messages overlap, as they all depend on absolute order
 Scenario 1: A,B,C,D,E all come online at the same time
 Eval phase, A thinks of itself as leader, B thinks of A as leader,
 C thinks of itself as leader, D,E think of A as leader
 Token phase:
 (1) A sends out a message X{A-ldr, A-src, mbrs-A,B,C,D} to B where X is the id for the message(and the view)
 (1) C sends out a message Y{C-ldr, C-src, mbrs-C,D,E} to D where Y is the id for the message(and the view)
 (2) B receives X{A-ldr, A-src, mbrs-A,B,C,D}, sends X{A-ldr, A-src, mbrs-A,B,C,D} to C 
 (2) D receives Y{C-ldr, C-src, mbrs-C,D,E} D is aware of A,B, sends Y{A-ldr, C-src, mbrs-A,B,C,D,E} to E
 (3) C receives X{A-ldr, A-src, mbrs-A,B,C,D}, sends X{A-ldr, A-src, mbrs-A,B,C,D,E} to D
 (3) E receives Y{A-ldr, C-src, mbrs-A,B,C,D,E} sends Y{A-ldr, C-src, mbrs-A,B,C,D,E} to A
 (4) D receives X{A-ldr, A-src, mbrs-A,B,C,D,E} sends sends X{A-ldr, A-src, mbrs-A,B,C,D,E} to A
 (4) A receives Y{A-ldr, C-src, mbrs-A,B,C,D,E}, holds the message, add E to its list of members
 (5) A receives X{A-ldr, A-src, mbrs-A,B,C,D,E} 
 At this point, the state looks like
 A - {A-ldr, mbrs-A,B,C,D,E, id=X}
 B - {A-ldr, mbrs-A,B,C,D, id=X}
 C - {A-ldr, mbrs-A,B,C,D,E, id=X}
 D - {A-ldr, mbrs-A,B,C,D,E, id=X}
 E - {A-ldr, mbrs-A,B,C,D,E, id=Y}
 
 A message doesn't stop until it reaches its original sender, unless its dropped by a higher leader.
 As you can see, E still thinks the viewId=Y, which is not correct. But at this point we have
 arrived at the same membership and all nodes are informed of each other.
 To synchronize the rest we simply perform the following check at A when A receives X:
 Original X{A-ldr, A-src, mbrs-A,B,C,D} == Arrived X{A-ldr, A-src, mbrs-A,B,C,D,E}
 Since the condition is false, A, will resend the token, and A sends X{A-ldr, A-src, mbrs-A,B,C,D,E} to B
 When A receives X again, the token is complete. 
 Optionally, A can send a message X{A-ldr, A-src, mbrs-A,B,C,D,E confirmed} to A,B,C,D,E who then
 install and accept the view.
 
 Lets assume that C1 arrives, C1 has lower priority than C, but higher priority than D.
 Lets also assume that C1 sees the following view {B,D,E}
 C1 waits for a token to arrive. When the token arrives, the same scenario as above will happen.
 In the scenario where C1 sees {D,E} and A,B,C cannot see C1, no token will ever arrive.
 In this case, C1 sends a Z{C1-ldr, C1-src, mbrs-C1,D,E} to D
 D receives Z{C1-ldr, C1-src, mbrs-C1,D,E} and sends Z{A-ldr, C1-src, mbrs-A,B,C,C1,D,E} to E
 E receives Z{A-ldr, C1-src, mbrs-A,B,C,C1,D,E} and sends it to A
 A sends Z{A-ldr, A-src, mbrs-A,B,C,C1,D,E} to B and the chain continues until A receives the token again.
 At that time A optionally sends out Z{A-ldr, A-src, mbrs-A,B,C,C1,D,E, confirmed} to A,B,C,C1,D,E
 
To ensure that the view gets implemented at all nodes at the same time, A will send out a VIEW_CONF message, this is the 'confirmed' message that is optional above.
Ideally, the interceptor below this one would be the TcpFailureDetector to ensure correct memberships
The example above, of course can be simplified with a finite statemachine:
 But I suck at writing state machines, my head gets all confused. One day I will document this algorithm though.
 Maybe I'll do a state diagram :)
 
State Diagrams
Initiate an electionReceive an election message
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classstatic classNested classes/interfaces inherited from interface org.apache.catalina.tribes.ChannelInterceptorChannelInterceptor.InterceptorEvent
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected static final byte[]Alive messageprotected static final byte[]Coordination confirmation, for blocking installationsprotected static final byte[]header for a coordination messageprotected static final byte[]Coordination requestprotected final AtomicBooleanprotected final Objectprotected MembershipOur nonblocking membershipprotected static final StringManagerprotected booleanprotected final intprotected Membershipprotected UniqueIdindicates that we are running an election and this is the one we are runningprotected MembershipOur current viewprotected UniqueIdOut current viewIdprotected final longTime to wait for coordination timeoutFields inherited from class org.apache.catalina.tribes.group.ChannelInterceptorBaseoptionFlag
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleanvoidReturns coordinator if one is availablegetLocalMember(boolean incAlive) Return the member that represents this node.Intercepts theChannel.getMember(Member)methodMember[]Get all current cluster membersMember[]getView()protected voidhalt()Block in/out messages while a election is going onprotected voidhandleMyToken(Member local, NonBlockingCoordinator.CoordinationMessage msg, Membership merged) protected voidhandleOtherToken(Member local, NonBlockingCoordinator.CoordinationMessage msg, Membership merged) protected voidprotected voidprotected booleanhasHigherPriority(Member[] complete, Member[] local) booleanhas membersvoidTheheartbeat()method gets invoked periodically to allow interceptors to clean up resources, time out object and perform actions that are unrelated to sending/receiving data.booleanbooleanprotected booleanvoidmemberAdded(Member member) A member was added to the groupvoidmemberAdded(Member member, boolean elect) protected booleanmemberAlive(Member mbr, long conTimeout) voidmemberDisappeared(Member member) A member was removed from the group
 If the member left voluntarily, the Member.getCommand will contain the Member.SHUTDOWN_PAYLOAD dataprotected MembershipvoidthemessageReceivedis invoked when a message is received.protected voidprotected voidrelease()Release lock for in/out messages election is completedprotected voidsendElectionMsg(Member local, Member next, NonBlockingCoordinator.CoordinationMessage msg) protected voidvoidsendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) ThesendMessagemethod is called when a message is being sent to one more destinations.protected voidvoidstart(int svc) Starts up the channel.voidstartElection(boolean force) voidstop(int svc) Shuts down the channel.protected voidWait for an election to endMethods inherited from class org.apache.catalina.tribes.group.ChannelInterceptorBasegetChannel, getNext, getOptionFlag, getPrevious, okToProcess, setChannel, setNext, setOptionFlag, setPrevious
- 
Field Details- 
sm
- 
COORD_HEADERprotected static final byte[] COORD_HEADERheader for a coordination message
- 
COORD_REQUESTprotected static final byte[] COORD_REQUESTCoordination request
- 
COORD_CONFprotected static final byte[] COORD_CONFCoordination confirmation, for blocking installations
- 
COORD_ALIVEprotected static final byte[] COORD_ALIVEAlive message
- 
waitForCoordMsgTimeoutprotected final long waitForCoordMsgTimeoutTime to wait for coordination timeout- See Also:
 
- 
viewOur current view
- 
viewIdOut current viewId
- 
membershipOur nonblocking membership
- 
suggestedviewIdindicates that we are running an election and this is the one we are running
- 
suggestedView
- 
startedprotected volatile boolean started
- 
startsvcprotected final int startsvc- See Also:
 
- 
electionMutex
- 
coordMsgReceived
 
- 
- 
Constructor Details- 
NonBlockingCoordinatorpublic NonBlockingCoordinator()
 
- 
- 
Method Details- 
startElection- Throws:
- ChannelException
 
- 
sendElectionMsgprotected void sendElectionMsg(Member local, Member next, NonBlockingCoordinator.CoordinationMessage msg) throws ChannelException - Throws:
- ChannelException
 
- 
sendElectionMsgToNextInlineprotected void sendElectionMsgToNextInline(Member local, NonBlockingCoordinator.CoordinationMessage msg) throws ChannelException - Throws:
- ChannelException
 
- 
createData
- 
alive
- 
memberAlive
- 
mergeOnArrive
- 
processCoordMessageprotected void processCoordMessage(NonBlockingCoordinator.CoordinationMessage msg) throws ChannelException - Throws:
- ChannelException
 
- 
handleTokenprotected void handleToken(NonBlockingCoordinator.CoordinationMessage msg, Membership merged) throws ChannelException - Throws:
- ChannelException
 
- 
handleMyTokenprotected void handleMyToken(Member local, NonBlockingCoordinator.CoordinationMessage msg, Membership merged) throws ChannelException - Throws:
- ChannelException
 
- 
handleOtherTokenprotected void handleOtherToken(Member local, NonBlockingCoordinator.CoordinationMessage msg, Membership merged) throws ChannelException - Throws:
- ChannelException
 
- 
handleViewConfprotected void handleViewConf(NonBlockingCoordinator.CoordinationMessage msg, Membership merged) throws ChannelException - Throws:
- ChannelException
 
- 
isViewConf
- 
hasHigherPriority
- 
getCoordinatorReturns coordinator if one is available- Returns:
- Member
 
- 
getView
- 
getViewId
- 
haltprotected void halt()Block in/out messages while a election is going on
- 
releaseprotected void release()Release lock for in/out messages election is completed
- 
waitForReleaseprotected void waitForRelease()Wait for an election to end
- 
startDescription copied from class:ChannelInterceptorBaseStarts up the channel. This can be called multiple times for individual services to start The svc parameter can be the logical or value of any constants- Specified by:
- startin interface- ChannelInterceptor
- Overrides:
- startin class- ChannelInterceptorBase
- Parameters:
- svc- int value of
 DEFAULT - will start all services
 MBR_RX_SEQ - starts the membership receiver
 MBR_TX_SEQ - starts the membership broadcaster
 SND_TX_SEQ - starts the replication transmitter
 SND_RX_SEQ - starts the replication receiver
- Throws:
- ChannelException- if a startup error occurs or the service is already started.
- See Also:
 
- 
stopDescription copied from class:ChannelInterceptorBaseShuts down the channel. This can be called multiple times for individual services to shutdown The svc parameter can be the logical or value of any constants- Specified by:
- stopin interface- ChannelInterceptor
- Overrides:
- stopin class- ChannelInterceptorBase
- Parameters:
- svc- int value of
 DEFAULT - will shutdown all services
 MBR_RX_SEQ - stops the membership receiver
 MBR_TX_SEQ - stops the membership broadcaster
 SND_TX_SEQ - stops the replication transmitter
 SND_RX_SEQ - stops the replication receiver
- Throws:
- ChannelException- if a startup error occurs or the service is already started.
- See Also:
 
- 
sendMessagepublic void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException Description copied from interface:ChannelInterceptorThesendMessagemethod is called when a message is being sent to one more destinations. The interceptor can modify any of the parameters and then pass on the message down the stack by invokinggetNext().sendMessage(destination,msg,payload)
 Alternatively the interceptor can stop the message from being sent by not invokinggetNext().sendMessage(destination,msg,payload)
 If the message is to be sent asynchronous the application can be notified of completion and errors by passing in an error handler attached to a payload object.
 The ChannelMessage.getAddress contains Channel.getLocalMember, and can be overwritten to simulate a message sent from another node.- Specified by:
- sendMessagein interface- ChannelInterceptor
- Overrides:
- sendMessagein class- ChannelInterceptorBase
- Parameters:
- destination- Member[] - the destination for this message
- msg- ChannelMessage - the message to be sent
- payload- InterceptorPayload - the payload, carrying an error handler and future useful data, can be null
- Throws:
- ChannelException- if a serialization error happens.
- See Also:
 
- 
messageReceivedDescription copied from interface:ChannelInterceptorthemessageReceivedis invoked when a message is received.ChannelMessage.getAddress()is the sender, or the reply-to address if it has been overwritten.- Specified by:
- messageReceivedin interface- ChannelInterceptor
- Overrides:
- messageReceivedin class- ChannelInterceptorBase
- Parameters:
- msg- ChannelMessage
 
- 
memberAddedDescription copied from interface:MembershipListenerA member was added to the group- Specified by:
- memberAddedin interface- MembershipListener
- Overrides:
- memberAddedin class- ChannelInterceptorBase
- Parameters:
- member- Member - the member that was added
 
- 
memberAdded
- 
memberDisappearedDescription copied from interface:MembershipListenerA member was removed from the group
 If the member left voluntarily, the Member.getCommand will contain the Member.SHUTDOWN_PAYLOAD data- Specified by:
- memberDisappearedin interface- MembershipListener
- Overrides:
- memberDisappearedin class- ChannelInterceptorBase
- Parameters:
- member- Member
- See Also:
 
- 
isHighestpublic boolean isHighest()
- 
isCoordinatorpublic boolean isCoordinator()
- 
heartbeatpublic void heartbeat()Description copied from interface:ChannelInterceptorTheheartbeat()method gets invoked periodically to allow interceptors to clean up resources, time out object and perform actions that are unrelated to sending/receiving data.- Specified by:
- heartbeatin interface- ChannelInterceptor
- Specified by:
- heartbeatin interface- Heartbeat
- Overrides:
- heartbeatin class- ChannelInterceptorBase
 
- 
hasMemberspublic boolean hasMembers()has members- Specified by:
- hasMembersin interface- ChannelInterceptor
- Overrides:
- hasMembersin class- ChannelInterceptorBase
- Returns:
- boolean - if the channel has members in its membership group
- See Also:
 
- 
getMembersGet all current cluster members- Specified by:
- getMembersin interface- ChannelInterceptor
- Overrides:
- getMembersin class- ChannelInterceptorBase
- Returns:
- all members or empty array
- See Also:
 
- 
getMemberDescription copied from interface:ChannelInterceptorIntercepts theChannel.getMember(Member)method- Specified by:
- getMemberin interface- ChannelInterceptor
- Overrides:
- getMemberin class- ChannelInterceptorBase
- Parameters:
- mbr- Member
- Returns:
- Member
- See Also:
 
- 
getLocalMemberReturn the member that represents this node.- Specified by:
- getLocalMemberin interface- ChannelInterceptor
- Overrides:
- getLocalMemberin class- ChannelInterceptorBase
- Parameters:
- incAlive- boolean
- Returns:
- Member
- See Also:
 
- 
setupMembershipprotected void setupMembership()
- 
fireInterceptorEvent- Specified by:
- fireInterceptorEventin interface- ChannelInterceptor
- Overrides:
- fireInterceptorEventin class- ChannelInterceptorBase
 
 
-