summaryrefslogtreecommitdiff
path: root/jstests/replsets/rename_replica_set.js
blob: 71b47b14814717c9fa6993ff1b4b1fbd67af2d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * Test the procedure for renaming a replica set.
 *
 * 1. Restart each node in the set in standalone mode.
 * 2. Replace each node's config doc with a doc that contains the new name.
 * 3. Restart the node as part of the newly named replica set.
 *
 * @tags: [requires_persistence]
 */

(function() {
"use strict";

const replTest = new ReplSetTest({name: 'testSet', nodes: 2});
let nodes = replTest.startSet();
replTest.initiate();

let primary = replTest.getPrimary();

const dbName = "test";
const collName = jsTestName();
let coll = primary.getDB(dbName)[collName];
const newReplSetName = "newTestSet";

// Make sure a write can be replicated to every node in the set. We set journaling to true to ensure
// this write is durable before restarting the nodes below.
assert.commandWorked(coll.insert({a: 1}, {"writeConcern": {"w": 2, "j": true}}));

// Restart all nodes in the set as standalones.
nodes = replTest.restart(nodes, {noReplSet: true});

// Change each node's config to have the new replica set name.
replTest.nodes.forEach(function(node) {
    let replsetColl = node.getDB("local").system.replset;
    let doc = replsetColl.findOne();

    let oldName = doc._id;
    doc._id = newReplSetName;

    // We can't update the doc since the replica set is the _id, so insert a new config and remove
    // the old one.
    replsetColl.insert(doc);
    replsetColl.remove({_id: oldName});

    // Restart the node now as part of the newly named replica set.
    replTest.restart(node, {replSet: newReplSetName});
});

// The nodes will only be able to elect a new primary if both nodes have the same replSet name.
primary = replTest.getPrimary();
let secondary = replTest.getSecondary();
coll = primary.getDB(dbName)[collName];

let primaryReplSetName = primary.getDB("local").system.replset.findOne()._id;
let secondaryReplSetName = secondary.getDB("local").system.replset.findOne()._id;
assert.eq(primaryReplSetName, newReplSetName);
assert.eq(secondaryReplSetName, newReplSetName);

// Make sure a write can still be replicated to every node in the set after the rename.
assert.commandWorked(coll.insert({b: 2}, {"writeConcern": {"w": 2}}));

replTest.stopSet();
}());