Database Reference
In-Depth Information
A common tick time setting is 2 seconds (2,000 milliseconds). This translates to an allow-
able session timeout of between 4 and 40 seconds.
There are a few considerations in selecting a session timeout. A low session timeout leads
to faster detection of machine failure. In the group membership example, the session
timeout is the time it takes for a failed machine to be removed from the group. Beware of
setting the session timeout too low, however, because a busy network can cause packets to
be delayed and may cause inadvertent session expiry. In such an event, a machine would
appear to “flap”: leaving and then rejoining the group repeatedly in a short space of time.
Applications that create more complex ephemeral state should favor longer session
timeouts, as the cost of reconstruction is higher. In some cases, it is possible to design the
application so it can restart within the session timeout period and avoid session expiry.
(This might be desirable to perform maintenance or upgrades.) Every session is given a
unique identity and password by the server, and if these are passed to ZooKeeper while a
connection is being made, it is possible to recover a session (as long as it hasn't expired).
An application can therefore arrange a graceful shutdown, whereby it stores the session
identity and password to stable storage before restarting the process, retrieving the stored
session identity and password, and recovering the session.
You should view this feature as an optimization that can help avoid expired sessions. It
does not remove the need to handle session expiry, which can still occur if a machine fails
unexpectedly, or even if an application is shut down gracefully but does not restart before
its session expires, for whatever reason.
As a general rule, the larger the ZooKeeper ensemble, the larger the session timeout
should be. Connection timeouts, read timeouts, and ping periods are all defined internally
as a function of the number of servers in the ensemble, so as the ensemble grows, these
periods decrease. Consider increasing the timeout if you experience frequent connection
loss. You can monitor ZooKeeper metrics — such as request latency statistics — using
JMX.
States
The ZooKeeper object transitions through different states in its lifecycle (see Fig-
ure 21-3 ) . You can query its state at any time by using the getState() method:
public States getState ()
States is an enum representing the different states that a ZooKeeper object may be in.
(Despite the enum's name, an instance of ZooKeeper may be in only one state at a
time.) A newly constructed ZooKeeper instance is in the CONNECTING state while it
Search WWH ::




Custom Search