编程知识 cdmana.com

Apache zookeeper - the role of leader in cluster_ Transaction request processing and scheduling analysis

 Insert picture description here

Leader The server ZooKeeper The main task of this paper is to deal with transactional session requests and management ZooKeeper Other role servers in the cluster

that After receiving a transactional session request from the client ,ZooKeeper How to determine the request type of the session in the cluster , And forwarding transactional requests ?


Transactional request processing

stay ZooKeeper After the cluster receives the session request operation from the client , The first step is to determine whether the request is a transactional session request .

For transactional session requests ,ZooKeeper The cluster server will forward the request to Leader Server to operate .Leader After executing the transactional session request inside the server , Then synchronize the data to other role servers , So as to ensure the execution order of transactional session requests , So as to ensure that the whole ZooKeeper Data consistency of cluster .

 Insert picture description here

stay ZooKeeper In the internal implementation of cluster , What is the way to ensure that all ZooKeeper All transactional session requests received by the cluster can be delivered to Leader The server processes it ?

stay ZooKeeper Within a cluster , Division in cluster Leader After a role server other than the server receives a transactional session request from a client , This session request must be forwarded to Leader The server does the processing . ZooKeeper In the cluster Follow and Observer The server , Will check whether the currently received session request is transactional , If it's a transactional request , Then the request will be REQUEST The message type is forwarded to Leader The server .

stay ZooKeeper After the server in the cluster receives the message , The message will be parsed . Analyze the original client session request contained in the message . Then submit the message to your own Leader Server request processing chain , Start a transactional session request operation . If it's not a transactional request ,ZooKeeper Clusters are left to Follow and Observer The role server processes the session request , Such as query data node information .


Leader Transaction analysis

Create node request initiated by client setData For example , Let's see specifically ZooKeeper The underlying process of cluster .

stay ZooKeeper The cluster received one from the client setData After the session request , Its internal processing logic can be divided into four parts , They are the pretreatment stage 、 The transaction phase 、 Transaction execution phase 、 Response client .

 Insert picture description here

Pretreatment stage

In the pretreatment stage , The main work is through the Internet I/O Receive session requests from clients . Determine whether the type of the session request is a transactional session request , The request will then be submitted to PrepRequestProcessor Processor processing . Encapsulate the request transaction header and check whether the session has expired , Finally, deserialize the transaction request information to create setDataRequest request , stay setDataRequest The record contains the path of the node where the data is to be created 、 Content information of data node and version information of data node . Finally, store the request in outstandingChanges Waiting in the queue for subsequent processing .
 Insert picture description here


The transaction phase

In the transaction phase ,ZooKeeper The session request will be submitted inside the cluster ProposalRequestProcessor Processor processing .

 Insert picture description here


Transaction execution phase

After the preprocessing stage and the voting initiation of the transaction session , A transactional session request is ready , And then there's the ZooKeeper The data change operation of this session is performed in the database of .

In the process of dealing with data changes ,ZooKeeper Internally, the transaction header and transaction body information of the request session will be directly delivered to the memory database ZKDatabase For transactional persistence operations . Then return ProcessTxnResult Object indicates whether the result of the operation was successful .

 Insert picture description here

Response phase

stay ZooKeeper The cluster has finished processing clients setData Method to send the data node to create a request , The processing results will be sent to the client .

In response to the client ,ZooKeeper The interior will first create a setDataResponse Response body type , This object mainly includes the data node created by the current session request , And its latest status field information stat.

Then create the request response header , The response header serves as the important information of client request response , The client is receiving ZooKeeper After the response of the cluster , By parsing the transaction in the response header information ZXID And request result identifier err To determine whether the session request is successfully executed .

 Insert picture description here


Source code analysis

First ,ZooKeeper After the cluster receives the transactional session request sent by the client , The request is preprocessed . At the code level ,ZooKeeper By calling PrepRequestProcessor Class to implement all the logic of the preprocessing phase .

It can be understood in this way : When processing client session requests , The first thing to call is PrepRequestProcessor class . And in the PrepRequestProcessor Inside , It's through pRequest Method to determine the type of session request sent by the client . If it's something like setData Data node creation and other transactional session requests , Just call pRequest2Txn Methods further processing .

protected void pRequest(Request request){
    

...

  switch (request.type) {
    

  case OpCode.setData:

    SetDataRequest setDataRequest = new SetDataRequest();                

    pRequest2Txn(request.type, zks.getNextZxid(), request, setDataRequest, true);

    break;

   }

}

And in the pRequest2Txn Inside the method , The main logic of the preprocessing phase is realized . This is shown in the following code , First, through checkSession Method to check whether the session request is valid ( For example, whether the session has expired ), Then call checkACL Check that the client initiating the session operation is in ZooKeeper Whether the server has the permission of related operations . Finally, the information related to the creation of the session , Such as path Node path 、data Node data information 、version Node version information and other fields are encapsulated into setDataRequest Type and pass in to setTxn In the method , Finally, it is added to the processing chain for processing .

case OpCode.setData:

    zks.sessionTracker.checkSession(request.sessionId, request.getOwner());

    SetDataRequest setDataRequest = (SetDataRequest)record;

    if(deserialize)

        ByteBufferInputStream.byteBuffer2Record(request.request, setDataRequest);

    path = setDataRequest.getPath();

    validatePath(path, request.sessionId);

    nodeRecord = getRecordForPath(path);

    checkACL(zks, request.cnxn, nodeRecord.acl, ZooDefs.Perms.WRITE, request.authInfo, path, null);

    int newVersion = checkAndIncVersion(nodeRecord.stat.getVersion(), setDataRequest.getVersion(), path);

    request.setTxn(new SetDataTxn(path, setDataRequest.getData(), newVersion));

    nodeRecord = nodeRecord.duplicate(request.getHdr().getZxid());

    nodeRecord.stat.setVersion(newVersion);

    addChangeRecord(nodeRecord);


Summary

It mainly combs ZooKeeper In the cluster Leader How does the server handle transactional session requests , And after processing the transactional session request , How to inform other role servers of synchronization operation .

Can say in ZooKeeper The cluster is in the process of transacting ,Follow and Observer The server is mainly responsible for receiving session requests from clients , And forward it to Leader The server . What actually handles the session request is Leader The server .

This raises a problem : When a business scenario has more query operations and less transactional operations such as create delete ,ZooKeeper The performance of the cluster will be very good .

And in extreme cases ,ZooKeeper The cluster has only transactional session requests and no query operations , that Follow and Observer The server can only act as a request forwarding server , All the conversation processing pressure is on Leader The server .

In terms of processing performance, the bottleneck of the whole cluster server depends on Leader Server performance .ZooKeeper The role of cluster can only be guaranteed in Leader When the node crashes , Re elect Leader The server guarantees the stability of the system . This is also ZooKeeper One drawback of design .

 Insert picture description here

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

Scroll to Top