diff options
-rw-r--r-- | jstests/replsets/initial_sync_oplog_rollover.js | 6 | ||||
-rw-r--r-- | jstests/replsets/rslib.js | 24 | ||||
-rw-r--r-- | jstests/replsets/too_stale_secondary.js | 4 |
3 files changed, 26 insertions, 8 deletions
diff --git a/jstests/replsets/initial_sync_oplog_rollover.js b/jstests/replsets/initial_sync_oplog_rollover.js index 0d8e60e5501..b0f7b10ad9f 100644 --- a/jstests/replsets/initial_sync_oplog_rollover.js +++ b/jstests/replsets/initial_sync_oplog_rollover.js @@ -10,6 +10,8 @@ (function() { "use strict"; +load("jstests/replsets/rslib.js"); + var name = 'initial_sync_oplog_rollover'; var replSet = new ReplSetTest({ name: name, @@ -31,10 +33,6 @@ var primary = replSet.getPrimary(); var coll = primary.getDB('test').foo; assert.writeOK(coll.insert({a: 1})); -function getFirstOplogEntry(conn) { - return conn.getDB('local').oplog.rs.find().sort({$natural: 1}).limit(1)[0]; -} - var firstOplogEntry = getFirstOplogEntry(primary); // Add a secondary node but make it hang before copying databases. diff --git a/jstests/replsets/rslib.js b/jstests/replsets/rslib.js index 25bfee53cfb..3b385e70e29 100644 --- a/jstests/replsets/rslib.js +++ b/jstests/replsets/rslib.js @@ -13,6 +13,7 @@ var waitForState; var reInitiateWithoutThrowingOnAbortedMember; var awaitRSClientHosts; var getLastOpTime; +var getFirstOplogEntry; var setLogVerbosity; var stopReplicationAndEnforceNewPrimaryToCatchUp; var setFailPoint; @@ -444,6 +445,29 @@ getLastOpTime = function(conn) { }; /** + * Returns the oldest oplog entry. + */ +getFirstOplogEntry = function(conn) { + let firstEntry; + // The query plan may yield between the cursor establishment and iterating to retrieve the first + // result. During this yield it's possible for the oplog to "roll over" or shrink. This is rare, + // but if these both happen the cursor will be unable to resume after yielding and return a + // "CappedPositionLost" error. This can be safely retried. + assert.soon(() => { + try { + firstEntry = conn.getDB('local').oplog.rs.find().sort({$natural: 1}).limit(1)[0]; + return true; + } catch (e) { + if (e.code == ErrorCodes.CappedPositionLost) { + return false; + } + throw e; + } + }); + return firstEntry; +}; + +/** * Set log verbosity on all given nodes. * e.g. setLogVerbosity(replTest.nodes, { "replication": {"verbosity": 3} }); */ diff --git a/jstests/replsets/too_stale_secondary.js b/jstests/replsets/too_stale_secondary.js index bc382274250..5bde4a3f320 100644 --- a/jstests/replsets/too_stale_secondary.js +++ b/jstests/replsets/too_stale_secondary.js @@ -33,10 +33,6 @@ load('jstests/replsets/rslib.js'); -function getFirstOplogEntry(conn) { - return conn.getDB('local').oplog.rs.find().sort({$natural: 1}).limit(1)[0]; -} - /** * Overflows the oplog of a given node. * |