diff --git a/jstests/replsets/optime.js b/jstests/replsets/optime.js index 3fd9aff2139..6d44c198216 100644 --- a/jstests/replsets/optime.js +++ b/jstests/replsets/optime.js @@ -59,7 +59,11 @@ function optimesAndWallTimesAreEqual(replTest, isPersistent) { var replTest = new ReplSetTest( {name: "replStatus", nodes: 3, oplogSize: 1, waitForKeys: true, nodeOptions: {syncdelay: 1}}); -replTest.startSet(); +const nodes = replTest.startSet(); + +// Tests that serverStatus oplog returns an error if the oplog collection doesn't exist. +assert.commandFailedWithCode(nodes[0].getDB('admin').serverStatus({oplog: true}), 17347); + replTest.initiate(); var master = replTest.getPrimary(); replTest.awaitReplication(); diff --git a/src/mongo/db/repl/replication_info.cpp b/src/mongo/db/repl/replication_info.cpp index 4856df7b512..be9c9023008 100644 --- a/src/mongo/db/repl/replication_info.cpp +++ b/src/mongo/db/repl/replication_info.cpp @@ -268,21 +268,27 @@ public: // TODO(siyuan) Output term of OpTime result.append("latestOptime", replCoord->getMyLastAppliedOpTime().getTimestamp()); - AutoGetOplog oplogRead(opCtx, OplogAccessMode::kRead); - auto earliestOplogTimestampFetch = - oplogRead.getCollection()->getRecordStore()->getEarliestOplogTimestamp(opCtx); - Timestamp earliestOplogTimestamp; - if (earliestOplogTimestampFetch.isOK()) { - earliestOplogTimestamp = earliestOplogTimestampFetch.getValue(); - } else { + auto earliestOplogTimestampFetch = [&] { + AutoGetOplog oplogRead(opCtx, OplogAccessMode::kRead); + if (!oplogRead.getCollection()) { + return StatusWith(ErrorCodes::NamespaceNotFound, "oplog doesn't exist"); + } + return oplogRead.getCollection()->getRecordStore()->getEarliestOplogTimestamp(opCtx); + }(); + + if (earliestOplogTimestampFetch.getStatus() == ErrorCodes::OplogOperationUnsupported) { + // Falling back to use getSingleton if the storage engine does not support + // getEarliestOplogTimestamp. BSONObj o; - uassert( - 17347, - "Problem reading earliest entry from oplog", - Helpers::getSingleton(opCtx, NamespaceString::kRsOplogNamespace.ns().c_str(), o)); - earliestOplogTimestamp = o["ts"].timestamp(); + if (Helpers::getSingleton(opCtx, NamespaceString::kRsOplogNamespace.ns().c_str(), o)) { + earliestOplogTimestampFetch = o["ts"].timestamp(); + } } - result.append("earliestOptime", earliestOplogTimestamp); + + uassert( + 17347, "Problem reading earliest entry from oplog", earliestOplogTimestampFetch.isOK()); + result.append("earliestOptime", earliestOplogTimestampFetch.getValue()); + return result.obj(); } } oplogInfoServerStatus;