编程知识 cdmana.com

Data synchronization strategy of leader and follower in Apache zookeeper cluster

 Insert picture description here


flow chart

 Insert picture description here

stay Leader After the node election , You need to put Leader The server and Follow The server synchronizes the data . To ensure that the whole ZooKeeper Under the premise of consistent server data in the cluster ,ZooKeeper Only clusters can provide external services .


why ?

Introduce ZooKeeper Before cluster data synchronization , It's important to understand why data synchronization is needed . stay ZooKeeper In the process of cluster service running , It is mainly responsible for processing and sending to ZooKeeper Cluster server client session request . These client session requests can be divided into transactional session requests and non transactional session requests , The essential difference between these two kinds of conversation is that , After executing the session request ,ZooKeeper Whether the cluster server state has changed .

The most common operation type of transactional session request is node creation 、 Delete 、 Update and other operations . The session request operations such as querying data nodes are non transactional , Because queries don't cause ZooKeeper Changes in the state of data on servers in a cluster .

We introduced , Distributed environment often appears CAP Consistency in definitions .

For example, when a ZooKeeper Cluster server ,Leader After a node handles the creation session of a node , The Leader A new data node is added to the server , If not ZooKeeper Data synchronization in the cluster , The data on other servers remains the same as before , The newly added node does not exist on the server .

When ZooKeeper When the cluster receives a query request from the client , The data node cannot be queried , This is it. A typical case of server data inconsistency in a cluster .

To avoid this happening , After the transaction request operation ,ZooKeeper Servers in the cluster need to synchronize data , and The main data synchronization is from Learnning Server synchronization Leader Data on the server .


How ?

Mainly through three aspects to explain ZooKeeper Synchronization methods in clusters , They are synchronization conditions 、 Synchronization process 、 Processing after synchronization .


When to trigger data synchronization mechanism ?

We know Leader In the election, the first thing to judge is that Leader Whether the server is different , To synchronize data in a cluster , The first thing you need to ZooKeeper In the cluster, there are data synchronization tools Learning The server .

in other words , When ZooKeeper The cluster elected Leader After node , Except for being elected Leader Server for , All the other servers act as Learnning The server , And to Leader Server registration . Then the system enters the process of data synchronization .


What data to synchronize

During data synchronization ,ZooKeeper The main job of a cluster is to take those that are not in Learnning The transactional requests executed on the server are synchronized to Learning Server .

Here, please pay attention to , Transactional session requests are synchronized , Non transactional requests such as data node queries are not within the scope of data synchronization .


Synchronization mode

In the specific implementation of data synchronization ,ZooKeeper Cluster also provides four synchronization modes ,

 Insert picture description here

DIFF Sync

DIFF Synchronization is the way of differential synchronization .

stay ZooKeeper In the cluster ,Leader The server detected Learnning After the existence of the server , First of all, it's going to be Learnning Server sends a DIFF Different instructions .

After receiving the order ,Learnning The server will synchronize the data in a differentiated way .

In the process ,Leader The server will put some Proposal Send to Learnning The server . after Learnning The server is receiving information from Leader Server's commit After the command, execute the operation of data persistence .


TRUNC+DIFF Sync

TRUNC+DIFF Synchronization means rolling back first and then performing differentiated synchronization , This kind of way usually happens in Learnning There is a transactional operation log on the server , But in the cluster Leader A situation that doesn't exist on the server .

The reason for this may be Leader The server has logged the transaction to the local transaction log , But it didn't start successfully Proposal technological process .

When this kind of problem arises ,ZooKeeper The cluster will roll back first , stay Learning Data on the server is rolled back to and Leader After the data on the server is consistent with the State , Proceed again DIFF Mode of data synchronization operation .


TRUNC Sync

TRUNC Synchronization means just rolling back operations , Will be Learnning The operation log data on the server is rolled back to the Leader When the operation log data on the server is consistent . And then it doesn't go on DIFF Mode of data synchronization operation .


SNAP Sync

SNAP Synchronization means full synchronization , Yes, it will Leader All data in the server's memory is synchronized to Learnning The server .

In the process of full synchronization ,Leader The server will first send to ZooKeeper In the cluster Learning Server sends a SNAP command , On receiving SNAP After the command , ZooKeeper In the cluster Learning The server starts full synchronization .

And then ,Leader The server will get the full data node and session timeout recorder from the memory database , Serialize them and transfer them to Learnning The server .Learnning After the server receives the full amount of data , It will be deserialized and loaded into an in memory database .


Processing after synchronization

The essence of data synchronization is comparison Leader The server and Learning The server , take Leader The data on the server is added to Learnning The server , then Learnning The redundant transaction log on the server is rolled back .

The previous introduction has completed the data comparison and transfer operation , The next step is Learning The received transaction log is executed on the server , To localize .


Source code analysis

First let's take a look Learnning How the server receives and judges the synchronization mode .

This is shown in the following code ,ZooKeeper The bottom layer implements a Learner class , This class can be seen as in a cluster Learnning Instance object of the server , And... In the cluster Learning The servers are one-to-one .

public class Learner {
    }

And in the Learner Class internal , Mainly through syncWithLeader Function to handle data from Leader Server commands . On receiving from Leader After the server's command , adopt qp.getType() Methods to determine the mode of data synchronization .

protected void syncWithLeader(long newLeaderZxid) throws Exception{
    

 if (qp.getType() == Leader.DIFF) {
    

    snapshotNeeded = false;

  }else if (qp.getType() == Leader.TRUNC) {
    

  }

}

After determining the way of data synchronization , Call again packetsCommitted.add(qp.getZxid()) Method to synchronize transaction operations to the processing queue , After that, we call the operation thread to process .

if (pif.hdr.getZxid() == qp.getZxid() && qp.getType() == Leader.COMMITANDACTIVATE) {
    

    QuorumVerifier qv = self.configFromString(new String(((SetDataTxn) pif.rec).getData()));

    boolean majorChange = self.processReconfig(qv, ByteBuffer.wrap(qp.getData()).getLong(),

            qp.getZxid(), true);

    if (majorChange) {
    

        throw new Exception("changes proposed in reconfig");

    }

}

if (!writeToTxnLog) {
    

    if (pif.hdr.getZxid() != qp.getZxid()) {
    

        LOG.warn("Committing " + qp.getZxid() + ", but next proposal is " + pif.hdr.getZxid());

    } else {
    

        zk.processTxn(pif.hdr, pif.rec);

        packetsNotCommitted.remove();

    }

} else {
    

    packetsCommitted.add(qp.getZxid());


 Insert picture description here

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

Scroll to Top