Skip to content

Commit bfab003

Browse files
author
Dave Cuthbert
authored
DOCSP-24455 BACKPORT (#2668)
* DOCSP-24455 BACKPORT * Version references * Missing reference
1 parent 09ac298 commit bfab003

File tree

5 files changed

+118
-85
lines changed

5 files changed

+118
-85
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: 103 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,21 @@ Definition
4040
.. list-table::
4141
:header-rows: 1
4242
:widths: 20 20 80
43-
43+
4444
* - Field
45-
4645
- Type
47-
4846
- Description
49-
47+
5048
* - ``compact``
51-
5249
- string
53-
5450
- The name of the collection.
55-
56-
57-
51+
5852
* - ``force``
59-
6053
- flag
61-
6254
- .. versionchanged:: 4.4
63-
55+
56+
.. _compact-force-option:
57+
6458
Optional. Starting in v4.4, if specified, forces
6559
:dbcommand:`compact` to run on the :term:`primary` in
6660
a :term:`replica set`. Before v4.4, this boolean field
@@ -69,19 +63,15 @@ Definition
6963
returned an error when run on a :term:`primary` if the
7064
value was ``false``, because the command blocked all other
7165
operations.
72-
66+
7367
Starting in v4.4, :dbcommand:`compact` does not block
7468
:ref:`crud` on the database it is compacting.
75-
69+
7670
* - ``comment``
77-
7871
- any
79-
8072
- .. include:: /includes/extracts/comment-content.rst
81-
8273
.. versionadded:: 4.4
8374

84-
8575
.. warning:: Always have an up-to-date backup before performing server
8676
maintenance such as the :dbcommand:`compact` operation.
8777

@@ -96,30 +86,6 @@ action on the target collection. The :authrole:`dbAdmin` role provides
9686
the required privileges for running :dbcommand:`compact` against
9787
non-system collections.
9888

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

@@ -156,28 +122,34 @@ Behavior
156122
Blocking
157123
~~~~~~~~
158124

159-
.. versionchanged:: 4.4
125+
Blocking behavior is version specific.
160126

161-
Starting in v4.4, on :ref:`WiredTiger <storage-wiredtiger>`,
162-
:dbcommand:`compact` only blocks the following metadata operations:
127+
.. list-table::
163128

164-
- :method:`db.collection.drop`
165-
- :method:`db.collection.createIndex` and
166-
:method:`db.collection.createIndexes`
167-
- :method:`db.collection.dropIndex` and
168-
:method:`db.collection.dropIndexes`
129+
* - Version
130+
- Blocking Behavior
169131

170-
:dbcommand:`compact` does not block :ref:`crud` for the database it is
171-
currently operating on.
132+
* - Pre 4.4
133+
- ``compact`` blocks all read and write activity.
172134

173-
Before v4.4, :dbcommand:`compact` blocked all operations for the
174-
database it was compacting, including :ref:`crud`, and was therefore
175-
recommended for use only during scheduled maintenance periods. Starting in
176-
v4.4, the :dbcommand:`compact` command is appropriate for use at any time.
135+
* - 4.4
136+
- - .. include:: /includes/reference/compact-methods-list.rst
137+
- All other operations are permitted.
177138

178-
You may view the intermediate progress either by viewing the
179-
:binary:`~bin.mongod` log file or by running the
180-
:method:`db.currentOp()` in another shell instance.
139+
* - Post 4.4.17
140+
- - .. include:: /includes/reference/compact-methods-list.rst
141+
- All other operations are permitted.
142+
- The :ref:`locking order <faq-concurrency-locking>` changes.
143+
144+
To run ``compact`` in a replica set, see
145+
:ref:`compact-cmd-replica-sets` for additional considerations.
146+
147+
Monitoring Progress
148+
~~~~~~~~~~~~~~~~~~~
149+
150+
To check the ``compact`` operation's progress, monitor the
151+
:binary:`~bin.mongod` log file or run :method:`db.currentOp()`
152+
from another shell instance.
181153

182154
Operation Termination
183155
~~~~~~~~~~~~~~~~~~~~~
@@ -204,45 +176,91 @@ Disk Space
204176
To see how the storage space changes for the collection, run the
205177
:dbcommand:`collStats` command before and after compaction.
206178

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

214-
:dbcommand:`compact` may require additional disk space to run on WiredTiger databases.
186+
:dbcommand:`compact` may require additional disk space to run on
187+
WiredTiger databases.
215188

216189
.. _compact-cmd-replica-sets:
217190

218191
Replica Sets
219192
~~~~~~~~~~~~
220193

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

224-
- Compact each member separately.
241+
* - Pre 4.4
242+
- - ``compact`` blocks all read and write activity.
243+
- No replication possible.
244+
- Secondary node is in :replstate:`RECOVERING` state.
225245

226-
- Ideally run :dbcommand:`compact` on a secondary. See option
227-
``force`` above for information regarding compacting the primary.
246+
* - 4.4
247+
- - ``compact`` blocks all write activity.
248+
- No replication possible.
249+
- Reads not permitted.
228250

229-
- Starting in MongoDB 4.4.9 (and 4.2.18): a secondary is
230-
not available when :dbcommand:`compact` is running. The secondary does
231-
not enter the :replstate:`RECOVERING` state.
251+
* - Post 4.4.17
252+
- - ``compact`` blocks all write activity.
253+
- No replication possible.
254+
- Reads permitted.
232255

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

240-
See :doc:`/reference/replica-states/` for more information about replica
241-
set member states.
259+
For more information about replica set member states, see
260+
See :ref:`replica-set-member-states`.
242261

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

247265
Sharded Clusters
248266
~~~~~~~~~~~~~~~~

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
======================================

source/tutorial/perform-maintence-on-replica-set-members.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _perform-maint-on-replica-set:
2+
13
==========================================
24
Perform Maintenance on Replica Set Members
35
==========================================

0 commit comments

Comments
 (0)