Class QuorumCnxManager
java.lang.Object
org.apache.zookeeper.server.quorum.QuorumCnxManager
This class implements a connection manager for leader election using TCP. It
maintains one connection for every pair of servers. The tricky part is to
guarantee that there is exactly one connection for every pair of servers that
are operating correctly and that can communicate over the network.
If two servers try to start a connection concurrently, then the connection
manager uses a very simple tie-breaking mechanism to decide which connection
to drop based on the IP addressed of the two parties.
For every peer, the manager maintains a queue of messages to send. If the
connection to any particular peer drops, then the sender thread puts the
message back on the list. As this implementation currently uses a queue
implementation to maintain messages to send to another peer, we add the
message to the tail of the queue, thus changing the order of messages.
Although this is not a problem for the leader election, it could be a problem
when consolidating peer communication. This is to be verified, though.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
class
Thread to listen on some portsstatic class
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final long
static final long
-
Constructor Summary
ConstructorsConstructorDescriptionQuorumCnxManager
(QuorumPeer self, long mySid, Map<Long, QuorumPeer.QuorumServer> view, QuorumAuthServer authServer, QuorumAuthLearner authLearner, int socketTimeout, boolean listenOnAllIPs, int quorumCnxnThreadsSize, boolean quorumSaslAuthEnabled) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts an element in therecvQueue
.void
Try to establish a connection with each server if one doesn't exist.boolean
connectedToPeer
(long peerSid) long
Return number of connection processing threads.long
Return number of worker threadsvoid
halt()
Flag that it is time to wrap up all activities and interrupt the listener.void
initiateConnection
(MultipleAddresses electionAddr, Long sid) First we create the socket, perform SSL handshake and authentication if needed.boolean
initiateConnectionAsync
(MultipleAddresses electionAddr, Long sid) Server will initiate the connection request to its peer server asynchronously via separate connection thread.boolean
pollRecvQueue
(long timeout, TimeUnit unit) Retrieves and removes a message at the head of this queue, waiting up to the specified wait time if necessary for an element to become available.void
receiveConnection
(Socket sock) If this server receives a connection request, then it gives up on the new connection if it wins.void
receiveConnectionAsync
(Socket sock) Server receives a connection request and handles it asynchronously via separate thread.void
softHalt()
A soft halt simply finishes workers.void
testInitiateConnection
(long sid) Invokes initiateConnection for testing purposesvoid
toSend
(Long sid, ByteBuffer b) Processes invoke this message to queue a message to send.
-
Field Details
-
PROTOCOL_VERSION_V1
public static final long PROTOCOL_VERSION_V1- See Also:
-
PROTOCOL_VERSION_V2
public static final long PROTOCOL_VERSION_V2- See Also:
-
maxBuffer
public static final int maxBuffer- See Also:
-
recvQueue
-
listener
-
-
Constructor Details
-
QuorumCnxManager
public QuorumCnxManager(QuorumPeer self, long mySid, Map<Long, QuorumPeer.QuorumServer> view, QuorumAuthServer authServer, QuorumAuthLearner authLearner, int socketTimeout, boolean listenOnAllIPs, int quorumCnxnThreadsSize, boolean quorumSaslAuthEnabled)
-
-
Method Details
-
testInitiateConnection
public void testInitiateConnection(long sid) Invokes initiateConnection for testing purposes- Parameters:
sid
-
-
initiateConnection
First we create the socket, perform SSL handshake and authentication if needed. Then we perform the initiation protocol. If this server has initiated the connection, then it gives up on the connection if it loses challenge. Otherwise, it keeps the connection. -
initiateConnectionAsync
Server will initiate the connection request to its peer server asynchronously via separate connection thread. -
receiveConnection
If this server receives a connection request, then it gives up on the new connection if it wins. Notice that it checks whether it has a connection to this server already or not. If it does, then it sends the smallest possible long value to lose the challenge. -
receiveConnectionAsync
Server receives a connection request and handles it asynchronously via separate thread. -
toSend
Processes invoke this message to queue a message to send. Currently, only leader election uses it. -
connectAll
public void connectAll()Try to establish a connection with each server if one doesn't exist. -
halt
public void halt()Flag that it is time to wrap up all activities and interrupt the listener. -
softHalt
public void softHalt()A soft halt simply finishes workers. -
getThreadCount
public long getThreadCount()Return number of worker threads -
getConnectionThreadCount
public long getConnectionThreadCount()Return number of connection processing threads. -
addToRecvQueue
Inserts an element in therecvQueue
. If the Queue is full, this methods removes an element from the head of the Queue and then inserts the element at the tail of the queue.- Parameters:
msg
- Reference to the message to be inserted in the queue
-
pollRecvQueue
public QuorumCnxManager.Message pollRecvQueue(long timeout, TimeUnit unit) throws InterruptedException Retrieves and removes a message at the head of this queue, waiting up to the specified wait time if necessary for an element to become available.BlockingQueue.poll(long, java.util.concurrent.TimeUnit)
- Throws:
InterruptedException
-
connectedToPeer
public boolean connectedToPeer(long peerSid) -
isReconfigEnabled
public boolean isReconfigEnabled()
-