zookeeer client 通信协议

时间:2023-03-09 16:28:56
zookeeer client 通信协议

这里主要记录zookeeper client通信协议的.在官方的文档里没找到协议相关部分.这里是记录的协议是通过分析客户端代码得来的.

一.通信流程

客户端发起连接,发送握手包进行timeout协商,协商成功后会返回一个session id和timeoout值.随后就可以进行正常通信,通信过程中要在timeout范围内发送ping包.

zookeeper client和server之间的通信协议基本规则就是发送请求获取响应.并根据响应做不同的动作.

发送数据格式为:

消息长度+xid+request. xid每次请求必须是唯一的.消息长度和xid同为4字节,命令长度为4字节且必须为request的开始4字节.

命令是从1到11的数字表示,close的命令为-11.不同类型请求request有些差异.详细在第二部分.

特殊请求具有固定的xid:watch_xid固定为-1,ping_xid为-2,auth_xid固定为-4.普通请求一般从0开始每次请求累加一次xid.

响应数据为:

消息长度+header+response.消息长度为4字节,表明header+response的总长度.

header为xid,zxid,err对应长度为4,8,4.response根据请求类型不同具有差别.详细在第二部分

根据header里xid的区别分为watch,ping,auth,data这四种类型

根据这四种类型来区分返回消息是事件,还是认证,心跳和请求数据.client并以此作出不同响应.

二.各命令request

1.首先是握手connect,

  request:

      protocol version+zxid+timeout+session id+passwd len+passwd+read only.对应的字节长度为4,8,4,8,4,16,1

         取值除timeout外其他几个皆可为0,password可以为任意16个字符.read_only为0或1(是布尔值).

   握手包没有xid和命令

  response:

    protocol version+timeout+session_id+passwd len+passwd+read only.

   握手响应包没有header.

2.ping

  request:

type (ping包只有一个字段就是命令值是11,它完整的发送包是4字节长度,4字节xid,4字节命令.)

  response:

      res_len+header+res (ping响应包一般只拆到header即可通过xid确认)

3.create

request:

      type+path_len+path+data_len+data+acl_len+acl+flags

      type=1,4字节

      path_len是4字节,为path的长度,path为需要创建的路径,支持utf8

      data_len为4字节,为data的长度,data为节点的值,支持utf8

      acl_len,4字节.表明acl的长度.

      acl,详见acl描述部分.

      flags为4字节.永久节点值为0,临时为1,时序节点为2,临时时序节点为前两者之和3.

   response:

      data_len+data (无特殊情况即不再将res_len和header写出来,下面各请求也将如此.如没有将表明)

      data_len为4字节,data可以使用utf8

4.delete

   request:

      type+path_len+path+protocol version

      type=2

      path_len,path同create request

      protocol version 4字节,为-1则不进行版本检查.否则版本不同则删除失败

   response:

      只返回res_len+header.不返回res_data.

5.exists

   request:

      type+path_len+path+watcher

      type=3

      path_len,path同create request

      watcher为布尔值.判断是否有事件注册.为1或0. 1字节

   response:

      stat

      stat由8,8,8,8,4,4,4,8,4,4,8字节顺序组成.

6.getdata

   request:

       type+path_len+path+watcher

       type=4.其他字段同exists request

   response:

       data_len+data+stat

       data_len为data长度,4字节.

       stat同exists response

7.setdata

   request:

       type+path_len+path+data_len+data+protocol version

       type=5,

       path,data字段同create request

       protocol version 字段同delete request.-1为不考虑版本.

   response:

       stat

       同exists response

8.getacl

   request:

       type+path_len+path

       type=6,其他字段同其他request

   response:

       acl count+acls+stat

       acl_count记录acls的数量,4字节,当值为-1时表示返回无,acls参见ACL部分.

       stat同其他部分

9.setacl

   request:

       type+path_len+path+acl_count+acls+protocol version

       type=7,path同其他request

       acl_count,acls同getacl response.

       pro version 同其他request

   response:

       stat

10.getchildren

   request:

       type+path_len+path+watcher

       type=8,其他字段同exists request

   response:

       children_count+childrens

       children_count记录childrens的个数.4字节,值为-1时表示返回无

       childrens是path_len+path的集合.

11.sync

   request:

       type+path_len+path

       type=9,所有字段同其他request

   response:

       path_len+path

       字段同其他response

12.getchildren2

   request:

       type+path_len+path+watcher

       type=12,其他字段同getchildren request

   response:

       返回同getchildren response.唯一不同是返回的最后多了stat.

13.checkversion

   request:

       type+path_len+path+protocol version

       type=13,其他字段同其他request

   response:

       无返回

14.transaction

   request:

       type+ops

       type=14

       ops为其他request的列表集.完整表示如下

       (op.type+flages+err+op.request)+(同样的集合)+....+(-1+1+-1)最后需要加上这个结束标志.

       op.request为其他类型的request不含type的部分.

       type 4字节,flages为0或1,1字节,err为数字,无错误为-1,4字节.结束部分的标志字节对应为4,1,4.

   response:

       返回比较杂.根据header.type有不同结果.

ACL:

   acl分两部分.一部分是标志.一部分是数据和id

   标志:

      all,read,write,create,delete,admin

      all=31,read=1,write=2,create=4,delete=8,admin=16 均为4字节

      若想表示其中某几项则使用对应项的值的和即可

   数据和id:

      数据和id都是字符串可以使用utf8,均为长度加字符.长度为4字节

   ACL完整标识:

      acl_标志+data_len+data+id_len+id