The zookeeper client can set watcher for a znode so that the client can obtain the appropriate event notification when the node changes. If you do not set the Watcher, you will not receive notification.
Taking zookeeper C client as an example, you can set the watcher flag at the same time when you call the following 3 functions, get the data or state of a node.
Zoo_exists//node exists. If the watcher is set, the node is deleted and the zoo_deleted_event is received, and the Zoo_created_event is received when the node is created.
Zoo_get//Get node data. If the watcher is set, the node data is changed and the Zoo_changed_event is received.
Zoo_get_children//Get a list of children's nodes. If set watcher, the child node number changes, receives zoo_child_event.
Attention:
1. Set a Watcher (can not be repeated settings, only the equivalent of setting up once) can only receive an event notification, then if the node changes again, will not be notified again, so every time after the event, need to reset the watcher node.
2. Zoo_exists can set watcher on nodes that have not yet been created, send created_event when nodes are created, or send deleted_event when a node is deleted.
3. Zoo_get,zoo_get_children cannot set watcher on a node that has not yet been created, and the watcher that was previously set is not valid after the node has been created.
(But after the node is created, if the Watcher is set through Zoo_esixts, the watcher previously Zoo_get settings appears to be valid)
4. Zoo_get, Zoo_get_children to the existence of the node set watcher, if the node is deleted, watcher will be invalidated, that is, after the node is recreated, the previously set watcher will become invalid.
Initialized path:/A |
|
|
|
|
|
|
|
|
|
|
|
|
|
watched nodes |
|
zoo_exists |
Zoo_get |
Zoo_get_children |
Action |
Event |
/A |
/a/b |
/a/b/c |
Event |
/A |
/a/b |
/a/b/c |
Event |
/A |
/a/b |
/a/b/c |
Create/a/b |
CREATED |
– |
√ |
– |
|
|
|
|
Child |
√ |
– |
– |
Create/a/b/c |
CREATED |
– |
– |
√ |
|
|
|
|
Child |
– |
X |
– |
Delete/a/b/c |
DELETED |
– |
– |
√ |
|
|
|
|
Child |
– |
X |
– |
Delete/a/b |
DELETED |
– |
√ |
– |
|
|
|
|
Child |
√ |
– |
– |
set/a data |
|
|
|
|
CHANGED |
√ |
– |
– |
|
|
|
|
Create/a/b |
CREATED |
– |
√ |
– |
|
|
|
|
Child |
√ |
– |
– |
set/a/b data |
|
|
|
|
CHANGED |
– |
√ |
– |
|
|
|
|
Create/a/b/c |
CREATED |
– |
– |
√ |
|
|
|
|
Child |
– |
X |
– |
SET/A/B/C data |
|
|
|
|
CHANGED |
– |
– |
√ |
|
|
|
|