Skip to content

Commit d180237

Browse files
jwilliams-mongoandf-mongodb
authored andcommitted
DOCSP-7666: Investigate changes in PM-812: Resumable RangeDeleter
1 parent d3a255f commit d180237

File tree

9 files changed

+187
-227
lines changed

9 files changed

+187
-227
lines changed

source/core/sharding-balancer-administration.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ This behavior also affects the :dbcommand:`moveChunk` command, and
231231
migration scripts that use the :dbcommand:`moveChunk` command may
232232
proceed more quickly.
233233

234-
In some cases, the delete phases may persist longer. If multiple delete
235-
phases are queued but not yet complete, a crash of the replica set's
236-
primary can orphan data from multiple migrations.
234+
In some cases, the delete phases may persist longer. Starting in MongoDB
235+
4.4, chunk migrations are enhanced to be more resilient in the event of
236+
a failover during the delete phase. Orphaned documents are cleaned up
237+
even if a replica set's primary crashes or restarts during this phase.
237238

238239
The ``_waitForDelete``, available as a setting for the balancer as well
239240
as the :dbcommand:`moveChunk` command, can alter the behavior so that
@@ -300,6 +301,22 @@ Maximum Number of Documents Per Chunk to Migrate
300301

301302
.. include:: /includes/limits-sharding-maximum-documents-chunk.rst
302303

304+
.. _range-deletion-performance-tuning:
305+
306+
Range Deletion Performance Tuning
307+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308+
309+
You can tune the performance impact of range deletions
310+
with :parameter:`rangeDeleterBatchSize` and
311+
:parameter:`rangeDeleterBatchDelayMS` parameters. For example:
312+
313+
- To limit the number of documents deleted per batch, you can set
314+
:parameter:`rangeDeleterBatchSize` to a small value such as ``32``.
315+
316+
- To add an additional delay between batch deletions, you can set
317+
:parameter:`rangeDeleterBatchDelayMS` above the current default of
318+
``20`` milliseconds.
319+
303320
.. _sharding-shard-size:
304321

305322
Shard Size

source/includes/extracts-server-status-projection-base.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ content: |
7979
- :serverstatus:`metrics.queryExecutor.collectionScans.nonTailable`
8080
- :serverstatus:`metrics.queryExecutor.collectionScans.total`
8181
82+
- Added new :serverstatus:`range deletion metrics
83+
<shardingStatistics.rangeDeleterTasks>` in
84+
:serverstatus:`shardingStatistics`.
85+
8286
- Starting in MongoDB 4.2.2, {{operationName}}:
8387
8488
- Added new transaction metrics in :serverstatus:`transactions` for

source/includes/steps-4.4-upgrade-sharded-cluster.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ content: |
303303
the sharded cluster, chunk migrations, splits, and merges
304304
can fail with ``ConflictingOperationInProgress``.
305305
306+
Any :term:`orphaned documents <orphaned document>` that exist on your
307+
shards will be cleaned up when you set the
308+
:dbcommand:`setFeatureCompatibilityVersion` to |newversion|. The
309+
cleanup process:
310+
311+
- Does not block the upgrade from completing, and
312+
- Is rate limited. To mitigate the potential effect on performance
313+
during orphaned document cleanup, see
314+
:ref:`range-deletion-performance-tuning`.
315+
306316
.. topic:: Additional Consideration
307317
308318
.. include:: /includes/fact-mongos-fcv.rst

source/reference/command/cleanupOrphaned.txt

Lines changed: 53 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ Definition
1515

1616
.. dbcommand:: cleanupOrphaned
1717

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.
2433

2534
To run, issue :dbcommand:`cleanupOrphaned` in the ``admin`` database
2635
directly on the :binary:`~bin.mongod` instance that is the primary
@@ -38,9 +47,9 @@ Definition
3847

3948
db.runCommand( {
4049
cleanupOrphaned: "<database>.<collection>",
41-
startingFromKey: <minimumShardKeyValue>,
42-
secondaryThrottle: <boolean>,
43-
writeConcern: <document>
50+
startingFromKey: <minimumShardKeyValue>, // deprecated
51+
secondaryThrottle: <boolean>, // deprecated
52+
writeConcern: <document> // deprecated
4453
} )
4554

4655
:dbcommand:`cleanupOrphaned` has the following fields:
@@ -60,148 +69,60 @@ Definition
6069

6170
- string
6271

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+
6776

6877
* - ``startingFromKey``
6978

7079
- document
7180

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.
8094

8195

8296
* - ``secondaryThrottle``
8397

8498
- boolean
8599

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.
95101

96102
* - ``writeConcern``
97103

98104
- document
99105

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.
109111

110112
Behavior
111113
--------
112114

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-
153115
.. _cleanupOrphaned-determine-range:
154116

155117
Determine Range
156118
~~~~~~~~~~~~~~~
157119

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``.
205126

206127
Required Access
207128
---------------
@@ -224,101 +145,13 @@ a subset of the following fields:
224145

225146
Equal to ``1`` on success.
226147

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:
318149

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.
320154

321-
nextKey = result.stoppedAtKey;
322-
}
155+
A value of ``0`` indicates that an error has occurred.
323156

324157
.. admin-only

0 commit comments

Comments
 (0)