static void zookeeper_get_children_completion( int rc, const struct String_vector *children, const void *arg) { char *path; int status; int i; zookeeper_resolver *r = (zookeeper_resolver *)arg; if (rc != 0) { gpr_log(GPR_ERROR, "Error in getting zookeeper children of %s", r->name); return; } if (children->count == 0) { gpr_log(GPR_ERROR, "Error in resolving zookeeper address %s", r->name); return; } r->resolved_addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); r->resolved_addrs->addrs = NULL; r->resolved_addrs->naddrs = 0; r->resolved_total = children->count; /** TODO: Replace expensive heap allocation with stack if we can get maximum length of zookeeper path */ for (i = 0; i < children->count; i++) { gpr_asprintf(&path, "%s/%s", r->name, children->data[i]); status = zoo_awget(r->zookeeper_handle, path, zookeeper_watcher, r, zookeeper_get_children_node_completion, r); gpr_free(path); if (status != 0) { gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", path); } } }
static void zookeeper_resolve_address(zookeeper_resolver *r) { int status; status = zoo_awget(r->zookeeper_handle, r->name, zookeeper_watcher, r, zookeeper_get_node_completion, r); if (status != 0) { gpr_log(GPR_ERROR, "Error in getting zookeeper node %s", r->name); } }
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; }
void ZKClient::Get(const std::string& _node, std::string* _data, sp_int32* _version, VCallback<> watcher, VCallback<sp_int32> cb) { LOG(INFO) << "Getting zknode " << _node << std::endl; ZKClientGetStructure* get_structure = new ZKClientGetStructure(); get_structure->result_ = _data; get_structure->version_ = _version; get_structure->cb_ = CreateCallback(this, &ZKClient::ZkActionCb, std::move(cb)); sp_int32 rc; if (!watcher) { rc = zoo_aget(zk_handle_, _node.c_str(), 0, GetCompletionWatcher, get_structure); } else { rc = zoo_awget(zk_handle_, _node.c_str(), CallWatcher, CreateCallback(this, &ZKClient::ZkWatcherCb, std::move(watcher)), GetCompletionWatcher, get_structure); } if (rc) { // There is nothing we can do here. Continuing will only make // other things fail LOG(FATAL) << "zoo_aget/zoo_awget returned non-zero " << rc << " errno: " << errno << " while getting " << _node << "\n"; } }