Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of zk client conversation

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the process of zk client conversation". In daily operation, I believe many people have doubts about the process of zk client conversation. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the process of zk client conversation?" Next, please follow the editor to study!

Client session process

Main core components of the zk client

Zk client session process

Initialization phase

Creation Pha

Response phase

UML diagram

Main components of zk client

Zookeeper client Portal

ClientWatchManager: client Watch Manager

HostProvider: client address Manager

ClientCnxn: core thread, including SendThread and EventThread threads

SendThread is an iUnix thread, which is mainly responsible for the network io between the client and the server

EventTrhead is the event thread

Initialization phase

It is mainly divided into:

1 initialize the ZooKeeper object

An watcher manager ClientWatcherManager is created during initialization by instantiating an object by calling the constructor of zookeeper

2 set session default watcher

If a watcher is passed in the construction, save it to ClientWatcherManager as the default watcher

3 build zk service address list manager HostProvider

Save the list of server addresses passed in the constructor in the server address list manager HostProvider

4 create and initialize client network connection

The zookeeper client creates a network connector, ClientCnxn, to manage client and server network interactions

During the creation of ClientCnxn, the client's two core queues, outGoingQueue and pendingQueue, are also initialized

Send a request queue as a client and a server response waiting queue

5 initialize SendThread and EventThread

Session creation phase

6 start SendThread and EventThread

SendTHread first judges the status of the current client, performs a series of clean work, and prepares the client to send a "session creation" request.

7 get a service address

Before creating a TCP, SendThread first needs to obtain the target address of a ZooKeeper server, usually taking an address randomly from the HostProvider, and then delegating it to ClientCnxnSocket to create a TCP connection with the Zookeeper server

8 create a tcp connection, and after obtaining a server address, clientCnxnSocket is responsible for creating a TCP persistent connection with the server

9 build ConnectRequest request

After the TCP connection is created, does it mean that the connection with the zookeeper server has been completed? Actually, no, the above steps only completed the socket connection from the network tcp layer, but did not complete the connection between the zookeeper client and the server.

SendThread will be responsible for the current request parameters, construct a ConnectRequest request, and put the Packet object wrapped in the network io layer into the sending queue outgoingQueue.

10 send request

ClientCnxnSocket will take the Packet to be sent from outgoingQueue, serialize it into bytebuffer, and send it.

Response processing phase

11 response processing

After receiving the server response, first determine whether the current client state is "initialized". If the initialization is not completed, the response must be the response of the session creation request, and give it to the readConnnectResult method to handle the response.

12 processing respinse

ClientCnxnSocket will deserialize the received server response, get the ConnectResponse object, and get the session sessionId assigned by the zookeeper server.

13 connection succeeded

Notify the SendThread thread to handle session parameter settings, including time settings, etc.

On the other hand, the address manager needs to be notified of the address of the server to which HostProvider is currently successfully connected

14 generate event

In order for the upper application to sense the successful creation of the session, SendThread generates an event SyncConnected-None, which represents the successful creation of the session and passes the event to the EventTread thread

15 query Watcher

After EventThread receives the event, the corresponding watcher is queried from the ClientWatchManager manager and placed in the waitingEvents queue.

16 handle events

Take out the watcher list corresponding to the event and call the process API to achieve the purpose of starting watcher.

Public ZooKeeper (String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly, HostProvider aHostProvider, ZKClientConfig clientConfig) throws IOException {LOG.info ("Initiating client connection, connectString=" + connectString + "sessionTimeout=" + sessionTimeout + "watcher=" + watcher); if (clientConfig = = null) {clientConfig = new ZKClientConfig ();} this.clientConfig = clientConfig; watchManager = defaultWatchManager (); watchManager.defaultWatcher = watcher ConnectStringParser connectStringParser = new ConnectStringParser (connectString); hostProvider = aHostProvider; cnxn = createConnection (connectStringParser.getChrootPath (), hostProvider, sessionTimeout, this, watchManager, getClientCnxnSocket (), canBeReadOnly); cnxn.start () } / @ VisibleForTestingprotected ClientCnxn createConnection (String chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper, ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly) throws IOException {return new ClientCnxn (chrootPath, hostProvider, sessionTimeout, this, watchManager, clientCnxnSocket, canBeReadOnly) At this point, the study of "what is the process of zk client conversation" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report