Skip to content

Commit 60c0dc1

Browse files
author
Dave Cuthbert
authored
DOCSP-24455 BACKPORT (#2666)
* DOCSP-24455 BACKPORT * Version refernces
1 parent 7594a0a commit 60c0dc1

File tree

4 files changed

+110
-67
lines changed

4 files changed

+110
-67
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
``compact`` blocks these operations:
2+
3+
- :method:`db.collection.drop()`
4+
- :method:`db.collection.createIndex()`
5+
- :method:`db.collection.createIndexes()`
6+
- :method:`db.collection.dropIndex()`
7+
- :method:`db.collection.dropIndexes()`
8+
- :dbcommand:`collMod`
9+

source/reference/command/compact.txt

Lines changed: 97 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ The command takes the following fields:
6363
- flag
6464
- .. versionchanged:: 4.4
6565

66+
.. _compact-force-option:
67+
6668
Optional. Starting in v4.4, if specified, forces
6769
:dbcommand:`compact` to run on the :term:`primary` in
6870
a :term:`replica set`. Before v4.4, this boolean field
@@ -95,30 +97,6 @@ action on the target collection. The :authrole:`dbAdmin` role provides
9597
the required privileges for running :dbcommand:`compact` against
9698
non-system collections.
9799

98-
For :ref:`system collections <metadata-system-collections>`, create a
99-
custom role that grants the :authaction:`compact` action on the system
100-
collection. You can then grant that role to a new or existing user and
101-
authenticate as that user to perform the :dbcommand:`compact` command.
102-
For example, the following operations create a custom role that grants
103-
the :authaction:`compact` action against specified database and
104-
collection:
105-
106-
.. code-block:: javascript
107-
108-
use admin
109-
db.createRole(
110-
{
111-
role: "myCustomCompactRole",
112-
privileges: [
113-
{
114-
resource: { "db" : "<database>" , "collection" : "<collection>" },
115-
actions: [ "compact" ]
116-
}
117-
],
118-
roles: []
119-
}
120-
)
121-
122100
For more information on configuring the ``resource`` document, see
123101
:ref:`resource-document`.
124102

@@ -155,28 +133,34 @@ Behavior
155133
Blocking
156134
~~~~~~~~
157135

158-
.. versionchanged:: 4.4
136+
Blocking behavior is version specific.
137+
138+
.. list-table::
139+
140+
* - Version
141+
- Blocking Behavior
159142

160-
Starting in v4.4, on :ref:`WiredTiger <storage-wiredtiger>`,
161-
:dbcommand:`compact` only blocks the following metadata operations:
143+
* - Pre 4.4
144+
- ``compact`` blocks all read and write activity.
162145

163-
- :method:`db.collection.drop`
164-
- :method:`db.collection.createIndex` and
165-
:method:`db.collection.createIndexes`
166-
- :method:`db.collection.dropIndex` and
167-
:method:`db.collection.dropIndexes`
146+
* - 4.4
147+
- - .. include:: /includes/reference/compact-methods-list.rst
148+
- All other operations are permitted.
168149

169-
:dbcommand:`compact` does not block :ref:`crud` for the database it is
170-
currently operating on.
150+
* - Post 4.4.17, 5.0.12, 6.0.2
151+
- - .. include:: /includes/reference/compact-methods-list.rst
152+
- All other operations are permitted.
153+
- The :ref:`locking order <faq-concurrency-locking>` changes.
171154

172-
Before v4.4, :dbcommand:`compact` blocked all operations for the
173-
database it was compacting, including :ref:`crud`, and was therefore
174-
recommended for use only during scheduled maintenance periods. Starting in
175-
v4.4, the :dbcommand:`compact` command is appropriate for use at any time.
155+
To run ``compact`` in a replica set, see
156+
:ref:`compact-cmd-replica-sets` for additional considerations.
176157

177-
You may view the intermediate progress either by viewing the
178-
:binary:`~bin.mongod` log file or by running the
179-
:method:`db.currentOp()` in another shell instance.
158+
Monitoring Progress
159+
~~~~~~~~~~~~~~~~~~~
160+
161+
To check the ``compact`` operation's progress, monitor the
162+
:binary:`~bin.mongod` log file or run :method:`db.currentOp()`
163+
from another shell instance.
180164

181165
Operation Termination
182166
~~~~~~~~~~~~~~~~~~~~~
@@ -203,45 +187,91 @@ Disk Space
203187
To see how the storage space changes for the collection, run the
204188
:dbcommand:`collStats` command before and after compaction.
205189

206-
On :ref:`WiredTiger <storage-wiredtiger>`, :dbcommand:`compact` attempts to
207-
reduce the required storage space for data and indexes in a collection, releasing
208-
unneeded disk space to the operating system. The effectiveness of this operation
209-
is workload dependent and no disk space may be recovered. This command is useful
210-
if you have removed a large amount of data from the collection, and do not plan
211-
to replace it.
190+
On :ref:`WiredTiger <storage-wiredtiger>`, :dbcommand:`compact` attempts
191+
to reduce the required storage space for data and indexes in a
192+
collection, releasing unneeded disk space to the operating system. The
193+
effectiveness of this operation is workload dependent and no disk space
194+
may be recovered. This command is useful if you have removed a large
195+
amount of data from the collection, and do not plan to replace it.
212196

213-
:dbcommand:`compact` may require additional disk space to run on WiredTiger databases.
197+
:dbcommand:`compact` may require additional disk space to run on
198+
WiredTiger databases.
214199

215200
.. _compact-cmd-replica-sets:
216201

217202
Replica Sets
218203
~~~~~~~~~~~~
219204

220-
:dbcommand:`compact` commands do not replicate to secondaries in a
221-
:term:`replica set`.
205+
You can use ``compact`` on collections and indexes that are stored in a
206+
replica set, however there are some important considerations:
207+
208+
- The primary node does not replicate the ``compact`` command to the
209+
secondaries.
210+
- The ``compact`` command blocks writes while it runs.
211+
- You should run ``compact`` on secondary nodes whenever possible. If
212+
you cannot run ``compact`` on secondaries, see the :ref:`force
213+
<compact-force-option>` option.
214+
- Starting in MongoDB 6.0.2 (and 5.0.12, and 4.4.17):
215+
216+
- A secondary node can replicate while ``compact`` is running.
217+
- Reads are permitted.
218+
219+
To run ``compact`` on a cluster
220+
221+
.. procedure::
222+
:style: connected
223+
224+
.. step:: Compact the secondary nodes.
225+
226+
Run ``compact`` on one of the secondary nodes. When ``compact``
227+
finishes, repeat the operation on each of the remaining
228+
secondaries in turn.
229+
230+
.. step:: Reassign the primary node.
231+
232+
To step down the current primary and trigger an election, use the
233+
:method:`rs.stepDown()` method. To nominate a particular secondary
234+
node, :ref:`adjust the member priority
235+
<replica-set-adjust-priority>`.
236+
237+
.. step:: Compact the old primary.
238+
239+
After stepping down, the old primary node becomes a secondary
240+
node. Run ``compact`` on the old primary node.
241+
242+
Version Specific Considerations for Secondary Nodes
243+
```````````````````````````````````````````````````
244+
245+
Blocking behavior on secondary nodes is version specific.
246+
247+
.. list-table::
248+
249+
* - Version
250+
- Blocking Behavior
222251

223-
- Compact each member separately.
252+
* - Pre 4.4
253+
- - ``compact`` blocks all read and write activity.
254+
- No replication possible.
255+
- Secondary node is in :replstate:`RECOVERING` state.
224256

225-
- Ideally run :dbcommand:`compact` on a secondary. See option
226-
``force`` above for information regarding compacting the primary.
257+
* - 4.4
258+
- - ``compact`` blocks all write activity.
259+
- No replication possible.
260+
- Reads not permitted.
227261

228-
- Starting in MongoDB 5.0.3 (and 4.4.9 and 4.2.18): a secondary is
229-
not available when :dbcommand:`compact` is running. The secondary does
230-
not enter the :replstate:`RECOVERING` state.
262+
* - Post 4.4.17, 5.0.12, 6.0.2
263+
- - ``compact`` blocks all write activity.
264+
- No replication possible.
265+
- Reads permitted.
231266

232-
- For previous MongoDB versions: on secondaries, :dbcommand:`compact`
233-
forces the secondary to enter the :replstate:`RECOVERING` state. Read
234-
operations issued to an instance in the :replstate:`RECOVERING` state
235-
will fail. This prevents clients from reading during the operation.
236-
When the operation completes, the secondary returns to
237-
:replstate:`SECONDARY` state.
267+
When ``compact`` completes, the secondary returns to the
268+
:replstate:`SECONDARY` state.
238269

239-
See :doc:`/reference/replica-states/` for more information about replica
240-
set member states.
270+
For more information about replica set member states, see
271+
See :ref:`replica-set-member-states`.
241272

242-
See :doc:`/tutorial/perform-maintence-on-replica-set-members` for an
243-
example replica set maintenance procedure to maximize availability
244-
during maintenance operations.
273+
For replica set maintenance and availability, see
274+
:ref:`perform-maint-on-replica-set`.
245275

246276
Sharded Clusters
247277
~~~~~~~~~~~~~~~~

source/reference/replica-states.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _replica-set-member-states:
2+
13
=========================
24
Replica Set Member States
35
=========================

source/tutorial/adjust-replica-set-member-priority.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _replica-set-adjust-priority:
2+
13
======================================
24
Adjust Priority for Replica Set Member
35
======================================

0 commit comments

Comments
 (0)