Zookeeper 一种简单的原子操作机制:

Zookeeper 一种简单的原子操作机制:

//zoo_get()方法: int zoo_get(zhandle_t * zh, const char *path, int watch,

//                   char *buffer, int *buffer_len, struct Stat *stat);

//zh:连接句柄, path:文件路径, watch: , buffer:获得的数据, stat:状态信//息(比较有用的如:version信息)

 

char tmpData[MAXNODENAMELENS];

uint32_t tmpDataSize = MAXNODENAMELENS - 1;

rc = zoo_get(zh, nodeName, 1, tmpData, &tmpDataSize, &stat);

sdsfree(nodeName);

if  (rc < 0) {

return rc;

}

tmpData[tmpDataSize] = '\0';

 

char *stop;

uint32_t expectGroupID = strtol(tmpData, &stop, 10);

if (expectGroupID != groupID_src)

{

SlogWrite(LL_FATAL, "ChangeBlockUtil:expectGroupID!=groupID_src.[expectGroupID=%d]", expectGroupID);

return -1;

}

 

// optimize: use version set

uint32_t version = stat.version;

sds data = sdscatprintf(sdsempty(), "%d", groupID_dst);

SlogWrite(LL_DEBUG, "Set %s value %s.", nodeName, data);

rc = zoo_set(zh, nodeName, data, sdslen(data), version);

sdsfree(nodeName);

sdsfree(data);

if (rc < 0)

{

SlogWrite(LL_FATAL, "ChangeBlockUtil: fail to zoo_set with version=%d", version);

return rc;

}

posted on 2014-04-16 18:45  程序猿猿猿  阅读(1050)  评论(0编辑  收藏  举报

导航