Skip to content

Commit

Permalink
Merge pull request cravall#78 from bekriebel/master
Browse files Browse the repository at this point in the history
addNewMembers: Check for an available _id value instead of incrementing
  • Loading branch information
cravall authored Mar 21, 2018
2 parents 19bdd13 + becab8b commit 770b1cd
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/lib/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,26 @@ var addNewReplSetMembers = function(db, addrToAdd, addrToRemove, shouldForce, do
var addNewMembers = function(rsConfig, addrsToAdd) {
if (!addrsToAdd || !addrsToAdd.length) return;

//Follows what is basically in mongo's rs.add function
var max = 0;
var memberIds = [];
var newMemberId = 0;

// Build a list of existing rs member IDs
for (var i in rsConfig.members) {
if (rsConfig.members[i]._id > max) {
max = rsConfig.members[i]._id;
}
memberIds.push(rsConfig.members[i]._id);
}

for (var i in addrsToAdd) {
var addrToAdd = addrsToAdd[i];

// Search for the next available member ID (max 255)
for (var i = newMemberId; i <= 255; i++) {
if (!memberIds.includes(i)) {
newMemberId = i;
memberIds.push(newMemberId);
break;
}
}

// Somehow we can get a race condition where the member config has been updated since we created the list of
// addresses to add (addrsToAdd) ... so do another loop to make sure we're not adding duplicates
var exists = false;
Expand All @@ -159,7 +167,7 @@ var addNewMembers = function(rsConfig, addrsToAdd) {
}

var cfg = {
_id: ++max,
_id: newMemberId,
host: addrToAdd
};

Expand Down

0 comments on commit 770b1cd

Please sign in to comment.