diff options
author | Benety Goh <benety@mongodb.com> | 2020-10-16 10:13:09 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-10-16 14:55:14 +0000 |
commit | 401d1f1a28dd94bd96de9dc733cd61184661ec15 (patch) | |
tree | bb4e76627117bf00d2fa957fe8f4a8bfd0740fa3 /src/mongo/db/repl_index_build_state.cpp | |
parent | b8017175e3ac2c6d37f60d3f4cd2efb5011d1a32 (diff) | |
download | mongo-401d1f1a28dd94bd96de9dc733cd61184661ec15.tar.gz |
SERVER-46995 convert ReplIndexBuildState to class and un-inline. add methods for resumable index builds
Diffstat (limited to 'src/mongo/db/repl_index_build_state.cpp')
-rw-r--r-- | src/mongo/db/repl_index_build_state.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/mongo/db/repl_index_build_state.cpp b/src/mongo/db/repl_index_build_state.cpp new file mode 100644 index 00000000000..a7617172312 --- /dev/null +++ b/src/mongo/db/repl_index_build_state.cpp @@ -0,0 +1,125 @@ +/** + * Copyright (C) 2020-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/db/repl_index_build_state.h" + +namespace mongo { + +namespace { + +/** + * Parses index specs to generate list of index names for ReplIndexBuildState initialization. + */ +std::vector<std::string> extractIndexNames(const std::vector<BSONObj>& specs) { + std::vector<std::string> indexNames; + for (const auto& spec : specs) { + std::string name = spec.getStringField(IndexDescriptor::kIndexNameFieldName); + invariant(!name.empty(), + str::stream() << "Bad spec passed into ReplIndexBuildState constructor, missing '" + << IndexDescriptor::kIndexNameFieldName << "' field: " << spec); + indexNames.push_back(name); + } + return indexNames; +} + +/** + * Returns true if the requested IndexBuildState transition is allowed. + */ +bool checkIfValidTransition(IndexBuildState::StateFlag currentState, + IndexBuildState::StateFlag newState) { + if ((currentState == IndexBuildState::StateFlag::kSetup && + newState == IndexBuildState::StateFlag::kInProgress) || + (currentState == IndexBuildState::StateFlag::kInProgress && + newState != IndexBuildState::StateFlag::kSetup) || + (currentState == IndexBuildState::StateFlag::kPrepareCommit && + newState == IndexBuildState::StateFlag::kCommitted)) { + return true; + } + return false; +} + +} // namespace + +void IndexBuildState::setState(StateFlag state, + bool skipCheck, + boost::optional<Timestamp> timestamp, + boost::optional<std::string> abortReason) { + if (!skipCheck) { + invariant(checkIfValidTransition(_state, state), + str::stream() << "current state :" << toString(_state) + << ", new state: " << toString(state)); + } + _state = state; + if (timestamp) + _timestamp = timestamp; + if (abortReason) { + invariant(_state == kAborted); + _abortReason = abortReason; + } +} + +ReplIndexBuildState::ReplIndexBuildState(const UUID& indexBuildUUID, + const UUID& collUUID, + const std::string& dbName, + const std::vector<BSONObj>& specs, + IndexBuildProtocol protocol) + : buildUUID(indexBuildUUID), + collectionUUID(collUUID), + dbName(dbName), + indexNames(extractIndexNames(specs)), + indexSpecs(specs), + protocol(protocol) { + waitForNextAction = std::make_unique<SharedPromise<IndexBuildAction>>(); + if (protocol == IndexBuildProtocol::kTwoPhase) + commitQuorumLock.emplace(indexBuildUUID.toString()); +} + +bool ReplIndexBuildState::isResumable() const { + stdx::unique_lock<Latch> lk(mutex); + return !_lastOpTimeBeforeInterceptors.isNull(); +} + +repl::OpTime ReplIndexBuildState::getLastOpTimeBeforeInterceptors() const { + stdx::unique_lock<Latch> lk(mutex); + return _lastOpTimeBeforeInterceptors; +} + +void ReplIndexBuildState::setLastOpTimeBeforeInterceptors(repl::OpTime opTime) { + stdx::unique_lock<Latch> lk(mutex); + _lastOpTimeBeforeInterceptors = std::move(opTime); +} + +void ReplIndexBuildState::clearLastOpTimeBeforeInterceptors() { + stdx::unique_lock<Latch> lk(mutex); + _lastOpTimeBeforeInterceptors = {}; +} + +} // namespace mongo |