Zookeeper如何正確設置和獲取watcher
Watcher 設置是開發中最常見的,需要搞清楚watcher的一些基本特徵,對於exists、getdata、getchild對於節點的不同操作會收到不同的 watcher信息
state= -113 認證失敗狀態
state= 1 連接建立中
state= 2 (暫時不清楚如何理解這個狀態,ZOO_ASSOCIATING_STATE)
state=3 連接已建立狀態
state= 999 無連接狀態
type=1 創建節點事件
type=2 刪除節點事件
type=3 更改節點事件
type=4 子節點列表變化事件
type= -1 會話session事件
type=-2 監控被移除事件
對父節點的變更以及孫節點的變更都不會觸發watcher,而對watcher本身節點以及子節點的變更會觸發watcher,具體參照下表。
操作 | 方法 | 觸發watcher | watcher state | watcher type | watcher path |
Create當前節點 | getdata | × | × | × | × |
getchildren | √ | 3 | 4 | √ | |
exists | × | × | × | × | |
set當前節點 | getdata | √ | 3 | 3 | √ |
getchildren | × | × | × | × | |
exists | √ | 3 | 3 | √ | |
delete當前節點 | getdata | √ | 3 | 2 | √ |
getchildren | √ | 3 | 2 | √ | |
exists | √ | 3 | 2 | √ | |
create子節點 | getdata | × | × | × | × |
getchildren | √ | 3 | 4 | √ | |
exists | × | × | × | × | |
set子節點 | getdata | × | × | × | × |
getchildren | × | × | × | × | |
exists | × | × | × | × | |
delete子節點 | getdata | × | × | × | × |
getchildren | √ | 3 | 4 | √ | |
exists | × | × | × | × | |
恢復連接 | getdata | √ | 1 | -1 | × |
getchildren | √ | 1 | -1 | × | |
exists | √ | 1 | -1 | × | |
恢復連接session未超時 | getdata | √ | -112 | -1 | × |
getchildren | √ | -112 | -1 | × | |
exists | √ | -112 | -1 | × | |
恢復連接session超時 | getdata | √ | 3 | -1 | × |
getchildren | √ | 3 | -1 | × | |
exists | √ | 3 | -1 | × |
注:×表示否,√表示是。
更多參見 http://tech.uc.cn/?p=1189,不過該文章中第五節對event和state的說明有誤,請仔細辨別。