-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathfsync.txt
273 lines (182 loc) · 7.34 KB
/
fsync.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
=====
fsync
=====
.. default-domain:: mongodb
.. facet::
:name: genre
:values: reference
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. meta::
:description: the fsync command flushes all pending writes from the storage layer to disk.
Definition
----------
.. dbcommand:: fsync
Flushes all pending writes from the storage layer to disk. When the ``lock``
field is set to ``true``, it sets a lock on the server or cluster to prevent
additional writes until the lock is released.
.. |fsyncLockUnlock| replace:: the ``fsync`` and
:dbcommand:`fsyncUnlock` commands
.. include:: /includes/fsync-mongos
As applications write data, MongoDB records the data in the storage layer
and then writes the data to disk.
Run ``fsync`` when you want to flush writes to disk.
.. include:: /includes/checkpoints.rst
.. include:: /includes/fsync-lock-command
Use this command to block writes when you want to perform backup
operations.
.. |method| replace:: :method:`db.fsyncLock` helper method
.. include:: /includes/fact-dbcommand-tip
Compatibility
-------------
This command is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-no-free-or-serverless.rst
.. include:: /includes/fact-environments-onprem-only.rst
Syntax
------
The command has the following syntax:
.. code-block:: javascript
db.adminCommand(
{
fsync: 1,
lock: <Boolean>,
fsyncLockAcquisitionTimeout: <integer>,
comment: <any>
}
)
Command Fields
--------------
The command has the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 60
* - Field
- Type
- Description
* - ``fsync``
- integer
- Enter "1" to apply :dbcommand:`fsync`.
* - ``fsyncLockAcquisitionTimeoutMillis``
- integer
- Optional. Specifies the amount of time in milliseconds to wait to
acquire locks. If the lock acquisition operation times out, the
command returns a failed response.
Default: ``90000``
.. versionadded:: 7.1
* - ``lock``
- boolean
- Optional. Takes a lock on the server or cluster and blocks all
write operations. Each ``fsync`` with ``lock`` operation
takes a lock.
* - ``comment``
- any
- .. include:: /includes/extracts/comment-content.rst
Considerations
--------------
.. include:: /includes/extracts/wt-fsync-lock-compatibility-command.rst
Impact on Larger Deployments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 7.1
When the ``fsync`` command runs on :program:`mongos`, it performs the fsync
operation on the entire cluster. By setting the ``lock`` field to ``true``,
it sets a lock on the cluster, preventing additional writes.
To take a usable self-managed backup, before locking a sharded cluster:
- Ensure that no chunk migration, resharding, or DDL operations are active.
- Stop the balancer to prevent additional chunk migrations from starting.
Alternatives with Journaling
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If your :program:`mongod` has :term:`journaling <journal>` enabled, use
:ref:`a file system or volume/block level snapshot tool <backup-with-journaling>`
to create a backup of the data set and the journal together as a single unit.
Lock Count
~~~~~~~~~~
The ``fsync`` command returns a document includes a ``lockCount`` field. When
run on :program:`mongod`, the count indicates the number of fsync locks set on
the server.
When run on a sharded cluster, :program:`mongos` sends the fsync operation to
each shard and returns the results, which includes the ``lockCount`` for each.
.. note::
If the ``lockCount`` field is greater than zero, all writes
are blocked on the server and cluster. To reduce the lock
count, use the :dbcommand:`fsyncUnlock` command.
Fsync Locks after Failures
~~~~~~~~~~~~~~~~~~~~~~~~~~
Fsync locks execute on the primary in a replica set or sharded cluster.
If the primary goes down or becomes unreachable due to network issues, the
cluster :ref:`elects <replica-set-elections>` a new primary from the available
secondaries. If a primary with an fsync lock goes down, the new primary does
**not** retain the fsync lock and can handle write operations. When elections
occur during backup operations, the resulting backup may be inconsistent or
unusable.
To recover from the primary going down:
#. Run the :dbcommand:`fsyncUnlock` command until the lock count reaches zero
to release the lock on all nodes.
#. Issue the :dbcommand:`fsync` command to reestablish the fsync lock on the
cluster.
#. Restart the backup.
Additionally, fsync locks are persistent. When the old primary comes online
again, you need to use the :dbcommand:`fsyncUnlock` command to release the lock
on the node.
Examples
--------
Fsync Lock
~~~~~~~~~~
.. note::
.. include:: /includes/extracts/wt-fsync-lock-compatibility-command.rst
The ``fsync`` command can lock an individual :program:`mongod` instance or a
sharded cluster through :program:`mongos`. When run with the ``lock`` field
set to ``true``, the fsync operation flushes all data to the storage layer and
blocks all additional write operations until you unlock the instance or
cluster.
To lock the database, use the ``fsync`` command to set the ``lock`` field
to ``true``:
.. code-block:: javascript
db.adminCommand( { fsync: 1, lock: true } )
The operation returns a document that includes the status of the
operation and the ``lockCount``:
.. code-block:: javascript
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : NumberLong(1),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
When locked, write operations are blocked. Separate connections may continue
read operations until the first attempt at a write operation, then they also
wait until the sever or cluster is unlocked.
.. important::
The fsync lock operation maintains a lock count.
To unlock a server or cluster for writes, the lock count
must be zero. That is, for the given number of times you perform an fsync
lock, you must issue a corresponding number of unlock operations to unlock
the server or cluster for writes.
Fsync Unlock
~~~~~~~~~~~~
To unlock a server of cluster, use the :dbcommand:`fsyncUnlock` command:
.. code-block:: javascript
db.adminCommand( { fsyncUnlock: 1 } )
Repeat this command as many times as needed to reduce the lock count to zero.
Once the lock count reaches zero, the server or cluster can resume writes.
Check Lock Status
~~~~~~~~~~~~~~~~~
To check the state of the fsync lock, use :method:`db.currentOp()`. Use
the following JavaScript function in the shell to test if the server or
cluster is currently locked:
.. code-block:: javascript
serverIsLocked = function () {
var co = db.currentOp();
if (co && co.fsyncLock) {
return true;
}
return false;
}
After loading this function into your :binary:`~bin.mongosh` session,
call it with the following syntax:
.. code-block:: javascript
serverIsLocked()
This function will return ``true`` if the server or cluster is
currently locked and ``false`` if the server or cluster is not locked.