Rollbacks During Replica Set Failover副本集故障切换期间的回滚

On this page本页内容

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. 只有当主节点接受了在主节点退出之前辅助节点没有成功复制的写操作时,才需要回滚。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. MongoDB试图避免回滚,这种情况应该很少发生。When a rollback does occur, it is often the result of a network partition. 当回滚发生时,通常是网络分区的结果。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配置回滚数据

Starting in version 4.0, MongoDB adds the parameter createRollbackDataFiles to control whether or not rollback files are created during rollbacks.从4.0版开始,MongoDB添加了参数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. 要获得集合名称,可以在MongoDB日志中搜索rollback fileFor 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:例如,如果日志文件是/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.

In versions 3.6 and earlier, rollback files are located directly under the <dbpath>/rollback directory with the filenames of the form <db>.<collection>.<timestamp>.bson.在3.6及更早版本中,回滚文件直接位于<dbpath>/rollback目录下,文件名的格式为<db>.<collection>.<timestamp>.bson

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

<dbpath>/rollbacktest.col.2020-02-19T04-46-22.0.bson

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.如果要回滚的操作是集合删除或文档删除,则集合删除或文件删除的回滚不会写入回滚数据目录。

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

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.如果需要回滚此类操作,则必须将受影响的节点与节点重新同步。

Size Limitations

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

Starting in version 4.0, MongoDB has no limit on the amount of data that can be rolled back.从4.0版开始,MongoDB对可回滚的数据量没有限制。

In previous versions, a mongod instance will not roll back more than 300 megabytes of data and requires manual intervention if more than 300 megabytes of data need to be rolled back.在以前的版本中,mongod实例不会回滚超过300 MB的数据,如果需要回滚超过300兆的数据,则需要手动干预。

Rollback Elapsed Time Limitations回滚已用时间限制

Starting in version 4.0, the rollback time limit defaults to 24 hours and is configurable using the parameter rollbackTimeLimitSecs:从版本4.0开始,回滚时间限制默认为24小时,可使用参数rollbackTimeLimitSecs进行配置:

  • In MongoDB 4.2+ and 4.0.13+, the rollback time limit is calculated between the first operation after the common point and the last point in the oplog for the member to roll back.在MongoDB 4.2+和4.0.13+中,回滚时间限制是在公共点之后的第一个操作和oplog中成员回滚的最后一个点之间计算的。
  • In MongoDB 4.0.0-4.0.12, the rollback time limit is calculated between the common point and the last point in the oplog for the member to roll back.在MongoDB 4.0.0-4.0.12中,回滚时间限制是在公共点和oplog中成员回滚的最后一点之间计算的。

In MongoDB 3.6 and earlier, the rollback time limit is not configurable. 在MongoDB 3.6及更早版本中,回滚时间限制是不可配置的。For these versions, rollback is limited by the amount of data, with a maximum of 300 megabytes.对于这些版本,回滚受到数据量的限制,最大为300 MB。

←  Replica Set ElectionsReplica Set Read and Write Semantics →