summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/bgsync.cpp
diff options
context:
space:
mode:
authorMatthew Russotto <matthew.russotto@10gen.com>2020-02-27 10:54:41 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-02-27 16:43:43 +0000
commita9cf638d70ed4377d4d1426c1d7fec1464417a08 (patch)
tree4244788772f1905c479310a3809695e61944bdd8 /src/mongo/db/repl/bgsync.cpp
parent854c689edafd92e198d1cc47175768be1d965e44 (diff)
downloadmongo-a9cf638d70ed4377d4d1426c1d7fec1464417a08.tar.gz
SERVER-39112 Remove 1-second delays when starting and stopping OplogApplier. Speeds up transition from primary drain mode to a writeable primary.
Diffstat (limited to 'src/mongo/db/repl/bgsync.cpp')
-rw-r--r--src/mongo/db/repl/bgsync.cpp23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/mongo/db/repl/bgsync.cpp b/src/mongo/db/repl/bgsync.cpp
index 56081734d67..b728e595684 100644
--- a/src/mongo/db/repl/bgsync.cpp
+++ b/src/mongo/db/repl/bgsync.cpp
@@ -138,7 +138,7 @@ void BackgroundSync::startup(OperationContext* opCtx) {
void BackgroundSync::shutdown(OperationContext* opCtx) {
stdx::lock_guard<Latch> lock(_mutex);
- _state = ProducerState::Stopped;
+ setState(lock, ProducerState::Stopped);
if (_syncSourceResolver) {
_syncSourceResolver->shutdown();
@@ -191,9 +191,13 @@ void BackgroundSync::_run() {
}
void BackgroundSync::_runProducer() {
- if (getState() == ProducerState::Stopped) {
- sleepsecs(1);
- return;
+ {
+ // This wait keeps us from spinning. We will re-check the condition in _produce(), so if
+ // the state changes after we release the lock, the behavior is still correct.
+ stdx::unique_lock<Latch> lk(_mutex);
+ _stateCv.wait(lk, [&]() { return _inShutdown || _state != ProducerState::Stopped; });
+ if (_inShutdown)
+ return;
}
auto memberState = _replCoord->getMemberState();
@@ -696,7 +700,7 @@ void BackgroundSync::clearSyncTarget() {
void BackgroundSync::stop(bool resetLastFetchedOptime) {
stdx::lock_guard<Latch> lock(_mutex);
- _state = ProducerState::Stopped;
+ setState(lock, ProducerState::Stopped);
log() << "Stopping replication producer";
_syncSourceHost = HostAndPort();
@@ -734,7 +738,7 @@ void BackgroundSync::start(OperationContext* opCtx) {
if (!_oplogApplier->getBuffer()->isEmpty()) {
log() << "going to start syncing, but buffer is not empty";
}
- _state = ProducerState::Running;
+ setState(lk, ProducerState::Running);
// When a node steps down during drain mode, the last fetched optime would be newer than
// the last applied.
@@ -798,11 +802,16 @@ BackgroundSync::ProducerState BackgroundSync::getState() const {
return _state;
}
+void BackgroundSync::setState(WithLock, ProducerState newState) {
+ _state = newState;
+ _stateCv.notify_one();
+}
+
void BackgroundSync::startProducerIfStopped() {
stdx::lock_guard<Latch> lock(_mutex);
// Let producer run if it's already running.
if (_state == ProducerState::Stopped) {
- _state = ProducerState::Starting;
+ setState(lock, ProducerState::Starting);
}
}