Here the main records of the Zookeeper Client communication protocol. The agreement is not found in the official documentation. Here is the protocol of the records obtained by analyzing the client code. I. Communication Process
The client initiates the connection and sends the handshake packet for a timeout negotiation, which returns a session ID and a timeoout value after the negotiation is successful. You can then communicate normally, sending a ping packet within the timeout range during the communication.
The basic rule of communication protocol between zookeeper client and server is to send a request to get a response. And do different actions according to the response .
The sending data format is:
Message length +xid+request. XID Each request must be unique. The message length and XID are the same 4 bytes, the command length is 4 bytes and must be the start of request 4 bytes.
The command is a number from 1 to 11, and the command for close is-11. There are some differences in request requests for different types. Detailed in the second part.
Special requests have fixed XID:WATCH_XID fixed to -1,PING_XID for -2,AUTH_XID fixed to-4. General requests are generally starting from 0 each time the request accumulates a XID.
The response data is:
Message length +header+response. The message length is 4 bytes, indicating the total length of the header+response.
Header for Xid,zxid,err the corresponding length of 4,8,4.response varies according to the request type. In detail in Part two
According to the XID in the header, the difference is divided into watch,ping,auth,data four kinds
Depending on the four types, the return message is the event, or the authentication, heartbeat, and request data. The client responds differently.
two. Request for each command
1. The first is handshake Connect,
Request
Protocol version+zxid+timeout+session id+passwd len+passwd+read only. The corresponding byte length is 4,8,4,8,4,16,1
Values other than timeout can be any of the 0,password 16 characters. Read_Only is 0 or 1 (is a Boolean value).
Handshake pack without XID and commands
Response
protocol version,timeout,session_id,passwd len,passwd,read only.
The handshake response package does not have a header.
2.ping
Request
The type (ping package has only one field is the command value is 11, its complete send packet is 4 bytes long, 4 bytes xid,4 byte command.)
Response
Res_len+header+res (ping response packet is generally only removed to the header can be confirmed by XID)
3.create
Request
Type+path_len+path+data_len+data+acl_len+acl+flags
type=1,4 bytes
Path_len is 4 bytes, the length of the path path, which is required to be created, supports UTF8
Data_len is 4 bytes, data is the length, data is the value of the node, support UTF8
acl_len,4 bytes. Indicates the length of the ACL.
ACL, see the ACL Description section.
The flags are 4 bytes.
Response
Data_len+data (no special case, no more res_len and headers are written, the following requests will be the same. If not indicated)
Data_len is 4 bytes, data can use UTF8
4.delete
Request
Type+path_len+path+protocol version
type=2
Path_len,path with Create request
Protocol version 4 bytes, and 1 is not versioned. If the version is different, the deletion fails
Response
Returns only Res_len+header. Do not return res_data.
5.exists
Request
Type+path_len+path+watcher
Type=3
Path_len,path with Create request
Watcher is a Boolean value. Determines whether an event is registered. 1 or 0.1 bytes
Response
Stat
The stat is made up of 8,8,8,8,4,4,4,8,4,4,8 byte order.
6.getdata
Request
Type+path_len+path+watcher
Type=4. Other fields with exists request
Response
Data_len+data+stat
Data_len is the data length, 4 bytes.
Stat with EXISTS response
7.setdata
Request
Type+path_len+path+data_len+data+protocol version
Type=4,
Path,data field with Create request
The Protocol version field is the same as delete request.-1 without regard to versions.
Response
Stat
With exists response
Acl:
ACLs are divided into two parts. Part is the flag. Part of the data and ID
Sign:
All,read,write,create,delete,admin
All=31,read=1,write=2,create=4,delete=8,admin=16 are 4 bytes
If you want to indicate that some of these items use the value of the corresponding item, you can
Data and ID:
Both the data and the ID are strings that can use UTF8, which are length plus characters. 4 bytes in length
ACL full identity:
ACL_ logo +data_len+data+id_len+id
Zookeeer Client Communication Protocol