Skip to content

2. AutoReconnect (Automatic Reconnection)

Pierre-Antoine Ganaye edited this page Jul 27, 2014 · 2 revisions

AutoReconnect is a dedicated feature of finagle-zookeeper, it allows the client (if activated) to manage the connection and session states during its life. For example each time a request is sent, the connection and session states are checked before sending a request, if everything is ok, the request is sent to the server, otherwise they are two possible cases :

  • Connection loss : the connection between the client and the server is no longer valid, AutoLinkManager will try to reconnect to a new server ( done faster if preventive search has already found an available server ), and send a ConnectRequest to the server using the last session's Ids. If the session is still valid, the client will reconnect smoothly to the server and set ZooKeeeper request sending back ( by unlocking PreProcessService ). If the session is no longer valid it will try to reconnect with a new session.

  • Session expired : the server has expired the session, the client will try to reconnect with a new session. Once the reconnection is done, it will set ZooKeeeper request sending back.

Each time ReconnectWithSession and ReconnectWithoutSession are called, the connection between the client and the server is checked, if it's broken the client will look for a new server ( priority to RW server ) and connect to it.

Handling failure during session Once the client is correctly connected to a server, PreProccessService is unlocked, this service is in charge of preparing each ZooKeeper request. By using an AsyncSemaphore we can ensure requests ordering. During reconnection the service is locked ( which will queue all incoming requests ) and then unlocked.

If connection is lost after sending a request, all pending requests ( ie requests without responses ) will be satisfied by CancelledRequestException.

About reconnection retries You can configure different options to let the client try to reconnect at multiple times :

  • timeBetweenAttempts : the duration between each reconnection attempts.

  • maxConsecutiveRetries : the maximum number of consecutive retries during reconnection.

  • maxReconnectAttempts : the total maximum number of reconnection attempts.

For example if timeBetweenAttempts = Some(1.minute), maxConsecutivesRetries = 10 and maxReconnectRetries = 5 then in the worst case, the client will try to reconnect 10 times consecutively, wait one minute, retry 10 times and so on until it has done this action 5 times.

Automatic Read-Write server search

This option allows the client to look for a new Read-Write mode server to connect to if the client is currently connected to a Read-Only mode server. Here is how it works, once the client has detected that it is connected to a RO mode server, it will start RW mode server search, by default the client will look for a RW server every minute ( autoRwServerSearch param ). Once the server is found, PreProcessService is locked, client reconnects with the last session ids or with a new session. After reconnection the PreProcessService is unlocked and pending requests are sent.

Preventive search

This option gives the possibility to index available RW and RO mode servers (one RW and one RO). It can increase reconnection speed after connection loss. The parameter to set up this option is preventiveSearch, by default the client will call preventiveRwServerSearch every 10 minutes.

Link checker

This option gives the possibility to check the connection and session states, the parameter to set up this option is timeBetweenLinkCheck, by default the client will operate a verification every 30 seconds. If something wrong is detected it will operate as described in the AutoReconnect section

Connection

There is no retry policy during first connection to a server, if the connection fails, it will throw an Exception without reconnecting.

Disconnection

If the deconnection fails, the client will not try to reconnect to a server and send a new closeSession request.


Related

How to set up AutoReconnect ?