-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
42 lines (39 loc) · 2.48 KB
/
notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---------
Zookeeper
---------
- A high performance coordination service. Used by many popular projects like
Kafka, Hadoop, HBase etc. It is a distributed system itself.
- Zookeeper's data model - a tree structure of sorts, where each node is called
a Znode.
- Znodes are a hybrid between File and directory. Two types: Persistent and Ephemeral.
- Persistent znodes are persisted even if the creating service disconnects from zookeeper and joins later.
- Ephemeral znodes are destroyed as soon as the creating service leaves the zookeeper cluster.
-------------------------
leader election algorithm
-------------------------
- Each joining node will submit its' candidacy to be the leader as soon as it joins the zookeeper by
creating a znode under a `/election` znode. Zookeeper is capable of guaranteeing a increasing sequence number
to the creating znodes as each joining service creates them. The service that creates the znode with the smallest
sequence number will declare itself as the leader.
---------------------
Watchers and triggers
---------------------
- With zookeeper, we can register a watcher when we call the methods like -
getChildren(..watcher) --> get notified when the list of znode's children changes.
getData(znodePath, watcher) --> get notified if a znode's data get modified.
exists() --> get notified if a znode gets deleted or created.
- The watcher allows us to get a notification when a change happens.
- public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) - also takes a watcher.
- Watchers registered with getChildren(), exists(), and getData() are one-time triggers, i.e. if we want to
get future notifications, we need to register the watcher again.
----------------------------
Leader re-election algorithm
----------------------------
- One way to trigger a re-election is that all the non-leader nodes can watch the leader's znode.
Since it is a ephemeral znode, if in case the leader dies, the znode will be delete and the non leader nodes
will get notified, and they can then initiate a leader reelection. But this approach suffers the problem of the
so called 'heard effect' (read more)
- Therefore, we can optimize it by letting each non leader node only watch its previous nodes' znode.
This way, if the leader dies, the only node that is going to be notified is its immediate successor, which can then
call getChildren to get the latest view of the znodes and decide if its znode has the lowest sequence number, and if so,
declare itself as the leader!