故障切换时的回滚¶
On this page
之前的 主节点 在 a 故障切换 后重新回归 复制集 时将会发生写操作的回滚。回滚只会发生在主节点的写操作 没能 成功在 从节点 上应用就Down 掉的情况下。当主节点重新以一个从节点加入复制集,它将进行 “回滚” ,其上得写操作将与复制集中其他成员的保持一致。
MongoDB会尽量避免回滚的发生。回滚如果确实发生了,往往是由于网络导致的。从节点如果无法跟上之前主节点上的写操作的吞吐,那么将会加剧回滚的影响范围。
当主节点在从节点完成写操作的复制后才Down 掉的或是主节点一直是可用的或是与多数节点可以沟通的,将 不会 发生回滚。
选取回滚的数据¶
When a rollback does occur, MongoDB writes the rollback data to BSON files in the rollback/ folder under the database’s dbPath directory. The names of rollback files have the following form:
<database>.<collection>.<timestamp>.bson
例如:
records.accounts.2011-05-09T18-10-04.0.bson
To read the contents of the rollback files, use bsondump. Based on the content and the knowledge of their applications, administrators can decide the next course of action to take.
避免复制集的回滚¶
我们可以通过设置 复制集的安全写级别 确保写操作应用到了整个复制集来避免回滚。
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.
With writeConcernMajorityJournalDefault set to false, MongoDB will not wait for w: "majority" writes to be durable before acknowledging the writes. As such, "majority" write operations could possibly roll back in the event of a loss of a replica set member.
注解
- Regardless of write concern, other clients using "local" (i.e. the default) readConcern can see the result of a write operation before the write operation is acknowledged to the issuing client.
- Clients using "local" (i.e. the default) readConcern can read data which may be subsequently rolled back.
Rollback Limitations¶
A mongod instance will not rollback more than 300 megabytes of data. If your system must rollback more than 300 megabytes, you must manually intervene to recover the data. If this is the case, the following line will appear in your mongod log:
[replica set sync] replSet syncThread: 13410 replSet too much data to roll back
这种情况下,直接保存数据或者强制节点执行数据初始化同步。如果进行数据的初始化同步呢,我们可以通过将该节点 dbPath 目录中的数据删除来从 “现有” 节点上进行数据同步。