@@ -59,6 +59,11 @@ new IS or S requests have been queued in the meantime. As a grant
59
59
will always move all other requests ahead in the queue, no starvation
60
60
of any request is possible.
61
61
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
+
62
67
.. [#mgl-ref] See the Uncyclopedia page on
63
68
`Multiple granularity locking
64
69
<http://en.wikipedia.org/wiki/Multiple_granularity_locking>`_ for
@@ -113,6 +118,11 @@ in the :doc:`current operation reporting </reference/method/db.currentOp>`
113
118
provides insight into the type of locks and amount of lock
114
119
contention in your :binary:`~bin.mongod` instance.
115
120
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
+
116
126
To terminate an operation, use :method:`db.killOp()`.
117
127
118
128
.. include:: /includes/replacement-mms.rst
@@ -130,6 +140,22 @@ also yield locks between individual document modifications in write
130
140
operations that affect multiple documents like
131
141
:method:`~db.collection.update()` with the ``multi`` parameter.
132
142
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
+
133
159
MongoDB's :ref:`MMAPv1 <storage-mmapv1>` storage engine uses
134
160
heuristics based on its access pattern to predict whether data is
135
161
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
138
164
data is available in memory, the operation will reacquire the lock
139
165
to complete the operation.
140
166
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:
146
168
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
+ ------------------------------------------------------
150
171
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)
152
215
153
- Which operations lock the database?
154
- -----------------------------------
216
+ - ``W`` (Exclusive)
155
217
156
- The following table lists common database operations and the types of
157
- locks they use.
218
+ -
158
219
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)
161
221
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)
163
237
164
238
Which administrative commands lock the database?
165
239
------------------------------------------------
@@ -174,30 +248,83 @@ other members of the set service load while maintenance is in progress.
174
248
The following administrative operations require an exclusive
175
249
lock at the database level for extended periods:
176
250
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
191
291
the lock for a very short time:
192
292
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`
201
328
202
329
.. _faq-concurrency-lock-multiple-dbs:
203
330
@@ -212,10 +339,6 @@ The following MongoDB operations lock multiple databases:
212
339
- :method:`db.repairDatabase()` obtains a global write lock and will block
213
340
other operations until it finishes.
214
341
215
- - :term:`Journaling <journal>`, which is an internal operation, locks
216
- all databases for short intervals. All databases share a single
217
- journal.
218
-
219
342
- :doc:`User authentication </core/authentication>` requires a read
220
343
lock on the ``admin`` database for deployments using :ref:`2.6 user
221
344
credentials <admin-system-users-collection>`. For deployments using
@@ -228,6 +351,9 @@ The following MongoDB operations lock multiple databases:
228
351
:binary:`~bin.mongod` to write to the primary's :term:`oplog` and
229
352
accounts for a small portion of the total time of the operation.
230
353
354
+ - Replica set member :doc:`state transitions
355
+ </reference/replica-states>` take global exlusive lock.
356
+
231
357
How does sharding affect concurrency?
232
358
-------------------------------------
233
359
0 commit comments