summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/replsets/replset1.js24
-rw-r--r--jstests/replsets/replset2.js40
-rw-r--r--jstests/replsets/replset3.js42
-rw-r--r--jstests/replsets/replsetadd.js59
-rw-r--r--shell/servers.js53
5 files changed, 161 insertions, 57 deletions
diff --git a/jstests/replsets/replset1.js b/jstests/replsets/replset1.js
index 28d58a3f6f2..3898b4cf5d7 100644
--- a/jstests/replsets/replset1.js
+++ b/jstests/replsets/replset1.js
@@ -1,6 +1,9 @@
-
doTest = function( signal ) {
+ // Test basic replica set functionality.
+ // -- Replication
+ // -- Failover
+
// Replica set testing API
// Create a new replica set test. Specify set name and the number of nodes you want.
var replTest = new ReplSetTest( {name: 'testSet', nodes: 3} );
@@ -31,20 +34,10 @@ doTest = function( signal ) {
// and slaves in the set and wait until the change has replicated.
replTest.awaitReplication();
- print("Node Ids");
- print(replTest.getNodeId( master ))
- print(replTest.getNodeId(replTest.liveNodes.slaves[0]));
- print(replTest.getNodeId(replTest.liveNodes.slaves[1]));
-
// Here's how to stop the master node
var master_id = replTest.getNodeId( master );
replTest.stop( master_id );
- sleep(10000);
-
- // Here's how to restart it
- replTest.start( master_id, {}, true );
-
// Now let's see who the new master is:
var new_master = replTest.getMaster();
@@ -52,6 +45,13 @@ doTest = function( signal ) {
var new_master_id = replTest.getNodeId( new_master );
assert( master_id != new_master_id, "Old master shouldn't be equal to new master." );
+
+ // Here's how to restart a node:
+ replTest.start( master_id, {}, true );
+
+
+ // Shut down the set and finish the test.
+ replTest.stopSet( signal );
}
-doTest( 9 );
+doTest( 15 );
diff --git a/jstests/replsets/replset2.js b/jstests/replsets/replset2.js
new file mode 100644
index 00000000000..c1ae1b9051a
--- /dev/null
+++ b/jstests/replsets/replset2.js
@@ -0,0 +1,40 @@
+
+doTest = function( signal ) {
+
+ // Test replication with getLastError
+
+ // Replica set testing API
+ // Create a new replica set test. Specify set name and the number of nodes you want.
+ var replTest = new ReplSetTest( {name: 'testSet', nodes: 3} );
+
+ // call startSet() to start each mongod in the replica set
+ // this returns a list of nodes
+ var nodes = replTest.startSet();
+
+ // Call initiate() to send the replSetInitiate command
+ // This will wait for initiation
+ replTest.initiate();
+
+ // Call getMaster to return a reference to the node that's been
+ // elected master.
+ var master = replTest.getMaster();
+
+ // Wait for replication to a single node
+ master.getDB("foo").bar.insert({n: 1});
+ var result = master.getDB("foo").runCommand({getlasterror: {w: 1, wtimeout: 20000}});
+ assert( result['ok'] == 1, "getLastError with w=2 failed");
+
+ // Wait for replication two two nodes
+ master.getDB("foo").bar.insert({n: 2});
+ var result = master.getDB("foo").runCommand({getlasterror: {w: 2, wtimeout: 20000}});
+ assert( result['ok'] == 1, "getLastError with w=2 failed");
+
+ // Wait for replication to three nodes
+ master.getDB("foo").bar.insert({n: 3});
+ var result = master.getDB("foo").runCommand({getlasterror: {w: 3, wtimeout: 20000}});
+ assert( result['ok'] == 1, "getLastError with w=2 failed");
+
+ replTest.stopSet( signal );
+}
+
+doTest( 15 );
diff --git a/jstests/replsets/replset3.js b/jstests/replsets/replset3.js
new file mode 100644
index 00000000000..cda84501c26
--- /dev/null
+++ b/jstests/replsets/replset3.js
@@ -0,0 +1,42 @@
+
+doTest = function( signal ) {
+
+ // Test replica set step down
+
+ // Replica set testing API
+ // Create a new replica set test. Specify set name and the number of nodes you want.
+ var replTest = new ReplSetTest( {name: 'testSet', nodes: 3} );
+
+ // call startSet() to start each mongod in the replica set
+ // this returns a list of nodes
+ var nodes = replTest.startSet();
+
+ // Call initiate() to send the replSetInitiate command
+ // This will wait for initiation
+ replTest.initiate();
+
+ // Get master node
+ var master = replTest.getMaster();
+
+ // Step down master
+ master.getDB("admin").runCommand({replSetStepDown: true});
+
+ try {
+ var new_master = replTest.getMaster();
+ }
+ catch( err ) {
+ throw( "Could not elect new master before timeout." );
+ }
+
+ assert( master != new_master, "Old master shouldn't be equal to new master." );
+
+ // Make sure that slaves are still up
+ var result = new_master.runCommand({replSetGetStatus: true});
+
+ assert( result['ok'] == 1, "Could not verify that slaves were still up:" + result );
+ printjson( result );
+
+ replTest.stopSet( 15 );
+}
+
+// doTest( 15 );
diff --git a/jstests/replsets/replsetadd.js b/jstests/replsets/replsetadd.js
index 921bd9b0afb..c20ac92bc4d 100644
--- a/jstests/replsets/replsetadd.js
+++ b/jstests/replsets/replsetadd.js
@@ -1,31 +1,36 @@
doTest = function( signal ) {
-// // Replica set testing API
-// // Create a new replica set test. Specify set name and the number of nodes you want.
-// var replTest = new ReplSetTest( {name: 'testSet', nodes: 2} );
-//
-// // this returns a list of nodes
-// var nodes = replTest.startSet();
-//
-// // This will wait for initiation
-// replTest.initiate();
-//
-// // Call getMaster to return a reference to the node that's been
-// // elected master.
-// var master = replTest.getMaster();
-//
-// // Start up a new node
-// replTest.add();
-//
-// var nodeList = replTest.nodeList();
-// printjson( nodeList );
-// var newHost = nodeList[nodeList.length-1];
-//
-// // Initiate new node
-// var result = master.getDB("admin").eval('rs.add(\'' + newHost + '\');');
-// printjson( result );
-//
-// assert( result['ok'] == 1 );
+ // Test add node
+
+ var replTest = new ReplSetTest( {name: 'testSet', nodes: 0} );
+
+ var first = replTest.add();
+
+ // Initiate replica set
+ assert.soon(function() {
+ var res = first.getDB("admin").runCommand({replSetInitiate: null});
+ return res['ok'] == 1;
+ });
+
+ // Get status
+ assert.soon(function() {
+ var result = first.getDB("admin").runCommand({replSetGetStatus: true});
+ return result['ok'] == 1;
+ });
+
+ // Start a second node
+ var second = replTest.add();
+
+ // Add the second node.
+ // This runs the equivalent of rs.add(newNode);
+ // Results in the following exception:
+ //m31000| Mon Jul 26 14:46:20 [conn1] replSet replSetReconfig all members seem up
+ //m31000| Mon Jul 26 14:46:20 [conn1] replSet info saving a newer config version to local.system.replset
+ //m31000| Mon Jul 26 14:46:20 [conn1] Assertion failure !sp.state.primary() db/repl/rs.h 184
+ //m31000| 0x10002dcde 0x100031d78 0x1001dc733 0x1001dce60 0x1001f03b4 0x1001f10eb 0x1002b37d4 0x1002b49aa 0x100181448 0x100184d72 0x10025bf93 0x10025f746 0x10033795d 0x100695404 0x7fff81429456 0x7fff81429309
+ replTest.reInitiate();
+
+ replTest.stopSet( signal );
}
-doTest( 9 );
+// doTest( 15 );
diff --git a/shell/servers.js b/shell/servers.js
index bb7afa06de6..9bd6691b79d 100644
--- a/shell/servers.js
+++ b/shell/servers.js
@@ -773,6 +773,8 @@ ReplTest.prototype.start = function( master , options , restart, norepl ){
var lockFile = this.getPath( master ) + "/mongod.lock";
removeFile( lockFile );
var o = this.getOptions( master , options , restart, norepl );
+
+
if ( restart )
return startMongoProgram.apply( null , o );
else
@@ -859,12 +861,9 @@ function skipIfTestingReplication(){
// ReplSetTest
ReplSetTest = function( opts ){
- if( !opts.nodes || opts.nodes < 2 )
- throw("ReplSetTest requires at least two nodes.");
-
this.name = opts.name || "testReplSet";
this.host = opts.host || getHostName();
- this.numNodes = opts.nodes || 3;
+ this.numNodes = opts.nodes || 0;
this.ports = allocatePorts( this.numNodes );
@@ -939,9 +938,9 @@ ReplSetTest.prototype.getOptions = function( n , extra , putBinaryFirst ){
for(i=0; i<this.ports.length; i++) {
// Don't include this node in the replica set list
- if(this.ports[i] == this.ports[n]) {
- continue;
- }
+ //if(this.ports[i] == this.ports[n]) {
+ // continue;
+ //}
var str = this.host + ":" + this.ports[i];
hosts.push(str);
@@ -1019,14 +1018,18 @@ ReplSetTest.prototype.getMaster = function( timeout ) {
}
// Add a node to the test set
-// Run this.reInitiate() to add the
-// node to the config.
-ReplSetTest.prototype.add = function() {
- var nextPort = this.ports[this.ports.length-1] + 1;
+ReplSetTest.prototype.add = function( config ) {
+ if(this.ports.length == 0) {
+ var nextPort = allocatePorts(1)[0];
+ }
+ else {
+ var nextPort = this.ports[this.ports.length-1] + 1;
+ }
print("Next port: " + nextPort);
this.ports.push(nextPort);
printjson(this.ports);
- var nextId = this.nodes.length;
+
+ var nextId = this.nodes.length;
printjson(this.nodes);
print(nextId);
var newNode = this.start(nextId);
@@ -1072,6 +1075,14 @@ ReplSetTest.prototype.initiate = function( cfg , initCmd , timeout ) {
});
}
+ReplSetTest.prototype.reInitiate = function() {
+ var master = this.nodes[0];
+ var c = master.getDB("local")['system.replset'].findOne();
+ var config = this.getReplSetConfig();
+ config.version = c.version + 1;
+ this.initiate( config , 'replSetReconfig' );
+}
+
ReplSetTest.prototype.awaitReplication = function() {
this.getMaster();
@@ -1091,7 +1102,10 @@ ReplSetTest.prototype.awaitReplication = function() {
synced = false;
}
}
- print("Synced = " + synced);
+
+ if(synced) {
+ print("Synced = " + synced);
+ }
return synced;
});
}
@@ -1100,6 +1114,8 @@ ReplSetTest.prototype.start = function( n , options , restart ){
var lockFile = this.getPath( n ) + "/mongod.lock";
removeFile( lockFile );
var o = this.getOptions( n , options , restart );
+
+ print("Starting....");
print( o );
if ( restart )
return startMongoProgram.apply( null , o );
@@ -1109,12 +1125,13 @@ ReplSetTest.prototype.start = function( n , options , restart ){
}
ReplSetTest.prototype.stop = function( n , signal ){
- print('*** ' + this.name + " completed successfully ***");
- return stopMongod( this.getPort( n ) , signal || 15 );
+ var port = this.getPort( n );
+ print('*** Shutting down mongod in port ' + port + ' ***');
+ return stopMongod( port , signal || 15 );
}
-ReplSetTest.prototype.stopSet = function() {
- for(i=0; i<this.ports.length; i++) {
- this.stop(n);
+ReplSetTest.prototype.stopSet = function( signal ) {
+ for(i=0; i < this.ports.length; i++) {
+ this.stop( i, signal );
}
}