100000个客户端同时尝试使用flatiron/cradle更新couchdb文档。如何处理所有的更新冲突?
解决方案:
var cradle = require('cradle');
var c = new(cradle.Connection)({cache: true} ) ;
var db = c.database('ml');
function asyncLoop(iterations, func, callback) {
var index = 0;
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (index < iterations) {
index++;
func(loop);
} else {
done = true;
callback();
}
},
iteration: function() {
return index - 1;
},
break: function() {
done = true;
callback();
}
};
loop.next();
return loop;
}
//----------------------------------------
function revloop(game,gamenumber, callback){
db.get('testdocument', function (err, doc) {
if (err) {
// Handle error
} else {
// Handle success
db.save('testdocument', doc._rev ,{
game:game,
gamenumber:gamenumber
}, function (err, res) {
if (err) {
console.log(err);
if(err.reason=='Document update conflict.')
{callback(0);}
else{console.log('other errror');callback(1)}
} else {
console.log('new game created');
callback(1);
// Handle success
}// success save opengames end bracket
});
}
});
}
//-----------------------------------
asyncLoop(100 ,function(loop){
revloop('b-against-c',3,function(callback){
if(callback == 0){
loop.next();
}
else{loop.break();}
});
} , function(){ console.log('loop finished')});
这个解决方案使用了Ivo Wetzel的异步循环。
下面是一个测试,有9个更新冲突是由向_rev: 添加字符串引起的var cradle = require('cradle');
var c = new(cradle.Connection)({cache: true} ) ;
var db = c.database('ml');
function asyncLoop(iterations, func, callback) {
var index = 0;
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (index < iterations) {
index++;
func(loop);
} else {
done = true;
callback();
}
},
iteration: function() {
return index - 1;
},
break: function() {
done = true;
callback();
}
};
loop.next();
return loop;
}
//----------------------------------------
var revchanger= 0;
function revloop(game,gamenumber, callback){
revchanger ++;
var addstuff = revchanger== 10? '':'123';// this will cause nine update conflicts
db.get('testdocument', function (err, doc) {
if (err) {
// Handle error
} else {
// Handle success
db.save('testdocument', doc._rev+addstuff ,{
game:game,
gamenumber:gamenumber
}, function (err, res) {
if (err) {
console.log(err);
if(err.reason=='Document update conflict.')
{callback(0);}
else{console.log('other errror');callback(1)}
} else {
console.log('new game created');
callback(1);
// Handle success
}// success save opengames end bracket
});
}
});
}
//-----------------------------------
asyncLoop(100 ,function(loop){
revloop('a-against-b',2,function(callback){
if(callback == 0){
loop.next();
}
else{loop.break();}
});
} , function(){ console.log('loop finished')});
创建数据库:createml.js:
var cradle = require('cradle');
var c = new(cradle.Connection) ;
var db = c.database('ml');
db.create();
创建testdocumentcreatetestdocument.js:
var cradle = require('cradle');
var db= new(cradle.Connection)().database('ml');
db.save('testdocument' , {
game: 'gamename1',
gamenumber:1
}, function (err, res) {
if (err) {
// Handle error
} else {
// Handle success
}
});
在浏览器中查看:
http://localhost:5984/ml/testdocument
我建议将couchdb与redis一起使用,因为redis是原子的。