Original link http://www.chepoo.com/zookeeper-session-timeout.html
1. Session Overview
In zookeeper, when a connection is established between the client and the server, the session is established, generating a globally unique session ID.
A long connection is maintained between the server and the client, and during session_timeout time, the server determines whether the client
Normal connection (the client periodically sends Heart_beat to the server, and the server resets the next session_timeout time).
Thus, under normal circumstances, the session is valid and the session information is saved on all the machines in the ZK cluster.
In the event of a network or other problem (for example, the ZK machine that the client is connected to hangs, or a network flash for any other reason),
The connection between the client and the server currently connected is broken, and this time the client will actively
Address List (when instantiating a ZK object, the parameter passed into the constructor method ConnectString) selects the new address to connect to.
2, the connection is broken
Well, it's basically the process of maintaining a session between the server and the client. In this process,
Users may see two types of exception Connectionloss (disconnection) and sessionexpired (session expiration).
Connection disconnect (Connectionloss) typically occurs when the network is flashing or the client is connected to a server that hangs
In this case, the zookeeper client will first perceive the exception, and the logic is triggered in the following way:
1234567891011121314151617181920212223242526 |
void Org.apache.zookeeper.ClientCnxn.SendThread.run () {...} catch (Throwable e) {if (closing) { if (log.isdebugenabled ()) {//closing so the is expected Log.debug ("a exception was T Hrown while closing send thread for session 0x "+ long.tohexstring (GetSessionID ()) + ":" + e.getmessage ()); } break; } else {//This is ugly, and you have a better to speak up if (e instanceof sessionexpiredexception) {Log.info (E.getmessage () + ", closing socket Connection"); } else if (e instanceof sessiontimeoutexception) {log.info (E.getmessage () + retry_conn_msg); } else if (e instanceof endofstreamexception) {log.info (E.getmessage () + retry_conn_msg); } else if (e instanceof rwserverfoundexception) {log.info (E.getmessage ()); } else {...}..} |
One scenario is that the server server hangs up, and this time, the ZK client prefers to catch exceptions, as follows:
After catching the exception, the ZK client prints a log similar to the following:
1 |
Endofstreamexception:unable to read additional data from server SessionID 0x13ab17ad9ec000b, likely server have closed Soc Ket |
Then do some work on the aftermath of the socket connection. Next is the client to re-select a server IP to try to connect, as shown in code c2-1,
The main thing here is to get a new server address from the address list to connect.
123456789101112131415161718192021222324252627 |
[c2-1] private void Startconnect () throws IOException {state = states.connecting; inetsocketaddress addr; if (rwserveraddress! = null) {addr = rwserveraddress; Rwserveraddress = null; } else {addr = Hostprovider.next (1000); } Log.info ("Opening socket connection to server" + addr); SetName (GetName (). replaceall ("\ \ *\\", "(" + addr.gethostname () + ":" + addr.getport () + ")"); try {zookeepersaslclient = new zookeepersaslclient ("zookeeper/" +addr.gethostname ()); } catch (Loginexception e) {Log.warn ("SASL Authentication failed:" + E + "would continue connection to Zookeeper Server without "+" SASL authentication, if Zookeeper server allows it. "); Eventthread.queueevent (New Watchedevent (Watcher.Event.EventType.None, Watcher.Event.Keepe rstate.authfailed, null)); } clientcnxnsocket.connect (addr); } |
During the program run, the entire process log is printed roughly as follows:
12345678910111213141516 |
2012-10-31 09:09:57,379-info [Main-sendthread (test.zookeeper.connection_string2:2181): [email protected]]- Unable to read additional data from server SessionID 0x23ab45c87df0000, likely server have closed socket, closing socket CO Nnection and attempting reconnect receive event notifications: Disconnected Get data success, Path:/nileader 2012-10-31 09:09:58,293-info [ [Email protected]] -Opening socket connection to server/1.2.1.1:2181 2012-10-31 09:09:58,294-warn [[email protected]]-secur ItyException:java.lang.SecurityException:Unable to locate a login configuration occurred when trying to find JAAS config Uration. 2012-10-31 09:09:58,295-info [[email protected]]-Client would not sasl-authenticate because the default JAA S configuration section ' Client ' could not being found. If you is not using SASL, your may ignore this. On the other hand, if you expected SASL to work, please fix your JAAS configuration. 2012-10-31 09:09:58,296-info [[Email protecTed]-Socket connection established to test.zookeeper.connection_string/1.2.1.1:2181, initiating session 2012-10-31 09:09:58,299-info [[email protected]]-Session establishment complete on server Test.zookeeper.connect ion_string/1.2.1.1:2181, SessionID = 0x23ab45c87df0000, negotiated timeout = 10000 received event notification: syncconnected |
Therefore, the process of "disconnection" is now at a glance, the core process is as follows:
ZK client captures "disconnected" exception--Get a new ZK address--try to connect
In this process, we can find that the whole process does not require the developer additional program intervention, is the ZK client itself will carry out,
Also, the session ID used is the same, so the conclusion is: Connectionloss occurs,
The application does not need to do anything, wait for the ZK client to establish a new connection.
3. Session Timeout
Sessionexpired occurs in the Blue text section above, this is usually the ZK client's connection to the server is broken, trying to connect to the new ZK machine,
However, if this process takes too long, it has not successfully connected to the server after session_timeout.
Then the server thinks the session is over (the server cannot confirm whether the client is actively ending the session because of other exceptions),
Since in ZK, many data and States are bound to the session, once the conversation fails, then ZK begins to clear the information about this session,
Includes the temporary node created by this session and all watcher registered. After this, the client may reconnect to the server after the network is restored.
Unfortunately, the server tells the client an exception: Sessionexpired (session expires). At this point the state of the client becomes closed state,
The application to do is to see their own application of the complex program, to re-instance zookeeper object,
Then all temporary data (including temporary nodes and registration watcher) is re-operated, in short, the session timeout is real in the ZK usage process.
So here is a simple summary, once a session timeout occurs, then all temporary data stored on ZK and registered subscribers will be removed,
A zookeeper client instance needs to be recreated at this point, and some additional processing is required for your own coding.
4. Session time
As already mentioned in the article "ZooKeeper API usage", when instantiating a ZK client, you need to set a session time-out.
One thing to note here is that the client is not free to set this session timeout, there is a limit to the session timeout on the ZK server side, the master
If both parameters of Minsessiontimeout and Maxsessiontimeout are set. (See this article "Zookeeper Administrator's Guide" for more information) session time-out limit,
If the timeout for the client setting is not in this range, it is forced to be set to the maximum or minimum time.
The default session timeout period is 2 * ticktime * ticktime. ZK is implemented as follows, which is handled in this method:
Zookeeperserver.processconnectrequest (Servercnxn cnxn, Bytebuffer incomingbuffer)
Therefore, if the application has special requirements for this session timeout, be sure to communicate with the ZK administrator to confirm that the server has set a limit on the session time.
"Go" ZooKeeper Session timeout