Zookeeper 简明教程

Zookeeper - Fundamentals

在深入探讨 ZooKeeper 的工作原理之前,让我们了解一下 ZooKeeper 的基本概念。在本章中,我们将讨论以下主题:

Before going deep into the working of ZooKeeper, let us take a look at the fundamental concepts of ZooKeeper. We will discuss the following topics in this chapter −

  1. Architecture

  2. Hierarchical namespace

  3. Session

  4. Watches

Architecture of ZooKeeper

请看以下图表。它描述了 ZooKeeper 的“客户端-服务器架构”。

Take a look at the following diagram. It depicts the “Client-Server Architecture” of ZooKeeper.

architecture of zookeeper

ZooKeeper 架构的一部分的每个组件在以下表格中都有说明。

Each one of the components that is a part of the ZooKeeper architecture has been explained in the following table.

Part

Description

Client

Clients, one of the nodes in our distributed application cluster, access information from the server. For a particular time interval, every client sends a message to the server to let the sever know that the client is alive. Similarly, the server sends an acknowledgement when a client connects. If there is no response from the connected server, the client automatically redirects the message to another server.

Server

Server, one of the nodes in our ZooKeeper ensemble, provides all the services to clients. Gives acknowledgement to client to inform that the server is alive.

Ensemble

Group of ZooKeeper servers. The minimum number of nodes that is required to form an ensemble is 3.

Leader

Server node which performs automatic recovery if any of the connected node failed. Leaders are elected on service startup.

Follower

Server node which follows leader instruction.

Hierarchical Namespace

以下图表描述了用于内存表示的 ZooKeeper 文件系统的树结构。ZooKeeper 节点称为 znode 。每个 znode 由一个名称标识并由路径(/)序列分隔。

The following diagram depicts the tree structure of ZooKeeper file system used for memory representation. ZooKeeper node is referred as znode. Every znode is identified by a name and separated by a sequence of path (/).

  1. In the diagram, first you have a root znode separated by “/”. Under root, you have two logical namespaces config and workers.

  2. The config namespace is used for centralized configuration management and the workers namespace is used for naming.

  3. Under config namespace, each znode can store upto 1MB of data. This is similar to UNIX file system except that the parent znode can store data as well. The main purpose of this structure is to store synchronized data and describe the metadata of the znode. This structure is called as ZooKeeper Data Model.

hierarchical namespace

ZooKeeper 数据模型中的每个 znode 都维护一个 stat 结构。一个状态信息简单地提供了 znode 的 metadata 。它包括版本号、操作控制列表 (ACL)、时间戳和数据长度。

Every znode in the ZooKeeper data model maintains a stat structure. A stat simply provides the metadata of a znode. It consists of Version number, Action control list (ACL), Timestamp, and Data length.

  1. Version number − Every znode has a version number, which means every time the data associated with the znode changes, its corresponding version number would also increased. The use of version number is important when multiple zookeeper clients are trying to perform operations over the same znode.

  2. Action Control List (ACL) − ACL is basically an authentication mechanism for accessing the znode. It governs all the znode read and write operations.

  3. Timestamp − Timestamp represents time elapsed from znode creation and modification. It is usually represented in milliseconds. ZooKeeper identifies every change to the znodes from “Transaction ID” (zxid). Zxid is unique and maintains time for each transaction so that you can easily identify the time elapsed from one request to another request.

  4. Data length − Total amount of the data stored in a znode is the data length. You can store a maximum of 1MB of data.

Types of Znodes

znode 被归类为持久性、顺序性和临时性。

Znodes are categorized as persistence, sequential, and ephemeral.

  1. Persistence znode − Persistence znode is alive even after the client, which created that particular znode, is disconnected. By default, all znodes are persistent unless otherwise specified.

  2. Ephemeral znode − Ephemeral znodes are active until the client is alive. When a client gets disconnected from the ZooKeeper ensemble, then the ephemeral znodes get deleted automatically. For this reason, only ephemeral znodes are not allowed to have a children further. If an ephemeral znode is deleted, then the next suitable node will fill its position. Ephemeral znodes play an important role in Leader election.

  3. Sequential znode − Sequential znodes can be either persistent or ephemeral. When a new znode is created as a sequential znode, then ZooKeeper sets the path of the znode by attaching a 10 digit sequence number to the original name. For example, if a znode with path /myapp is created as a sequential znode, ZooKeeper will change the path to /myapp0000000001 and set the next sequence number as 0000000002. If two sequential znodes are created concurrently, then ZooKeeper never uses the same number for each znode. Sequential znodes play an important role in Locking and Synchronization.

Sessions

会话对于 ZooKeeper 的操作非常重要。会话中的请求按先进先出的顺序执行。客户端连接到服务器后,将建立会话,并为客户端分配一个会话 session id

Sessions are very important for the operation of ZooKeeper. Requests in a session are executed in FIFO order. Once a client connects to a server, the session will be established and a session id is assigned to the client.

客户端在特定时间间隔发送 heartbeats 以保持会话有效。如果 ZooKeeper 集群未从客户端收到心跳时间超过在服务启动时指定的周期(会话超时),它将判定客户端已死亡。

The client sends heartbeats at a particular time interval to keep the session valid. If the ZooKeeper ensemble does not receive heartbeats from a client for more than the period (session timeout) specified at the starting of the service, it decides that the client died.

会话超时通常以毫秒为单位表示。当会话因任何原因而结束时,在此会话期间创建的临时 znode 也会被删除。

Session timeouts are usually represented in milliseconds. When a session ends for any reason, the ephemeral znodes created during that session also get deleted.

Watches

监控器是一种简单的机制,供客户端获得 ZooKeeper 集群中更改的通知。客户端可以在读取特定 znode 时设置监控。监控器向注册的客户端发送有关 znode(客户端上进行注册的 znode)中任何更改的通知。

Watches are a simple mechanism for the client to get notifications about the changes in the ZooKeeper ensemble. Clients can set watches while reading a particular znode. Watches send a notification to the registered client for any of the znode (on which client registers) changes.

Znode 更改是对与 znode 关联的数据的修改或对 znode 子项的修改。监控器只会触发一次。如果客户端再次需要通知,必须通过另一项读取操作来完成。当连接会话过期时,客户端将与服务器断开连接,关联的监控器也将被移除。

Znode changes are modification of data associated with the znode or changes in the znode’s children. Watches are triggered only once. If a client wants a notification again, it must be done through another read operation. When a connection session is expired, the client will be disconnected from the server and the associated watches are also removed.