Definition定义
fsyncFlushes all pending writes from the storage layer to disk. When the将所有待处理的写入从存储层刷新到磁盘。当lockfield is set totrue, it sets a lock on the server or cluster to prevent additional writes until the lock is released.lock字段设置为true时,它会在服务器或集群上设置一个锁,以防止在锁释放之前进行额外的写入。Starting in MongoDB 7.1 (also available starting in 7.0.2, 6.0.11, and 5.0.22) the从MongoDB 7.1开始(从7.0.2、6.0.11和5.0.22开始也可用),fsyncandfsyncUnlockcommands can run onmongosto lock and unlock a sharded cluster.fsync和fsyncUnlock命令可以在mongos上运行,以锁定和解锁分片集群。As applications write data, MongoDB records the data in the storage layer and then writes the data to disk.当应用程序写入数据时,MongoDB会将数据记录在存储层中,然后将数据写入磁盘。Run当您想刷新对磁盘的写入时,请运行fsyncwhen you want to flush writes to disk.fsync。To provide durable data, WiredTiger uses checkpoints.为了提供持久的数据,WiredTiger使用检查点。For more details, see Journaling and the WiredTiger Storage Engine.有关更多详细信息,请参阅日志和WiredTiger存储引擎。Important
Servers maintain an fsync lock count.服务器维护fsync锁计数。The锁定字段设置为fsynccommand with thelockfield set totrueincrements the lock count while thefsyncUnlockcommand decrements it.true的fsync命令递增锁定计数,而fsyncUnlock命令递减锁定计数。To enable writes on a locked server or cluster, call the要在锁定的服务器或群集上启用写入,请调用fsyncUnlockcommand until the lock count reaches zero.fsyncUnlock命令,直到锁定计数达到零。Use this command to block writes when you want to perform backup operations.当您想要执行备份操作时,使用此命令阻止写入。Tip
In在mongosh, this command can also be run through thedb.fsyncLock()helper method.mongosh中,此命令也可以通过db.fsyncLock()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, but they may not return the same level of information as database commands.mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
fsync: 1,
lock: <Boolean>,
fsyncLockAcquisitionTimeout: <integer>,
comment: <any>
}
)Command Fields命令字段
The command has the following fields:该命令包含以下字段:
fsync | Enter "1" to apply fsync. | |
fsyncLockAcquisitionTimeoutMillis |
Default:
| |
lock | fsync with lock operation takes a lock.lock的fsync操作都需要一个锁。 | |
comment |
|
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命令可确保使用cp、scp或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开始使用复制的文件,其中包含用户写入的数据,这些数据与锁定的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 effect 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 Backup Methods for a Self-Managed Deployment.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅自我管理部署的备份方法。
Impact on Larger Deployments对大规模部署的影响
New in version 7.1.在版本7.1中新增。
When the 当fsync命令在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.mongos上运行时,它会在整个集群上执行fsync操作。通过将lock字段设置为true,它在集群上设置了一个锁,防止了额外的写入。
To take a usable self-managed backup, before locking a sharded cluster:要在锁定分片群集之前进行可用的自我管理备份,请执行以下操作:
Ensure that no chunk migration, resharding, or DDL operations are active.确保没有块迁移、重新标记或DDL操作处于活动状态。Stop the balancer to prevent additional chunk migrations from starting.停止平衡器以防止启动额外的块迁移。
Alternatives with Journaling日志记录的替代方案
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.mongod启用了日志记录,请使用文件系统或卷/块级快照工具将数据集和日志作为一个单元一起创建备份。
Lock Count锁计数
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.fsync命令返回一个包含lockCount字段的文档。在mongod上运行时,计数表示服务器上设置的fsync锁的数量。
When run on a sharded cluster, 在分片集群上运行时,mongos sends the fsync operation to each shard and returns the results, which includes the lockCount for each.mongos向每个分片发送fsync操作并返回结果,其中包括每个分片的lockCount。
Note
If the 如果lockCount field is greater than zero, all writes are blocked on the server and cluster. lockCount字段大于零,则服务器和集群上的所有写入都将被阻止。To reduce the lock count, use the 要减少锁计数,请使用fsyncUnlock command.fsyncUnlock命令。
Fsync Locks after Failures故障后Fsync锁定
Fsync locks execute on the primary in a replica set or sharded cluster.Fsync锁在副本集或分片集群的主服务器上执行。
If the primary goes down or becomes unreachable due to network issues, the cluster elects 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.如果具有fsync锁的主服务器发生故障,则新主服务器不会保留fsync锁,可以处理写操作。在备份操作期间发生选举时,产生的备份可能不一致或不可用。
To recover from the primary going down:要从主故障中恢复:
Run the运行fsyncUnlockcommand until the lock count reaches zero to release the lock on all nodes.fsyncUnlock命令,直到锁计数达到零,以释放所有节点上的锁。Issue the发出fsynccommand to reestablish the fsync lock on the cluster.fsync命令以在集群上重新建立fsync锁。Restart the backup.重新启动备份。
Additionally, fsync locks are persistent. When the old primary comes online again, you need to use the 此外,fsync锁是持久的。当旧主节点再次联机时,您需要使用fsyncUnlock command to release the lock on the node.fsyncUnlock命令释放节点上的锁。
Examples示例
Fsync LockFsync锁
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命令可确保使用cp、scp或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开始使用复制的文件,其中包含用户写入的数据,这些数据与锁定的mongod上的用户写入数据无法区分。
The data files of a locked 锁定的mongod may change due to operations such as journaling syncs or WiredTiger snapshots. While this has no effect on the logical data (e.g. data accessed by clients), some backup utilities may detect these changes and emit warnings or fail with errors. mongod的数据文件可能会因日志同步或WiredTiger快照等操作而更改。虽然这对逻辑数据(例如客户端访问的数据)没有影响,但一些备份实用程序可能会检测到这些更改并发出警告或出现错误。For more information on MongoDB-recommended backup utilities and procedures, see Backup Methods for a Self-Managed Deployment.有关MongoDB推荐的备份实用程序和过程的更多信息,请参阅自我管理部署的备份方法。
The fsync命令可以通过fsync command can lock an individual mongod instance or a sharded cluster through mongos. mongos锁定单个mongod实例或分片集群。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.lock字段设置为true运行时,fsync操作会将所有数据刷新到存储层,并阻止所有其他写入操作,直到您解锁实例或集群。
To lock the database, use the 要锁定数据库,请使用fsync command to set the lock field to true:fsync命令将lock字段设置为true:
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" : Long(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.fsync锁操作保持锁计数。
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锁的给定次数,您必须发出相应数量的解锁操作,以解锁服务器或集群进行写入。
Fsync UnlockFsync解锁
To unlock a server of cluster, use the 要解锁群集的服务器,请使用fsyncUnlock command:fsyncUnlock命令:
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 要检查fsync锁的状态,请使用db.currentOp(). Use the following JavaScript function in the shell to test if the server or cluster is currently locked:db.currentOp()。在shell中使用以下JavaScript函数来测试服务器或集群当前是否已锁定:
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 server or cluster is currently locked and false if the server or cluster is not locked.true,如果服务器或群集中未锁定,则返回false。