zookeeer client 通訊協議

2022-08-02 05:51:11 字數 4278 閱讀 6302

客戶端發起連線,傳送握手包進行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並以此作出不同響應.

1.首先是握手connect,

request:

protocolversion+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

高通 低通濾波

一 關於影象高頻和低頻 1.1 對影象高頻訊號和低頻訊號的理解 影象中的低頻訊號和高頻訊號也叫做低頻分量和高頻分量。簡單一點說,影象中的高頻分量,指的是影象強度 亮度 灰度 變化劇烈的地方,也就是 我們常說的邊緣 輪廓 影象中的低頻分量,指的是影象強度 亮度 灰度 變換平緩的地方,也就是大片色塊的地...

Android Notification通知的運用

package gongzibai.co.cc public class ea2activity extends activity else if status arg2 equals 離開 else if status arg2 equals 回家 override public void onn...

低通取樣和帶通取樣定理

耐奎斯特取樣定理 2 我們假設有乙個 時間連續訊號 x t 的頻帶在 0,f h 之間,以採 樣速率為連續訊號頻率 2 倍 f s 2f h 的採 樣速率對 x t 進行等間隔取樣,得到時間離 散的取樣訊號 x n x nt s 其中 ts 1 f s為取樣間隔 則原始訊號 x t 將被所得到的採 ...