ZooKeeperStringResult ZooKeeperNodeManager::GetNode( const std::string& path, IZooKeeperNodeEventHandler* eventHandler ) { // insert into cache before calling wget to avoid race condition Insert( path, eventHandler ); Stat stat; memset( &stat, 0, sizeof( stat ) ); char data[MAX_DATA_BUFFER]; int length = MAX_DATA_BUFFER; // gets the data associated with a node synchronously and set a watcher int result = zoo_wget( m_zooKeeperSessionManager->GetHandle(), path.c_str(), ZooKeeperEvent::EventHandler, this, data, &length, &stat ); if( result == ZOK ) { return std::make_pair( std::string( data, length ), true ); } else if( result == ZNONODE ) { // implement wexists? return std::make_pair( std::string(), false ); } throw ZooKeeperException( result ); }
int Cluster::FetchClusterTopo() { std::string partitions_path = "/" + g_db->GetConf().cluster_name + "/partitions"; std::string nodes_path = "/" + g_db->GetConf().cluster_name + "/nodes"; char bufer[2028]; int err = zoo_wget(m_zk, nodes_path.c_str(), ZKGetWatchCallback, this, bufer, 0, NULL); err = zoo_get(m_zk, partitions_path.c_str(), 0, bufer, 0, NULL); return -1; }
bool cached_zk::read_(const string& path, string& out) { char buf[1024]; int buflen = 1024; int rc = zoo_wget(zh_, path.c_str(), cached_zk::update_cache, this, buf, &buflen, NULL); if (rc == ZOK) { out = string(buf, buflen); return buflen <= 1024; } else { LOG(ERROR) << zerror(rc); return false; } }
string GetDataBuilderImpl::forPath(string path, int length) { struct Stat *s = NULL; if (this->stat != NULL) { s = this->stat; } char buff[length]; if (watcherFn == NULL) { zoo_get(zk, path.c_str(), 0, buff, &length, s); } else { zoo_wget(zk, path.c_str(), watcherFn, watcherCtx, buff, &length, s); } if (length == -1) { return string(); } else { return string(buff, length); } }
static VALUE method_get(VALUE self, VALUE reqid, VALUE path, VALUE async, VALUE watch) { char * data ; int data_len = MAX_ZNODE_SIZE; struct Stat stat; int rc; VALUE output ; STANDARD_PREAMBLE(self, zk, reqid, path, async, watch, data_ctx, watch_ctx, call_type); /* ugh */ data = malloc(MAX_ZNODE_SIZE); switch (call_type) { case SYNC: rc = zoo_get(zk->zh, RSTRING_PTR(path), 0, data, &data_len, &stat); break; case SYNC_WATCH: rc = zoo_wget(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, data, &data_len, &stat); break; case ASYNC: rc = zoo_aget(zk->zh, RSTRING_PTR(path), 0, zkrb_data_callback, data_ctx); break; case ASYNC_WATCH: rc = zoo_awget(zk->zh, RSTRING_PTR(path), zkrb_state_callback, watch_ctx, zkrb_data_callback, data_ctx); break; } output = rb_ary_new(); rb_ary_push(output, INT2FIX(rc)); if (IS_SYNC(call_type) && rc == ZOK) { if (data_len == -1) rb_ary_push(output, Qnil); /* No data associated with path */ else rb_ary_push(output, rb_str_new(data, data_len)); rb_ary_push(output, zkrb_stat_to_rarray(&stat)); } free(data); return output; }
ZooKeeperStringResult ZooKeeperNodeManager::GetNode( const std::string& path ) { Stat stat; memset( &stat, 0, sizeof( stat ) ); char data[MAX_DATA_BUFFER]; int length = MAX_DATA_BUFFER; // gets the data associated with a node synchronously int result = zoo_wget( m_zooKeeperSessionManager->GetHandle(), path.c_str(), NULL, NULL, data, &length, &stat ); if( result == ZOK ) { return std::make_pair( std::string( data, length ), true ); } else if( result == ZNONODE ) { return std::make_pair( std::string(), false ); } throw ZooKeeperException( result ); }
int zoo_wget_int(zhandle_t *zh, const char *path, watcher_fn watcher, unsigned long watcherCtx, char *buffer, int* buffer_len, struct Stat *stat) { return zoo_wget(zh, path, watcher, (void*)watcherCtx, buffer, buffer_len, stat); }
int main() { char buffer[512]; char p[2048]; char *cert=0; char appId[64]; strcpy(appId, "example.foo_test"); cert = foo_get_cert_once(appId); if(cert!=0) { fprintf(stderr, "Certificate for appid [%s] is [%s]\n",appId,cert); strncpy(p,cert, sizeof(p)-1); free(cert); } else { fprintf(stderr, "Certificate for appid [%s] not found\n",appId); strcpy(p, "dummy"); } zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR); zh = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0); zh2 = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0); // zookeeper_init是一个异步函数需要等待zh->state非0 while(zoo_state(zh) == 0) {} while(zoo_state(zh2) == 0) {} // struct Id id_tmp = {(char*)"digest", (char*)"user:tpUq/4Pn5A64fVZyQ0gOJ8ZWqkY="}; int rc = zoo_add_auth(zh,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0); if (rc == ZOK){ printf("zoo add auth sucess\n"); } else { printf("zoo add auth failed\n"); } // struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_ALL, id_tmp}};//, {ZOO_PERM_READ, ZOO_AUTH_IDS}, {ZOO_PERM_WRITE, ZOO_AUTH_IDS}}; // struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL}; rc = zoo_create(zh,"/xyz","value", 5, &ZOO_CREATOR_ALL_ACL/*&CREATE_ONLY*/, ZOO_EPHEMERAL, buffer, sizeof(buffer)-1); if (rc == ZOK) { printf("create ok \n"); } else { printf("create faild%d\n" ,rc); } struct ACL_vector ret; printf("zoo set acl %d\n", zoo_get_acl(zh, "/xyz", &ret, 0)); rc = zoo_exists(zh, "/xyz", 0, 0); if (rc != ZOK) { printf("%d exist fail\n", rc); } else { printf("/xyz is exist\n"); } /** this operation will fail with a ZNOAUTH error */ int buflen= sizeof(buffer); struct Stat stat; rc = zoo_exists(zh2, "/xyz", 0, 0); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("zh2 exist %s\n", buffer); } rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("get data %s\n", buffer); } if (rc == ZOK){ printf("zoo create xyz sucess\n"); } else { printf("zoo create xyz failed %s\n", zerror(rc)); } rc = zoo_add_auth(zh2,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0); if (rc == ZOK){ printf("zoo add auth sucess\n"); } else { printf("zoo add auth failed\n"); } rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("get data %s\n", buffer); } rc = zoo_wget(zh2, "/xyz", watcher, NULL, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("wget sucess %s\n", buffer); } zookeeper_close(zh); return 0; }