Thursday, August 17, 2017

We continue discussing the ZooKeeper. It is a co-ordination service with elements from group messaging, shared registers and distributed lock services. It provides a interface that guarantees wait-free property and FIFO execution of requests from each client.  Requests across all clients are also linearized.
We saw a few examples of primitives that are possible with ZooKeeper. These primitives exist only on the client side. For example, 1) ZooKeeper can be used to implement dynamic configuration in a distributed application.  This is one of the basic schemes where each configuration is stored in the form of ZNode and tasks that are dependent on each other are stored as Rendezvous nodes. 2) GroupMembership is another common co-ordination requirement. This primitive is implemented using ephemeral nodes. 3) ZooKeeper can also be used to implement locks. Applications implement locks using lock files represented by a ZNode. To acquire a lock, the client tries to create the designated node with the ephemeral flag. If the create succeeds, the client holds the lock otherwise the client watches the node in case the leader dies. Whenever the znode is deleted, other clients may try to acquire the lock.  We see that this locking scheme  ensures that the removal of a znode causes only one client  to wake up. There is no polling or timeouts. and the state of the ZNodes helps with monitoring, troubleshooting and debugging.  In addition to simple locks, ZooKeeper also has read-write locks These locks differ from simple locks where only earlier write lock znodes prevent a client from obtaining a read lock. Read locks may be shared. Here the herd effect is put to use so that all read clients may be used.
Double barriers are yet another locking mechanism. They enable client to synchronize the beginning and end of a computation.  The threshold for the barrier decides how many processes join. They leave the barrier only when they are done. These processes create and delete a child of the znode maintained by the barrier. Processes can leave the barrier when all of the processes have removed their children. When the threshold is exceeded, a ready znode is created which is watched by all waiting processes. This allows the processes to know when to enter. Similarly to leave, the processes watch for a znode to disappear. Together these locking services enable a wide variety of applications.  For example, Yahoo web crawler uses this for its page fetching service. Katta, a distributed indexer uses this for coordination. Even high traffic Yahoo message broker which is a distributed publish-subscribe system uses this for managing configuration, failure detection and group membership.
#codingexercise
Given a sorted string, find number of occurrences of a given character in string.
int binary_search(String input, int start, int end, char val)
{
int mid = (start + end)/2;
if (input[mid] == val) return mid;
if (start == end && input[mid] != val) return -1;
if (input[mid] < val)
return binary_search(nums, mid+1, end, val);
else
return binary_search(nums, start, mid, val);
}
int GetCount(String input, char val)
{
int current = binary_search(input, 0, input.Length - 1, val);
if (current == -1) return 0;
while (current >= 0 && input[current] == val) current--;
current++;
int start = current;
while (current <= input.Length-1 && input[current] == val)
          current++;
current--;
int end = current;
return end-start+1;
}
Optimization: We could also skip ahead to resume counting in the other direction from the location we started 

No comments:

Post a Comment