cleanupOrphaned

在本页面

Definition

从分片中删除其分片键值落入不属于该分片的单个或单个连续范围内的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值(如果已指定)来确定要检查孤立文档的范围的开始:

cleanupOrphaned从确定范围的开头删除孤立文档,并在属于该分片的块范围的开头结束。

考虑以下关键空间,其中文档分布在Shard AShard B之间。

Shard A拥有:

Shard B拥有:

如果在Shard A上,cleanupOrphaned命令以startingFromKey: { x: -70 }或属于Chunk 1Chunk 2范围的任何其他值运行,则cleanupOrphaned命令检查{ x: 25 } --> { x: 175 }Chunk 3范围以删除孤立数据。

如果在Shard B上以startingFromKey: { x: -70 }或属于Chunk 1的范围的任何其他值运行cleanupOrphaned命令,则cleanupOrphaned命令检查Chunk 1Chunk 2的组合连续范围,即{ x: minKey } --> { x: 25 }以删除孤立数据。

Required Access

在以authorization运行的系统上,您必须具有clusterAdmin特权才能运行cleanupOrphaned

Output

Return Document

每个cleanupOrphaned命令都会返回一个文档,其中包含以下字段的子集:

1表示cleanupOrphaned扫描了指定的分片键范围,删除了该范围内找到的所有孤立文档,并确认所有删除均已复制到该分片副本集的大多数成员中。如果确认未在 1 小时内到达,则cleanupOrphaned超时。

0可能表示以下两种情况之一:

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;
}
首页