@@ -15,12 +15,21 @@ Definition
15
15
16
16
.. dbcommand:: cleanupOrphaned
17
17
18
- Deletes from a shard the :term:`orphaned documents <orphaned
19
- document>` whose shard key values fall into a single or a single
20
- contiguous range that do not belong to the shard. For example, if
21
- two contiguous ranges do not belong to the shard, the
22
- :dbcommand:`cleanupOrphaned` examines both ranges for orphaned
23
- documents.
18
+ .. versionchanged:: 4.4
19
+
20
+ Starting in MongoDB 4.4, :term:`chunk` migrations and orphaned
21
+ document cleanup are more resilient to failover. The cleanup process
22
+ automatically resumes in the event of a failover. You no longer need
23
+ to run the :dbcommand:`cleanupOrphaned` command to clean up orphaned
24
+ documents. Instead, use this command to wait for orphaned documents
25
+ in a chunk range from a shard key's
26
+ :doc:`minKey</reference/bson-types>` to its
27
+ :doc:`maxKey</reference/bson-types>` for a specified namespace to be
28
+ cleaned up from a majority of a shard's members.
29
+
30
+ In MongoDB 4.2 and earlier, :dbcommand:`cleanupOrphaned` initiated
31
+ the cleanup process for orphaned documents in a specified namespace
32
+ and shard key range.
24
33
25
34
To run, issue :dbcommand:`cleanupOrphaned` in the ``admin`` database
26
35
directly on the :binary:`~bin.mongod` instance that is the primary
@@ -38,9 +47,9 @@ Definition
38
47
39
48
db.runCommand( {
40
49
cleanupOrphaned: "<database>.<collection>",
41
- startingFromKey: <minimumShardKeyValue>,
42
- secondaryThrottle: <boolean>,
43
- writeConcern: <document>
50
+ startingFromKey: <minimumShardKeyValue>, // deprecated
51
+ secondaryThrottle: <boolean>, // deprecated
52
+ writeConcern: <document> // deprecated
44
53
} )
45
54
46
55
:dbcommand:`cleanupOrphaned` has the following fields:
@@ -60,148 +69,60 @@ Definition
60
69
61
70
- string
62
71
63
- - The namespace, i.e. both the database and the collection name, of the
64
- sharded collection for which to clean the orphaned data.
65
-
66
-
72
+ - The namespace, i.e. both the database and the collection name,
73
+ of the sharded collection for which to wait for cleanup of the
74
+ orphaned data.
75
+
67
76
68
77
* - ``startingFromKey``
69
78
70
79
- document
71
80
72
- - Optional. The :term:`shard key` value that determines the lower bound of the
73
- cleanup range. The default value is ``MinKey``.
74
-
75
- If the range that contains the specified ``startingFromKey`` value
76
- belongs to a chunk owned by the shard, :dbcommand:`cleanupOrphaned`
77
- continues to examine the next ranges until it finds a range not owned
78
- by the shard. See :ref:`cleanupOrphaned-determine-range` for details.
79
-
81
+ - Deprecated. Starting in MongoDB 4.4, the value of this field
82
+ is not used to determine the bounds of the cleanup range. The
83
+ :dbcommand:`cleanupOrphaned` command waits until
84
+ all orphaned documents in all ranges are cleaned up from the
85
+ shard before completing, regardless of the presence of or the
86
+ value of ``startingFromKey``.
87
+
88
+ .. note::
89
+
90
+ The :binary:`~bin.mongod` continues to validate that the
91
+ ``startingFromKey`` value matches the shard key pattern,
92
+ even though it is not used to determine the bounds of the
93
+ cleanup range.
80
94
81
95
82
96
* - ``secondaryThrottle``
83
97
84
98
- boolean
85
99
86
- - Optional. If ``true``, each delete operation must be replicated to another
87
- secondary before the cleanup operation proceeds further. If
88
- ``false``, do not wait for replication. Defaults to ``false``.
89
-
90
- Independent of the ``secondaryThrottle`` setting, after the final
91
- delete, :dbcommand:`cleanupOrphaned` waits for all deletes to
92
- replicate to a majority of replica set members before returning.
93
-
94
-
100
+ - Deprecated. Starting in MongoDB 4.4, this field has no effect.
95
101
96
102
* - ``writeConcern``
97
103
98
104
- document
99
105
100
- - Optional. A document that expresses the :doc:`write concern
101
- </reference/write-concern>` that the ``secondaryThrottle`` will use to
102
- wait for the secondaries when removing orphaned data.
103
-
104
- ``writeConcern`` requires ``secondaryThrottle: true``.
105
-
106
-
107
-
108
-
106
+ - Deprecated. Starting in MongoDB 4.4, this field has no effect.
107
+ Orphaned documents are always cleaned up from a majority of a
108
+ shard's members (``{ writeConcern: { w: "majority" } }``)
109
+ before the :dbcommand:`cleanupOrphaned` command returns a
110
+ response.
109
111
110
112
Behavior
111
113
--------
112
114
113
- Performance
114
- ~~~~~~~~~~~
115
-
116
- :dbcommand:`cleanupOrphaned` scans the documents in the shard to
117
- determine whether the documents belong to the shard. As such, running
118
- :dbcommand:`cleanupOrphaned` can impact performance; however,
119
- performance will depend on the number of orphaned documents in the
120
- range.
121
-
122
- To remove all orphaned documents in a shard, you can run the command in
123
- a loop (see :ref:`cleanupOrphaned-example-all-orphaned` for an
124
- example). If concerned about the performance impact of this operation,
125
- you may prefer to include a pause in-between iterations.
126
-
127
- Alternatively, to mitigate the impact of :dbcommand:`cleanupOrphaned`,
128
- you may prefer to run the command at off peak hours.
129
-
130
- It is also possible to tune the performance impact of
131
- :dbcommand:`cleanupOrphaned` with the parameters,
132
- :parameter:`rangeDeleterBatchSize` and
133
- :parameter:`rangeDeleterBatchDelayMS`. For example:
134
-
135
- - To limit the number of documents deleted per batch, you can set
136
- :parameter:`rangeDeleterBatchSize` to a small value such as ``32``.
137
-
138
- - To add an additional delay between batch deletions, you can set
139
- :parameter:`rangeDeleterBatchDelayMS` above the current default of
140
- ``20`` milliseconds.
141
-
142
- .. note::
143
-
144
- Changing :parameter:`rangeDeleterBatchSize` and
145
- :parameter:`rangeDeleterBatchDelayMS` affect all range deletion,
146
- including during normal :ref:`chunk
147
- migration <chunk-migration-procedure>`.
148
-
149
- If you are temporarily modifying one or both of these parameters to
150
- run :dbcommand:`cleanupOrphaned`, return to their previous values
151
- after you run :dbcommand:`cleanupOrphaned`.
152
-
153
115
.. _cleanupOrphaned-determine-range:
154
116
155
117
Determine Range
156
118
~~~~~~~~~~~~~~~
157
119
158
- The :dbcommand:`cleanupOrphaned` command uses the ``startingFromKey``
159
- value, if specified, to determine the start of the range to examine for
160
- orphaned document:
161
-
162
- - If the ``startingFromKey`` value falls into a range for a chunk not
163
- owned by the shard, :dbcommand:`cleanupOrphaned` begins examining at
164
- the start of this range, which may not necessarily be the
165
- ``startingFromKey``.
166
-
167
- - If the ``startingFromKey`` value falls into a range for a chunk owned
168
- by the shard, :dbcommand:`cleanupOrphaned` moves onto the next range
169
- until it finds a range for a chunk not owned by the shard.
170
-
171
- The :dbcommand:`cleanupOrphaned` deletes orphaned documents from the
172
- start of the determined range and ends at the start of the chunk range
173
- that belongs to the shard.
174
-
175
- Consider the following key space with documents distributed across
176
- ``Shard A`` and ``Shard B``.
177
-
178
- .. include:: /images/sharding-cleanup-orphaned.rst
179
-
180
- ``Shard A`` owns:
181
-
182
- - ``Chunk 1`` with the range ``{ x: minKey } --> { x: -75 }``,
183
-
184
- - ``Chunk 2`` with the range ``{ x: -75 } --> { x: 25 }``, and
185
-
186
- - ``Chunk 4`` with the range ``{ x: 175 } --> { x: 200 }``.
187
-
188
- ``Shard B`` owns:
189
-
190
- - ``Chunk 3`` with the range ``{ x: 25 } --> { x: 175 }`` and
191
-
192
- - ``Chunk 5`` with the range ``{ x: 200 } --> { x: maxKey }``.
193
-
194
- If on ``Shard A``, the :dbcommand:`cleanupOrphaned` command runs with
195
- ``startingFromKey: { x: -70 }`` or any other value belonging to range for
196
- ``Chunk 1`` or ``Chunk 2``, the :dbcommand:`cleanupOrphaned` command examines
197
- the ``Chunk 3`` range of ``{ x: 25 } --> { x: 175 }`` to delete
198
- orphaned data.
199
-
200
- If on ``Shard B``, the :dbcommand:`cleanupOrphaned` command runs with
201
- the ``startingFromKey: { x: -70 }`` or any other value belonging to range
202
- for ``Chunk 1``, the :dbcommand:`cleanupOrphaned` command examines the
203
- combined contiguous range for ``Chunk 1`` and ``Chunk 2``, namely ``{
204
- x: minKey } --> { x: 25 }`` to delete orphaned data.
120
+ Starting in MongoDB 4.4, the value of this field is not used to
121
+ determine the bounds of the cleanup range. The
122
+ :dbcommand:`cleanupOrphaned` command waits until all orphaned documents
123
+ in all ranges in the namespace are cleaned up from the shard before
124
+ completing, regardless of the presence of or value of
125
+ ``startingFromKey``.
205
126
206
127
Required Access
207
128
---------------
@@ -224,101 +145,13 @@ a subset of the following fields:
224
145
225
146
Equal to ``1`` on success.
226
147
227
- A value of ``1`` indicates that :dbcommand:`cleanupOrphaned` scanned
228
- the specified shard key range, deleted any orphaned documents
229
- found in that range, and confirmed that all deletes replicated to a
230
- majority of the members of that shard's replica set. If confirmation
231
- does not arrive within 1 hour, :dbcommand:`cleanupOrphaned`
232
- times out.
233
-
234
- A value of ``0`` could indicate either of two cases:
235
-
236
- - :dbcommand:`cleanupOrphaned` found orphaned documents on the
237
- shard but could not delete them.
238
-
239
- - :dbcommand:`cleanupOrphaned` found and deleted orphaned
240
- documents, but could not confirm replication before the 1
241
- hour timeout. In this case, replication does occur but only
242
- after :dbcommand:`cleanupOrphaned` returns.
243
-
244
- .. data:: cleanupOrphaned.stoppedAtKey
245
-
246
- The upper bound of the cleanup range of shard keys. If present, the
247
- value corresponds to the lower bound of the next chunk on the shard.
248
- The absence of the field signifies that the cleanup range was the
249
- uppermost range for the shard.
250
-
251
- .. _cleanuporphaned-examples:
252
-
253
- Examples
254
- --------
255
-
256
- The following examples run the :dbcommand:`cleanupOrphaned` command
257
- directly on the primary of the shard.
258
-
259
- Remove Orphaned Documents for a Specific Range
260
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261
-
262
- For a sharded collection ``info`` in the ``test`` database, a shard
263
- owns a single chunk with the range: ``{ x: MinKey } --> { x: 10 }``.
264
-
265
- The shard also contains documents whose shard keys values fall in a
266
- range for a chunk *not* owned by the shard: ``{ x: 10 } --> { x: MaxKey
267
- }``.
268
-
269
- To remove orphaned documents within the ``{ x: 10 } => { x: MaxKey }``
270
- range, you can specify a ``startingFromKey`` with a value that falls into
271
- this range, as in the following example:
272
-
273
- .. code-block:: javascript
274
-
275
- db.adminCommand( {
276
- "cleanupOrphaned": "test.info",
277
- "startingFromKey": { x: 10 },
278
- "secondaryThrottle": true
279
- } )
280
-
281
- Or you can specify a ``startingFromKey`` with a value that falls into the
282
- previous range, as in the following:
283
-
284
- .. code-block:: javascript
285
-
286
- db.adminCommand( {
287
- "cleanupOrphaned": "test.info",
288
- "startingFromKey": { x: 2 },
289
- "secondaryThrottle": true
290
- } )
291
-
292
- Since ``{ x: 2 }`` falls into a range that belongs to a chunk owned by
293
- the shard, :dbcommand:`cleanupOrphaned` examines the next range to find
294
- a range not owned by the shard, in this case ``{ x: 10 } => { x: MaxKey
295
- }``.
296
-
297
- .. _cleanupOrphaned-example-all-orphaned:
298
-
299
- Remove All Orphaned Documents from a Shard
300
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
301
-
302
- :dbcommand:`cleanupOrphaned` examines documents from a single
303
- contiguous range of shard keys. To remove all orphaned documents from
304
- the shard, you can run :dbcommand:`cleanupOrphaned` in a loop, using
305
- the returned ``stoppedAtKey`` as the next ``startingFromKey``, as in
306
- the following:
307
-
308
- .. code-block:: javascript
309
-
310
- var nextKey = { };
311
- var result;
312
-
313
- while ( nextKey != null ) {
314
- result = db.adminCommand( { cleanupOrphaned: "test.user", startingFromKey: nextKey } );
315
-
316
- if (result.ok != 1)
317
- print("Unable to complete at this time: failure or timeout.")
148
+ A value of ``1`` indicates that either:
318
149
319
- printjson(result);
150
+ - No orphaned documents remain in the ``cleanupOrphaned`` namespace
151
+ on the shard, or
152
+ - The collection referenced in the ``cleanupOrphaned`` namespace is
153
+ not sharded.
320
154
321
- nextKey = result.stoppedAtKey;
322
- }
155
+ A value of ``0`` indicates that an error has occurred.
323
156
324
157
.. admin-only
0 commit comments