zookeeper c api 之Stat结构
来源:互联网 发布:java读取usb接口 编辑:程序博客网 时间:2024/06/05 07:53
在头文件zookeeper.jute.h中,找到Stat这个结构,首先需要明白这个变量的特点:
1 存在大部分的zookeeper c api的参数中
2 作为znode的节点信息变量传递出去,供外部查询和使用
结构声明如下:
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;
};
举一个节点的例子可以见
[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
要地说明cZxid表示创建该节点时候的zxid,mZxid表示当前的,zxid是用来为选举leader服务的,具体可以阅读zookeeper的原理。
我通常会用到ctime和mtime,前者表示创建的时间,后者表示最近一次更新的时间。要注意的是ctime就定死了创建的那一刻,而mtime会在你调用更改节点函数的时候重新设置;但是exists和get是不会引起其更新的。这个特点可以给我们在业务上很灵活的发挥作用——比如我们想作一些过期判断等就可以运用此参数。但是需要注意的是,我们stat命令出来的是时间字符串,而Stat结构里面是时间戳,两者需要做一个转换,转换的方法可以参考如下:
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);
当然你可以可以参考zookeeper源码里的东西:
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);}
其他几个变量一般供查询使用,比如你想了解有几个子节点,就查看numChildren参数。
根据自己的业务需求,适当地使用他们。
在zookeeper的api中,大部分都有两套接口,一个是不需要传Stat stat参数的,一个是需要传入stat的;选择哪一个就在于你是否想获取znode的状态。
一般而言,我们主要想关心的是znode的版本、时间戳等。
特别要注意的是——结构体变量中的类型和命令出来的信息的类型之间的转换使用。
- zookeeper c api 之Stat结构
- zookeeper c api 之Stat结构及封装
- linux C之stat()
- linux C之stat()
- linux C之stat()
- linux C之stat()
- linux C之stat()
- zookeeper C API实例
- zookeeper C API
- Zookeeper C API 指南
- zookeeper C API
- zookeeper C API 编译
- Zookeeper之JAVA API
- ZooKeeper之Java API
- 文件结构体之struct stat
- IAR之C-STAT,C-RUN
- Zookeeper C API学习总结
- Zookeeper实战之使用Zookeeper API
- zookeeper安装配置
- zookeeper如何永久监听
- IOCP简单过程
- zookeeper C API 的安装和使用指南
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- zookeeper c api 之Stat结构
- Linux /etc/passwd & /etc/shadow 详解 2
- zoo_add_auth 参数无效问题
- zookeeper c api 常见变量注释
- Java 程序打包安装过程
- hbase配置详解
- MVC @using (Html.BeginForm())
- Java和eclipse常用操作
- Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值