【发布时间】:2014-12-03 10:13:17
【问题描述】:
我正在尝试实现一个 C 应用程序,该应用程序将监控即将到来的 couchbase 远程集群上发生的 writes / modifications / new documents 事件来自不同的应用程序。我现在熟悉 couchbase C SDK 和同步实例,但我无法将它与 libevent 结合起来进行异步 I/O。
我阅读了 couchbase libevent plugin documentation 和 external event loop integration example,但我无法理解如何告诉我的 event_base,例如:
在此存储桶上监控此文件并在修改时向我发送回调
这是我目前所做的:
首先,我创建了我的 libevent IO 选项
struct event_base *mEvbase = event_base_new();
lcb_t instance;
lcb_error_t err;
struct lcb_create_io_ops_st ciops;
lcb_io_opt_t ioops;
memset(&ciops, 0, sizeof(ciops));
ciops.v.v0.type = LCB_IO_OPS_LIBEVENT;
ciops.v.v0.cookie = mEvbase;
err = lcb_create_libevent_io_opts(0, &ioops, mEvbase);
if (err != LCB_SUCCESS) {
ERRORMSG0("Failed to create an IOOPS structure for libevent: %s\n", lcb_strerror(NULL, error));
}
然后我创建我的实例:
struct lcb_create_st create_options;
std::string host = std::string("couchbase://192.168.130.10/");
host.append(bucket);
const char password[] = "password";
create_options.version = 3;
create_options.v.v3.connstr = host.c_str();
create_options.v.v3.passwd = password;
create_options.v.v3.io = ioops;
//Creating a lcb instance
err = lcb_create(&instance, &create_options);
if (err != LCB_SUCCESS) {
die(NULL, "Couldn't create couchbase handler\n", err);
return;
}
/* Assign the handlers to be called for the operation types */
lcb_set_bootstrap_callback(instance, bootstrap_callback);
lcb_set_get_callback(instance, generic_get_callback);
lcb_set_store_callback(instance, generic_store_callback);
然后我安排连接。
//We now schedule a connection to the server
err = lcb_connect(instance);
if (err != LCB_SUCCESS) {
die(instance, "Couldn't schedule connection\n", err);
lcb_destroy(instance);
}
lcb_set_cookie(instance, mEvbase);
我使用 libcouchbase 版本 2.0.17、libevent 核心版本 2.0.so.5.1.9 和 libevent extra 版本 2.0.so.5.1.9。使用上面的代码,我的实例无法连接到 couchbase。我收到以下警告:
event_pending: event has no event_base set.
event_add: event has no event_base set.
所以这里有两个问题:我无法使用上面的代码连接,我不知道从哪个方向开始接收事件。如果有人将我指向这个简单案例的链接或编码示例,它将解除对我的阻止。
【问题讨论】:
-
当项目被修改时,您将请求服务器通知。不幸的是,SDK 本身目前不支持此功能。但是,您可以查看 TAP 或 DCP 以满足您的需求。请澄清这是否不是您想要的。
-
感谢您的回答。你能给我一个 TAP 或 DCP 的链接吗,因为我不知道它是什么,Google 不会提供帮助。
-
TAP 和 DCP 是节点用来在彼此之间复制数据的内部协议。虽然这些是内部协议,但您仍然可以使用它们。目前,Java SDK 有一个 TAP API。 C SDK 目前没有 TAP 接口。见couchbase.com/autodocs/couchbase-java-client-1.1.4/com/…
-
检查
libcouchbase_libevent.so插件是否链接到 libcouchbase 使用的同一个 libevent。我不确定这些警告来自哪里,但它们可能直接来自您的代码或 libevent(我们在库核心中没有此类警告)。 -
此外,将
LCB_LOGLEVEL环境变量设置为5以获得更详细的日志记录并了解可能发生的情况。
标签: c events couchbase libevent