diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-01-16 16:50:03 -0500 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-02-13 09:36:41 -0500 |
commit | 52b87981917afaa4b93a73218404690a69036016 (patch) | |
tree | feb93a2c19c37d8f2502478b4e1cb8fe49d4dd26 | |
parent | deb68db9838f307f0fd6ab3e74c741383b702075 (diff) | |
download | mongo-52b87981917afaa4b93a73218404690a69036016.tar.gz |
SERVER-9043/SERVER-31156 Link flushRouterConfig on MongoD and make it accept a namespace argument
(cherry picked from commit e4f26d25632f94a6577028ccefd32069550628b6)
(cherry picked from commit dbcfdd8a27453e2ff552bcaa6679aac1cbc1d428)
-rw-r--r-- | jstests/sharding/features1.js | 6 | ||||
-rw-r--r-- | jstests/sharding/jumbo1.js | 10 | ||||
-rw-r--r-- | jstests/sharding/multi_mongos2a.js | 7 | ||||
-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) | 57 | ||||
-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.h | 9 | ||||
-rw-r--r-- | src/mongo/s/is_mongos.cpp | 19 | ||||
-rw-r--r-- | src/mongo/s/is_mongos.h | 20 |
18 files changed, 118 insertions, 48 deletions
diff --git a/jstests/sharding/features1.js b/jstests/sharding/features1.js index 254e919f237..119a14f5d57 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 4aed86feeae..84b6426afc1 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. @@ -49,11 +46,12 @@ // Check that the jumbo chunk did not move, which shouldn't be possible. var jumboChunk = s.getDB('config').chunks.findOne({ns: 'test.foo', min: {$lte: {x: 0}}, max: {$gt: {x: 0}}}); - assert.eq('shard0001', jumboChunk.shard, 'jumbo chunk ' + tojson(jumboChunk) + ' was moved'); + 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 e76dd547e85..451bdbd80f1 100644 --- a/jstests/sharding/multi_mongos2a.js +++ b/jstests/sharding/multi_mongos2a.js @@ -5,7 +5,8 @@ var st = new ShardingTest({shards: 2, mongos: 2}); assert.commandWorked(st.s0.adminCommand({enablesharding: "test"})); - st.ensurePrimaryShard('test', 'shard0001'); + 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 33c7ddb545f..815328c3aed 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 457f536038e..c04b2448a4c 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 455b82da31b..b79557d86af 100644 --- a/src/mongo/db/catalog/coll_mod.cpp +++ b/src/mongo/db/catalog/coll_mod.cpp @@ -55,12 +55,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 71bca81f279..34867aa9623 100644 --- a/src/mongo/db/cloner.cpp +++ b/src/mongo/db/cloner.cpp @@ -65,7 +65,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/log.h" #include "mongo/util/mongoutils/str.h" diff --git a/src/mongo/db/s/balancer/balancer.cpp b/src/mongo/db/s/balancer/balancer.cpp index 30d766726bf..6dcf93c5293 100644 --- a/src/mongo/db/s/balancer/balancer.cpp +++ b/src/mongo/db/s/balancer/balancer.cpp @@ -385,7 +385,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 5cee3021388..894bc6cb43c 100644 --- a/src/mongo/db/s/sharding_server_status.cpp +++ b/src/mongo/db/s/sharding_server_status.cpp @@ -34,19 +34,15 @@ #include "mongo/db/commands/server_status.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 4def356d037..59a989bd478 100644 --- a/src/mongo/s/SConscript +++ b/src/mongo/s/SConscript @@ -351,7 +351,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 e9ae91e094d..1cc594cdb83 100644 --- a/src/mongo/s/catalog_cache.cpp +++ b/src/mongo/s/catalog_cache.cpp @@ -274,6 +274,17 @@ void CatalogCache::invalidateShardedCollection(const NamespaceString& nss) { it->second->collections[nss.ns()].needsRefresh = true; } +void CatalogCache::purgeCollection(const NamespaceString& nss) { + stdx::lock_guard<stdx::mutex> lg(_mutex); + + auto itDb = _databases.find(nss.db()); + if (itDb == _databases.end()) { + return; + } + + itDb->second->collections.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 f64ceff1e0e..19c3f0eb43b 100644 --- a/src/mongo/s/catalog_cache.h +++ b/src/mongo/s/catalog_cache.h @@ -111,6 +111,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 23fe7a7a7d6..d080e2fe098 100644 --- a/src/mongo/s/commands/SConscript +++ b/src/mongo/s/commands/SConscript @@ -9,7 +9,8 @@ env.Library( target='shared_cluster_commands', source=[ 'cluster_commands_helpers.cpp', - 'cluster_get_shard_map_cmd.cpp', + 'flush_router_config_cmd.cpp', + 'get_shard_map_cmd.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/db/commands', @@ -42,7 +43,6 @@ env.Library( 'cluster_explain_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_get_last_error_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 6caea4afe56..a8553e0e555 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 true; } - 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; } - virtual void help(std::stringstream& help) const { - help << "flush all router config"; + void help(std::stringstream& help) const override { + help << "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) { + void addRequiredPrivileges(const std::string& dbname, + const BSONObj& cmdObj, + std::vector<Privilege>* out) 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 c823a081a90..c823a081a90 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.h b/src/mongo/s/grid.h index 32dbc76777f..7df58106677 100644 --- a/src/mongo/s/grid.h +++ b/src/mongo/s/grid.h @@ -31,6 +31,9 @@ #pragma once #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" #include "mongo/stdx/mutex.h" @@ -38,12 +41,9 @@ namespace mongo { class BalancerConfiguration; -class CatalogCache; -class ShardingCatalogClient; class ClusterCursorManager; class OperationContext; class ServiceContext; -class ShardRegistry; namespace executor { struct ConnectionPoolStats; @@ -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 |