0

我发现了一些回调签名,例如data_completion_t, string_completion_t。但我想知道 zookeeper 是如何在异步 API 中调用这些函数的。

它是否使用单个线程来接收来自 Zookeeper 的响应?所以我必须user_data在回调中添加互斥锁来保护。还是每次调用另一个异步 API 时都会检查回调?

4

1 回答 1

1

我测试了C mt库,发现zookeeper又为IO和补全创建了2个线程,补全函数是在补全线程中调用的,所以这些函数应该是顺序调用的。

2012-10-09 15:35:36,904:60028(0x7fff7d0f7180):ZOO_INFO@zookeeper_init@786: Initiating client connection, host=127.0.0.1:3000 sessionTimeout=5000 watcher=0x0 sessionId=0 sessionPasswd=<null> context=0x0 flags=0
2012-10-09 15:35:36,904:60028(0x7fff7d0f7180):ZOO_DEBUG@start_threads@221: starting threads...
2012-10-09 15:35:36,905:60028(0x10af45000):ZOO_DEBUG@do_completion@459: started completion thread
2012-10-09 15:35:36,905:60028(0x10aec2000):ZOO_DEBUG@do_io@367: started IO thread
2012-10-09 15:35:36,905:60028(0x7fff7d0f7180):ZOO_DEBUG@zoo_aset@2700: Sending request xid=0x5073d3c9 for path [/mm/mmidc] to 127.0.0.1:3000
2012-10-09 15:35:36,905:60028(0x10aec2000):ZOO_INFO@check_events@1703: initiated connection to server [127.0.0.1:3000]
2012-10-09 15:35:36,909:60028(0x10aec2000):ZOO_INFO@check_events@1750: session establishment complete on server [127.0.0.1:3000], sessionId=0x13a43632d85000f, negotiated timeout=5000
2012-10-09 15:35:36,909:60028(0x10aec2000):ZOO_DEBUG@check_events@1756: Calling a watcher for a ZOO_SESSION_EVENT and the state=ZOO_CONNECTED_STATE
2012-10-09 15:35:36,909:60028(0x10af45000):ZOO_DEBUG@process_completions@2107: Calling a watcher for node [], type = -1 event=ZOO_SESSION_EVENT
2012-10-09 15:35:36,910:60028(0x10aec2000):ZOO_DEBUG@process_sync_completion@1868: Processing sync_completion with type=1 xid=0x5073d3c9 rc=-101
zoo_set2: no node

非常简单的测试代码,zookeeper服务器是一个独立的监听localhost:3000

  int main()
  {

    zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);

    const char* host = "127.0.0.1:3000";

    zhandle_t *zh;
    clientid_t myid;
    zh = zookeeper_init(host, NULL, 5000, &myid, NULL, 0);

    struct Stat stat;
    const char* line = "/test";
    const char* ptr = "hello, world";
    int ret = zoo_set2(zh, line, ptr, strlen(ptr), -1, &stat);
    printf("zoo_set2: %s\n", zerror(ret));

  }

实际上,这些在文档中都有解释。http://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html

C 绑定

The C binding has a single-threaded and multi-threaded library. The multi-threaded library is easiest to use and is most similar to the Java API. This library will create an IO thread and an event dispatch thread for handling connection maintenance and callbacks. The single-threaded library allows ZooKeeper to be used in event driven applications by exposing the event loop used in the multi-threaded library.

于 2012-10-09T07:40:46.983 回答