summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2017-10-18 15:56:34 -0400
committerBenety Goh <benety@mongodb.com>2017-11-17 13:18:45 -0500
commit8c2ece023718fd8b23629a55392399dae0be1581 (patch)
treee9889898774934f20647604f3005fb579be9b4dd
parent657bd4c2285367278f43992a33942268ebf6919e (diff)
downloadmongo-8c2ece023718fd8b23629a55392399dae0be1581.tar.gz
SERVER-31587 limit scope of OperationContext in ReplBatcher
(cherry picked from commit 29140e49080de0c25482a2b13d1cc51c7b0096bf)
-rw-r--r--src/mongo/db/repl/sync_tail.cpp47
1 files changed, 34 insertions, 13 deletions
diff --git a/src/mongo/db/repl/sync_tail.cpp b/src/mongo/db/repl/sync_tail.cpp
index 6c4d88132eb..0c7c9ca3e76 100644
--- a/src/mongo/db/repl/sync_tail.cpp
+++ b/src/mongo/db/repl/sync_tail.cpp
@@ -627,28 +627,49 @@ public:
}
private:
+ /**
+ * Calculates batch limit size (in bytes) using the maximum capped collection size of the oplog
+ * size.
+ * Batches are limited to 10% of the oplog.
+ */
+ std::size_t _calculateBatchLimitBytes() {
+ auto opCtx = cc().makeOperationContext();
+ auto oplogMaxSizeResult =
+ _storageInterface->getOplogMaxSize(opCtx.get(), NamespaceString(rsOplogName));
+ auto oplogMaxSize = fassertStatusOK(40301, oplogMaxSizeResult);
+ return std::min(oplogMaxSize / 10, std::size_t(replBatchLimitBytes));
+ }
+
+ /**
+ * If slaveDelay is enabled, this function calculates the most recent timestamp of any oplog
+ * entries that can be be returned in a batch.
+ */
+ boost::optional<Date_t> _calculateSlaveDelayLatestTimestamp() {
+ auto service = cc().getServiceContext();
+ auto replCoord = ReplicationCoordinator::get(service);
+ auto slaveDelay = replCoord->getSlaveDelaySecs();
+ if (slaveDelay <= Seconds(0)) {
+ return {};
+ }
+ return Date_t::now() - slaveDelay;
+ }
+
void run() {
Client::initThread("ReplBatcher");
- OperationContextImpl txn;
- auto replCoord = ReplicationCoordinator::get(&txn);
- const auto oplogMaxSize = fassertStatusOK(
- 40301, _storageInterface->getOplogMaxSize(&txn, NamespaceString(rsOplogName)));
-
- // Batches are limited to 10% of the oplog.
BatchLimits batchLimits;
batchLimits.ops = replBatchLimitOperations;
- batchLimits.bytes = std::min(oplogMaxSize / 10, size_t(replBatchLimitBytes));
+ batchLimits.bytes = _calculateBatchLimitBytes();
while (!_inShutdown.load()) {
- const auto slaveDelay = replCoord->getSlaveDelaySecs();
- batchLimits.slaveDelayLatestTimestamp = (slaveDelay > Seconds(0))
- ? (Date_t::now() - slaveDelay)
- : boost::optional<Date_t>();
+ batchLimits.slaveDelayLatestTimestamp = _calculateSlaveDelayLatestTimestamp();
OpQueue ops;
// tryPopAndWaitForMore adds to ops and returns true when we need to end a batch early.
- while (!_inShutdown.load() &&
- !_syncTail->tryPopAndWaitForMore(&txn, &ops, batchLimits)) {
+ {
+ auto opCtx = cc().makeOperationContext();
+ while (!_inShutdown.load() &&
+ !_syncTail->tryPopAndWaitForMore(opCtx.get(), &ops, batchLimits)) {
+ }
}
// For pausing replication in tests