1000000个客户端尝试同时使用flatiron/cradle更新couchdb文档。 如何处理所有更新冲突?
答案 0 :(得分:0)
这里是解决方案:
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的精彩asynchronous Loop。
此处测试,通过向_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')});
创建数据库'ml': createml.js:
var cradle = require('cradle');
var c = new(cradle.Connection) ;
var db = c.database('ml');
db.create();
创建一个testdocument: createtestdocument.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
答案 1 :(得分:0)
我建议将couchdb与redis一起使用,因为redis是原子的。