Today is mainly said source analysis, source analysis of the client startup process and source analysis of the server startup process. Finally, the operation and maintenance of ZooKeeper are discussed. Source: github.com/limingios/n…

(I) Service startup process

  • Initialize the

Server1, server2, server3, do not know which is the leader, which is the follower, start complete

  • Election algorithm

Select leader, Follower, and Observer

How are they elected? Two ways

1. The JPS view

2.vim zkServer.sh

To ensure the consistency and availability of write operations. Zookeeper specially designs a consistency protocol named Atomic Broadcast (ZAB) to support crash recovery. Based on this protocol, ZooKeeper implements a master-slave system architecture to maintain data consistency among all copies in the cluster. According to the ZAB protocol, all write operations must be completed by the Leader. The Leader writes to the local daily before copying to all the Follower nodes. Once the Leader node fails to work, ZAB can automatically choose a suitable replacement from the followers node, namely the new Leader. This process is called leadership election, which is the most important and complex process in ZAB protocol. FastLeaderElection is a Fast Paxos algorithm implementation.

3. Check the QuorumPeerMain

QuorumPeerConfig calls the parse method of this method

Load the configuration file FileInputStream, load

The load load load0

When configuring the file at home, is it a bit surprising to see the keyword obtained in parseProperties? Is it corresponding to our zoo. CFG file?

The start method of the main class

CnxnFactrory. Start () starts socker programming by NIO reading and writing operations. 3. StartLeaderElection algorithm, which selects the leader

4. The algorithms involved are ZAB and Paxos. Zab is a practical algorithm (simplified based on the idea of Paxos), while Paxos is a theoretical algorithm (a process of American presidential election). Specific Paxos theory: reference source code: inside the document. Start means to start work.

  • The write operation

1. The client sends a write request to the Leader. 2. The Leader sends the write request to all the followers in the form of a Proposal 4. The Leader sends a Commint to all followers and observers after receiving an ACK (the Leader has an ACK by default). 5

Note that the Leader does not need to receive all the ACK messages from the followers but only needs to receive more than half of them. In addition, although the Leader does not have a vote on his OWN ACK Observer, he still needs to synchronize the Leader’s data so that he can return as much new data as possible when processing read requests.

In zoo. CFG, select a node and add: observer

Since the obServer does not have the right to vote, in practice, if more machines are allowed, it is suggested to add an obServer to reduce the consumption of resources without voting and to relieve the pressure of other machines. This is the eunuch of ancient imperial palace, can work also rest assured, have no discourse right, only can work.

(II) The client’s startup process

  • Client startup process
public ZooKeeper(String connectString,  int sessionTimeout, Watcher watcher,boolean canBeReadOnly) throws IOException
{
LOG .info( "Initiating client connection, connectString=" + connectString
+  " sessionTimeout=" + sessionTimeout +  " watcher=" + watcher);
watchManager. defaultWatcher = watcher;

ConnectStringParser connectStringParser =  new ConnectStringParser(
connectString);

HostProvider hostProvider =  new StaticHostProvider(
connectStringParser.getServerAddresses());// Get the IP port number

cnxn =  new ClientCnxn(connectStringParser.getChrootPath(),
hostProvider, sessionTimeout,  this,  watchManager,
getClientCnxnSocket (), canBeReadOnly);// Create a ClientCnxn object

cnxn.start();// Non-thread threads start
}
Copy the code
  • Org. Apache. The zookeeper. ClientCnxn# ClientCnxn launched two threads initialized the send and event
public ClientCnxn(String chrootPath, HostProvider hostProvider,  int
sessionTimeout, ZooKeeper zooKeeper,ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket,long sessionId,  byte[] sessionPasswd,  boolean canBeReadOnly) {
  this. zooKeeper = zooKeeper;
  this. watcher = watcher;
  this. sessionId = sessionId;
  this. sessionPasswd = sessionPasswd;
  this. sessionTimeout = sessionTimeout;
  this. hostProvider = hostProvider;
  this. chrootPath = chrootPath;
  connectTimeout = sessionTimeout / hostProvider.size();
  readTimeout = sessionTimeout * 2 / 3;
  readOnly = canBeReadOnly;
  sendThread =  new SendThread(clientCnxnSocket);
  eventThread =  new EventThread();
}
Copy the code
  • The queue consists of three queues

OutgoingQueue, which is used to store packets waiting to be sent. PendingQueue this queue is used to store packets that have been sent and are waiting for a response from the server. WaitingEvents, where the server response is encapsulated as an event and placed in the queue for processing.

The user sends a request to read the data node. The request is encapsulated into a packet of the corresponding type and placed in the outgoingQueue. SendThread takes the packet out of the queue and sends it to the server and adds the packet to the pendingQueue. When sendThread receives the response from the server, it will compare it with the first packet in the pendingQueue queue to determine whether it is the expected response packet (it can be seen that the packet is processed in serial), and finally encapsulate the response packet into the corresponding type of events and add it to waitingEvents. It is retrieved by eventThread for processing.

(3) Operation and maintenance

  • Aliyun ZooKeeper interface tool

Official website: github.com/alibaba/tao… Because it has not been updated for too long, it is not recommended to use it

  • zkweb

Zookeeper Web management and monitoring interface, using the built-in H2 database, this version is based on Taobao god Yasenagat zkWeb source code based on a large upgrade and modification, mainly added cluster monitoring and internationalization functions, direct Java-JAR or put the war package into Tomcat can run! Official website: github.com/zhitom/zkwe…

  • Common O&M commands

The detailed configuration information of the server is displayed

echo conf|nc localhost 2181
Copy the code

Displays detailed information about all client connections on a specified server, including client IP addresses and session ids. Connection IP address, port number, number of packets sent, number of packets received, session Id of the connection, last operation/command, connection time stamp, timeout period (unconfirmed), last ZXID, last, response time stamp, connection time delay information, etc

echo cons|nc localhost 2181
Copy the code

Functional commands. Reset statistics for all connections

echo crst|nc localhost 2181
Copy the code

Executed against the Leader to output information about sessions and temporary nodes in all wait queues

echo dump|nc localhost 2181
Copy the code

The server status and connected client information are displayed

echo stat|nc localhost 2181
Copy the code

List all watcher information, grouped by watcher sessions, and list which paths the session subscribed to.

echo wchc|nc localhost 2181
Copy the code

Output some ZK runtime information version, delay, received packets, sent packets, number of connections, number of outstanding client requests, leader/follower status, number of ZNodes, number of watches, number of temporary nodes, approximate data size should be a sum, open file descriptor Number, maximum file descriptor number, fllower number, and so on

echo mntr | nc localhost 2181
Copy the code

PS: ZooKeeper comes to an end, the cornerstone of ZooKeeper Internet, which must understand the principle, have basic use, and must climb over a wall in the way of architecture