Zookeeper's "session termination" appears.

Source: Internet
Author: User

Reprint Please specify source: Jiq's technical Blog

After my tests, I came to the following conclusions about session termination:

The client creates a zookeeper instance to connect to the zookeeper server and sets the session timeout to 10s.

(1) If the zookeeper server is forced to shut down (simulating its crash), the client immediately receives the disconnected connection disconnect event, waits half an hour, starts the zookeeper server again, the client receives the Syncconnected connection establishment event, The previously registered watcher is still valid, and the temporary node is presumed to be as effective.

(2) If disconnecting the network cable from the client and the server (analog networks disconnected), the client immediately receives the disconnected connection disconnect event, waits for 11s to plug in the network cable again, the client receives the expired session termination event, and the previously registered watcher is no longer valid, presumably the temporary node will also be deleted.


Conclusion:

The client connection to the server session time-out is logged by the server, the client sends a heartbeat to the server at intervals (less than this session time), and the server does not set the session with this client as invalid!!!

If the service side itself died, will not give this session event countdown, if the client shuts down or disconnect, will be a session termination countdown, the time will be determined that the session is invalid, the client died.


There is also the possibility of session termination: That is, the client and the service-side network Flash, and then the zookeeper client will automatically select the next zookeeper server from the service-side address list to re-connect, if the re-connect time exceeds the set timeout time, such as 10s, also send session timeout.

You might say how the re-connect is more than 10s? If you are at the client and the countdown is still 7s time to send the heartbeat to the server, unfortunately the network just disconnect, and then choose a re-connected, the re-connected more than 3s, then timed out.


So, when client developers are doing zookeeper development, if you register to watcher on the server and create temporary nodes that are critical, you may not need to worry about the session invalidation because the zookeeper servers hang up, but be sure to be defensive because the network is disconnected, Then after the session timeout period and then automatically re-establish the connection, because this time the session terminated!!!

If the network often flash, but the disconnection time is not long, in order to seek insurance, you can talk about the session timeout set a bit longer, generally 30s.

Can I set it to an infinite length, such as a few days?


My test procedure is attached below:

Clients registered with the watcher:

public class Zookeepertest implements watcher{private countdownlatch latch = new Countdownlatch (1);p ublic ZooKeeper Getco Nnection () throws Exception{zookeeper ZK = new ZooKeeper ("192.168.1.108:2181", this); latch.await (); return ZK;} @Override public     void Process (Watchedevent event) {         System.out.println ("Received events Notification:" + event.getstate () + "\ n path is:" +event.getpath ());         if (keeperstate.syncconnected = = Event.getstate ()) {             latch.countdown ();         }     }     public static void Main (string[] args) throws Exception    {    zookeepertest obj = new Zookeepertest ();    ZooKeeper ZK = obj. Getconnection ();        System.in.read ();    Zk.close ();    } }

First the zookeeper server is forced to shut down, after a long, long time to re-open, the output is as follows:

Receive event notification: syncconnected
Path is: null
Receive event notification: disconnected
Path is: null
Receive event notification: syncconnected
Path is: null


And then run my own program to modify the Zookeeper node:

public class Nodechangetest implements watcher{private countdownlatch latch = new Countdownlatch (1);p ublic ZooKeeper GetC Onnection () throws Exception{zookeeper ZK = new ZooKeeper ("192.168.1.108:2181", this); latch.await (); return ZK;} @Override public     void Process (Watchedevent event) {         System.out.println ("received incident notification:" + event.getstate () + "\ n"  );         if (keeperstate.syncconnected = = Event.getstate ()) {             latch.countdown ();         }} public     static void main (String [] args) throws Exception {nodechangetest test = new Nodechangetest (); ZooKeeper ZK = Test. Getconnection (); Zk.setdata ("/test", "ZNode2". GetBytes (),-1); Zk.close ();}}


Receive the following information indicating the watch time trigger:

Receive event notification: syncconnected
The path is:/test

If the network cable is disconnected, after 5s and plug will have the following output:

Receive event notification: syncconnected
Path is: null
Receive event notification: disconnected
Path is: null
Receive event notification: Expired
Path is: null


The program that modifies the Zookeeper node also has no response.

Zookeeper's "session termination" appears.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.