Docs HomeMongoDB Manual

Rollbacks During Replica Set Failover副本集故障转移期间的回滚

A rollback reverts write operations on a former primary when the member rejoins its replica set after a failover. 当成员在故障转移后重新加入其副本集时,回滚将恢复对以前primary的写操作。A rollback is necessary only if the primary had accepted write operations that the secondaries had not successfully replicated before the primary stepped down. 只有当主服务器接受了在主服务器退出之前secondary未成功复制的写入操作时,才需要回滚。When the primary rejoins the set as a secondary, it reverts, or "rolls back," its write operations to maintain database consistency with the other members.当主成员作为辅助成员重新加入集合时,它会恢复或“回滚”其写操作,以保持与其他成员的数据库一致性。

MongoDB attempts to avoid rollbacks, which should be rare. When a rollback does occur, it is often the result of a network partition. MongoDB试图避免回滚,这种情况应该很少见。当回滚确实发生时,它通常是网络分区的结果。Secondaries that can not keep up with the throughput of operations on the former primary, increase the size and impact of the rollback.无法跟上前一个主操作吞吐量的次要操作会增加回滚的大小和影响。

A rollback does not occur if the write operations replicate to another member of the replica set before the primary steps down and if that member remains available and accessible to a majority of the replica set.如果写操作在主步骤关闭之前复制到副本集的另一个成员,并且该成员对副本集的大多数成员仍然可用和可访问,则不会发生回滚。

Collect Rollback Data集合回滚数据

Configure Rollback Data配置回滚数据

The createRollbackDataFiles parameter controls whether or not rollback files are created during rollbacks.createRollbackDataFiles参数控制回滚期间是否创建回滚文件。

Rollback Data回滚数据

By default, when a rollback occurs, MongoDB writes the rollback data to BSON files.默认情况下,当发生回滚时,MongoDB会将回滚数据写入BSON文件。

Note

Rollback Directory Change回滚目录更改

Starting in Mongo 4.4, the rollback directory for a collection is named after the collection's UUID rather than the collection namespace.从Mongo 4.4开始,集合的回滚目录以集合的UUID而不是集合命名空间命名。

For each collection whose data is rolled back, the rollback files are located in a <dbpath>/rollback/<collectionUUID> directory and have filenames of the form:对于数据被回滚的每个集合,回滚文件位于<dbpath>/rollback/<collectionUUID>目录中,其文件名形式如下:

removed.<timestamp>.bson

For example, if data for the collection comments in the reporting database rolled back:例如,如果reporting数据库中集合comments的数据回滚:

<dbpath>/rollback/20f74796-d5ea-42f5-8c95-f79b39bad190/removed.2020-02-19T04-57-11.0.bson

where <dbpath> is the mongod's dbPath.其中<dbpath>mongoddbPath

Tip

Collection Name集合名称

To get the collection name, you can search for rollback file in the MongoDB log. For example, if the log file is /var/log/mongodb/mongod.log, you can use grep to search for instances of "rollback file" in the log:要获得集合名称,您可以在MongoDB日志中搜索rollback file(回滚文件)。例如,如果日志文件是/var/log/mongodb/mongod.log,则可以使用grep在日志中搜索"rollback file"的实例:

grep "rollback file" /var/log/mongodb/mongod.log

Alternatively, you can loop through all the databases and run db.getCollectionInfos() for the specific UUID until you get a match. 或者,您可以循环遍历所有数据库,并为特定的UUID运行db.getCollectionInfos(),直到得到匹配项。For example:例如:

var mydatabases=db.adminCommand("listDatabases").databases;
var foundcollection=false;

for (var i = 0; i < mydatabases.length; i++) {
let mdb = db.getSiblingDB(mydatabases[i].name);
collections = mdb.getCollectionInfos( { "info.uuid": UUID("20f74796-d5ea-42f5-8c95-f79b39bad190") } );

for (var j = 0; j < collections.length; j++) { // Array of 1 element
foundcollection=true;
print(mydatabases[i].name + '.' + collections[j].name);
break;
}

if (foundcollection) { break; }
}

For each collection whose data is rolled back, the rollback files are located in a <dbpath>/rollback/<db>.<collection> directory and have filenames of the form:对于数据被回滚的每个集合,回滚文件都位于<dbpath>/rollback/<db>.<collection>目录,并具有以下格式的文件名:

removed.<timestamp>.bson

For example, if data for the collection comments in the reporting database rolled back:例如,如果reporting数据库中集合comments的数据回滚:

<dbpath>/rollback/reporting.comments/removed.2019-01-31T02-57-40.0.bson

where <dbpath> is the mongod's dbPath.其中<dbpath>mongoddbPath

Rollback Data Exclusion回滚数据排除

If the operation to roll back is a collection drop or a document deletion, the rollback of the collection drop or document deletion is not written to the rollback data directory.如果回滚操作是删除集合或文档,则不会将删除集合或删除文档的回滚写入回滚数据目录。

Warning

In MongoDB 4.4 and later, if write operations use { w: 1 } write concern, the rollback directory may exclude writes submitted after an oplog hole if the primary restarts before the write operation completes.在MongoDB 4.4及更高版本中,如果写入操作使用{ w: 1 }写入关注,则如果主目录在写入操作完成之前重新启动,则回滚目录可能会排除oplog漏洞后提交的写入。

Read Rollback Data读取回滚数据

To read the contents of the rollback files, use bsondump. 要读取回滚文件的内容,请使用bsondumpBased on the content and the knowledge of their applications, administrators can decide the next course of action to take.管理员可以根据应用程序的内容和知识来决定下一步的操作。

Avoid Replica Set Rollbacks避免副本集回滚

For replica sets, the write concern { w: 1 } only provides acknowledgement of write operations on the primary. 对于副本集,写入关注{ w: 1 }只提供对主服务器上写操作的确认。Data may be rolled back if the primary steps down before the write operations have replicated to any of the secondaries. 如果在写入操作复制到任何辅助设备之前主设备停止运行,则数据可能会回滚。This includes data written in multi-document transactions that commit using { w: 1 } write concern.这包括在使用{ w: 1 }写入关注提交的多文档事务中写入的数据。

Journaling and Write Concern majority记者和写入关注majority

To prevent rollbacks of data that have been acknowledged to the client, run all voting members with journaling enabled and use { w: "majority" } write concern to guarantee that the write operations propagate to a majority of the replica set nodes before returning with acknowledgement to the issuing client.若要防止回滚已向客户端确认的数据,请在启用日志记录的情况下运行所有投票成员,并使用{ w: "majority" }写入关注,以确保写入操作在返回到发出确认的客户端之前传播到大多数副本集节点。

Starting in MongoDB 5.0, { w: "majority" } is the default write concern for most MongoDB deployments. 从MongoDB 5.0开始,{ w: "majority" }是大多数MongoDB部署的默认写入关注。See Implicit Default Write Concern.请参阅隐式默认写入关注

With writeConcernMajorityJournalDefault set to false, MongoDB does not wait for w: "majority" writes to be written to the on-disk journal before acknowledging the writes. writeConcernMajorityJournalDefault设置为false的情况下,MongoDB不会等待w: "majority"写入到磁盘日志中,然后再确认写入。As such, "majority" write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set.因此,在给定副本集中的大多数节点发生瞬时丢失(例如崩溃和重新启动)的情况下,"majority"写入操作可能会回滚。

Visibility of Data That Can Be Rolled Back可回滚数据的可见性

  • Regardless of a write's write concern, other clients using "local" or "available" read concern can see the result of a write operation before the write operation is acknowledged to the issuing client.无论写入的写入关注如何,使用"local""available"读取关注的其他客户端都可以在向发出请求的客户端确认写入操作之前看到写入操作的结果。
  • Clients using "local" or "available" read concern can read data which may be subsequently rolled back during replica set failovers.使用"local""available"读取关注的客户端可以读取数据,这些数据随后可能在副本集故障切换期间回滚。

For operations in a multi-document transaction, when a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. 对于多文档事务中的操作,当事务提交时,事务中所做的所有数据更改都将保存并在事务外部可见。That is, a transaction will not commit some of its changes while rolling back others.也就是说,事务在回滚其他更改时不会提交某些更改。

Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.在事务提交之前,在事务中所做的数据更改在事务外部是不可见的。

However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. 然而,当一个事务写入多个分片时,并不是所有的外部读取操作都需要等待提交的事务的结果在分片中可见。For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern "local" can read the results of write 1 without seeing write 2.例如,如果事务已提交,并且写1在分片a上可见,但写2在分片B上还不可见,则外部读取时关注"local"可以读取写1的结果,而不会看到写2

Rollback Considerations回滚注意事项

User Operations用户操作

Starting in version 4.2, MongoDB kills all in-progress user operations when a member enters the ROLLBACK state.从4.2版本开始,当成员进入ROLLBACK状态时,MongoDB会终止所有正在进行的用户操作。

Index Builds索引构建

For more information on the index build process, see Index Builds on Populated Collections.有关索引生成过程的详细信息,请参阅填充集合上的索引生成

Index Operations When "majority" Read Concern is Disabled禁用"majority"读取关注时的索引操作

Disabling "majority" read concern prevents collMod commands which modify an index from rolling back. 禁用"majority"读取关注可防止修改索引的collMod命令回滚If such an operation needs to be rolled back, you must resync the affected nodes with the primary node.如果需要回滚这样的操作,则必须将受影响的节点与primary节点重新同步。

Size Limitations尺寸限制

MongoDB does not limit the amount of data you can roll back.MongoDB不限制您可以回滚的数据量。

Rollback Elapsed Time Limitations回滚运行时间限制

The rollback time limit defaults to 24 hours and is configurable using the rollbackTimeLimitSecs parameter.回滚时间限制默认为24小时,可使用rollbackTimeLimitSecs参数进行配置。

MongoDB measures elapsed time as the time between the first common operation in the oplogs to the last entry in the oplog of the member being rolled back.MongoDB测量运行时间,即从oplog中的第一个常用操作到被回滚成员的oplog中最后一个条目之间的时间。