[Zookeeper reading notes-3] Client principle

1 Basic concepts

ClientCnxn is a network connector that manages the network interaction between the client and the server.

ClientWatchManager saves the client's watcher

HostProvider server address list manager

OutgoingQueue client's request sending queue

pendingQueue client server response waiting queue

SendThread manages all network IO on the client and server

The SendThread of the client sends PING packets to the server through a certain frequency to implement heartbeat detection and maintain the session life cycle of the client and the server;

The TCP connection between the client and the server is disconnected during the session period and will automatically reconnect. This process is transparent;

 

SendThread manages all client request sending and response receiving;

SendThread passes the server-side events to EventThread for processing;

EventThread handles client events and triggers watcher registration registered by the client.

2 Client initialization and startup process

  1. Set the default watcher
  2. Set Zookeeper server address list
  3. Create ClientCnxn

Create a Zookeeper construction method and pass in the watcher object, then this watcher object will be saved in the defaultWatcher as the default watcher during the client session.

2.1 Initial stage of session creation

1 Initialize the Zookeeper object and create the client watcher manager ClientWatcherManager

2 Set the default Watcher for the session. The Zookeeper object created when the incoming Watcher object is saved in the ClientWatcherManager

3 Construct Zookeeper server address list manager HostProvider

4 Create and initialize the client network connection from ClientCnxn to manage the network interaction between the client and the server;

    At the same time, the client will initialize the queue outgoingQueue for the client's request sending queue and pendingQueue for the server's response waiting queue;

    At the same time, the client must also create ClientCnxnSocket as the underlying IO processor

5 Initialize the network thread SendThread to manage the network connection between the client and the server, and use ClientCnxnSocket as the underlying IO processor

   Initialize the network thread EventThread to handle client events, and initialize the waiting event queue waitingEvents to store all events waiting to be processed by the client.

2.2 Session creation phase 

6 Start SendThread, EventThread

   SendThread first judges the status of the client, performs some cleanup work, and prepares the client for sending a "session creation" request.

7 Get a list of server addresses

   SendThread first randomly obtains a Zookeeper address from HostProvider and hands it to ClientCnxnSocket to create a TCP connection with the Zookeeper server.

8 Create a TCP connection

Obtain a server address, ClientCnxnSocket is responsible for creating a long TCP connection with the server

9 Construct connectRequest request

SendThread is responsible for constructing ConnectRequest, which represents that the client wants to connect with the server to create a session. The ZK client will also wrap the request into a Packet object of the network layer and put it in the outgoingQueue of the request sending queue.

10 Send request

ClientCnxnSocket takes a Packet object sent from outgoingQueue and serializes it into a ByteBuffer object to send to the server

2.3 Response processing stage

11 Receive server response

When ClientCnxnSocket receives a response from the server, it first determines whether the current client status is "initialized". If it is not initialized, the response is considered to be a response to the session creation request. The readConnectResult method directly processes the response.

12 Handling Response

ClientCnxnSocket deserializes the server response, gets the ConnectResponse object, and obtains the SessionId assigned by the ZK server.

13 Successful connection

After the connection is successful, the gay SendThread thread further sets the parameters such as readTimeout, connectTimeout, and updates the client status;

Inform the address manager that the HostProvider currently connects to the server address successfully.

14 Generate event SyncConnected-None

SendThread generates events for the purpose of upper-layer applications being aware of the successful session creation. Successfully created a session between the client and the server, and passed the event to the EventThread thread.

15 Get Watcher

EventThread obtains the corresponding watcher from ClientWatcherManager after receiving the event, directly finds the default watcher stored in step 2 for SyncConnected-None, and puts it into the waitingEvents queue in EventThread.

16 Handling events

EventThread gets the pending watcher object from the waitingEvents queue and calls the object's process method.

2.4 Server address list

How does the server address list passed in when creating the ZK client take effect? Connect the servers in order or randomly?

The server address list resolver ConenctStringParser parses the incoming address list into chrootPath and saves the server address list

-chroot means that each client sets a namespace for itself, and then the operation of the client will be limited to its own namespace. For example "192.168.1.1:2181, 192.168.1.2:2181/apps/a"

-ConenctStringParser encapsulates the IP and port into the InetSocketAddress object and saves it in the ConenctStringParser.serverAddresses property in the form of ArrayList. The processed address list will be further encapsulated into StaticHostProvider (providing the number of server addresses, address lists, and callback methods for clients and The server successfully created the connection).

StaticHostProvider implements the interface HostProvider

-Resolve addresses and ports and encapsulate them into sets

-Call Collections.shuffle to break up the address list and assemble it into a circular circular queue, and obtain address information from this queue during use

2.5 Network IO

1 Request to send

The OutgoingQueue queue extracts a Packet object that can be sent, and at the same time generates the client request serial number XID, sets it in the Packet request header, and then serializes and sends it.

After the request is sent, immediately save the Packet to the pendingQueue and wait for the response from the server to process it accordingly.

2 Response reception

After receiving the response from the server, the client performs different processing according to the different types of client requests.

The client has not been initialized, indicating that the client and the server are creating a session, and directly serialize the received ByteBuffer (incomingBuffer) into a ConnectResponse object.

The client is in a normal session cycle, and the received server response is an event. The client serializes the received ByteBuffer (incomingBuffer) into a WatcherEvent object and puts the object into the queue to be processed.

If it is a regular request (Create, GetData, Exists, etc.), take a Packet object from the pendingQueue queue for processing. The client checks the XID contained in the server's response to ensure the order of request processing, and then serializes the received ByteBuffer (incomingBuffer) into a Response object.

The finishPacket method handles logic such as Watcher registration.

 

 

Published 61 original articles · won praise 2 · Views 7302

Guess you like

Origin blog.csdn.net/hebaojing/article/details/105027237