6.0 Basic Principles of Zookeeper Session
Category Zookeeper Tutorial
The connection between the client and the server is based on a persistent TCP connection, with the client connecting to the server's default port 2181, which is also the session session.
From the moment the first connection is established, the client begins the lifecycle of the session, with the client sending ping packet requests to the server, and each session can set a timeout period.
Session Creation
sessionID: Session ID, used to uniquely identify a session. Each time the client creates a session, Zookeeper assigns a globally unique sessionID. The source code for creating a sessionID in Zookeeper is similar to SessionTrackerImpl.
Timeout: Session timeout time. When the client constructs an instance of Zookeeper, it sends the configured timeout time to the server, and the server will finally confirm the session timeout time according to its own timeout time limit.
TickTime: The next session timeout time point, with a default of 2000 milliseconds. It can be configured in the zoo.cfg configuration file, facilitating the server's bucket strategy management for session sessions.
isClosing: This attribute marks whether a session has been closed. When the server detects that the session has expired and is invalid, the session is marked as "closed" and no longer processes new requests for that session.
Session Status
Below are several important states:
-
connecting: Connecting, once the session is established, the status is connecting, which is very short in time.
-
connected: Connected, the status after a successful connection.
-
closed: Closed, which occurs when the session expires, usually due to network failure, client reconnection failure, server downtime, or client-initiated disconnection.
Session Timeout Management (Bucket Strategy + Session Activation)
During operation, the leader server of Zookeeper periodically checks for session timeouts at intervals of ExpirationInterval, in milliseconds, with the default value being tickTime, and performs a session timeout check every tickTime.
The calculation method for ExpirationTime is:
ExpirationTime = CurrentTime + SessionTimeout;
ExpirationTime = (ExpirationTime / ExpirationInterval + 1) * ExpirationInterval;
During the operation of Zookeeper, the client sends requests (including read and write) or ping requests to the server within the session timeout expiration range, commonly known as heartbeat detection to complete session activation, thereby maintaining the validity of the session.
Session activation process:
After activation, the process of migrating the session is carried out, and then a new round begins:
5.0 ZooKeeper Data Model znode Structure Detailed Explanation
6.0 Basic Principles of Zookeeper Session