diff options
author | Esha Maharishi <esha.maharishi@mongodb.com> | 2018-02-06 11:42:36 -0500 |
---|---|---|
committer | Esha Maharishi <esha.maharishi@mongodb.com> | 2018-02-06 13:03:46 -0500 |
commit | adc3397b43548d9ef0b12cb8b61f57cec5bd25e1 (patch) | |
tree | 06309381be50b0255535716e909fe9fb969c3761 /src/mongo/s | |
parent | d213908da9ddb54cfddfde0d5d88a4e4e1f70bd6 (diff) | |
download | mongo-adc3397b43548d9ef0b12cb8b61f57cec5bd25e1.tar.gz |
SERVER-33022 make the CatalogCache cache the databaseVersion in the DatabaseInfoEntry (re-commit with bug fix)
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/catalog_cache.cpp | 11 | ||||
-rw-r--r-- | src/mongo/s/catalog_cache.h | 6 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commands_helpers.cpp | 11 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commands_helpers.h | 7 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_get_shard_version_cmd.cpp | 39 |
5 files changed, 46 insertions, 28 deletions
diff --git a/src/mongo/s/catalog_cache.cpp b/src/mongo/s/catalog_cache.cpp index a1f44b678f2..34e346af58b 100644 --- a/src/mongo/s/catalog_cache.cpp +++ b/src/mongo/s/catalog_cache.cpp @@ -337,8 +337,11 @@ std::shared_ptr<CatalogCache::DatabaseInfoEntry> CatalogCache::_getDatabase(Oper collectionEntries[coll.getNs().ns()].needsRefresh = true; } - return _databases[dbName] = std::shared_ptr<DatabaseInfoEntry>(new DatabaseInfoEntry{ - dbDesc.getPrimary(), dbDesc.getSharded(), std::move(collectionEntries)}); + return _databases[dbName] = + std::make_shared<DatabaseInfoEntry>(DatabaseInfoEntry{dbDesc.getPrimary(), + dbDesc.getSharded(), + std::move(collectionEntries), + dbDesc.getVersion()}); } void CatalogCache::_scheduleCollectionRefresh(WithLock lk, @@ -484,6 +487,10 @@ bool CachedDatabaseInfo::shardingEnabled() const { return _db->shardingEnabled; } +boost::optional<DatabaseVersion> CachedDatabaseInfo::databaseVersion() const { + return _db->databaseVersion; +} + CachedCollectionRoutingInfo::CachedCollectionRoutingInfo(ShardId primaryId, std::shared_ptr<ChunkManager> cm) : _primaryId(std::move(primaryId)), _cm(std::move(cm)) {} diff --git a/src/mongo/s/catalog_cache.h b/src/mongo/s/catalog_cache.h index aeeae8cb25e..b4b93eced47 100644 --- a/src/mongo/s/catalog_cache.h +++ b/src/mongo/s/catalog_cache.h @@ -34,6 +34,7 @@ #include "mongo/s/catalog_cache_loader.h" #include "mongo/s/chunk_manager.h" #include "mongo/s/client/shard.h" +#include "mongo/s/database_version_gen.h" #include "mongo/stdx/memory.h" #include "mongo/stdx/mutex.h" #include "mongo/util/concurrency/notification.h" @@ -171,6 +172,9 @@ private: bool shardingEnabled; StringMap<CollectionRoutingInfoEntry> collections; + + // Optional while featureCompatibilityVersion 3.6 is supported. + boost::optional<DatabaseVersion> databaseVersion; }; using DatabaseInfoMap = StringMap<std::shared_ptr<DatabaseInfoEntry>>; @@ -245,6 +249,8 @@ public: bool shardingEnabled() const; + boost::optional<DatabaseVersion> databaseVersion() const; + private: friend class CatalogCache; diff --git a/src/mongo/s/commands/cluster_commands_helpers.cpp b/src/mongo/s/commands/cluster_commands_helpers.cpp index 7d4312ecd53..2f985d2aee6 100644 --- a/src/mongo/s/commands/cluster_commands_helpers.cpp +++ b/src/mongo/s/commands/cluster_commands_helpers.cpp @@ -393,17 +393,6 @@ bool appendEmptyResultSet(BSONObjBuilder& result, Status status, const std::stri return CommandHelpers::appendCommandStatus(result, status); } -CachedCollectionRoutingInfo getShardedCollection(OperationContext* opCtx, - const NamespaceString& nss) { - auto routingInfo = - uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, nss)); - uassert(ErrorCodes::NamespaceNotSharded, - str::stream() << "Collection " << nss.ns() << " is not sharded.", - routingInfo.cm()); - - return routingInfo; -} - StatusWith<CachedDatabaseInfo> createShardDatabase(OperationContext* opCtx, StringData dbName) { auto dbStatus = Grid::get(opCtx)->catalogCache()->getDatabase(opCtx, dbName); if (dbStatus == ErrorCodes::NamespaceNotFound) { diff --git a/src/mongo/s/commands/cluster_commands_helpers.h b/src/mongo/s/commands/cluster_commands_helpers.h index 60e153fde20..da6ad16c513 100644 --- a/src/mongo/s/commands/cluster_commands_helpers.h +++ b/src/mongo/s/commands/cluster_commands_helpers.h @@ -146,13 +146,6 @@ int getUniqueCodeFromCommandResults(const std::vector<Strategy::CommandResult>& bool appendEmptyResultSet(BSONObjBuilder& result, Status status, const std::string& ns); /** - * Abstracts the common pattern of refreshing a collection and checking if it is sharded used across - * multiple commands. - */ -CachedCollectionRoutingInfo getShardedCollection(OperationContext* opCtx, - const NamespaceString& nss); - -/** * If the specified database exists already, loads it in the cache (if not already there) and * returns it. Otherwise, if it does not exist, this call will implicitly create it as non-sharded. */ diff --git a/src/mongo/s/commands/cluster_get_shard_version_cmd.cpp b/src/mongo/s/commands/cluster_get_shard_version_cmd.cpp index 21a77e97e2a..589398e86b6 100644 --- a/src/mongo/s/commands/cluster_get_shard_version_cmd.cpp +++ b/src/mongo/s/commands/cluster_get_shard_version_cmd.cpp @@ -37,6 +37,7 @@ #include "mongo/db/commands.h" #include "mongo/s/catalog_cache.h" #include "mongo/s/commands/cluster_commands_helpers.h" +#include "mongo/s/database_version_gen.h" #include "mongo/s/grid.h" #include "mongo/util/log.h" @@ -76,7 +77,12 @@ public: } std::string parseNs(const std::string& dbname, const BSONObj& cmdObj) const override { - return CommandHelpers::parseNsFullyQualified(dbname, cmdObj); + BSONElement first = cmdObj.firstElement(); + uassert(ErrorCodes::BadValue, + str::stream() << "namespace has invalid type " << typeName(first.type()), + first.canonicalType() == canonicalizeBSONType(mongo::String)); + const NamespaceString nss(first.valueStringData()); + return nss.ns(); } bool run(OperationContext* opCtx, @@ -85,15 +91,32 @@ public: BSONObjBuilder& result) override { const NamespaceString nss(parseNs(dbname, cmdObj)); - auto routingInfo = getShardedCollection(opCtx, nss); - const auto cm = routingInfo.cm(); - - for (const auto& chunk : cm->chunks()) { - log() << redact(chunk->toString()); + const auto catalogCache = Grid::get(opCtx)->catalogCache(); + + if (nss.coll().empty()) { + // Return the database's information. + auto cachedDbInfo = uassertStatusOK(catalogCache->getDatabase(opCtx, nss.ns())); + result.append("primaryShard", cachedDbInfo.primaryId().toString()); + result.append("shardingEnabled", cachedDbInfo.shardingEnabled()); + if (cachedDbInfo.databaseVersion()) { + result.append("version", cachedDbInfo.databaseVersion()->toBSON()); + } + } else { + // Return the collection's information. + auto cachedCollInfo = + uassertStatusOK(catalogCache->getCollectionRoutingInfo(opCtx, nss)); + uassert(ErrorCodes::NamespaceNotSharded, + str::stream() << "Collection " << nss.ns() << " is not sharded.", + cachedCollInfo.cm()); + const auto cm = cachedCollInfo.cm(); + + for (const auto& chunk : cm->chunks()) { + log() << redact(chunk->toString()); + } + + cm->getVersion().addToBSON(result, "version"); } - cm->getVersion().addToBSON(result, "version"); - return true; } |