Skip to content

Commit c00bee5

Browse files
benetyevergreen
authored andcommitted
SERVER-39484 IndexBuildsCoordinator receives notifications on rollback/step up to primary
This partially restores some of the replication state transition functions removed in commit adab670.
1 parent 4706c3b commit c00bee5

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

src/mongo/db/index_builds_coordinator.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,29 @@ void logFailure(Status status,
160160
<< replState->collectionUUID << " ): " << status;
161161
}
162162

163+
/**
164+
* Iterates over index builds with the provided function.
165+
*/
166+
void forEachIndexBuild(
167+
const std::vector<std::shared_ptr<ReplIndexBuildState>>& indexBuilds,
168+
StringData logPrefix,
169+
std::function<void(std::shared_ptr<ReplIndexBuildState> replState)> onIndexBuild) {
170+
if (indexBuilds.empty()) {
171+
return;
172+
}
173+
174+
log() << logPrefix << "active index builds: " << indexBuilds.size();
175+
176+
for (auto replState : indexBuilds) {
177+
std::string indexNamesStr;
178+
str::joinStringDelim(replState->indexNames, &indexNamesStr, ',');
179+
log() << logPrefix << replState->buildUUID << ": collection: " << replState->collectionUUID
180+
<< "; indexes: " << replState->indexNames.size() << " [" << indexNamesStr << "]";
181+
182+
onIndexBuild(replState);
183+
}
184+
}
185+
163186
} // namespace
164187

165188
const auto getIndexBuildsCoord =
@@ -398,6 +421,21 @@ void IndexBuildsCoordinator::abortIndexBuildByBuildUUID(OperationContext* opCtx,
398421
}
399422
}
400423

424+
void IndexBuildsCoordinator::onStepUp(OperationContext* opCtx) {
425+
log() << "IndexBuildsCoordinator::onStepUp - this node is stepping up to primary";
426+
427+
auto indexBuilds = _getIndexBuilds();
428+
auto onIndexBuild = [](std::shared_ptr<ReplIndexBuildState> replState) {};
429+
forEachIndexBuild(indexBuilds, "IndexBuildsCoordinator::onStepUp - "_sd, onIndexBuild);
430+
}
431+
432+
void IndexBuildsCoordinator::onRollback(OperationContext* opCtx) {
433+
log() << "IndexBuildsCoordinator::onRollback - this node is entering the rollback state";
434+
auto indexBuilds = _getIndexBuilds();
435+
auto onIndexBuild = [](std::shared_ptr<ReplIndexBuildState> replState) {};
436+
forEachIndexBuild(indexBuilds, "IndexBuildsCoordinator::onRollback - "_sd, onIndexBuild);
437+
}
438+
401439
void IndexBuildsCoordinator::recoverIndexBuilds() {
402440
// TODO: not yet implemented.
403441
}
@@ -1305,6 +1343,17 @@ StatusWith<std::shared_ptr<ReplIndexBuildState>> IndexBuildsCoordinator::_getInd
13051343
return it->second;
13061344
}
13071345

1346+
std::vector<std::shared_ptr<ReplIndexBuildState>> IndexBuildsCoordinator::_getIndexBuilds() const {
1347+
std::vector<std::shared_ptr<ReplIndexBuildState>> indexBuilds;
1348+
{
1349+
stdx::unique_lock<Latch> lk(_mutex);
1350+
for (auto pair : _allIndexBuilds) {
1351+
indexBuilds.push_back(pair.second);
1352+
}
1353+
}
1354+
return indexBuilds;
1355+
}
1356+
13081357
ScopedStopNewDatabaseIndexBuilds::ScopedStopNewDatabaseIndexBuilds(
13091358
IndexBuildsCoordinator* indexBuildsCoordinator, StringData dbName)
13101359
: _indexBuildsCoordinatorPtr(indexBuildsCoordinator), _dbName(dbName.toString()) {

src/mongo/db/index_builds_coordinator.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ class IndexBuildsCoordinator {
210210
const UUID& buildUUID,
211211
const std::string& reason);
212212

213+
/**
214+
* Invoked when the node enters the primary state.
215+
* Unblocks index builds that have been waiting to commit/abort during the secondary state.
216+
*/
217+
void onStepUp(OperationContext* opCtx);
218+
219+
/**
220+
* Invoked when the node enters the rollback state.
221+
* Unblocks index builds that have been waiting to commit/abort during the secondary state.
222+
*/
223+
void onRollback(OperationContext* opCtx);
224+
213225
/**
214226
* TODO: This is not yet implemented.
215227
*/
@@ -418,6 +430,12 @@ class IndexBuildsCoordinator {
418430
*/
419431
StatusWith<std::shared_ptr<ReplIndexBuildState>> _getIndexBuild(const UUID& buildUUID) const;
420432

433+
/**
434+
* Returns a snapshot of active index builds. Since each index build state is reference counted,
435+
* it is fine to examine the returned index builds without re-locking 'mutex'.
436+
*/
437+
std::vector<std::shared_ptr<ReplIndexBuildState>> _getIndexBuilds() const;
438+
421439
// Protects the below state.
422440
mutable Mutex _mutex = MONGO_MAKE_LATCH("IndexBuildsCoordinator::_mutex");
423441

src/mongo/db/repl/SConscript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ env.Library(
128128
'$BUILD_DIR/mongo/client/connection_pool',
129129
'$BUILD_DIR/mongo/client/fetcher',
130130
'$BUILD_DIR/mongo/db/concurrency/write_conflict_exception',
131+
'$BUILD_DIR/mongo/db/index_builds_coordinator_interface',
131132
'$BUILD_DIR/mongo/db/service_context',
132133
'$BUILD_DIR/mongo/util/concurrency/thread_pool',
133134
],
@@ -1197,6 +1198,7 @@ env.Library(
11971198
'repl_server_parameters',
11981199
'$BUILD_DIR/mongo/db/commands/mongod_fcv',
11991200
'$BUILD_DIR/mongo/db/commands/test_commands_enabled',
1201+
'$BUILD_DIR/mongo/db/index_builds_coordinator_interface',
12001202
],
12011203
)
12021204

src/mongo/db/repl/bgsync.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "mongo/db/concurrency/replication_state_transition_lock_guard.h"
4646
#include "mongo/db/concurrency/write_conflict_exception.h"
4747
#include "mongo/db/dbhelpers.h"
48+
#include "mongo/db/index_builds_coordinator.h"
4849
#include "mongo/db/repl/data_replicator_external_state_impl.h"
4950
#include "mongo/db/repl/oplog.h"
5051
#include "mongo/db/repl/oplog_interface_local.h"
@@ -633,6 +634,8 @@ void BackgroundSync::_runRollback(OperationContext* opCtx,
633634
// are visible before potentially truncating the oplog.
634635
storageInterface->waitForAllEarlierOplogWritesToBeVisible(opCtx);
635636

637+
IndexBuildsCoordinator::get(opCtx)->onRollback(opCtx);
638+
636639
auto storageEngine = opCtx->getServiceContext()->getStorageEngine();
637640
if (!forceRollbackViaRefetch.load() && storageEngine->supportsRecoverToStableTimestamp()) {
638641
log() << "Rollback using 'recoverToStableTimestamp' method.";

src/mongo/db/repl/replication_coordinator_external_state_impl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "mongo/db/dbdirectclient.h"
5454
#include "mongo/db/dbhelpers.h"
5555
#include "mongo/db/free_mon/free_mon_mongod.h"
56+
#include "mongo/db/index_builds_coordinator.h"
5657
#include "mongo/db/jsobj.h"
5758
#include "mongo/db/kill_sessions_local.h"
5859
#include "mongo/db/logical_clock.h"
@@ -468,6 +469,8 @@ OpTime ReplicationCoordinatorExternalStateImpl::onTransitionToPrimary(OperationC
468469

469470
_dropAllTempCollections(opCtx);
470471

472+
IndexBuildsCoordinator::get(opCtx)->onStepUp(opCtx);
473+
471474
notifyFreeMonitoringOnTransitionToPrimary();
472475

473476
// It is only necessary to check the system indexes on the first transition to master.

0 commit comments

Comments
 (0)