Introduction to Zookeeper

Source of learning Materials:www.bilibili.com/video/BV1to…

The characteristics of

  • One leader and multiple followers
  • More than half of the nodes are viable, usually an odd number of servers
  • Small amount of data, fast synchronization time, can read the latest data in a certain period of time
  • Data update atomicity
  • Update requests are executed sequentially
  • Global data consistency

The data structure

It is similar to a file system tree structure. Each ZNode is 1MB by default and cannot store large amounts of data. Each node can be uniquely identified by a path.

Application scenarios

  • Unified Naming Service

Similar function implementation: Nginx

  • Unified Cluster Management
  • Unified Configuration Management
  • Dynamic upper and lower limits of the server
  • Soft load balancing

The cluster

Election mechanism – first activation

If the other party’s MYID is larger than mine, I will vote for it. When a node receives more than half of the votes, it will be elected as the leader. Other looking nodes will be updated as followers and will not vote any more. Sid: consistent with myID ZXID: transaction ID EPOCH: id of the tenure. If the leader does not exist, it is the logical clock

Election mechanism – Not the first time

Leader is

When communicating with other nodes, the leader is informed that the leader exists and the election is abandoned

Leader hang up

Relaunch the election, compare (epoch, ZXID, SID), who is older as the leader

Waging a script

#! /bin/bashcase $1 in
"satrt") {for i in hadoop102 hadoop103 hadoop104
    doEcho -------$I start -------- SSH $I"Start command"
    done
}
"stop"){
}
"status"){
}
Copy the code

The node type

  • Permanent node
  • Temporary node (does not exist when the client is disconnected)

Listening to the principle of

The connector thread will send the registered listening time to the server. The server will send the change time to the listener thread. The listener thread calls the process method to process. (Registration can only take effect once)

Writing Data Principle

  • Write requests are sent to followers

case

The server is dynamically connected and offline

A distributed lock

disadvantages

  • Session connections are asynchronous and need to be controlled by Countdownlatch
  • High code complexity
  • Watch requires repeated registration
  • Mature framework: Curator

The interview questions

How many ZKS are suitable for the production environment

  • 10 servers, 3 ZKS
  • 20 servers, 5 ZKS
  • 100 servers, 11 ZKS
  • 200 servers, 11 ZKS

Too much ZK leads to greater communication delay

Common commands

ls, get, create, delete