diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-01-16 16:50:03 -0500 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-01-21 12:12:14 -0500 |
commit | dbcfdd8a27453e2ff552bcaa6679aac1cbc1d428 (patch) | |
tree | a782d3598e696beba7cab89592846e21083306f9 | |
parent | 785e8eaba42b3cafb4f1da227fea706cd5b172b7 (diff) | |
download | mongo-dbcfdd8a27453e2ff552bcaa6679aac1cbc1d428.tar.gz |
SERVER-9043/SERVER-31156 Link flushRouterConfig on MongoD and make it accept a namespace argument
(cherry picked from commit e4f26d25632f94a6577028ccefd32069550628b6)
-rw-r--r-- | jstests/sharding/features1.js | 6 | ||||
-rw-r--r-- | jstests/sharding/jumbo1.js | 7 | ||||
-rw-r--r-- | jstests/sharding/multi_mongos2a.js | 5 | ||||
-rw-r--r-- | src/mongo/client/replica_set_monitor.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/auth/authz_session_external_state_s.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/catalog/coll_mod.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/cloner.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/s/balancer/balancer.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/sharding_server_status.cpp | 6 | ||||
-rw-r--r-- | src/mongo/s/SConscript | 4 | ||||
-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/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/s/commands/flush_router_config_cmd.cpp (renamed from src/mongo/s/commands/cluster_flush_router_config_cmd.cpp) | 55 | ||||
-rw-r--r-- | src/mongo/s/commands/get_shard_map_cmd.cpp (renamed from src/mongo/s/commands/cluster_get_shard_map_cmd.cpp) | 0 | ||||
-rw-r--r-- | src/mongo/s/grid.cpp | 4 | ||||
-rw-r--r-- | src/mongo/s/grid.h | 5 | ||||
-rw-r--r-- | src/mongo/s/is_mongos.cpp | 19 | ||||
-rw-r--r-- | src/mongo/s/is_mongos.h | 20 |
19 files changed, 113 insertions, 46 deletions
diff --git a/jstests/sharding/features1.js b/jstests/sharding/features1.js index 0a31b5c94c9..4f36d5675d6 100644 --- a/jstests/sharding/features1.js +++ b/jstests/sharding/features1.js @@ -238,6 +238,12 @@ assert.eq(3, r.databases.length, tojson(r)); assert.eq("number", typeof(r.totalSize), "listDatabases 3 : " + tojson(r)); + // --- flushRouterconfig --- + assert.commandWorked(s.s0.adminCommand({flushRouterConfig: 1})); + assert.commandWorked(s.s0.adminCommand({flushRouterConfig: true})); + assert.commandWorked(s.s0.adminCommand({flushRouterConfig: 'TestDB'})); + assert.commandWorked(s.s0.adminCommand({flushRouterConfig: 'TestDB.TestColl'})); + s.stop(); })(); diff --git a/jstests/sharding/jumbo1.js b/jstests/sharding/jumbo1.js index 8bcaf621a51..573d7b1e5f7 100644 --- a/jstests/sharding/jumbo1.js +++ b/jstests/sharding/jumbo1.js @@ -9,10 +9,7 @@ var db = s.getDB("test"); - var big = ""; - while (big.length < 10000) { - big += "."; - } + const big = 'X'.repeat(10000); // Create sufficient documents to create a jumbo chunk, and use the same shard key in all of // them so that the chunk cannot be split. @@ -52,10 +49,10 @@ s.getDB('config').chunks.findOne({ns: 'test.foo', min: {$lte: {x: 0}}, max: {$gt: {x: 0}}}); assert.eq( s.shard1.shardName, jumboChunk.shard, 'jumbo chunk ' + tojson(jumboChunk) + ' was moved'); + // TODO: SERVER-26531 Make sure that balancer marked the first chunk as jumbo. // Assumption: balancer favors moving the lowest valued chunk out of a shard. // assert(jumboChunk.jumbo, tojson(jumboChunk)); s.stop(); - })(); diff --git a/jstests/sharding/multi_mongos2a.js b/jstests/sharding/multi_mongos2a.js index f0e7a104e60..451bdbd80f1 100644 --- a/jstests/sharding/multi_mongos2a.js +++ b/jstests/sharding/multi_mongos2a.js @@ -6,6 +6,7 @@ assert.commandWorked(st.s0.adminCommand({enablesharding: "test"})); st.ensurePrimaryShard('test', st.shard1.shardName); + assert.commandWorked(st.s0.adminCommand({shardcollection: "test.foo", key: {num: 1}})); assert.writeOK(st.s0.getDB('test').existing.insert({_id: 1})); @@ -21,9 +22,7 @@ to: st.getOther(st.getPrimaryShard("test")).name })); - assert.commandWorked(st.s0.adminCommand({flushRouterConfig: 1})); - - assert.eq(1, st.s0.getDB('test').existing.count({_id: 1})); // SERVER-2828 + assert.eq(1, st.s0.getDB('test').existing.count({_id: 1})); assert.eq(1, st.s1.getDB('test').existing.count({_id: 1})); st.stop(); diff --git a/src/mongo/client/replica_set_monitor.cpp b/src/mongo/client/replica_set_monitor.cpp index f06438ce475..187b0c70462 100644 --- a/src/mongo/client/replica_set_monitor.cpp +++ b/src/mongo/client/replica_set_monitor.cpp @@ -45,7 +45,6 @@ #include "mongo/db/operation_context.h" #include "mongo/db/repl/bson_extract_optime.h" #include "mongo/db/server_options.h" -#include "mongo/s/grid.h" #include "mongo/stdx/condition_variable.h" #include "mongo/stdx/mutex.h" #include "mongo/stdx/thread.h" diff --git a/src/mongo/db/auth/authz_session_external_state_s.cpp b/src/mongo/db/auth/authz_session_external_state_s.cpp index d69ece1adbb..9ddd73bb38a 100644 --- a/src/mongo/db/auth/authz_session_external_state_s.cpp +++ b/src/mongo/db/auth/authz_session_external_state_s.cpp @@ -36,7 +36,6 @@ #include "mongo/client/dbclientinterface.h" #include "mongo/db/auth/authorization_manager.h" #include "mongo/db/jsobj.h" -#include "mongo/s/grid.h" namespace mongo { diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp index e33408295ad..876f5e6d0b9 100644 --- a/src/mongo/db/catalog/coll_mod.cpp +++ b/src/mongo/db/catalog/coll_mod.cpp @@ -56,12 +56,10 @@ #include "mongo/s/catalog/type_collection.h" #include "mongo/s/client/shard_registry.h" #include "mongo/s/grid.h" -#include "mongo/s/sharding_initialization.h" #include "mongo/util/fail_point_service.h" #include "mongo/util/log.h" namespace mongo { - namespace { // Causes the server to hang when it attempts to assign UUIDs to the provided database (or all diff --git a/src/mongo/db/cloner.cpp b/src/mongo/db/cloner.cpp index 9d48f821e3a..e836e233681 100644 --- a/src/mongo/db/cloner.cpp +++ b/src/mongo/db/cloner.cpp @@ -66,7 +66,6 @@ #include "mongo/db/server_parameters.h" #include "mongo/db/service_context.h" #include "mongo/db/storage/storage_options.h" -#include "mongo/s/grid.h" #include "mongo/util/assert_util.h" #include "mongo/util/fail_point_service.h" #include "mongo/util/log.h" diff --git a/src/mongo/db/s/balancer/balancer.cpp b/src/mongo/db/s/balancer/balancer.cpp index d027601f284..57be235c1a0 100644 --- a/src/mongo/db/s/balancer/balancer.cpp +++ b/src/mongo/db/s/balancer/balancer.cpp @@ -383,7 +383,7 @@ void Balancer::_mainThread() { shardingContext->catalogClient() ->logAction(opCtx.get(), "balancer.round", "", roundDetails.toBSON()) - .transitional_ignore(); + .ignore(); } LOG(1) << "*** End of balancing round"; diff --git a/src/mongo/db/s/sharding_server_status.cpp b/src/mongo/db/s/sharding_server_status.cpp index 87c831755ca..6b6031c8a4c 100644 --- a/src/mongo/db/s/sharding_server_status.cpp +++ b/src/mongo/db/s/sharding_server_status.cpp @@ -35,19 +35,15 @@ #include "mongo/db/s/active_migrations_registry.h" #include "mongo/db/s/sharding_state.h" #include "mongo/db/s/sharding_statistics.h" -#include "mongo/db/server_options.h" #include "mongo/s/balancer_configuration.h" #include "mongo/s/catalog_cache.h" #include "mongo/s/client/shard_registry.h" #include "mongo/s/grid.h" +#include "mongo/s/is_mongos.h" namespace mongo { namespace { -bool isClusterNode() { - return serverGlobalParams.clusterRole != ClusterRole::None; -} - class ShardingServerStatus final : public ServerStatusSection { public: ShardingServerStatus() : ServerStatusSection("sharding") {} diff --git a/src/mongo/s/SConscript b/src/mongo/s/SConscript index 807107415ef..e18c7652c28 100644 --- a/src/mongo/s/SConscript +++ b/src/mongo/s/SConscript @@ -430,7 +430,9 @@ env.Library( source=[ 'is_mongos.cpp', ], - LIBDEPS=[], + LIBDEPS=[ + '$BUILD_DIR/mongo/db/server_options_core', + ], ) env.CppUnitTest( diff --git a/src/mongo/s/catalog_cache.cpp b/src/mongo/s/catalog_cache.cpp index 8e34fbf4db8..6024e2c15ae 100644 --- a/src/mongo/s/catalog_cache.cpp +++ b/src/mongo/s/catalog_cache.cpp @@ -383,6 +383,17 @@ void CatalogCache::invalidateShardedCollection(const NamespaceString& nss) { itDb->second[nss.ns()]->needsRefresh = true; } +void CatalogCache::purgeCollection(const NamespaceString& nss) { + stdx::lock_guard<stdx::mutex> lg(_mutex); + + auto itDb = _collectionsByDb.find(nss.db()); + if (itDb == _collectionsByDb.end()) { + return; + } + + itDb->second.erase(nss.ns()); +} + void CatalogCache::purgeDatabase(StringData dbName) { stdx::lock_guard<stdx::mutex> lg(_mutex); _databases.erase(dbName); diff --git a/src/mongo/s/catalog_cache.h b/src/mongo/s/catalog_cache.h index e7e73f79503..f7f1553cbe5 100644 --- a/src/mongo/s/catalog_cache.h +++ b/src/mongo/s/catalog_cache.h @@ -219,6 +219,12 @@ public: void invalidateShardedCollection(const NamespaceString& nss); /** + * Non-blocking method, which removes the entire specified collection from the cache (resulting + * in full refresh on subsequent access) + */ + void purgeCollection(const NamespaceString& nss); + + /** * Non-blocking method, which removes the entire specified database (including its collections) * from the cache. */ diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript index c0f42ef7d59..a8f05260726 100644 --- a/src/mongo/s/commands/SConscript +++ b/src/mongo/s/commands/SConscript @@ -20,7 +20,8 @@ env.Library( env.Library( target='shared_cluster_commands', source=[ - 'cluster_get_shard_map_cmd.cpp', + 'flush_router_config_cmd.cpp', + 'get_shard_map_cmd.cpp', ], LIBDEPS_PRIVATE=[ '$BUILD_DIR/mongo/db/commands', @@ -58,7 +59,6 @@ env.Library( 'cluster_filemd5_cmd.cpp', 'cluster_find_and_modify_cmd.cpp', 'cluster_find_cmd.cpp', - 'cluster_flush_router_config_cmd.cpp', 'cluster_fsync_cmd.cpp', 'cluster_ftdc_commands.cpp', 'cluster_geo_near_cmd.cpp', diff --git a/src/mongo/s/commands/cluster_flush_router_config_cmd.cpp b/src/mongo/s/commands/flush_router_config_cmd.cpp index ee4f6947264..aa8ef21ed43 100644 --- a/src/mongo/s/commands/cluster_flush_router_config_cmd.cpp +++ b/src/mongo/s/commands/flush_router_config_cmd.cpp @@ -28,11 +28,14 @@ * it in the license file. */ +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding + #include "mongo/platform/basic.h" #include "mongo/db/commands.h" -#include "mongo/s/catalog_cache.h" #include "mongo/s/grid.h" +#include "mongo/s/is_mongos.h" +#include "mongo/util/log.h" namespace mongo { namespace { @@ -45,32 +48,58 @@ public: return AllowedOnSecondary::kAlways; } - virtual bool adminOnly() const { + bool adminOnly() const override { return true; } - - virtual bool supportsWriteConcern(const BSONObj& cmd) const override { + bool supportsWriteConcern(const BSONObj& cmd) const override { return false; } std::string help() const override { - return "flush all router config"; + return "Flushes the cached routing information for a single collection, entire database " + "(and its collections) or all databases, which would cause full reload from the " + "config server on the next access.\n" + "Usage:\n" + "{flushRouterConfig: 1} flushes all databases\n" + "{flushRouterConfig: 'db'} flushes only the given database (and its collections)\n" + "{flushRouterconfig: 'db.coll'} flushes only the given collection"; } - virtual void addRequiredPrivileges(const std::string& dbname, - const BSONObj& cmdObj, - std::vector<Privilege>* out) const { + void addRequiredPrivileges(const std::string& dbname, + const BSONObj& cmdObj, + std::vector<Privilege>* out) const override { ActionSet actions; actions.addAction(ActionType::flushRouterConfig); out->push_back(Privilege(ResourcePattern::forClusterResource(), actions)); } - virtual bool run(OperationContext* opCtx, - const std::string& dbname, - const BSONObj& cmdObj, - BSONObjBuilder& result) { - Grid::get(opCtx)->catalogCache()->purgeAllDatabases(); + bool run(OperationContext* opCtx, + const std::string& dbname, + const BSONObj& cmdObj, + BSONObjBuilder& result) override { + auto const grid = Grid::get(opCtx); + uassert(ErrorCodes::ShardingStateNotInitialized, + "Sharding is not enabled", + grid->isShardingInitialized()); + + auto const catalogCache = grid->catalogCache(); + + const auto argumentElem = cmdObj.firstElement(); + if (argumentElem.isNumber() || argumentElem.isBoolean()) { + LOG(0) << "Routing metadata flushed for all databases"; + catalogCache->purgeAllDatabases(); + } else { + const auto ns = argumentElem.checkAndGetStringData(); + if (nsIsDbOnly(ns)) { + LOG(0) << "Routing metadata flushed for database " << ns; + catalogCache->purgeDatabase(ns); + } else { + const NamespaceString nss(ns); + LOG(0) << "Routing metadata flushed for collection " << nss; + catalogCache->purgeCollection(nss); + } + } result.appendBool("flushed", true); return true; diff --git a/src/mongo/s/commands/cluster_get_shard_map_cmd.cpp b/src/mongo/s/commands/get_shard_map_cmd.cpp index 45413d2969b..45413d2969b 100644 --- a/src/mongo/s/commands/cluster_get_shard_map_cmd.cpp +++ b/src/mongo/s/commands/get_shard_map_cmd.cpp diff --git a/src/mongo/s/grid.cpp b/src/mongo/s/grid.cpp index cd9544670ce..25b90d5ede4 100644 --- a/src/mongo/s/grid.cpp +++ b/src/mongo/s/grid.cpp @@ -45,11 +45,9 @@ #include "mongo/util/log.h" namespace mongo { - namespace { -// Global grid instance const auto grid = ServiceContext::declareDecoration<Grid>(); -} +} // namespace Grid::Grid() = default; diff --git a/src/mongo/s/grid.h b/src/mongo/s/grid.h index 7c84f931a77..778775b7fdf 100644 --- a/src/mongo/s/grid.h +++ b/src/mongo/s/grid.h @@ -32,6 +32,7 @@ #include "mongo/db/repl/optime.h" #include "mongo/s/catalog/sharding_catalog_client.h" +#include "mongo/s/catalog_cache.h" #include "mongo/s/client/shard_registry.h" #include "mongo/stdx/functional.h" #include "mongo/stdx/memory.h" @@ -40,7 +41,6 @@ namespace mongo { class BalancerConfiguration; -class CatalogCache; class ClusterCursorManager; class OperationContext; class ServiceContext; @@ -52,8 +52,7 @@ class TaskExecutorPool; } // namespace executor /** - * Holds the global sharding context. Single instance exists for a running server. Exists on - * both MongoD and MongoS. + * Contains the sharding context for a running server. Exists on both MongoD and MongoS. */ class Grid { public: diff --git a/src/mongo/s/is_mongos.cpp b/src/mongo/s/is_mongos.cpp index 2a4ebb2f53e..1855e475081 100644 --- a/src/mongo/s/is_mongos.cpp +++ b/src/mongo/s/is_mongos.cpp @@ -28,18 +28,31 @@ * it in the license file. */ +#include "mongo/platform/basic.h" + #include "mongo/s/is_mongos.h" +#include "mongo/db/server_options.h" + namespace mongo { namespace { bool mongosState = false; } // namespace -} // namespace mongo -bool mongo::isMongos() { +bool isMongos() { return mongosState; } -void mongo::setMongos(const bool state) { +void setMongos(const bool state) { mongosState = state; } + +bool isClusterNode() { + return serverGlobalParams.clusterRole != ClusterRole::None; +} + +bool isClusterNodeOrRouter() { + return isClusterNode() || isMongos(); +} + +} // namespace mongo diff --git a/src/mongo/s/is_mongos.h b/src/mongo/s/is_mongos.h index 2567f7ba6d9..90184105c8f 100644 --- a/src/mongo/s/is_mongos.h +++ b/src/mongo/s/is_mongos.h @@ -31,9 +31,25 @@ #pragma once namespace mongo { -/** Returns true when the running process is `mongos` and false otherwise. */ + +/** + * Returns true when the running process is `mongos` and false otherwise. + */ bool isMongos(); -/** Set the global state flag indicating whether the running process is `mongos` or not. */ +/** + * Set the global state flag indicating whether the running process is `mongos` or not. + */ void setMongos(const bool state = true); + +/** + * Returns whether this node is config server or a shard. + */ +bool isClusterNode(); + +/** + * Returns whether this node is config server, shard or mongos. + */ +bool isClusterNodeOrRouter(); + } // namespace mongo |