Docs 菜单

fsync

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.

Starting in MongoDB 7.0.2 (also available starting in 6.0.11, and 5.0.22) the fsync and fsyncUnlock commands can run on mongos to lock and unlock a sharded cluster.

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.

为了提供持久性数据,WiredTiger 使用了检查点。 有关详细信息,请参阅《日志和 WiredTiger 存储引擎》。

重要

Servers maintain an fsync lock count. The fsync command with the lock field set to true increments the lock count while the fsyncUnlock command decrements it. To enable writes on a locked server or cluster, call the fsyncUnlock command until the lock count reaches zero.

Use this command to block writes when you want to perform backup operations.

提示

mongosh 中,该命令也可通过 db.fsyncLock() 辅助方法运行。

辅助方法对 mongosh 用户来说很方便,但它们返回的信息级别可能与数据库命令不同。如果不追求方便或需要额外的返回字段,请使用数据库命令。

此命令可用于以下环境中托管的部署:

该命令具有以下语法:

db.adminCommand(
{
fsync: 1,
lock: <Boolean>,
fsyncLockAcquisitionTimeout: <integer>,
comment: <any>
}
)

该命令具有以下字段:

字段
类型
说明

fsync

整型

Enter "1" to apply fsync.

fsyncLockAcquisitionTimeoutMillis

整型

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

版本 7.0.2 新增内容

lock

布尔

Optional. Takes a lock on the server or cluster and blocks all write operations. Each fsync with lock operation takes a lock.

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

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. A mongod started using the copied files contains user-written data that is indistinguishable from the user-written data on the locked mongod.

锁定mongod的数据文件可能会因日志同步WiredTiger快照等操作而发生变化。 虽然这对逻辑数据(例如 客户端访问的数据),但某些备份实用程序可能会检测到这些更改并发出警告,或者失败并显示错误。 有关MongoDB的更多信息 - 推荐的备份实用程序和过程,请参阅自管理部署的备份方法。

版本 7.0.2 新增内容

When the fsync command runs on 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.

If your mongod has journaling enabled, use a file system or volume/block level snapshot tool to create a backup of the data set and the journal together as a single unit.

The fsync command returns a document includes a lockCount field. When run on mongod, the count indicates the number of fsync locks set on the server.

When run on a sharded cluster, mongos sends the fsync operation to each shard and returns the results, which includes the lockCount for each.

注意

If the lockCount field is greater than zero, all writes are blocked on the server and cluster. To reduce the lock count, use the fsyncUnlock command.

Fsync 锁在副本集或分片集群中的主节点上执行。

如果主节点因网络问题宕机或无法访问,集群会从可用的从节点中选举出新的主节点。如果带有 fsync 锁的主节点宕机,新的主节点不会保留 fsync 锁,并且可以处理写操作。备份操作过程中发生选举时,生成的备份可能不一致或无法使用。

要从主节点降级中恢复:

  1. Run the fsyncUnlock command until the lock count reaches zero to release the lock on all nodes.

  2. 发出fsync命令以在集群上重新建立 fsync 锁。

  3. 重新启动备份。

Additionally, fsync locks are persistent. When the old primary comes online again, you need to use the fsyncUnlock command to release the lock on the node.

注意

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. A mongod started using the copied files contains user-written data that is indistinguishable from the user-written data on the locked mongod.

锁定mongod的数据文件可能会因日志同步WiredTiger快照等操作而发生变化。 虽然这对逻辑数据(例如 客户端访问的数据),但某些备份实用程序可能会检测到这些更改并发出警告,或者失败并显示错误。 有关MongoDB的更多信息 - 推荐的备份实用程序和过程,请参阅自管理部署的备份方法。

The fsync command can lock an individual mongod instance or a sharded cluster through 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:

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

The operation returns a document that includes the status of the operation and the lockCount:

{
"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.

重要

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.

To unlock a server of cluster, use the fsyncUnlock command:

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.

To check the state of the fsync lock, use db.currentOp(). Use the following JavaScript function in the shell to test if the server or cluster is currently locked:

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:

serverIsLocked()

This function will return true if the server or cluster is currently locked and false if the server or cluster is not locked.