summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/move_primary_source_manager.h
diff options
context:
space:
mode:
authorBlake Oler <blake.oler@mongodb.com>2018-03-13 16:09:21 -0400
committerBlake Oler <blake.oler@mongodb.com>2018-03-19 16:53:06 -0400
commit30314ef38819701600a5617390fe3721e2b9dbdf (patch)
tree5a3947ec5451f6d5a0852a906a479b6a00f0cc3a /src/mongo/db/s/move_primary_source_manager.h
parent33da87d9b0f5dd6f1b2c8d454b22345a1d0f1d4e (diff)
downloadmongo-30314ef38819701600a5617390fe3721e2b9dbdf.tar.gz
SERVER-33206 Create MovePrimarySourceManager and add clone command
Diffstat (limited to 'src/mongo/db/s/move_primary_source_manager.h')
-rw-r--r--src/mongo/db/s/move_primary_source_manager.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/mongo/db/s/move_primary_source_manager.h b/src/mongo/db/s/move_primary_source_manager.h
new file mode 100644
index 00000000000..4c9eb5a63c9
--- /dev/null
+++ b/src/mongo/db/s/move_primary_source_manager.h
@@ -0,0 +1,146 @@
+/**
+ * Copyright (C) 2018 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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.
+ */
+
+#pragma once
+
+#include "mongo/base/disallow_copying.h"
+#include "mongo/db/s/database_sharding_state.h"
+#include "mongo/s/request_types/move_primary_gen.h"
+#include "mongo/util/timer.h"
+
+namespace mongo {
+
+class OperationContext;
+class Shard;
+struct ShardingStatistics;
+class Status;
+
+/**
+ * The donor-side movePrimary state machine. This object must be created and owned by a single
+ * thread, which controls its lifetime and should not be passed across threads. Unless explicitly
+ * indicated its methods must not be called from more than one thread and must not be called while
+ * any locks are held.
+ *
+ * The intended workflow is as follows:
+ * - Acquire a distributed lock on the database whose primary is about to be moved.
+ * - Instantiate a MovePrimarySourceManager on the stack.
+ * - Call clone to start and finish cloning of the unsharded collections.
+ * - Call updatePrimary to indicate the new primary in the config server metadata.
+ * - Call clearStaleCollections to drop now-unused collections (and potentially databases) on the
+ * old primary.
+ *
+ * At any point in time it is safe to let the MovePrimarySourceManager object go out of scope in
+ * which case the destructor will take care of clean up based on how far we have advanced.
+ */
+class MovePrimarySourceManager {
+ MONGO_DISALLOW_COPYING(MovePrimarySourceManager);
+
+public:
+ /**
+ * Instantiates a new movePrimary source manager. Must be called with the distributed lock
+ * acquired in advance (not asserted).
+ *
+ * May throw any exception. Known exceptions (TODO) are:
+ * - InvalidOptions if the operation context is missing database version
+ * - StaleConfigException if the expected database version does not match what we find it
+ * to be after acquiring the distributed lock.
+ */
+
+ MovePrimarySourceManager(OperationContext* opCtx,
+ ShardMovePrimary requestArgs,
+ StringData dbname,
+ std::unique_ptr<Shard> fromShard,
+ std::unique_ptr<Shard> toShard);
+ ~MovePrimarySourceManager();
+
+ /**
+ * Returns the namespace for which this source manager is active.
+ */
+ NamespaceString getNss() const;
+
+ /**
+ * Contacts the recipient shard and tells it to start cloning the specified chunk. This method
+ * will fail if for any reason the recipient shard fails to complete the cloning sequence.
+ *
+ * Expected state: kCreated
+ * Resulting state: kCloning on success, kDone on failure
+ */
+ Status clone(OperationContext* opCtx);
+
+ /**
+ * May be called at any time. Unregisters the movePrimary source manager from the database and
+ * logs an error in the change log to indicate that the migration has failed.
+ *
+ * Expected state: Any
+ * Resulting state: kDone
+ */
+ void cleanupOnError(OperationContext* opCtx);
+
+private:
+ static BSONObj _buildMoveLogEntry(const std::string& db,
+ const std::string& from,
+ const std::string& to) {
+ BSONObjBuilder details;
+ details.append("database", db);
+ details.append("from", from);
+ details.append("to", to);
+
+ return details.obj();
+ }
+
+ // Used to track the current state of the source manager. See the methods above, which have
+ // comments explaining the various state transitions.
+ // TODO: Will tweak these as further work on moving movePrimary to shards
+ enum State { kCreated, kCloning, kCloneCompleted, kCriticalSection, kDone };
+
+ /**
+ * Called when any of the states fails. May only be called once and will put the migration
+ * manager into the kDone state.
+ */
+ void _cleanup(OperationContext* opCtx);
+
+ // The parameters to the movePrimary command
+ const ShardMovePrimary _requestArgs;
+
+ // The database whose primary we are moving.
+ const StringData _dbname;
+
+ // The donor shard
+ const std::unique_ptr<Shard> _fromShard;
+
+ // The recipient shard
+ const std::unique_ptr<Shard> _toShard;
+
+ // Times the entire movePrimary operation
+ const Timer _entireOpTimer;
+
+ // The current state. Used only for diagnostics and validation.
+ State _state{kCreated};
+};
+
+} // namespace mongo