On this page
cleanupOrphaned
在本页面
Definition
cleanupOrphaned
- 2.6 版的新功能。
从分片中删除其分片键值落入不属于该分片的单个或单个连续范围内的orphaned documents。例如,如果两个连续范围不属于该分片,则cleanupOrphaned将检查两个范围中是否有孤立文档。
若要运行,请直接在admin
数据库中的cleanupOrphaned上(作为该碎片的主要副本集成员)在admin
数据库中发出。您无需在运行cleanupOrphaned之前禁用平衡器。
Note
不要在mongos实例上运行cleanupOrphaned。
cleanupOrphaned具有以下语法:
db.runCommand( {
cleanupOrphaned: "<database>.<collection>",
startingFromKey: <minimumShardKeyValue>,
secondaryThrottle: <boolean>,
writeConcern: <document>
} )
cleanupOrphaned具有以下字段:
Field | Type | Description |
---|---|---|
cleanupOrphaned |
string | 用于清理孤立数据的分片集合的名称空间,即数据库和集合名称。 |
startingFromKey |
document | 可选的。 shard key值确定清理范围的下限。默认值为MinKey 。 |
如果包含指定startingFromKey
值的范围属于该分片拥有的块,则cleanupOrphanedcontinue 检查下一个范围,直到找到该分片不拥有的范围。有关详情,请参见Determine Range。
| secondaryThrottle
|布尔值|可选。如果true
,则必须先将每个删除操作复制到另一个辅助操作,然后再进一步执行清除操作。如果是false
,请不要 await 复制。默认为false
。
|与secondaryThrottle
设置无关,在最终删除之后,cleanupOrphanedawait 所有删除复制到大多数副本集成员,然后再返回。
| writeConcern
|文档|可选。表示write concern的文档,secondaryThrottle
会在删除孤立数据时使用它们来 await 次级。writeConcern
要求secondaryThrottle: true
。|
Behavior
Performance
cleanupOrphaned扫描分片中的文档,以确定文档是否属于该分片。因此,运行cleanupOrphaned会影响性能;但是,性能将取决于该范围内孤立文档的数量。
要删除分片中的所有孤立文档,您可以循环运行命令(有关示例,请参见从分片中删除所有孤立文档)。如果担心此操作对性能的影响,您可能希望在迭代之间包含一个暂停。
或者,为减轻cleanupOrphaned的影响,您可能希望在非高峰时间运行该命令。
Determine Range
cleanupOrphaned命令使用startingFromKey
值(如果已指定)来确定要检查孤立文档的范围的开始:
如果
startingFromKey
值落入该碎片不拥有的块的范围内,则cleanupOrphaned在该范围的开始处开始检查,该范围不一定是startingFromKey
。如果
startingFromKey
值落入该碎片拥有的块的范围内,则cleanupOrphaned移动到下一个范围,直到找到该碎片不拥有的块的范围。
cleanupOrphaned从确定范围的开头删除孤立文档,并在属于该分片的块范围的开头结束。
考虑以下关键空间,其中文档分布在Shard A
和Shard B
之间。
Shard A
拥有:
Chunk 1
,范围为{ x: minKey } --> { x: -75 }
,Chunk 2
,范围为{ x: -75 } --> { x: 25 }
,以及Chunk 4
,范围为{ x: 175 } --> { x: 200 }
。
Shard B
拥有:
Chunk 3
,范围为{ x: 25 } --> { x: 175 }
,并且Chunk 5
,范围为{ x: 200 } --> { x: maxKey }
。
如果在Shard A
上,cleanupOrphaned命令以startingFromKey: { x: -70 }
或属于Chunk 1
或Chunk 2
范围的任何其他值运行,则cleanupOrphaned命令检查{ x: 25 } --> { x: 175 }
的Chunk 3
范围以删除孤立数据。
如果在Shard B
上以startingFromKey: { x: -70 }
或属于Chunk 1
的范围的任何其他值运行cleanupOrphaned命令,则cleanupOrphaned命令检查Chunk 1
和Chunk 2
的组合连续范围,即{ x: minKey } --> { x: 25 }
以删除孤立数据。
Required Access
在以authorization运行的系统上,您必须具有clusterAdmin特权才能运行cleanupOrphaned。
Output
Return Document
每个cleanupOrphaned命令都会返回一个文档,其中包含以下字段的子集:
cleanupOrphaned.
ok
- 成功等于
1
。
- 成功等于
值1
表示cleanupOrphaned扫描了指定的分片键范围,删除了该范围内找到的所有孤立文档,并确认所有删除均已复制到该分片副本集的大多数成员中。如果确认未在 1 小时内到达,则cleanupOrphaned超时。
值0
可能表示以下两种情况之一:
cleanupOrphaned在分片上找到了孤立的文档,但无法删除它们。
cleanupOrphaned找到并删除了孤立的文档,但是在 1 小时超时之前无法确认复制。在这种情况下,确实会发生复制,但是仅在返回cleanupOrphaned之后。
cleanupOrphaned.
stoppedAtKey
- 分片键清除范围的上限。如果存在,则该值对应于分片上下一个块的下限。没有该字段表示清除范围是该碎片的最高范围。
Examples
以下示例直接在分片的主数据库上运行cleanupOrphaned命令。
删除特定范围内的孤立文档
对于test
数据库中的分片集合info
,分片拥有单个块,其范围为{ x: MinKey } --> { x: 10 }
。
分片还包含文档,其分片键值位于该分片不属于的块{ x: 10 } --> { x: MaxKey }
的范围内。
要删除{ x: 10 } => { x: MaxKey }
范围内的孤立文档,您可以指定一个startingFromKey
,其值在该范围内,如以下示例所示:
db.adminCommand( {
"cleanupOrphaned": "test.info",
"startingFromKey": { x: 10 },
"secondaryThrottle": true
} )
或者,您可以指定一个startingFromKey
且其值属于先前的范围,如下所示:
db.adminCommand( {
"cleanupOrphaned": "test.info",
"startingFromKey": { x: 2 },
"secondaryThrottle": true
} )
由于{ x: 2 }
属于该碎片拥有的块的范围,因此cleanupOrphaned检查下一个范围以查找该碎片不拥有的范围,在本例中为{ x: 10 } => { x: MaxKey }
。
从分片中删除所有孤立文档
cleanupOrphaned从单个连续的分片键范围检查文档。要从分片中删除所有孤立文档,您可以使用返回的stoppedAtKey
作为下一个startingFromKey
循环运行cleanupOrphaned,如下所示:
var nextKey = { };
var result;
while ( nextKey != null ) {
result = db.adminCommand( { cleanupOrphaned: "test.user", startingFromKey: nextKey } );
if (result.ok != 1)
print("Unable to complete at this time: failure or timeout.")
printjson(result);
nextKey = result.stoppedAtKey;
}