mongodb 系列 ~ chunk失败下的孤儿文档

一 描述
orphaned document是指在分片集群模式环境下,一些同时存在于不同shard上的同一数据值的
document.
二 产生原因
:在balancer执行moveChunk迁移的过程中,mongod实例异常切换甚至宕机,导致迁移过程失败或者部分完成,就会残留同一数据值的document.而为了加速chunk 迁移的速度,因此delete phase不会立刻执行,而是放入一个队列,异步执行,此时如果crash,就可能产生孤儿文档,
三 现象
孤儿文档在mongodb集群的表象就是count数据不准,因为它会把孤儿文档也算进去,但是你find是始终只有一条记录,孤儿文档是看不到的.
通过mongos用db.collections.find().explain(true)中的chunkSkips可以知道某个shard的孤立文档数
四 解决方法
1.使用cleanupOrphaned解决孤儿文档
cleanupOrphaned要在shard的primary上执行
db.runCommand( {
cleanupOrphaned: "<database>.<collection>",
startingFromKey: <minimumShardKeyValue>,
secondaryThrottle: <boolean>,
writeConcern: <document>
} )
示例:
db.adminCommand( {
"cleanupOrphaned": "test.info",
"startingFromKey": { x: 10 },
"secondaryThrottle": true
} )
由于孤立文档删除以后,cleanupOrphaned命令即执行结束.这里我们使用循环删除,来删除所有孤立文档.
use admin
var nextKey={ };
var result;
while ( nextKey != null ) {
  result=db.runCommand( { cleanupOrphaned: "mydb.mycol1", startingFromKey: nextKey } );
  if (result.ok != 1)
     print("Unable to complete at this time: failure or timeout.")
  printjson(result);
  nextKey=result.stoppedAtKey;
}

原文地址:https://www.cnblogs.com/danhuangpai/p/14431647.html