Sunday, 23 June 2019

Update Priority and votes options in Replica Set config

To update priority and votes in rc.conf():


Important notes:

  -  Re-configuring priority can force the current primary to step down, which causes an election. The rs.reconfig() shell method can force the current primary to step down. When the primary steps down, the mongod closes all client connections and it takes 10-20 seconds to elect new primary. So make priority setting changes during scheduled maintenance periods only.

  -  Avoid re-configuring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.

 Note: MongoDB version 4.0.9.

Steps:

Check the priority and votes of all replica members:

rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 0
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5d0e133f74cae82235a32ff7")
}
}

In the above output, member[2] has priority and votes as 0. So it can' not participate in election and it has no option to become as primary.

Use 'cfg' variable to assign replica set configuration (You can use any name as variable).

rs0:PRIMARY> cfg = rs.conf()
{
"_id" : "rs0",
"version" : 5,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 0
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5d0e133f74cae82235a32ff7")
}
}

Now change the priority and votes using below commands:

rs0:PRIMARY> cfg.members[2].priority = 1;
1
rs0:PRIMARY> cfg.members[2].votes = 1;
1

Reconfig the configuration to apply changes:

rs0:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1561205134, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1561205134, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

Now check updated configuration values:

rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5d0e133f74cae82235a32ff7")
}
}

Note:
Configuration changes should be done in Primary only, you can not do in secondaries.

rs0:SECONDARY> rs.reconfig(cfg)
{
"operationTime" : Timestamp(1561205414, 1),
"ok" : 0,
"errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is SECONDARY; use the \"force\" argument to override",
"code" : 10107,
"codeName" : "NotMaster",
"$clusterTime" : {
"clusterTime" : Timestamp(1561205414, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}


Note: Base information from MongoDB docs.


                                                                                                                   -- Thiru //

Priority and Votes in Replica Set config in MongoDB


For High Availability, In Replica set, priority and votes fields are very important and play key role while electing new primary in case of any issue with current primary.

Priority:


Based on value assigned to priority field, it has priority to participate in election and to become as a primary. By default all secondary members are eligible to become as primary though election process.

To modify priorities, you can update array of members in replica set configuration. The array index begins with 0.

General form like, members[n].priority = value.

       Ex:    members[0].priority = 0.5
members[1].priority = 2.

In the above example, member 1 has high priority to become as primary, member 0 has low priority.

We can give any number between 0 and 1000 as a value to priority field of members. The default value for the priority field is 1.

To block a member from becoming as a primary, change the priority value of that member to 0.

         Ex: members[2].priority = 0.

Votes and voting:


A replica set can have up to 50 members, but voting members should be 7 only, remaining nodes we can add as non-voting members. These non-voting members can have data and will allow read connections from client application, but don't participate and vote in elections.

For non-voting members, Priority and votes value should be equal to 0.
Members with priority greater than 0 cannot have 0 votes. But members with votes equal to 1, can have priority as 0, like Arbiter node.

Arbiter node has priority value equal to 0 as it should not become as primary as it doesn't hold any data and it has votes equal to 1 as it should participate in election to elect new primary. If your replica set has even number of nodes, you can add an arbiter node to avoid tie during elections for new primary.


We can check priority and votes with rs.conf() by connecting to database server.

rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "localhost:27000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:27001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "localhost:27002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5d0e133f74cae82235a32ff7")
}
}

Note: Base information from MongoDB docs.

Updating values of priority and votes in replica set configuration, will share in next post .. :)


                                                                                                                       -- Thiru //