rs.reconfigForPSASet()

On this page本页内容

rs.reconfigForPSASet( memberIndex, config, { options } )

New in version 5.0.在版本5.0中新增

Safely perform some reconfiguration changes on a primary-secondary-arbiter (PSA) replica set or on a replica set that is changing to a PSA architecture. 在主辅助仲裁器(PSA)副本集或正在更改为PSA架构的副本集上安全地执行一些重新配置更改。You should only use this method in one of the following cases:只能在以下情况之一下使用此方法:

  • You want to reconfigure a secondary in an existing three-member replica set with a PSA architecture to become a voting, data-bearing node with a non-zero priority.您希望在具有PSA架构的现有三成员副本集中重新配置辅助节点,使其成为具有非零priority的投票、数据承载节点。
  • You want to add a new voting, data-bearing node with a non-zero priority to an existing two-member replica set that contains one voting, data-bearing node and one arbiter.您希望向包含一个投票、数据承载节点和一个仲裁器的现有两成员副本集添加一个具有非零优先级的新投票、数据携带节点。
Warning警告

If the secondary you are adding is lagged and the resulting replica set is a PSA configuration, the first configuration change will change the number of nodes that need to commit a change with "majority". 如果要添加的辅助节点已滞后,并且生成的副本集是PSA配置,则第一次配置更改将更改需要提交更改的节点数,并显示"majority"In this case, your commit point will lag until the secondary has caught up.在这种情况下,您的提交点将延迟,直到辅助节点赶上。

For details about the behavior of this method, see Behavior.有关此方法行为的详细信息,请参阅行为

Syntax语法

The rs.reconfigForPSASet() method has the following syntax:rs.reconfigForPSASet()方法具有以下语法:

rs.reconfigForPSASet(
  memberIndex: <num>,
  config: <configuration>,
  {
    "force" : <boolean>,
    "maxTimeMS" : <int>
  }
)
Parameter参数Type类型Description描述
memberIndexintegerThe index of the secondary that is being added or modified.正在添加或修改的辅助索引。
configdocumentA document that specifies the new configuration of a replica set.指定副本集的新配置的文档
forceboolean

Optional可选

Warning警告

Running the rs.reconfigForPSASet() method with force: true is not recommended and can lead to committed writes being rolled back.不建议使用force:true运行rs.reconfigForPSASet()方法,这可能导致提交的写入被回滚。

Specify true to force the available replica set members to accept the new configuration. 指定true可强制可用副本集成员接受新配置。Defaults to false.默认为false

Force reconfiguration can result in unexpected or undesired behavior, including rollback of "majority" committed writes.强制重新配置可能导致意外或不期望的行为,包括回滚"majority"提交的写入。

maxTimeMSinteger

Optional可选。

Specifies a cumulative time limit in milliseconds for processing each reconfiguration during the rs.reconfigForPSASet() operation. 指定在rs.reconfigForPSASet()操作期间处理每次重新配置的累积时间限制(以毫秒为单位)。By default, rs.reconfigForPSASet() waits indefinitely for the replica configurations to propagate to a majority of replica set members.默认情况下,rs.reconfigForPSASet()无限期地等待副本配置传播到大多数副本集成员。

Behavior行为

The rs.reconfigForPSASet() method reconfigures your replica set in two steps:rs.reconfigForPSASet()方法分两步重新配置副本集:

  1. The method reconfigures your replica set to add or modify a secondary with { votes: 1, priority: 0 }.该方法重新配置您的副本集,以添加或修改具有{ votes: 1, priority: 0 }的辅助副本。
  2. Once the added or modified secondary has caught up with all committed writes, the method reconfigures the secondary to have the priority specified in the rs.reconfigForPSASet() command ({ votes: 1, priority: <num> }).一旦添加或修改的辅助文件赶上了所有提交的写入,该方法将重新配置辅助文件,使其具有rs.reconfigForPSASet()命令中指定的priority{ votes: 1, priority: <num> })。

The two-step approach avoids the possibility of rolling back committed writes in the case of a failover to the new secondary before the new secondary has all committed writes from the previous configuration.两步方法避免了在故障转移到新辅助设备的情况下,在新辅助设备完成以前配置中的所有提交写入之前回滚提交写入的可能性。

Example示例

A replica set named rs0 has the following configuration:名为rs0的副本集具有以下配置:

{
   "_id" : "rs0",
   "version" : 1,
   "term": 1,
   "members" : [
      {
         "_id" : 0,
         "host" : "mongodb0.example.net:27017",
         "arbiterOnly" : false,
         "buildIndexes" : true,
         "hidden" : false,
         "priority" : 1,
         "tags" : {},
         "secondaryDelaySecs" : Long("0"),
         "votes" : 1
      },
      {
         "_id" : 2,
         "host" : "mongodb2.example.net:27017",
         "arbiterOnly" : true,
         "buildIndexes" : true,
         "hidden" : false,
         "priority" : 0,
         "tags" : {},
         "secondaryDelaySecs" : Long("0"),
         "votes" : 1
      }
   ],
   "protocolVersion" : Long("1"),
   "writeConcernMajorityJournalDefault": true,
   "settings" : {
      "chainingAllowed" : true,
      "heartbeatIntervalMillis" : 2000,
      "heartbeatTimeoutSecs" : 10,
      "electionTimeoutMillis" : 10000,
      "catchUpTimeoutMillis" : 2000,
      "getLastErrorModes" : {},
      "getLastErrorDefaults" : {
         "w" : 1,
         "wtimeout" : 0
      },
      "replicaSetId" : ObjectId("60e6f83923193faa336889d2")
   }
}

The following sequence of operations add a new secondary to the replica set. 以下操作序列将向副本集添加一个新的辅助副本。The operations are issued in the mongosh shell while connected to the primary.这些操作在连接到主服务器时在mongosh shell中发出。

cfg = rs.conf();
cfg["members"] = [
  {
     "_id" : 0,
     "host" : "mongodb0.example.net:27017",
     "arbiterOnly" : false,
     "buildIndexes" : true,
     "hidden" : false,
     "priority" : 1,
     "tags" : {},
     "secondaryDelaySecs" : Long("0"),
     "votes" : 1
  },
  {
     "_id" : 1,
     "host" : "mongodb2.example.net:27017",
     "arbiterOnly" : false,
     "buildIndexes" : true,
     "hidden" : false,
     "priority" : 2,
     "tags" : {},
     "secondaryDelaySecs" : Long("0"),
     "votes" : 1
  },
  {
     "_id" : 2,
     "host" : "mongodb2.example.net:27017",
     "arbiterOnly" : true,
     "buildIndexes" : true,
     "hidden" : false,
     "priority" : 0,
     "tags" : {},
     "secondaryDelaySecs" : Long("0"),
     "votes" : 1
  }
]
rs.reconfigForPSASet(1, cfg);
  1. The first statement uses the rs.conf() method to retrieve a document containing the current configuration for the replica set and stores the document in the local variable cfg.第一条语句使用rs.conf()方法检索包含副本集当前配置的文档,并将该文档存储在本地变量cfg中。
  2. The second statement adds the new secondary to the members array. 第二条语句将新的辅助项添加到members数组中。In this configuration the new secondary is added at memberIndex1. 在该配置中,新的辅助节点添加到memberIndex1The memberIndex is the same as the array index. memberIndex与数组索引相同。For additional settings, see replica set configuration settings.有关其他设置,请参阅副本集配置设置
  3. The last statement calls the rs.reconfigForPSASet() method with the memberIndex 1 and the modified cfg. 最后一条语句使用memberIndex 1和修改后的cfg调用rs.reconfigForPSASet()方法。The memberIndex is the array position of the new member in the members array. memberIndex是新成员在members数组中的数组位置。Upon successful reconfiguration, the replica set configuration resembles the following:成功重新配置后,副本集配置如下所示:

    {
       "_id" : "rs0",
       "version" : 1,
       "term": 1,
       "members" : [
          {
             "_id" : 0,
             "host" : "mongodb0.example.net:27017",
             "arbiterOnly" : false,
             "buildIndexes" : true,
             "hidden" : false,
             "priority" : 1,
             "tags" : {},
             "secondaryDelaySecs" : Long("0"),
             "votes" : 1
          },
          {
             "_id" : 1,
             "host" : "mongodb2.example.net:27017",
             "arbiterOnly" : false,
             "buildIndexes" : true,
             "hidden" : false,
             "priority" : 2,
             "tags" : {},
             "secondaryDelaySecs" : Long("0"),
             "votes" : 1
          },
          {
             "_id" : 2,
             "host" : "mongodb2.example.net:27017",
             "arbiterOnly" : true,
             "buildIndexes" : true,
             "hidden" : false,
             "priority" : 0,
             "tags" : {},
             "secondaryDelaySecs" : Long("0"),
             "votes" : 1
          }
       ],
       "protocolVersion" : Long("1"),
       "writeConcernMajorityJournalDefault": true,
       "settings" : {
          "chainingAllowed" : true,
          "heartbeatIntervalMillis" : 2000,
          "heartbeatTimeoutSecs" : 10,
          "electionTimeoutMillis" : 10000,
          "catchUpTimeoutMillis" : 2000,
          "getLastErrorModes" : {},
          "getLastErrorDefaults" : {
             "w" : 1,
             "wtimeout" : 0
          },
          "replicaSetId" : ObjectId("60e6f83923193faa336889d2")
       }
    }
←  rs.reconfig()rs.remove() →