Without using the Raft algorithm, you can easily do cluster leader election

With the rapid development of the Internet, if the server does not use a cluster model, I am embarrassed to go out for an interview. Most of the cluster models currently known are deployed based on the centralized idea, and the centralized idea is based on the server election leader rule. The well-known consensus algorithm Raft can realize the election of the cluster, but the Raft algorithm is not General programmers can master.

The main purpose of the cluster election is to make the cluster work normally. Can the cluster election work be done without using complex algorithms such as Raft? Of course it can, but with the help of other technologies, today I will talk about how to use zookeeper to get the cluster election leader.

Zookeeper

ZooKeeper is a distributed, open source distributed application coordination service, an open source implementation of Google's Chubby, and an important component of Hadoop and Hbase. It is a software that provides consistent services for distributed applications. The functions provided include: configuration maintenance, domain name services, distributed synchronization, group services, etc.

The Zookeeper node is similar to the UNIX file system. It is a data model with a simple directory tree structure. In this tree, each node can be used as a ZNode, and each ZNode can be identified by a unique path.
image

Temporary node

In Zookeeper, the node (ZNode) is divided into several types, one of which is a temporary node. It has a characteristic: its life cycle is bound to the session of the client that created the node. Once the client is dropped or Down the machine, this node will be automatically deleted.

Watch

Zookeeper provides a node change notification mechanism, that is, the Watch mechanism. Each client can select any node to monitor. If the monitored node or child node changes, all listening clients will be notified. Based on this principle, the cluster server's automatic discovery mechanism can be realized.

Cluster election

The most important thing for a distributed cluster service is to provide uninterrupted services, or fault tolerance. When a node exits the cluster due to a failure or a new node joins the cluster, the service capability of the cluster will not be affected. When the Leader node fails, the automatic election function can be realized without manual intervention. How to use Zookeeper to do it?

There must be several conventions first:

  • All cluster servers monitor the same Zookeeper node, which acts as a storage for cluster information
  • Servers in the cluster can use ip or machine name as node names, and no duplicate node names are allowed
  • By default, the cluster uses the server with the smallest name order as the leader

The specific process is as follows:

  • When the first server of the cluster starts, register its information to the Zookeeper fixed node and monitor the changes of this node. If you find that you only have one server, you are elected Leader by default.
  • When other servers start and register information to the same node, and monitor changes in this node information. Found that there is already a Leader, working as a follower by default.
  • When the leader goes offline due to a failure, the information will be automatically deleted from Zookeeper, and other nodes will receive a notification, and then the leader node will be kicked out to enter the next leader election process.
  • Among the surviving servers, according to the agreement, the server with the smallest name is elected as the leader and sends notifications to other servers. At this time, the cluster can continue to work normally.
  • If the non-Leader node goes offline, the process is similar to the above, but the election process is omitted.

It doesn't matter even if clients continue to drop during the election process, because Zookeeper can guarantee the final data consistency, plus the constraint that the smallest name we agreed on is Leader, the final cluster state will reach stability.

Here is a new question. Will there be a split-brain problem when using Zookeeper to conduct cluster elections?

More exciting articles

image

Guess you like

Origin blog.51cto.com/zhanlang/2532769