diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2020-06-16 07:53:22 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-06-21 12:32:58 +0000 |
commit | 33201c068f9ec37196190be083d61f46dc83d470 (patch) | |
tree | 59a30062c8fd5ece6cecc4702c28d2d5417b5b8c /src/mongo/db/s/shard_local.cpp | |
parent | 85707dafe1d2894505fdcf74336f93ed7a5f6636 (diff) | |
download | mongo-33201c068f9ec37196190be083d61f46dc83d470.tar.gz |
SERVER-48775 Move all Sharding unit-tests that assume a MongoD to the db/s directory
With this change there are no more references from mongo/s (which is
code common between MongoS and MongoD) and mongo/db/s (which is code
specific for MongoD only).
Diffstat (limited to 'src/mongo/db/s/shard_local.cpp')
-rw-r--r-- | src/mongo/db/s/shard_local.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/mongo/db/s/shard_local.cpp b/src/mongo/db/s/shard_local.cpp new file mode 100644 index 00000000000..6b18e6fa849 --- /dev/null +++ b/src/mongo/db/s/shard_local.cpp @@ -0,0 +1,190 @@ +/** + * Copyright (C) 2018-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/s/shard_local.h" + +#include "mongo/client/remote_command_targeter.h" +#include "mongo/db/catalog/index_catalog.h" +#include "mongo/db/catalog_raii.h" +#include "mongo/db/concurrency/write_conflict_exception.h" +#include "mongo/db/index/index_descriptor.h" +#include "mongo/db/index_builds_coordinator.h" +#include "mongo/db/repl/repl_client_info.h" +#include "mongo/db/repl/repl_set_config.h" +#include "mongo/db/repl/replication_coordinator.h" +#include "mongo/db/server_options.h" +#include "mongo/util/assert_util.h" +#include "mongo/util/scopeguard.h" + +namespace mongo { + +ShardLocal::ShardLocal(const ShardId& id) : Shard(id), _rsLocalClient() { + // Currently ShardLocal only works for config servers. If we ever start using ShardLocal on + // shards we'll need to consider how to handle shards. + invariant(serverGlobalParams.clusterRole == ClusterRole::ConfigServer); +} + +const ConnectionString ShardLocal::getConnString() const { + return repl::ReplicationCoordinator::get(getGlobalServiceContext()) + ->getConfig() + .getConnectionString(); +} + +std::shared_ptr<RemoteCommandTargeter> ShardLocal::getTargeter() const { + MONGO_UNREACHABLE; +}; + +const ConnectionString ShardLocal::originalConnString() const { + // Return the local connection string here as this method is only used for updating the + // ShardRegistry and we don't need a mapping from hosts in the replica set config to the shard + // for local shards. + return ConnectionString::forLocal(); +} + +void ShardLocal::updateReplSetMonitor(const HostAndPort& remoteHost, + const Status& remoteCommandStatus) { + MONGO_UNREACHABLE; +} + +void ShardLocal::updateLastCommittedOpTime(LogicalTime lastCommittedOpTime) { + MONGO_UNREACHABLE; +} + +LogicalTime ShardLocal::getLastCommittedOpTime() const { + MONGO_UNREACHABLE; +} + +std::string ShardLocal::toString() const { + return getId().toString() + ":<local>"; +} + +bool ShardLocal::isRetriableError(ErrorCodes::Error code, RetryPolicy options) { + if (options == RetryPolicy::kNoRetry) { + return false; + } + + if (options == RetryPolicy::kIdempotent) { + return code == ErrorCodes::WriteConcernFailed; + } else { + invariant(options == RetryPolicy::kNotIdempotent); + return false; + } +} + +StatusWith<Shard::CommandResponse> ShardLocal::_runCommand(OperationContext* opCtx, + const ReadPreferenceSetting& unused, + StringData dbName, + Milliseconds maxTimeMSOverrideUnused, + const BSONObj& cmdObj) { + return _rsLocalClient.runCommandOnce(opCtx, dbName, cmdObj); +} + +StatusWith<Shard::QueryResponse> ShardLocal::_runExhaustiveCursorCommand( + OperationContext* opCtx, + const ReadPreferenceSetting& readPref, + StringData dbName, + Milliseconds maxTimeMSOverride, + const BSONObj& cmdObj) { + MONGO_UNREACHABLE; +} + +StatusWith<Shard::QueryResponse> ShardLocal::_exhaustiveFindOnConfig( + OperationContext* opCtx, + const ReadPreferenceSetting& readPref, + const repl::ReadConcernLevel& readConcernLevel, + const NamespaceString& nss, + const BSONObj& query, + const BSONObj& sort, + boost::optional<long long> limit) { + return _rsLocalClient.queryOnce(opCtx, readPref, readConcernLevel, nss, query, sort, limit); +} + +Status ShardLocal::createIndexOnConfig(OperationContext* opCtx, + const NamespaceString& ns, + const BSONObj& keys, + bool unique) { + invariant(ns.db() == "config" || ns.db() == "admin"); + + try { + AutoGetOrCreateDb autoDb(opCtx, ns.db(), MODE_IX); + AutoGetCollection autoColl(opCtx, ns, MODE_X); + auto collection = autoColl.getCollection(); + if (!collection) { + CollectionOptions options; + options.uuid = UUID::gen(); + writeConflictRetry(opCtx, "ShardLocal::createIndexOnConfig", ns.ns(), [&] { + WriteUnitOfWork wunit(opCtx); + auto db = autoDb.getDb(); + collection = db->createCollection(opCtx, ns, options); + invariant(collection, + str::stream() << "Failed to create collection " << ns.ns() + << " in config database for indexes: " << keys); + wunit.commit(); + }); + } + auto indexCatalog = collection->getIndexCatalog(); + IndexSpec index; + index.addKeys(keys); + index.unique(unique); + index.version(int(IndexDescriptor::kLatestIndexVersion)); + auto removeIndexBuildsToo = false; + auto indexSpecs = indexCatalog->removeExistingIndexes( + opCtx, + uassertStatusOK( + collection->addCollationDefaultsToIndexSpecsForCreate(opCtx, {index.toBSON()})), + removeIndexBuildsToo); + + if (indexSpecs.empty()) { + return Status::OK(); + } + + writeConflictRetry(opCtx, "ShardLocal::createIndexOnConfig", ns.ns(), [&] { + WriteUnitOfWork wunit(opCtx); + auto fromMigrate = true; + IndexBuildsCoordinator::get(opCtx)->createIndexesOnEmptyCollection( + opCtx, collection->uuid(), indexSpecs, fromMigrate); + wunit.commit(); + }); + } catch (const DBException& e) { + return e.toStatus(); + } + + return Status::OK(); +} + +void ShardLocal::runFireAndForgetCommand(OperationContext* opCtx, + const ReadPreferenceSetting& readPref, + const std::string& dbName, + const BSONObj& cmdObj) { + MONGO_UNREACHABLE; +} + +} // namespace mongo |