Docs HomeMongoDB Manual

rs.stepDown()

On this page本页内容

Description描述

rs.stepDown(stepDownSecs, secondaryCatchUpPeriodSecs)

Instructs the primary of the replica set to become a secondary. 指示复制副本集的primary变为secondaryAfter the primary steps down, eligible secondaries will hold an election for primary.初选结束后,符合条件的中学将选举primary

The method does not immediately step down the primary. If no electable secondaries are up to date with the primary, the primary waits up to secondaryCatchUpPeriodSecs (by default 10 seconds) for a secondary to catch up. 该方法不会立即降级primary。如果没有electable的辅助设备是主设备的最新版本,则primary最多等待secondaryCatchUpPeriodSecs(默认情况下为10秒),以便secondary赶上。Once an electable secondary is available, the method steps down the primary.一旦有了可选举的次级,该方法就会逐步淘汰初级。

Once stepped down, the original primary becomes a secondary and is ineligible from becoming primary again for the remainder of time specified by stepDownSecs.一旦退出,原始的主节点将成为辅助节点,并且在stepDownSecs指定的剩余时间内没有资格再次成为主节点。

For a detailed explanation of the method 's execution, see Behavior.有关该方法执行的详细说明,请参阅行为

Note

The method is only valid against the primary and throws an error if run on a non-primary member.该方法仅对主成员有效,如果在非主成员上运行,则会引发错误。

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the replSetStepDown command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

The rs.stepDown() method has the following parameters:rs.stepDown()方法具有以下参数:

Parameter参数Type类型Description描述
stepDownSecsnumberThe number of seconds to step down the primary, during which time the stepdown member is ineligible for becoming primary. 退出主要成员的秒数,在此期间,退出成员没有资格成为主要成员。If you specify a non-numeric value, the command uses 60 seconds.如果指定非数字值,则该命令将使用60秒。
The stepdown period starts from the time that the mongod receives the command. The stepdown period must be greater than the secondaryCatchUpPeriodSecs. 递减期从mongod接收到命令的时间开始。递减周期必须大于secondaryCatchUpPeriodSecs
secondaryCatchUpPeriodSecsnumberOptional.可选的。The number of seconds that mongod will wait for an electable secondary to catch up to the primary.mongod将等待可选举的辅助设备赶上主设备的秒数。
When specified, secondaryCatchUpPeriodSecs overrides the default wait time of 10 seconds. 如果指定,secondaryCatchUpPeriodSecs将覆盖默认的10秒等待时间。

Behavior行为

Concurrent Operations并行操作

The rs.stepDown() method attempts to terminate long running user operations that block the primary from stepping down, such as an index build, a write operation or a map-reduce job.rs.stepDown()方法试图终止阻止主进程退出的长时间运行的用户操作,例如索引构建、写入操作或映射减少作业。

Availability of Eligible Secondaries合格借调人员的可用性

The method then initiates a catchup period where it waits up to secondaryCatchUpPeriodSeconds, by default 10 seconds, for a secondary to become up-to-date with the primary. 然后,该方法启动一个追赶期,等待secondaryCatchUpPeriodSeconds(默认情况下为10秒),以使secondary与primary同步。The primary only steps down if a secondary is up-to-date with the primary during the catchup period to prevent rollbacks.只有当辅助设备在追赶期间与主设备处于最新状态时,主设备才会降级,以防止回滚

If no electable secondary meets this criterion by the end of the waiting period, the primary does not step down and the method errors. 如果在等待期结束时没有可选择的辅助设备符合此标准,则主设备不会退出,方法会出错。

Once the primary steps down successfully, that node cannot become the primary for the remainder of the stepDownSecs period, which began when the node received the method.一旦主节点成功退出,该节点就不能在stepDownSecs周期的剩余时间内成为主节点,该周期从节点接收方法时开始。

Client Connections客户端连接

Starting in MongoDB 4.2, rs.stepDown() method no longer closes all client connections.从MongoDB 4.2开始,rs.stepDown()方法不再关闭所有客户端连接。

In MongoDB 4.0 and earlier, rs.stepDown() method closes all client connections during the step down. Because the disconnect includes the connection used to run the method, you cannot retrieve the return status of the method if the method completes successfully. 在MongoDB 4.0及更早版本中,rs.stepDown()方法在关闭过程中关闭所有客户端连接。由于断开连接包括用于运行该方法的连接,因此如果该方法成功完成,则无法检索该方法的返回状态。You can only retrieve the return status of the method if it errors. When running the 4.0 and earlier method in a script, the script should account for this behavior.只有当方法出错时,才能检索该方法的返回状态。在脚本中运行4.0及更早版本的方法时,脚本应该考虑到这种行为。

Writes During Stepdown降级期间的写入

Note

All writes to the primary fail during the period starting when the rs.stepDown() method is received until either a new primary is elected, or if there are no electable secondaries, the original primary resumes normal operation.从接收到rs.stepDown()方法开始的时间段内,所有对主存储器的写入都会失败,直到选择了新的主存储器,或者如果没有可选择的辅助存储器,原始主存储器将恢复正常操作。

Writes that were in progress when rs.stepDown() is run are killed. rs.stepDown()运行时正在进行的写入将被终止。In-progress transactions also fail with TransientTransactionError and can be retried as a whole进行中的事务也会因TransientTransactionError而失败,并且可以作为一个整体重试.

The time period where writes fail is at maximum:写入失败的时间段最多为:

secondaryCatchUpPeriodSecs (10s by default)(默认为10s) + electionTimeoutMillis (10s by default).(默认为10s)。

Election Handoff选举交接

When you step down a primary using rs.stepDown() or replSetStepDown without setting the force field to true, the stepped-down primary nominates an eligible secondary to call an election immediately.当您使用rs.stepDown()replSetStepDown在未将force字段设置为true的情况下退出primary时,退出的primary会提名一个合格的secondary立即进行选举。