This is the 21st day of my participation in the August Text Challenge.More challenges in August

When it comes to network communication protocols, we all know that the most commonly used network communication protocol is TCP/IP. Zookeeper implements its own communication mode based on TCP/IP.

ZooKeeper protocol description

Zookeeper provides two communication protocols: request protocol and response protocol. In ZooKeeper, a client request protocol consists of a request header and a request body. The response protocol on the primary server consists of a response header and a response body.

This section describes the structure of the network communication protocol in ZooKeeper. Let’s take a closer look at how the underlying network communication protocol is implemented internally in ZooKeeper.

Underlying implementation of the ZooKeeper protocol

The request protocol is used by the Zookeeper Client to send requests to the Zookeeper Server, including the request header and request body. For example, we often use the session creation, data node query and other operations. The Zookeeper client sends a request protocol to the Zookeeper server over the network.

Zookeeper request protocol

Now that we know what a request protocol is, let’s look at the underlying implementation of a request header in a request protocol.

As we know, the RequestHeader class is used as the RequestHeader in Zookeeper.

The RequestHeader class implements the Record interface for subsequent serialization operations in network transport. There are only two property fields in the RequestHeader class: XID and Type. Xid indicates the client id, which records the sequence of initiating client requests. Type indicates the type of the requested operation.

Public class RequestHeader implements Record{// Implements Record; Record the sequence in which client requests are sent private int xID; Private int type; }Copy the code

Now that you have an understanding of the underlying implementation of the request header in the request protocol, let’s look at the request body of the request protocol.

The request body of a protocol contains all contents of the protocol processing logic, and all operations of a session request are covered in the request body. In the internal implementation of ZooKeeper, different structures are used to encapsulate the request body according to different request operation types. Next, we will introduce the corresponding request body of session creation, node query and node update, and take a deep look at how ZooKeeper is implemented internally.

The call request is created

When the ZooKeeper client initiates a session, it sends a session creation request to the ZooKeeper server. The request notifies the ZooKeeper server that it needs to process an access link from the client.

And all the information required by the server to process the session creation request is included in the request body, using the ConnectRequest class to encapsulate the request body, ConnectRequest class to achieve the Record interface to serialize the operation, its internal includes a total of five attribute fields.

Public class ConnectRequest implements Record {private int Specifies the version of the request protocol protocolVersion; // Server zxID number that received the last response private Long lastZxidSeen; // Session timeOut private int timeOut; SessionId private Long sessionId; // Session password password private byte[] passwd; }Copy the code

Node query request

When we query the data node on the ZooKeeper server through the client API, the client sends the GetDataRequest session request to the server.

The GetDataRequest class is used to encapsulate the request body, and the GetDataRequest class implements the Record interface for serialization operations. It has two attributes, a character type path and a Boolean type watch.

Public class GetDataRequest implements Record {// Node full path private String path; Private Boolean watch; }Copy the code

Node query request

Next, we look at the last type of session operation, the update operation of a node.

When the Zookeeper client sends a node query request to the Zookeeper server, it actually sends the request protocol for the update operation. ZooKeeper uses the SetDataRequest class to encapsulate the request body inside the protocol. There are also three properties in SetDataRequest: Path, data, and Version.

Public class GetDataRequest implements Record {// Node full path private String path; Private Boolean watch; }Copy the code

So far, we have described the internal structure and underlying implementation of the request protocol sent by the ZooKeeper client in a network session request. Zookeeper’s request protocol is introduced, and the Zookeeper response protocol is introduced.

Response protocol of Zookeeper

Simply put, the response protocol parses the request protocol and responds to the request received from the Zookeeper client. Corresponding to Zookeeper’s request protocol, Zookeeper’s response protocol also consists of a response header and a response body, which needs to encapsulate the response body according to different request types.

After the Zookeeper server receives a request from the Zookeeper client, the ReplyHeader class parses the request header and encapsulates the response header.

The ReplyHeader class implements the serialization of the Record interface, which consists of three attributes: xID, zxID, and ERR.

Public class ReplyHeader implements Record {// ReplyHeader implements Record; Record the sequence in which client requests are sent private int xID; // transaction id private long zxID; // Error status code private int err; }Copy the code

The request protocol is similar to the response request. In the internal implementation of ZooKeeper, different structures are used to encapsulate the response body according to different response operation types. Next, we will introduce the corresponding response body of three types of response, namely session creation, node query and node update, and take a deep look at how ZooKeeper is implemented internally.

Session Creation Response

After the Zookeeper server processes a session connection request initiated by the Zookeeper client, the Zookeeper server returns a Response to the request.

ZooKeeper is implemented through the ConnectRespose class. The ConnectRespose class implements the serialization operation of the Record interface. There are four properties in this class, The protocols are protocolVersion, timeOut, sessionId, and Passwd.

Public class ConnectResponse implements Record {// Version information of the request protocol private int protocolVersion; // Session timeOut private int timeOut; // sessionId private long sessionId; // Session password private byte[] passwd; }Copy the code

Responding to node query Similarly, when the Zookeeper client sends a request to query node data, the Zookeeper server returns node data to the client based on the node path sent by the client and after verifying that the client has corresponding permissions.

The ZooKeeper server uses the GetDataResponse class to encapsulate the queried node information into the request body of the response protocol. The GetDataResponse class implements the Record interface for serialization, which includes two property fields, data and STAT.

Public class GetDataResponse implements Record {private byte[] data; / / node status information private org. Apache. The zookeeper. Data. The Stat Stat. }Copy the code

Response node update

After the Zookeeper client sends a node change operation, the Zookeeper server sends a response to the Zookeeper client after processing the related logic.

When the Zookeeper server receives the node update request from the Zookeeper client, the response protocol request body of the node update operation uses the SetDataResponse class to encapsulate the response body, which implements the Record interface to serialize the operation. And there is only one STAT property field in the class.