我正在使用 Couchbase 2.0、node.js 0.8.19、couchbase 模块 0.0.11 和 libcouchbase 2.0.3 在 Mac OS X Mountain Lion 10.8.2 上对项目进行原型设计。
如果 Couchbase 存储桶为空(这两个文档不存在),则下面的代码会使用以下代码使节点进程崩溃。我使用调试构建节点并使用 gdb 进行回溯。
node_g(96149,0x7fff75619180) malloc: *** error for object 0x10300fb7f: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted.
0x00007fff8bbf7212 in __pthread_kill ()
(gdb) backtrace
#0 0x00007fff8bbf7212 in __pthread_kill ()
#1 0x00007fff8cd6eaf4 in pthread_kill ()
#2 0x00007fff8cdb2dce in abort ()
#3 0x00007fff8cd86959 in free ()
#4 0x0000000101e91c33 in lcb_server_purge_implicit_responses ()
#5 0x0000000101e89f09 in lcb_server_event_handler ()
#6 0x0000000101adebab in maybe_callout [inlined] () at :34
#7 0x0000000101adebab in async_cb (handle=<value temporarily unavailable, due to optimizations>, status=0) at ../io/common.c:68
#8 0x00000001000442ea in uv__async_io (loop=<value temporarily unavailable, due to optimizations>, handle=0x1c042a000000000, events=1060378680) at ../deps/uv/src/unix/async.c:117
#9 0x0000000100049c24 in ev_invoke_pending (loop=0x10082bd68) at ../deps/uv/src/unix/ev/ev.c:2145
#10 0x00000001000445e8 in uv__run (loop=0x10082b420) at ../deps/uv/src/unix/core.c:248
#11 0x0000000100044517 in uv_run (loop=0x10082b420) at ../deps/uv/src/unix/core.c:265
#12 0x0000000100008d9b in node::Start (argc=<value temporarily unavailable, due to optimizations>, argv=0x7fff5fbffb98) at ../src/node.cc:2974
#13 0x0000000100000cb4 in start ()
如果这两个文档已经存在,则此方法有效。仅当文档不存在时才会崩溃。如果我不使用 multi-get(例如视图)来检查文档是否存在 - 它可以工作。如果您问我为什么要明确检查是否存在并且不依赖 CAS - 大多数文档已经存在,所以无论如何我都必须在更新之前提取它们。
相同的代码在具有相同版本的 node.js 和库的(亚马逊托管的)CentOS 6 映像上运行良好。
我做错了什么还是Mac OS X有问题?这是一个简单的例子来说明问题。完整代码将现有数据与新文档合并,并进行 CAS 检查,此处省略。
我找不到在哪里报告 Couchbase 的错误。JIRA不开放,论坛有点蹩脚。
var async = require("async");
var couchBase = require("couchbase");
var json = {
"id1": {
"id": "id1",
"name": "Name 1"
},
"id2": {
"id": "id2",
"name": "Name 2"
}
};
var config = {
"host": "localhost",
"port": 8091,
"username": "Administrator",
"password": "password",
"bucket": "requests"
};
couchBase.connect(config, function(err, bucket) {
if (err) {
console.log("Unable to connect to Couchbase bucket [" + config.bucket + "]", err);
process.exit(1);
}
console.log("Connected to Couchbase");
var ids = [];
var jsonDocs = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
var jsonDoc = json[key];
var id = jsonDoc.id;
jsonDocs.push(jsonDoc);
ids.push(id);
}
}
bucket.get(ids, null, function(err, docs, metas) {
if (err) {
for (var j = 0; j < err.length; j++) {
if (err[j].code != 13) {
console.log({ err: err }, "Unable to get existing entries using multiple get. Error in element [" + j + "]");
process.exit(1);
}
}
}
console.log("Checked all docs for existance");
async.map(jsonDocs, function(doc, callback) {
bucket.set(doc.id, doc, {}, function(err) {
callback(err);
});
}, function(err, results) {
if (err) {
console.log("Unable to save all entries", err);
process.exit(1);
}
console.log("Saved all entries");
process.exit(0);
});
});
});