Stat structure of zookeeper C API

Source: Internet
Author: User

Find the stat structure in the header file zookeeper. Jute. H. First, you need to understand the features of this variable:

1. Most zookeeper c api parameters exist.

2. It is passed out as the node information variable of znode for external query and use.

The structure declaration is as follows:

Struct stat {
Int64_t czxid;
Int64_t mzxid;
Int64_t ctime;
Int64_t mtime;
Int32_t version;
Int32_t cversion;
Int32_t aversion;
Int64_t ephemeralowner;
Int32_t datalength;
Int32_t numchildren;
Int64_t pzxid;
};

For an example of a node, see

[ZK: 10.15.107.155: 3352 (connected) 111] STAT/C/HFX/SW1
Czxid = 0x2000007df
Ctime = Thu Mar 14 11:08:28 CST 2013
Mzxid = 0x200000a87
Mtime = Thu Mar 14 15:00:34 CST 2013
Pzxid = 0x2000007df
Cversion = 0
Dataversion= 608
Aclversion = 0
Ephemeralowner = 0x0
Datalength = 6
Numchildren = 0

The czxid indicates the zxid when the node is created, the mzxid indicates the current, and the zxid is used to serve the election leader. For details, refer to the principles of zookeeper.

I usually use ctime and mtime. The former indicates the creation time, and the latter indicates the last update time. Note that ctime is the moment when the node function is created, and mtime is reset when you call the change node function. However, exists and get will not cause updates. This feature can be used flexibly in our business-for example, we can use this parameter if we want to make some expiration judgments. However, we must note that the STAT command generates a time string, while the stat structure contains a timestamp. The two must be converted. The conversion method can be referred to below:

time_t tmtime = stat.mtime/1000;struct tm * last = localtime(&tmtime);char tmtimes[16];sprintf(tmtimes, "%4d%02d%02d%02d%02d%02d", last->tm_year+1900, last->tm_mon+1, last->tm_mday, last->tm_hour, last->tm_min, last->tm_sec);int64_t lasttime = (int64_t)strtoll(tmtimes, NULL, 10);

Of course, you can refer to the zookeeper source code:

void dumpStat(const struct Stat *stat) {    char tctimes[40];    char tmtimes[40];    time_t tctime;    time_t tmtime;    if (!stat) {        fprintf(stderr,"null\n");        return;    }       tctime = stat->ctime/1000;    tmtime = stat->mtime/1000;    fprintf(stderr, "\tctime = %s\tczxid=%llx\n"    "\tmtime=%s\tmzxid=%llx\n"    "\tversion=%x\taversion=%x\n"    "\tephemeralOwner = %llx\n",    ctime_r(&tctime, tctimes), _LL_CAST_ stat->czxid, ctime_r(&tmtime, tmtimes),    _LL_CAST_ stat->mzxid,    (unsigned int)stat->version, (unsigned int)stat->aversion,    _LL_CAST_ stat->ephemeralOwner);}

Other variables are generally used for query. For example, if you want to know several subnodes, you can view the numchildren parameter.

Use them as appropriate based on your business needs.

Most zookeeper APIs have two sets of interfaces. One is that the stat parameter is not required, and the other is that the stat parameter is required; whether or not you want to obtain the status of znode.

Generally, we mainly want to focus on znode versions and timestamps.

Note that the conversion between the type in the struct variable and the type of the information obtained from the command is used.

Related Article

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.