summaryrefslogtreecommitdiff
path: root/jstests/replsets/remove1.js
blob: ebd17d659c7ecbe98b3d1c04c356476fbd98f4a4 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* test removing a node from a replica set
 *
 * Start set with three nodes
 * Initial sync
 * Remove slave1
 * Remove slave2
 * Bring slave1 back up
 * Bring slave2 back up
 * Add them back as slave
 * Make sure everyone's secondary
 */

load("jstests/replsets/rslib.js");
var name = "removeNodes";
var host = getHostName();


print("Start set with three nodes");
var replTest = new ReplSetTest( {name: name, nodes: 3} );
var nodes = replTest.startSet();
replTest.initiate();
var master = replTest.getMaster();


print("Initial sync");
master.getDB("foo").bar.baz.insert({x:1});

replTest.awaitReplication();


print("Remove slave2");
var config = replTest.getReplSetConfig();

config.members.pop();
config.version = 2;
try {
  master.getDB("admin").runCommand({replSetReconfig:config});
}
catch(e) {
  print(e);
}
reconnect(master);


print("Remove slave1");
config.members.pop();
config.version = 3;
try {
  master.getDB("admin").runCommand({replSetReconfig:config});
}
catch(e) {
  print(e);
}
reconnect(master);

print("sleeping 1");
sleep(10000);
// these are already down, but this clears their ports from memory so that they
// can be restarted later
stopMongod(replTest.getPort(1));
stopMongod(replTest.getPort(2));


print("Bring slave1 back up");
var paths = [ replTest.getPath(1), replTest.getPath(2) ];
var ports = allocatePorts(2, replTest.getPort(2)+1);
var args = ["mongod", "--port", ports[0], "--dbpath", paths[0], "--noprealloc", "--smallfiles", "--rest"];
var conn = startMongoProgram.apply( null, args );
conn.getDB("local").system.replset.remove();
printjson(conn.getDB("local").runCommand({getlasterror:1}));
print(conn);
print("sleeping 2");
sleep(10000);
stopMongod(ports[0]);

replTest.restart(1);


print("Bring slave2 back up");
args[2] = ports[1];
args[4] = paths[1];
conn = startMongoProgram.apply( null, args );
conn.getDB("local").system.replset.remove();
print("path: "+paths[1]);
print("sleeping 3");
sleep(10000);
stopMongod(ports[1]);

replTest.restart(2);
sleep(10000);


print("Add them back as slaves");
config.members.push({_id:1, host : host+":"+replTest.getPort(1)});
config.members.push({_id:2, host : host+":"+replTest.getPort(2)});
config.version = 4;
wait(function() {
    try {
      master.getDB("admin").runCommand({replSetReconfig:config});
    }
    catch(e) {
      print(e);
    }
    reconnect(master);

    master.setSlaveOk();
    var newConfig = master.getDB("local").system.replset.findOne();
    return newConfig.version == 4;
  });


print("Make sure everyone's secondary");
wait(function() {
    var status = master.getDB("admin").runCommand({replSetGetStatus:1});
    occasionally(function() {
        printjson(status);
      });
    
    if (!status.members || status.members.length != 3) {
      return false;
    }

    for (var i = 0; i<3; i++) {
      if (status.members[i].state != 1 && status.members[i].state != 2) {
        return false;
      }
    }
    return true;
  });

replTest.stopSet();