Zookeeper Development FAQs

Source: Internet
Author: User
Tags zookeeper client

Background and purpose

Zookeeper in the development process encountered some common problems, in order to follow up development do not make the same mistake, summarize this kind of problem, and analyze and solve.

Suitable for people

It is mainly suitable for zookeeper development, testing and operation and maintenance related personnel.

Problem and resolution one, about the use of zookeeper_init function problem Description:

When the developer calls the Zookeeper_init function, returning a non-empty handle zhandle_t *zh, the initialization is considered successful, which may cause subsequent operations to fail.

Problem Analysis:

zhandle_t *zookeeper_init (const char *host, WATCHER_FN fn, int recv_timeout,const clientid_t *clientid, void *context, The INT flags function returns a handle to the Zookeeper client's communication with the server, and usually we only determine whether the zookeeper client is connected to the zookeeper server based on the return handle condition. If the handle is empty, it is considered to be a failure, and non-null succeeds. In fact, Zookeeper_init creates a handle to the communication with the zookeeper server and a session corresponding to the handle, and the creation of the session is an asynchronous process, and Zookeeper_init returns an available handle only if the session is established successfully.

Problem solving:

How to correctly determine the success of Zookeepr_init initialization can be done in the following three ways
1, judge whether the state of the handle is Zoo_connected_state status, through Zoo_state (ZH) to determine whether the state value is zoo_connected_state.

C
123456789 void ensureconnected() { pthread_mutex_lock(&lock); While (zoo_state(zh)!=zoo_connected_state)     { pthread_cond_wait(&cond, &lock);     } pthread_mutex_unlock(&lock); }

2, set watcher in Zookeeper_init, when zookeeper client and server session is established, trigger watcher when Watcher State = 3 (zoo_connected_state), type = -1 (zoo_session_event), the confirmation session was established successfully, zookeeper client initialization is successful and can be followed up.
3, the business can make a guarantee, call Zookeeper_init return handle zh, through the handle to try to do zoo_exists () or zoo_get_data () operation, according to the results of the operation to determine whether the initialization is successful.

Second, how to solve the problem of Session failure Description:

The session fails, causing the registered watcher to be lost.

Problem Analysis:

If the zookeeper client and the server have not established a connection within the negotiated timeout period, when the client is connected to the server again, all watcher have been deleted by the server because the session is invalid. This causes all watcher to be re-registered.
Session fails, zookeeper client and server reconnect all watcher will receive two trigger, first wathetr state = 1,type = 1 (state = 1 means connecting, type =-1 means Sessio n event); the second watcher state = -112,type =-1 (state =-112 indicates session expiration).

Problem solving:

There are two ways to solve the session failure problem
1, get trigger session failure watcher, business re-register all watcher.
2, can not be solved at all, but reduce the probability of the session failure. A longer session time-out time is set by zookeeper client and server. (Refer to the next question)

Third, why Zookeeper_init set recv_timeout longer but no effect problem description:

Zookeeper_init set Recv_timeout 100000ms, but the client disconnects from the server 30s the session fails.

Problem Analysis:

About determining the session timeout: The timeout set in Zookeeper_init is not a true session timeout, the session timeout requires server to negotiate with the client and the business is Zoo_recv_timeout ( zhandle_t* zh) Gets the time-out period after the server negotiates with the client. Server: Minsessiontimeout (default is: Ticktime * 2), maxsessiontimeout (default = Ticktime *), ticktime default value is 2000ms. So the session range is 4s ~ 40s. Client: sessiontimeout, no default value, set Recv_timeout value when creating instance. It is often assumed that the zookeeper client was created with a sessiontimeout of 100s, without altering the configuration of the server side, and the default value would not take effect. Cause: The zookeeper instance of the client sends the Sessiontimeout parameter to the server when the connection is created, and the server is set according to the corresponding minsession/maxsession timeout. Forces the modification of the Sessiontimeout parameter, which is the parameter returned to 4s~40s. Therefore, the server will not necessarily be the client's sessiontimeout as the session expire management time.

Problem solving:

When you increase the zookeeper_init recv_timeout size, you need to configure the value of Ticktime.
Ticktime settings are in the Conf/zoo.cfg file

# The number of milliseconds of each
tickticktime=2000 (default)
Note: Ticktime Heartbeat basic time unit milliseconds, ZK basically all the time is an integer multiple of this time.
Iv. Zoo_get_children Memory leak problem Description:

A memory leak occurred while calling the Zoo_get_children function.

Problem Analysis:

By looking at the code, the problem is found in Zooapi int zoo_get_children (zhandle_t *zh, const char *path, int watch,struct string_vector *strings), the API s The Tring_vector *strings structure is defined as follows:

C
12345 struct string_vector { int32_t count; Char * *data; };

When the Zookeeper API returns GetChildren results through the string_vector struct, malloc allocates memory, stores all of the child nodes in data, and frees up memory that needs to be handled by the client.

Problem solving:

Call Zoo_get_children (zh, path, watch, strings); The method to free memory by calling Zookeper: Deallocate_string_vector (Strings).

C
12345678910111213 int deallocate_string_vector(struct string_vector *v){ if (v-data)     { int32_t i; For (i = 0; I < v, Count; I+ +)         { deallocate_string(&v,data[i]);         }Free (v-data); v-data = 0;     } return 0; }

V. How to set up and get watcher correctly

Watcher Setup is the most common development, need to understand some of the basic characteristics of watcher, for exists, GetData, getchild for the different operations of the node will receive different watcher information. Changes to the parent node and changes to the grandchild node do not trigger watcher, and changes to the watcher itself and child nodes trigger watcher, as specified in the following table.

Operation Method Trigger Watcher Watcher State Watcher Type Watcher Path
Create current node GetData X X X X
GetChildren 3 4
Exists X X X X
Set current node GetData 3 3
GetChildren X X X X
Exists 3 3
Delete current node GetData 3 2
GetChildren 3 2
Exists 3 2
Create child node GetData X X X X
GetChildren 3 4
Exists X X X X
Set child node GetData X X X X
GetChildren X X X X
Exists X X X X
Delete child node GetData X X X X
GetChildren 3 4
Exists X X X X
Restore connection GetData 1 -1 X
GetChildren 1 -1 X
Exists 1 -1 X
Restore connection session not timed out GetData -112 -1 X
GetChildren -112 -1 X
Exists -112 -1 X
Restore Connection Session Timeout GetData 3 -1 X
GetChildren 3 -1 X
Exists 3 -1 X
Note: state = 2 indicates a delete event, State = 3 indicates a node data change, State =4 represents a child node event, state =-1 indicates a session event. Type = 112 indicates session failure; type = 1 means session is established; Tpye = 3 indicates success of session establishment. x indicates no, √ indicates yes. Six, zookeeper connection number problem Description:

The zookeeper server is healthy and the client connection is abnormal.

Problem Analysis:

This is because the number of zookeeper client connections has exceeded the configured maximum number of connections that zookeeper server obtains. This causes the Zookeeper client connection to fail.

Workaround:

Modify the Conf/zoo.cfg file in the Zookeeper installation directory. Change the Maxclientcnxns parameter to a larger value.

Vii. Zookeeper Server installation related issues One: Whether you can install only one zookeeper server. Question Answer:

Can be installed, there is no leader,follow at this time, Zookeeper server status is standalone.

./zkserver.sh Status
JMX enabled by default
Using config:/home/bbs/zookeeper-3.4.5/bin/. /conf/zoo.cfg
Mode:standalone

About the ZOO.CFG configuration:

ticktime=2000initlimit=10
Synclimit=5
Datadir=. /data
clientport=4181
Problem two: the development of not enough machines, on one machine installed three zookeeper server cluster. Question Answer:

This installation mode can only be said to be a pseudo-cluster mode. Three zookeeper servers are installed on the same server (platform), and you need to ensure that clientport is not the same.
Unzip the zookeeper installation package separately in the three directory Server1,server2,server3 configuration file zoo.cfg
Server1 configuration file Zoo.cfg,server1 add myID content to 1 in the data directory.

Datadir=. /datadata
Logdir=. /datalog
clientport=5181
server.1=platform:5888:6888
Server.2= platform:5889:6889
server.3= platform:5890:6890

Server2 configuration file Zoo.cfg,server1 add myID content to 2 in the data directory.

Datadir=. /datadata
Logdir=. /datalog
clientport=6181
server.1=platform:5888:6888
Server.2= platform:5889:6889
server.3= platform:5890:6890

Server3 configuration file Zoo.cfg,server1 add myID content to 3 in the data directory.

Datadir=. /datadata
Logdir=. /datalog
clientport=7181
server.1=platform:5888:6888
Server.2= platform:5889:6889
server.3= platform:5890:6890

Zookeeper Development FAQs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.