fsync

On this page本页内容

Definition定义

fsync

Forces the mongod process to flush all pending writes from the storage layer to disk and locks the entiremongod instance to prevent additional writes until the user releases the lock with a corresponding fsyncUnlock. 强制mongod进程将所有挂起的写入从存储层刷新到磁盘,并锁定整个mongod实例以防止额外的写入,直到用户使用相应的fsyncUnlock释放锁定。Optionally, you can use fsync to lock the mongod instance and block write operations for the purpose of capturing backups.视需要,您可以使用fsync锁定mongod实例并阻止写操作以捕获备份。

As applications write data, MongoDB records the data in the storage layer and then writes the data to disk within the syncPeriodSecs interval, which is 60 seconds by default. 当应用程序写入数据时,MongoDB将数据记录在存储层中,然后在syncPeriodSecs间隔内(默认为60秒)将数据写入磁盘。Run fsync when you want to flush writes to disk ahead of that interval.当您希望在该间隔之前将写操作刷新到磁盘时,请运行fsync

The fsync command has the following syntax:fsync命令具有以下语法:

{ fsync: 1, lock: <Boolean>, comment: <any> }

The fsync command has the following fields:fsync命令包含以下字段:

Field字段Type类型Description描述
fsyncintegerEnter "1" to apply fsync.输入“1”以应用fsync
lockbooleanOptional. 可选。Takes a lock on the mongod instance and blocks all write operations. 锁定mongod实例并阻止所有写入操作。Each fsync with lock operation takes a lock. 每个带lockfsync操作都需要一个锁。
commentany

Optional. 可选。A user-provided comment to attach to this command. 用户提供了附加到此命令的注释。Once set, this comment appears alongside records of this command in the following locations:设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

To run the fsync command, use the db.adminCommand() method:要运行fsync命令,请使用db.adminCommand()方法:

db.adminCommand( { fsync: 1, ... } )

Considerations注意事项

fsync command with the lock option ensures that the data files are safe to copy using low-level backup utilities such as cp, scp, or tar. 带有lock选项的fsync命令可确保使用低级备份实用程序(如cpscptar)安全复制数据文件。A mongod started using the copied files contains user-written data that is indistinguishable from the user-written data on the locked mongod.开始使用复制文件的mongod包含用户写入的数据,这些数据与锁定mongod上的用户写入数据无法区分。

The data files of a locked mongod may change due to operations such as journaling syncs or WiredTiger snapshots. 锁定mongod的数据文件可能会因日志同步WiredTiger快照等操作而更改。While this has no affect on the logical data (e.g. data accessed by clients), some backup utilities may detect these changes and emit warnings or fail with errors. 虽然这对逻辑数据(例如,客户端访问的数据)没有影响,但一些备份实用程序可能会检测到这些更改并发出警告或出现错误。For more information on MongoDB- recommended backup utilities and procedures, see MongoDB Backup Methods.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅MongoDB备份方法

Impact on Larger Deployments对大型部署的影响

An fsync lock is only possible on individualmongod instances of a sharded cluster, not on the entire cluster. fsync锁只能在分片集群的单个mongod实例上使用,而不能在整个集群上使用。To back up an entire sharded cluster, please see Backup and Restore Sharded Clusters for more information.要备份整个分片化集群,请参阅备份和还原分片化集群以了解更多信息。

Alternatives with Journaling日志记录替代方案

If your mongod has journaling enabled, please use file system or volume/block level snapshot tool to create a backup of the data set and the journal together as a single unit.如果您的mongod已启用日志记录,请使用文件系统或卷/块级快照工具将数据集和日志作为一个单元一起创建备份。

fsync with lock: true

Changed in version 3.4.在版本3.4中更改

The { fsync: 1, lock: true } command now returns a lockCount in the return document.{ fsync: 1, lock: true }命令现在在返回文档中返回lockCount

After { fsync: 1, lock: true } runs on a mongod, all write operations will block. mongod上运行{ fsync: 1, lock: true }后,所有写操作都将被阻止。mongosh provides a helper method db.fsyncLock().提供了一个助手方法db.fsyncLock()

Note注意

The { fsync: 1, lock: true } operation maintain a lock count. { fsync: 1, lock: true }操作维护一个锁计数。Each { fsync: 1, lock: true } operation increments the lock count.每个{ fsync: 1, lock: true }操作都会增加锁计数。

To unlock a mongod instance for writes, the lock count must be zero. 要解锁mongod实例进行写入,锁计数必须为零。That is, for a given number of { fsync: 1, lock: true } operation, you must issue a corresponding number of unlock operations in order to unlock the instance for writes. 也就是说,对于给定数量的{ fsync: 1, lock: true }操作,必须发出相应数量的解锁操作才能解锁实例以进行写入。To unlock, see db.fsyncUnlock().要解锁,请参阅db.fsyncUnlock()

Examples示例

Lock mongod Instance锁定mongod实例

Note注意

fsync command with the lock option ensures that the data files are safe to copy using low-level backup utilities such as cp, scp, or tar. 带有lock选项的fsync命令可确保使用低级备份实用程序(如cpscptar)安全复制数据文件。A mongod started using the copied files contains user-written data that is indistinguishable from the user-written data on the locked mongod.开始使用复制文件的mongod包含用户写入的数据,这些数据与锁定mongod上的用户写入数据无法区分。

The data files of a locked mongod may change due to operations such as journaling syncs or WiredTiger snapshots. 锁定mongod的数据文件可能会因日志同步WiredTiger快照等操作而更改。While this has no affect on the logical data (e.g. data accessed by clients), some backup utilities may detect these changes and emit warnings or fail with errors. 虽然这对逻辑数据(例如,客户端访问的数据)没有影响,但一些备份实用程序可能会检测到这些更改并发出警告或出现错误。For more information on MongoDB- recommended backup utilities and procedures, see MongoDB Backup Methods.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅MongoDB备份方法

The primary use of fsync is to lock the mongod instance in order to back up the files within mongod 's dbPath. fsync的主要用途是锁定mongod实例,以便备份mongoddbPath中的文件。The operation flushes all data to the storage layer and blocks all write operations until you unlock the mongod instance.该操作将所有数据刷新到存储层,并阻止所有写入操作,直到解锁mongod实例。

To lock the database, use the lock field set to true:要锁定数据库,请使用设置为truelock字段:

db.adminCommand( { fsync: 1, lock: true } )

The operation returns a document that includes the status of the operation and the lockCount:该操作返回一个文档,其中包含操作状态和lockCount

{
   "info" : "now locked against writes, use db.fsyncUnlock() to unlock",
   "lockCount" : NumberLong(1),
   "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
   "ok" : 1
}

You may continue to perform read operations on a mongod instance that has a fsync lock. 您可以继续对具有fsync锁的mongod实例执行读取操作。However, after the first write operation all subsequent read operations wait until you unlock the mongod instance.然而,在第一次写入操作之后,所有后续的读取操作都会等待,直到您解锁mongod实例。

Important重要

The { fsync: 1, lock: true } operation maintain a lock count.{ fsync: 1, lock: true }操作维护一个锁计数。

To unlock a mongod instance for writes, the lock count must be zero. 要解锁mongod实例进行写入,锁计数必须为零。That is, for a given number of { fsync: 1, lock: true } operation, you must issue a corresponding number of unlock operations in order to unlock the instance for writes.也就是说,对于给定数量的{fsync:1,lock:true}操作,必须发出相应数量的解锁操作才能解锁实例以进行写入。

Unlock mongod Instance解锁mongod实例

To unlock the mongod, use db.fsyncUnlock():要解锁mongod,请使用db.fsyncUnlock()

db.fsyncUnlock();

Repeat the db.fsyncUnlock() to reduce the lock count to zero to unlock the instance for writes.重复db.fsyncUnlock()将锁计数减为零,以解锁实例进行写入。

Check Lock Status检查锁定状态

To check the state of the fsync lock, use db.currentOp(). 要检查fsync锁的状态,请使用db.currentOp()Use the following JavaScript function in the shell to test if mongod instance is currently locked:在shell中使用以下JavaScript函数测试mongod实例当前是否被锁定:

serverIsLocked = function () {
                     var co = db.currentOp();
                     if (co && co.fsyncLock) {
                         return true;
                     }
                     return false;
                 }

After loading this function into your mongosh session, call it with the following syntax:将此函数加载到mongosh会话后,使用以下语法调用它:

serverIsLocked()

This function will return true if the mongod instance is currently locked and false if the mongod is not locked.如果mongod实例当前被锁定,该函数将返回true,如果mongod未被锁定,则返回false

←  filemd5fsyncUnlock →