diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2014-10-15 10:50:05 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2014-10-28 11:49:33 -0400 |
commit | a0e5fbf5e64bd98be827cd11a1d77ade9b5b3128 (patch) | |
tree | e1905267a39d676ff8ff5af8252165f0a32c1fd5 /jstests | |
parent | 17917954b999b689cac37695e2885b2d35be9014 (diff) | |
download | mongo-a0e5fbf5e64bd98be827cd11a1d77ade9b5b3128.tar.gz |
SERVER-15836 Clean up several rollback js tests.
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/replsets/auth3.js | 93 | ||||
-rw-r--r-- | jstests/replsets/rollback.js | 23 | ||||
-rw-r--r-- | jstests/replsets/rollback2.js | 7 | ||||
-rwxr-xr-x | jstests/replsets/rollback3.js | 7 | ||||
-rw-r--r-- | jstests/replsets/rslib.js | 51 |
5 files changed, 104 insertions, 77 deletions
diff --git a/jstests/replsets/auth3.js b/jstests/replsets/auth3.js index 8218dc12e60..1cccd8a4d4b 100644 --- a/jstests/replsets/auth3.js +++ b/jstests/replsets/auth3.js @@ -1,66 +1,57 @@ -var path = "jstests/libs/"; - -var rs = new ReplSetTest({"nodes" : {node0 : {}, node1 : {}, arbiter : {}}, keyFile : path+"key1"}); -rs.startSet(); -rs.initiate(); - -master = rs.getMaster(); -print("adding user"); -master.getDB("admin").createUser({user: "foo", pwd: "bar", roles: jsTest.adminUserRoles}, - {w: 2, wtimeout: 30000}); - -var checkValidState = function(i) { - assert.soon(function() { - var result = rs.nodes[i].getDB("admin").runCommand({isMaster : 1}); - printjson(result); - return result.secondary || result.ismaster; +(function () { + "use strict"; + var keyfile = "jstests/libs/key1"; + var master; + var rs = new ReplSetTest({ + nodes : { node0 : {}, node1 : {}, arbiter : {}}, + keyFile : keyfile }); -}; + rs.startSet(); + rs.initiate(); -var safeInsert = function() { master = rs.getMaster(); - master.getDB("admin").auth("foo", "bar"); - assert.writeOK(master.getDB("foo").bar.insert({ x: 1 })); -} + jsTest.log("adding user"); + master.getDB("admin").createUser({user: "foo", pwd: "bar", roles: jsTest.adminUserRoles}, + {w: 2, wtimeout: 30000}); + + var safeInsert = function() { + master = rs.getMaster(); + master.getDB("admin").auth("foo", "bar"); + assert.writeOK(master.getDB("foo").bar.insert({ x: 1 })); + } -print("authing"); -assert.soon(function() { + jsTest.log("authing"); for (var i=0; i<2; i++) { - checkValidState(i); - - // if this is run before initial sync finishes, we won't be logged in - var res = rs.nodes[i].getDB("admin").auth("foo", "bar"); - if (res != 1) { - print("couldn't log into "+rs.nodes[i].host); - return false; - } + assert(rs.nodes[i].getDB("admin").auth("foo", "bar"), + "could not log into " + rs.nodes[i].host); } - return true; -}); -print("make common point"); + jsTest.log("make common point"); + + safeInsert(); + authutil.asCluster(rs.nodes, keyfile, function() { rs.awaitReplication(); }); -safeInsert(); -authutil.asCluster(rs.nodes, 'jstests/libs/key1', function() { rs.awaitReplication(); }); + jsTest.log("write stuff to 0&2") + rs.stop(1); -print("write stuff to 0&2") -rs.stop(1); + master = rs.getMaster(); + master.getDB("admin").auth("foo", "bar"); + master.getDB("foo").bar.drop(); + jsTest.log("last op: " + + tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next())); -master = rs.getMaster(); -master.getDB("admin").auth("foo", "bar"); -master.getDB("foo").bar.drop(); -print("last op: "+tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next())); + jsTest.log("write stuff to 1&2") + rs.stop(0); + rs.restart(1); -print("write stuff to 1&2") -rs.stop(0); -rs.restart(1); + safeInsert(); + jsTest.log("last op: " + + tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next())); -safeInsert(); -print("last op: "+tojson(master.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next())); + rs.restart(0); -rs.restart(0); + jsTest.log("doing rollback!"); -print("doing rollback!"); + authutil.asCluster(rs.nodes, keyfile, function () { rs.awaitSecondaryNodes(); }); -checkValidState(0); -checkValidState(1); +}()); diff --git a/jstests/replsets/rollback.js b/jstests/replsets/rollback.js index f741b9b1df0..38a3a495875 100644 --- a/jstests/replsets/rollback.js +++ b/jstests/replsets/rollback.js @@ -17,6 +17,7 @@ * Finally, we expect either A or B to roll back its 3 divergent documents and acquire the other * node's. */ +load("jstests/replsets/rslib.js"); (function () { "use strict"; @@ -33,7 +34,7 @@ var replTest = new ReplSetTest({ name: 'unicomplex', nodes: 3, oplogSize: 1 }); var nodes = replTest.nodeList(); - //print(tojson(nodes)); + //jsTest.log(tojson(nodes)); var conns = replTest.startSet(); var r = replTest.initiate({ "_id": "unicomplex", @@ -61,7 +62,7 @@ /* force the oplog to roll */ if (new Date() % 2 == 0) { - print("ROLLING OPLOG AS PART OF TEST (we only do this sometimes)"); + jsTest.log("ROLLING OPLOG AS PART OF TEST (we only do this sometimes)"); var pass = 1; var first = a.getSisterDB("local").oplog.rs.find().sort({ $natural: 1 }).limit(1)[0]; a.roll.insert({ x: 1 }); @@ -81,10 +82,10 @@ } pass++; } - print("PASSES FOR OPLOG ROLL: " + pass); + jsTest.log("PASSES FOR OPLOG ROLL: " + pass); } else { - print("NO ROLL"); + jsTest.log("NO ROLL"); } assert.writeOK(a.bar.insert({ q: 1, a: "foo" })); @@ -105,27 +106,29 @@ // a should not have the new data as it was partitioned. replTest.partition(1, 2); - print("*************** wait for server to reconnect ****************"); + jsTest.log("*************** wait for server to reconnect ****************"); replTest.unPartition(0, 2); - print("*************** B ****************"); + jsTest.log("*************** B ****************"); assert.soon(function () { try { return !B.isMaster().ismaster; } catch(e) { return false; } }); - print("*************** A ****************"); + jsTest.log("*************** A ****************"); assert.soon(function () { try { return A.isMaster().ismaster; } catch(e) { return false; } }); assert(a.bar.count() == 3, "t is 3"); - a.bar.insert({ q: 7 }); - a.bar.insert({ q: 8 }); + assert.writeOK(a.bar.insert({ q: 7 })); + assert.writeOK(a.bar.insert({ q: 8 })); + // A is 1 2 3 7 8 // B is 1 2 3 4 5 6 // bring B back online replTest.unPartition(0, 1); replTest.unPartition(1, 2); + + awaitOpTime(b.getMongo(), getLatestOp(a_conn).ts); replTest.awaitReplication(); checkFinalResults(a); checkFinalResults(b); replTest.stopSet(15); }()); - diff --git a/jstests/replsets/rollback2.js b/jstests/replsets/rollback2.js index 301ca7a8792..9fb10f730c0 100644 --- a/jstests/replsets/rollback2.js +++ b/jstests/replsets/rollback2.js @@ -11,6 +11,7 @@ * 6. B rejoins the set and goes through the rollback process. * 7. The contents of A and B are compare to ensure the rollback results in consistent nodes. */ +load("jstests/replsets/rslib.js"); (function () { "use strict"; @@ -105,9 +106,9 @@ // put B back in contact with A and arbiter, as A is primary, B will rollback and then catch up replTest.unPartition(1, 2); replTest.unPartition(0, 1); - - assert.soon(function () { try { return B.isMaster().secondary; } catch(e) { return false; } }); - + + awaitOpTime(b.getMongo(), getLatestOp(a_conn).ts); + // await steady state and ensure the two nodes have the same contents replTest.awaitReplication(); checkFinalResults(a); diff --git a/jstests/replsets/rollback3.js b/jstests/replsets/rollback3.js index e54778f8b71..959dad91955 100755 --- a/jstests/replsets/rollback3.js +++ b/jstests/replsets/rollback3.js @@ -11,6 +11,7 @@ * 6. B rejoins the set and goes through the rollback process. * 7. The contents of A and B are compare to ensure the rollback results in consistent nodes. */ +load("jstests/replsets/rslib.js"); (function () { "use strict"; @@ -128,9 +129,9 @@ // put B back in contact with A and arbiter, as A is primary, B will rollback and then catch up replTest.unPartition(1, 2); replTest.unPartition(0, 1); - - assert.soon(function () { try { return B.isMaster().secondary; } catch(e) { return false; } }); - + + awaitOpTime(b.getMongo(), getLatestOp(a_conn).ts); + // await steady state and ensure the two nodes have the same contents replTest.awaitReplication(); checkFinalResults(a); diff --git a/jstests/replsets/rslib.js b/jstests/replsets/rslib.js index e6c557d7255..f926640e2c5 100644 --- a/jstests/replsets/rslib.js +++ b/jstests/replsets/rslib.js @@ -1,8 +1,10 @@ - +var wait, occasionally, reconnect, getLatestOp, waitForAllMembers, reconfig, awaitOpTime; +(function () { +"use strict"; var count = 0; var w = 0; -var wait = function(f,msg) { +wait = function(f,msg) { w++; var n = 0; while (!f()) { @@ -25,16 +27,17 @@ var wait = function(f,msg) { * } * </pre> */ -var occasionally = function(f, n) { +occasionally = function(f, n) { var interval = n || 4; if (count % interval == 0) { f(); } count++; }; - -var reconnect = function(a) { - wait(function() { + +reconnect = function(a) { + wait(function() { + var db; try { // make this work with either dbs or connections if (typeof(a.getDB) == "function") { @@ -56,7 +59,7 @@ var reconnect = function(a) { }; -var getLatestOp = function(server) { +getLatestOp = function(server) { server.getDB("admin").getMongo().setSlaveOk(); var log = server.getDB("local")['oplog.rs']; var cursor = log.find({}).sort({'$natural': -1}).limit(1); @@ -67,7 +70,7 @@ var getLatestOp = function(server) { }; -var waitForAllMembers = function(master, timeout) { +waitForAllMembers = function(master, timeout) { var failCount = 0; assert.soon( function() { @@ -97,7 +100,7 @@ var waitForAllMembers = function(master, timeout) { print( "All members are now in state PRIMARY, SECONDARY, or ARBITER" ); }; -var reconfig = function(rs, config) { +reconfig = function(rs, config) { "use strict"; var admin = rs.getMaster().getDB("admin"); var e; @@ -111,8 +114,36 @@ var reconfig = function(rs, config) { } } - master = rs.getMaster().getDB("admin"); + var master = rs.getMaster().getDB("admin"); waitForAllMembers(master); return master; }; + +awaitOpTime = function (node, opTime) { + var ts, ex; + assert.soon(function () { + try { + // The following statement extracts the timestamp field from the most recent element of + // the oplog, and stores it in "ts". + ts = node.getDB("local")['oplog.rs'].find({}).sort({'$natural': -1}).limit(1).next().ts; + if ((ts.t == opTime.t) && (ts.i == opTime.i)) { + return true; + } + ex = null; + return false; + } + catch (ex) { + return false; + } + }, function () { + var message = "Node " + node + " only reached optime " + tojson(ts) + " not " + + tojson(opTime); + if (ex) { + message += "; last attempt failed with exception " + tojson(ex); + } + return message; + }); +}; + +}()); |