ZooKeeper 类是ZooKeeper 客户端库的主要类。要使用ZooKeeper服务,应用程序必须首先实例化ZooKeeper类的对象。所有的迭代都将通过调用ZooKeeper类的方法来完成。除非另有说明,否则此类的方法是线程安全的。
一旦建立到服务器的连接,会话ID就分配给客户机。客户机将定期向服务器发送心跳以保持会话有效。
只要客户端的会话ID保持有效,应用程序就可以通过客户端调用ZooKeeper API。
如果由于某种原因,客户机在很长一段时间内(例如超过sessionTimeout值)无法向服务器发送心跳,服务器将使会话过期,会话ID将变为无效。客户端对象将不再可用。要进行ZooKeeper API调用,应用程序必须创建一个新的客户机对象。
如果客户机当前连接到的ZooKeeper服务器出现故障或没有响应,客户机将在其会话ID过期之前自动尝试连接到其他服务器。如果成功,应用程序可以继续使用客户端。
ZooKeeper API方法要么是同步的,要么是异步的。同步方法会一直阻塞,直到服务器响应为止。异步方法只是将发送请求排队,然后立即返回。它们接受一个回调对象,该对象将在成功执行请求或出错时执行,返回代码(rc)指示错误。
一些成功的ZooKeeper API调用可以将监视留在ZooKeeper服务器的“数据节点”上。其他成功的ZooKeeper API调用可以触发这些监视。一旦一个手表被触发,一个事件将被传递给客户机,客户机首先离开了手表。每个表只能触发一次。因此,每离开一只手表,最多会向客户发送一个事件。
客户机需要一个实现观察程序接口的类的对象来处理传递给客户机的事件。当客户机断开当前连接并重新连接到服务器时,所有现有的监视都被认为是被触发的,但未传递的事件将丢失。为了模拟这一点,客户机将生成一个特殊的事件来告诉事件处理程序连接已经断开。此特殊事件具有类型eventnone和状态skeeperstatedisconnected。
This is the main class of ZooKeeper client library. To use a ZooKeeper service, an application must first instantiate an object of ZooKeeper class. All the iterations will be done by calling the methods of ZooKeeper class. The methods of this class are thread-safe unless otherwise noted.
Once a connection to a server is established, a session ID is assigned to the client. The client will send heart beats to the server periodically to keep the session valid.
The application can call ZooKeeper APIs through a client as long as the session ID of the client remains valid.
If for some reason, the client fails to send heart beats to the server for a prolonged period of time (exceeding the sessionTimeout value, for instance), the server will expire the session, and the session ID will become invalid. The client object will no longer be usable. To make ZooKeeper API calls, the application must create a new client object.
If the ZooKeeper server the client currently connects to fails or otherwise does not respond, the client will automatically try to connect to another server before its session ID expires. If successful, the application can continue to use the client.
The ZooKeeper API methods are either synchronous or asynchronous. Synchronous methods blocks until the server has responded. Asynchronous methods just queue the request for sending and return immediately. They take a callback object that will be executed either on successful execution of the request or on error with an appropriate return code (rc) indicating the error.
Some successful ZooKeeper API calls can leave watches on the "data nodes" in the ZooKeeper server. Other successful ZooKeeper API calls can trigger those watches. Once a watch is triggered, an event will be delivered to the client which left the watch at the first place. Each watch can be triggered only once. Thus, up to one event will be delivered to a client for every watch it leaves.
A client needs an object of a class implementing Watcher interface for processing the events delivered to the client. When a client drops current connection and re-connects to a server, all the existing watches are considered as being triggered but the undelivered events are lost. To emulate this, the client will generate a special event to tell the event handler a connection has been dropped. This special event has type EventNone and state sKeeperStateDisconnected.