编程知识 cdmana.com

Apache zookeeper - the role of follow in clusters_ Non transaction request processing and leader election analysis

 Insert picture description here

Pre

stay ZooKeeper In the cluster ,Leader The server is mainly responsible for processing transactional session requests from clients , And after processing the transactional session request , Management and coordination ZooKeeper In the cluster Follow and Observer Such as role server data synchronization .

therefore , stay ZooKeeper In the cluster ,Leader The server is the core server , One ZooKeeper Services run in cluster mode , There must be a Leader The server .

And in the ZooKeeper In the cluster , It's guaranteed by collapsing the election ZooKeeper Clusters can always have a Leader The server provides external services .

So in ZooKeeper A cluster elects Leader In the process of ,Follow What does the server do ?

 Insert picture description here

Understand the processing logic and related operations of the server in different states , Help us to master the whole ZooKeeper The running process of cluster service , In our daily work , Better develop ZooKeeper Related services , And in the process of operation and maintenance to quickly locate the problem , Build a more efficient and stable ZooKeeper The server .


Non transactional request processing

stay ZooKeeper After the cluster receives the request from the client , It will first determine the type of the session request , If it is a transactional request .

The so-called transactional request , Refer to ZooKeeper After the server executes the session request , Whether it will cause the data or state of the server executing the session request to change , This leads to data inconsistency with servers in other clusters .

Here we take the data node query request initiated by the client as an example , Look at the ZooKeeper The implementation of non transactional requests .

When ZooKeeper After the cluster receives the query session request sent by the client , The client request is assigned to Follow The server does the processing .

And in the Follow Inside the server , It also uses Responsibility chain To handle every session request from the client .

Leader The processing chain process of the server , Contains the preprocessor phase respectively 、Proposal Commit processor phase and final Processor stage .

And Leader The difference in the process is , stay Follow During the execution of the processing chain of the role server ,FollowerRequestProcessor As the first processor , It is mainly responsible for filtering whether the session request is a transactional session request . If it's a transactional session request , Then forward it to Leader Server to operate . If it's not a transactional session request , It's up to Follow The next processor in the server processing chain processes .

And the next processor is CommitProcessor , The role of the processor is to match the transactional requests from other servers in the cluster with the submitted requests from the local servers . The way to match is , Will be performed locally sumbit Submit a request , Same as other servers in the cluster Commit Session requests to match , After the match is completed, it will be handed over to Follow The next processor in the processing chain processes .

Final , When a client session goes through Final After the processor operates , That's the whole thing Follow Server session processing , And respond the result to the client .


Source code analysis

ZooKeeper After the cluster receives the request from the client , Will give the request to Follow The server does the processing . and Follow The first call inside the server is FollowerZooKeeperServer class , The purpose of this class is to encapsulate Follow Server properties and behavior , You can think of this class as a Follow Server code abstraction .

FollowerZooKeeperServer Class inherited LearnerZooKeeperServer . In a FollowerZooKeeperServer Intra class , Defines a core ConcurrentLinkedQueue Queue field of type , Used to store received session requests .

 Insert picture description here
Is defined. FollowerZooKeeperServer After the class , In this category setupRequestProcessors Function , Defined Handling the chain of responsibility , Specifies the processors in the processing chain . This is shown in the following code , The starting processors are defined in order FollowerRequestProcessor 、 Commit processor CommitProcessor、 Synchronous processor SendAckRequestProcessor And the final processor FinalProcessor.

protected void setupRequestProcessors() {
    

    RequestProcessor finalProcessor = new FinalRequestProcessor(this);

    commitProcessor = new CommitProcessor(finalProcessor,

            Long.toString(getServerId()), true, getZooKeeperServerListener());

    commitProcessor.start();

    firstProcessor = new FollowerRequestProcessor(this, commitProcessor);

    ((FollowerRequestProcessor) firstProcessor).start();

    syncProcessor = new SyncRequestProcessor(this,

            new SendAckRequestProcessor((Learner)getFollower()));

    syncProcessor.start();


The election process

Introduction after Follow After the server processes non transactional requests , Now let's learn more about Follow Another major function of the server : stay Leader When the server crashes , Re elect Leader The server .

ZooKeeper Cluster re election Leader In essence, the process is only Follow Participate in server work .

And in the ZooKeeper Cluster re election Leader The node process can be divided into Leader Failure detection 、 Re election Leader 、Follow Server role change 、 Cluster synchronization steps .

 Insert picture description here

Leader Failure detection

stay ZooKeeper In the cluster , When Leader When the server fails ,ZooKeeper The cluster will re elect a new Leader The server . in other words ,Leader Failure of the server triggers ZooKeeper Start new Leader The election of the server , So in ZooKeeper In the cluster , And how to discover Leader The server failed ?

Here we will introduce Leader Failure detection . And the method of keeping the client active introduced before , It is sent to the server periodically through the client Ping Request to implement .

stay ZooKeeper In the cluster , Probe Leader The way the server survives is very similar to keeping the client active . First ,Follow The server will report to Leader Server send Network request , After receiving the request ,Leader The server will return a response packet to Follow The server , And in the Follow The server receives Leader After the response from the server , If you judge Leader The server is running normally , Data synchronization and service forwarding will continue , conversely , Is to Leader Server re election operation .


Leader Re election

When Follow Server to the Leader After the server sends the status request packet , If not Leader Server return information , At this time , If it's an individual in a cluster Follow The server found a return error , It doesn't lead to ZooKeeper The cluster immediately re elected Leader The server , Instead, we should Follow The state of the server changes to LOOKING state , And vote on the Internet , When ZooKeeper More machines in the cluster vote , Finally, when the voting results meet the majority principle .ZooKeeper Will re elect Leader The server .


Follow Role change

stay ZooKeeper In the cluster ,Follow Server as Leader The candidate for the server , When elected to Leader After the server , Its presence ZooKeeper In the cluster Follow role , And then it changes . That is to change into Leader The server , And as a ZooKeeper In the cluster Leader Role server provides external services .


Cluster synchronization data

stay ZooKeeper The cluster successfully elected Leader The server , And candidates Follow After the role of the server changes . To avoid data inconsistencies during this period ,ZooKeeper Before the cluster provides services to the outside world , Will pass Leader Role server management synchronizes other role servers .


The source code parsing

First ,ZooKeeper The cluster will judge Leader Whether the server fails , And the way to judge is Follow Server to the Leader The server sends the request packet , after Follow After the server receives the response data , To analyze , This is shown in the following code ,Follow The server will be based on the returned data , Judge Leader The running state of the server , If you return LOOKING keyword , It is indicated that the cluster has the following characteristics Leader The server is not communicating properly .

switch (rstate) {
    

case 0:

    ackstate = QuorumPeer.ServerState.LOOKING;

    break;

case 1:

    ackstate = QuorumPeer.ServerState.FOLLOWING;

    break;

case 2:

    ackstate = QuorumPeer.ServerState.LEADING;

    break;

case 3:

    ackstate = QuorumPeer.ServerState.OBSERVING;

    break;

default:

    continue;

after , stay ZooKeeper A cluster election Leader Server time , It's through FastLeaderElection Class implements the . This class implements TCP Mode of communication connection , Used in ZooKeeper Cluster with other Follow The server coordinates communication .

 Insert picture description here

FastLeaderElection Class inherited Election Interface , Define the implementation class that is used for election . And inside it , Some communication parameters are defined , such as finalizeWait The final waiting time 、 Maximum notification interval maxNotificationInterval etc. .

In the course of the election , First call ToSend Functional direction ZooKeeper Other role servers in the cluster send local voting information , Other servers receive voting information , Will verify the validity of voting information and other operations , after ZooKeeper Cluster statistics voting information , If more than half of the machines vote consistently , Then the cluster selects a new one Leader The server .

static public class ToSend {
    

    static enum mType {
    crequest, challenge, notification, ack}

    ToSend(mType type,

            long leader,

            long zxid,

            long electionEpoch,

            ServerState state,

            long sid,

            long peerEpoch,

            byte[] configData) {
    

        this.leader = leader;

        this.zxid = zxid;

        this.electionEpoch = electionEpoch;

        this.state = state;

        this.sid = sid;

        this.peerEpoch = peerEpoch;

        this.configData = configData;

    }

Summary

We know that ZooKeeper In the cluster Follow The function and function of the server .Follow The server ZooKeeper In the process of cluster service running , Responsible for processing non transactional requests such as queries from clients . When ZooKeeper The old ones in the cluster Leader When the server fails , Re elect a new one as a voter Leader The server .

Here we should pay attention to a problem , That's re election Leader In the process of the server ,ZooKeeper In theory, clusters can't handle transactional requests . therefore , Send to ZooKeeper Transactional sessions in the cluster will be suspended , Not for the time being , Until a new one is elected Leader After the server to operate .

 Insert picture description here

版权声明
本文为[Little craftsman]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225043318261M.html

Scroll to Top