예제 #1
0
void
ZkClient::connect(const folly::fbstring& serverList) {
    zhandle_t* zhandle = zookeeper_init(
                             serverList.c_str(),
                             ZkClient::watcherCallback,
                             ZK_TIMEOUT,
                             nullptr,
                             this,
                             0
                         );
    CHECK(nullptr != zhandle) << "zookeeper_init() failed";
    LOG(INFO) << "zhandle created: " << zhandle;
    zHandle_.reset(zhandle);
}
예제 #2
0
void
ZkClient::createEphemeral(
    const folly::fbstring& path,
    const folly::fbstring& data) {
    zhandle_t* zhandle = zHandle_.get();
    CHECK(nullptr != zhandle) << "zhandle is NULL";
    int code = zoo_create(
                   zhandle,
                   path.c_str(),
                   data.data(),
                   data.length(),
                   &ZOO_READ_ACL_UNSAFE,
                   ZOO_EPHEMERAL,
                   nullptr,
                   0
               );
    CHECK(ZOK == code)
            << "zoo_acreate() failed with error: "
            << zooErrorCodeToString(code);
}
예제 #3
0
folly::fbstring
ZkClient::createEphemeralSequence(
    const folly::fbstring& path,
    const folly::fbstring& data) {
    zhandle_t* zhandle = zHandle_.get();
    CHECK(nullptr != zhandle) << "zhandle is NULL";
    folly::fbstring newPath;
    newPath.resize(path.size() * 3);
    int code = zoo_create(
                   zhandle,
                   path.c_str(),
                   data.data(),
                   data.length(),
                   &ZOO_READ_ACL_UNSAFE,
                   ZOO_EPHEMERAL | ZOO_SEQUENCE,
                   (char*) newPath.data(),
                   newPath.size()
               );
    CHECK(ZOK == code)
            << "zoo_acreate() failed with error: "
            << zooErrorCodeToString(code);
    return std::move(newPath);
}
예제 #4
0
void
ZkClient::subscribeDataChanges(
    const folly::fbstring& path,
    DataChangeCallback dataChangeCallback) {
    {
        toft::Mutex::Locker locker(&dataChangeCallbackMapMutex_);
        dataChangeCallbackMap_[path] = std::move(dataChangeCallback);
    }
    DataChangeCallbackContext* context = new DataChangeCallbackContext();
    context->path = path;
    context->zkClient = this;
    zhandle_t* zhandle = zHandle_.get();
    CHECK(nullptr != zhandle) << "zhandle is NULL";
    int returnCode = zoo_aget(
                         zhandle,
                         path.c_str(),
                         1,
                         ZkClient::dataChangeCallback,
                         context
                     );
    CHECK(ZOK == returnCode)
            << "zoo_aget() failed with error: "
            << zooErrorCodeToString(returnCode);
}