Skip to content

Commit 440c37c

Browse files
committed
DOCS-11335, DOCS-7395, DOCS-9845: validate and locks and part1 of concurrency faq update
1 parent 40d1bc4 commit 440c37c

File tree

3 files changed

+191
-49
lines changed

3 files changed

+191
-49
lines changed

source/faq/concurrency.txt

Lines changed: 168 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ new IS or S requests have been queued in the meantime. As a grant
5959
will always move all other requests ahead in the queue, no starvation
6060
of any request is possible.
6161

62+
In :method:`db.serverStatus()` and :method:`db.currentOp()` output, the
63+
lock modes are represented as follows:
64+
65+
.. include:: /includes/fact-lock-modes.rst
66+
6267
.. [#mgl-ref] See the Uncyclopedia page on
6368
`Multiple granularity locking
6469
<http://en.wikipedia.org/wiki/Multiple_granularity_locking>`_ for
@@ -113,6 +118,11 @@ in the :doc:`current operation reporting </reference/method/db.currentOp>`
113118
provides insight into the type of locks and amount of lock
114119
contention in your :binary:`~bin.mongod` instance.
115120

121+
In :method:`db.serverStatus()` and :method:`db.currentOp()` output, the
122+
lock modes are represented as follows:
123+
124+
.. include:: /includes/fact-lock-modes.rst
125+
116126
To terminate an operation, use :method:`db.killOp()`.
117127

118128
.. include:: /includes/replacement-mms.rst
@@ -130,6 +140,22 @@ also yield locks between individual document modifications in write
130140
operations that affect multiple documents like
131141
:method:`~db.collection.update()` with the ``multi`` parameter.
132142

143+
For storage engines supporting document level :term:`concurrency
144+
control`, such as :doc:`WiredTiger </core/wiredtiger>`, yielding is not
145+
necessary when accessing storage as the :term:`intent locks <intent
146+
lock>`, held at the global, database and collection level, do not block
147+
other readers and writers. However, operations will periodically yield,
148+
such as:
149+
150+
- to avoid long-lived storage transactions because these can
151+
potentially require holding a large amount of data in memory;
152+
153+
- to serve as interruption points so that you can kill long running
154+
operations;
155+
156+
- to allow operations that require exclusive access to a collection
157+
such as index/collection drops and creations.
158+
133159
MongoDB's :ref:`MMAPv1 <storage-mmapv1>` storage engine uses
134160
heuristics based on its access pattern to predict whether data is
135161
likely in physical memory before performing a read. If MongoDB
@@ -138,28 +164,76 @@ will yield its lock while MongoDB loads the data into memory. Once
138164
data is available in memory, the operation will reacquire the lock
139165
to complete the operation.
140166

141-
For storage engines supporting document level :term:`concurrency
142-
control`, such as :doc:`WiredTiger </core/wiredtiger>`, yielding is not
143-
necessary when accessing storage as the :term:`intent locks <intent
144-
lock>`, held at the global, database and collection level, do not block
145-
other readers and writers.
167+
.. _faq-concurrency-operations-locks:
146168

147-
.. versionchanged:: 2.6
148-
MongoDB does not yield locks when scanning an index even if it
149-
predicts that the index is not in memory.
169+
What locks are taken by some common client operations?
170+
------------------------------------------------------
150171

151-
.. _faq-concurrency-operations-locks:
172+
The following table lists some operations and the types of locks they
173+
use for document level locking storage engines:
174+
175+
.. list-table::
176+
:header-rows: 1
177+
178+
* - Operation
179+
180+
- Database
181+
182+
- Collection
183+
184+
* - Issue a query
185+
186+
- ``r`` (Intent Shared)
187+
188+
- ``r`` (Intent Shared)
189+
190+
* - Insert data
191+
192+
- ``w`` (Intent Exclusive)
193+
194+
- ``w`` (Intent Exclusive)
195+
196+
* - Remove data
197+
198+
- ``w`` (Intent Exclusive)
199+
200+
- ``w`` (Intent Exclusive)
201+
202+
* - Update data
203+
204+
- ``w`` (Intent Exclusive)
205+
206+
- ``w`` (Intent Exclusive)
207+
208+
* - Perform Aggregation
209+
210+
- ``r`` (Intent Shared)
211+
212+
- ``r`` (Intent Shared)
213+
214+
* - Create an index (Foreground)
152215

153-
Which operations lock the database?
154-
-----------------------------------
216+
- ``W`` (Exclusive)
155217

156-
The following table lists common database operations and the types of
157-
locks they use.
218+
-
158219

159-
.. todo In the table below (in the include), the issue of blocked
160-
JavaScript might no longer apply in version 2.4, which will use V8.
220+
* - Create an index (Background)
161221

162-
.. include:: /includes/table/lock-behavior-per-operation.rst
222+
- ``w`` (Intent Exclusive)
223+
224+
- ``w`` (Intent Exclusive)
225+
226+
* - List collections
227+
228+
- ``R`` (Shared)
229+
230+
-
231+
232+
* - Map-reduce
233+
234+
- ``W`` (Exclusive) and ``R`` (Shared)
235+
236+
- ``w`` (Intent Exclusive) and ``r`` (Intent Shared)
163237

164238
Which administrative commands lock the database?
165239
------------------------------------------------
@@ -174,30 +248,83 @@ other members of the set service load while maintenance is in progress.
174248
The following administrative operations require an exclusive
175249
lock at the database level for extended periods:
176250

177-
- :method:`db.collection.createIndex()`, when issued
178-
*without* setting ``background`` to ``true``,
179-
- :dbcommand:`reIndex`,
180-
- :dbcommand:`compact`,
181-
- :method:`db.repairDatabase()`,
182-
- :method:`db.createCollection()`, when creating a very large
183-
(i.e. many gigabytes) capped collection,
184-
- :method:`db.collection.validate()`, and
185-
- :method:`db.copyDatabase()`. This operation may lock all
186-
databases. See :ref:`faq-concurrency-lock-multiple-dbs`.
187-
- :dbcommand:`convertToCapped`
188-
- :dbcommand:`cloneCollectionAsCapped`
189-
190-
The following administrative commands lock the database but only hold
251+
.. list-table::
252+
253+
* - Commands
254+
- Methods
255+
256+
* - :dbcommand:`cloneCollectionAsCapped`
257+
-
258+
259+
* - :dbcommand:`compact`
260+
-
261+
262+
* - :dbcommand:`convertToCapped`
263+
-
264+
265+
* - :dbcommand:`copydb`. This operation may lock all databases. See
266+
:ref:`faq-concurrency-lock-multiple-dbs`.
267+
268+
- :method:`db.copyDatabase()`. This operation may lock all
269+
databases. See :ref:`faq-concurrency-lock-multiple-dbs`.
270+
271+
* - :dbcommand:`create` when creating a very large (i.e. many
272+
gigabytes) capped collection
273+
- :method:`db.createCollection()` when creating a very large (i.e.
274+
many gigabytes) capped collection
275+
276+
* - :dbcommand:`createIndexes` for indexes *without* ``background``
277+
set to ``true``
278+
279+
- :method:`db.collection.createIndex()` and
280+
:method:`db.collection.createIndexes()` issued *without*
281+
``background`` set to ``true``
282+
283+
* - :dbcommand:`reIndex`
284+
- :method:`db.collection.reIndex()`
285+
286+
* - :dbcommand:`repairDatabase`
287+
- :method:`db.repairDatabase()`
288+
289+
290+
The following administrative operations lock the database but only hold
191291
the lock for a very short time:
192292

193-
- :method:`db.collection.dropIndex()`,
194-
- :method:`db.getLastError()`,
195-
- :method:`db.isMaster()`,
196-
- :method:`rs.status()` (i.e. :dbcommand:`replSetGetStatus`),
197-
- :method:`db.serverStatus()`,
198-
- :method:`db.auth()`, and
199-
- :method:`db.addUser()`.
200-
- :method:`db.collection.renameCollection()`
293+
.. list-table::
294+
295+
* - Commands
296+
- Methods
297+
298+
* - :dbcommand:`authenticate`
299+
300+
- :method:`db.auth()`
301+
302+
303+
* - :dbcommand:`createUser`
304+
- :method:`db.createUser()`
305+
306+
307+
* - :dbcommand:`dropIndexes`
308+
- :method:`db.collection.dropIndex()`
309+
310+
* - :dbcommand:`getLastError`
311+
- :method:`db.getLastError()`
312+
313+
* - :dbcommand:`isMaster`
314+
- :method:`db.isMaster()`
315+
316+
* - :dbcommand:`replSetGetStatus`
317+
- :method:`rs.status()`
318+
319+
320+
* - :dbcommand:`renameCollection`
321+
- :method:`db.collection.renameCollection()`
322+
323+
* - :dbcommand:`serverStatus`
324+
325+
- :method:`db.serverStatus()`
326+
327+
.. seealso:: :ref:`faq-concurrency-lock-multiple-dbs`
201328

202329
.. _faq-concurrency-lock-multiple-dbs:
203330

@@ -212,10 +339,6 @@ The following MongoDB operations lock multiple databases:
212339
- :method:`db.repairDatabase()` obtains a global write lock and will block
213340
other operations until it finishes.
214341

215-
- :term:`Journaling <journal>`, which is an internal operation, locks
216-
all databases for short intervals. All databases share a single
217-
journal.
218-
219342
- :doc:`User authentication </core/authentication>` requires a read
220343
lock on the ``admin`` database for deployments using :ref:`2.6 user
221344
credentials <admin-system-users-collection>`. For deployments using
@@ -228,6 +351,9 @@ The following MongoDB operations lock multiple databases:
228351
:binary:`~bin.mongod` to write to the primary's :term:`oplog` and
229352
accounts for a small portion of the total time of the operation.
230353

354+
- Replica set member :doc:`state transitions
355+
</reference/replica-states>` take global exlusive lock.
356+
231357
How does sharding affect concurrency?
232358
-------------------------------------
233359

source/reference/command/validate.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ Definition
2525
:dbcommand:`validate` command does not support :doc:`views
2626
</core/views>` and raises an error when run against a view.
2727

28-
The ``validate`` command can be slow, particularly on larger data
29-
sets. While the ``validate`` command is running, it holds an
30-
exclusive lock on the collection. This will block all reads and
31-
writes until the validate command finishes.
3228

3329
:dbcommand:`validate` has the following prototype form:
3430

@@ -54,6 +50,18 @@ Definition
5450

5551
db.collection.validate();
5652

53+
Behavior
54+
--------
55+
56+
The :dbcommand:`validate` command can be slow, particularly on
57+
larger data sets.
58+
59+
The :dbcommand:`validate` command obtains an exclusive lock ``W`` on
60+
the collection. This will block all reads and writes on the collection
61+
until the operation finishes. When run on a secondary, the
62+
:dbcommand:`validate` operation can block all other operations on that
63+
secondary until it finishes.
64+
5765
Examples
5866
--------
5967

source/reference/method/db.collection.validate.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ Description
2525

2626
.. include:: /includes/apiargs/method-db.collection.validate-param.rst
2727

28-
The :method:`~db.collection.validate()` method output provides an
28+
The :method:`db.collection.validate()` method output provides an
2929
in-depth view of how the collection
3030
uses storage. Be aware that this command is potentially resource
3131
intensive and may impact the performance of your MongoDB
3232
instance.
3333

34-
The :method:`~db.collection.validate()` method is a wrapper
34+
The :method:`db.collection.validate()` method is a wrapper
3535
around the :dbcommand:`validate` :term:`database
3636
command`.
3737

38-
.. seealso:: :doc:`/reference/command/validate`
38+
Behavior
39+
--------
40+
41+
:method:`db.collection.validate()` obtains an exclusive lock on the
42+
collection. This will block all reads and writes on the collection
43+
until the operation finishes. When run on a secondary, the operation
44+
can sblock all other operations on that secondary until it finishes.
45+
46+
.. seealso:: :doc:`/reference/command/validate`

0 commit comments

Comments
 (0)