Package org.jgroups.blocks
Class VotingAdapter
- java.lang.Object
-
- org.jgroups.blocks.VotingAdapter
-
- All Implemented Interfaces:
VoteResponseProcessor,MembershipListener,MessageListener
public class VotingAdapter extends java.lang.Object implements MessageListener, MembershipListener, VoteResponseProcessor
Voting adapter provides a voting functionality for an application. There should be at most oneVotingAdapterlistening on oneChannelinstance. Each adapter can have zero or more registeredVotingListenerinstances that will be called during voting process.Decree is an object that has some semantic meaning within the application. Each voting listener receives a decree and can respond with either
trueor false. If the decree has no meaning for the voting listener, it is required to throwVoteException. In this case this specific listener will be excluded from the voting on the specified decree. After performing local voting, this voting adapter sends the request back to the originator of the voting process. Originator receives results from each node and decides if all voting process succeeded or not depending on the consensus type specified during voting.- Version:
- $Id: VotingAdapter.java,v 1.10 2006/09/27 12:42:53 belaban Exp $
- Author:
- Roman Rokytskyy (rrokytskyy@acm.org), Robert Schaffar-Taurok (robert@fusion.at)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classVotingAdapter.FailureVoteResultClass that represents a result of local voting on the failed node.static classVotingAdapter.VoteResultThis class represents the result of local voting.
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.commons.logging.Loglogstatic intVOTE_ALLThis consensus type means that at least one positive vote and no negative votes are required for the voting to succeed.static intVOTE_ANYThis consensus type means that at least one positive vote is required for the voting to succeed.static intVOTE_MAJORITYThis consensus type means that number of positive votes should be greater than number of negative votes.
-
Constructor Summary
Constructors Constructor Description VotingAdapter(PullPushAdapter adapter, java.io.Serializable id)VotingAdapter(Channel channel)Creates an instance of the VoteChannel that uses JGroups for communication between group members.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddMembershipListener(MembershipListener l)voidaddVoteListener(VotingListener listener)Adds voting listener.voidblock()Blocks the channel until the ViewAccepted is invoked.static java.lang.StringgetConsensusStr(int consensusType)Convert consensus type into string representation.java.util.CollectiongetMembers()byte[]getState()Get the channel state.VotingAdapter.VoteResultlocalVote(java.lang.Object decree)This method performs voting on the specific decree between all local voteListeners.booleanprocessResponses(RspList responses, int consensusType, java.lang.Object decree)Processes the response list and makes a decision according to the type of the consensus for current voting.voidreceive(Message msg)Receive the message.voidremoveMembershipListener(MembershipListener l)voidremoveVoteListener(VotingListener listener)Removes voting listener.voidsetState(byte[] state)Set the channel state.voidsuspect(Address suspected)Callback for notification that one node is suspectedvoidviewAccepted(View newView)Callback for notification about the new view of the group.booleanvote(java.lang.Object decree, int consensusType, long timeout)Performs actual voting on the VoteChannel using the JGroups facilities for communication.booleanvote(java.lang.Object decree, int consensusType, long timeout, VoteResponseProcessor voteResponseProcessor)Performs actual voting on the VoteChannel using the JGroups facilities for communication.booleanvote(java.lang.Object decree, long timeout)Vote on the specified decree requiring all nodes to vote.booleanvote(java.lang.Object decree, long timeout, VoteResponseProcessor voteResponseProcessor)Vote on the specified decree requiring all nodes to vote.
-
-
-
Field Detail
-
VOTE_ANY
public static final int VOTE_ANY
This consensus type means that at least one positive vote is required for the voting to succeed.- See Also:
- Constant Field Values
-
VOTE_ALL
public static final int VOTE_ALL
This consensus type means that at least one positive vote and no negative votes are required for the voting to succeed.- See Also:
- Constant Field Values
-
VOTE_MAJORITY
public static final int VOTE_MAJORITY
This consensus type means that number of positive votes should be greater than number of negative votes.- See Also:
- Constant Field Values
-
log
protected final org.apache.commons.logging.Log log
-
-
Constructor Detail
-
VotingAdapter
public VotingAdapter(Channel channel)
Creates an instance of the VoteChannel that uses JGroups for communication between group members.- Parameters:
channel- JGroups channel.
-
VotingAdapter
public VotingAdapter(PullPushAdapter adapter, java.io.Serializable id)
-
-
Method Detail
-
getMembers
public java.util.Collection getMembers()
-
addMembershipListener
public void addMembershipListener(MembershipListener l)
-
removeMembershipListener
public void removeMembershipListener(MembershipListener l)
-
vote
public boolean vote(java.lang.Object decree, int consensusType, long timeout) throws ChannelExceptionPerforms actual voting on the VoteChannel using the JGroups facilities for communication.- Throws:
ChannelException
-
vote
public boolean vote(java.lang.Object decree, int consensusType, long timeout, VoteResponseProcessor voteResponseProcessor) throws ChannelExceptionPerforms actual voting on the VoteChannel using the JGroups facilities for communication.- Throws:
ChannelException
-
processResponses
public boolean processResponses(RspList responses, int consensusType, java.lang.Object decree) throws ChannelException
Processes the response list and makes a decision according to the type of the consensus for current voting.Note: we do not support voting in case of Byzantine failures, i.e. when the node responds with the fault message.
- Specified by:
processResponsesin interfaceVoteResponseProcessor- Parameters:
responses- The responsesconsensusType- The consensusType of the votedecree- The vote decree- Returns:
- boolean
- Throws:
ChannelException
-
viewAccepted
public void viewAccepted(View newView)
Callback for notification about the new view of the group.- Specified by:
viewAcceptedin interfaceMembershipListener
-
suspect
public void suspect(Address suspected)
Callback for notification that one node is suspected- Specified by:
suspectin interfaceMembershipListener
-
block
public void block()
Blocks the channel until the ViewAccepted is invoked.- Specified by:
blockin interfaceMembershipListener
-
getState
public byte[] getState()
Get the channel state.- Specified by:
getStatein interfaceMessageListener- Returns:
- always
null, we do not have any group-shared state.
-
receive
public void receive(Message msg)
Receive the message. All messages are ignored.- Specified by:
receivein interfaceMessageListener- Parameters:
msg- message to check.
-
setState
public void setState(byte[] state)
Set the channel state. We do nothing here.- Specified by:
setStatein interfaceMessageListener
-
vote
public boolean vote(java.lang.Object decree, long timeout) throws ChannelExceptionVote on the specified decree requiring all nodes to vote.- Parameters:
decree- decree on which nodes should vote.timeout- time during which nodes can vote.- Returns:
trueif nodes agreed on a decree, otherwisefalse- Throws:
ChannelException- if something went wrong.
-
vote
public boolean vote(java.lang.Object decree, long timeout, VoteResponseProcessor voteResponseProcessor) throws ChannelExceptionVote on the specified decree requiring all nodes to vote.- Parameters:
decree- decree on which nodes should vote.timeout- time during which nodes can vote.voteResponseProcessor- processor which will be called for every response that is received.- Returns:
trueif nodes agreed on a decree, otherwisefalse- Throws:
ChannelException- if something went wrong.
-
addVoteListener
public void addVoteListener(VotingListener listener)
Adds voting listener.
-
removeVoteListener
public void removeVoteListener(VotingListener listener)
Removes voting listener.
-
localVote
public VotingAdapter.VoteResult localVote(java.lang.Object decree)
This method performs voting on the specific decree between all local voteListeners.
-
getConsensusStr
public static java.lang.String getConsensusStr(int consensusType)
Convert consensus type into string representation. This method is useful for debugginf.- Parameters:
consensusType- type of the consensus.- Returns:
- string representation of the consensus type.
-
-