diff options
author | Andrew Morrow <acm@mongodb.com> | 2016-04-23 18:13:24 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2016-06-16 17:54:40 -0400 |
commit | 3ab27a1be85d33f07a4aa9a5e84cfc4f3231ee86 (patch) | |
tree | 723a0802b4768ceeab17b43afd5876fcf41d7a3e /src/mongo | |
parent | dc73a4b9567e1ebdb6ba6c9a41a919a0c7c6adf0 (diff) | |
download | mongo-3ab27a1be85d33f07a4aa9a5e84cfc4f3231ee86.tar.gz |
SERVER-23103 Refactor access to haveLocalShardingInfo
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/commands/find_and_modify.cpp | 1 | ||||
-rw-r--r-- | src/mongo/s/SConscript | 10 | ||||
-rw-r--r-- | src/mongo/s/client/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/s/d_state.cpp | 15 | ||||
-rw-r--r-- | src/mongo/s/local_sharding_info.cpp | 71 | ||||
-rw-r--r-- | src/mongo/s/local_sharding_info.h (renamed from src/mongo/s/d_state.h) | 13 | ||||
-rw-r--r-- | src/mongo/scripting/SConscript | 6 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/db.cpp | 2 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/dbcollection.cpp | 2 | ||||
-rw-r--r-- | src/mongo/shell/clientAndShell.cpp | 4 | ||||
-rw-r--r-- | src/mongo/unittest/crutch.cpp | 4 |
11 files changed, 109 insertions, 22 deletions
diff --git a/src/mongo/db/commands/find_and_modify.cpp b/src/mongo/db/commands/find_and_modify.cpp index e6db597545e..a31e16e8c32 100644 --- a/src/mongo/db/commands/find_and_modify.cpp +++ b/src/mongo/db/commands/find_and_modify.cpp @@ -64,7 +64,6 @@ #include "mongo/db/repl/replication_coordinator_global.h" #include "mongo/db/s/collection_sharding_state.h" #include "mongo/db/write_concern.h" -#include "mongo/s/d_state.h" #include "mongo/util/log.h" #include "mongo/util/scopeguard.h" diff --git a/src/mongo/s/SConscript b/src/mongo/s/SConscript index c4411742da5..b6951e785aa 100644 --- a/src/mongo/s/SConscript +++ b/src/mongo/s/SConscript @@ -310,6 +310,16 @@ env.CppUnitTest( ) env.Library( + target='local_sharding_info', + source=[ + 'local_sharding_info.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/db/service_context', + ], +) + +env.Library( target='serveronly', source=[ 'd_sharding_server_status.cpp', diff --git a/src/mongo/s/client/SConscript b/src/mongo/s/client/SConscript index 6c1263d354d..fca2f7b746e 100644 --- a/src/mongo/s/client/SConscript +++ b/src/mongo/s/client/SConscript @@ -24,8 +24,7 @@ env.Library( ], LIBDEPS_TAGS=[ # Circular with both coreshard and coredb! Also relies on some - # symbols like usingAShardConnection that are not uniquely - # defined. + # symbols that are not uniquely defined. 'incomplete', ], ) diff --git a/src/mongo/s/d_state.cpp b/src/mongo/s/d_state.cpp index 612ed3ba54b..3f3d9dc08b1 100644 --- a/src/mongo/s/d_state.cpp +++ b/src/mongo/s/d_state.cpp @@ -30,8 +30,7 @@ #include "mongo/platform/basic.h" -#include "mongo/s/d_state.h" - +#include "mongo/base/init.h" #include "mongo/db/auth/action_set.h" #include "mongo/db/auth/action_type.h" #include "mongo/db/auth/authorization_manager.h" @@ -46,6 +45,8 @@ #include "mongo/db/s/operation_sharding_state.h" #include "mongo/db/s/sharded_connection_info.h" #include "mongo/db/s/sharding_state.h" +#include "mongo/db/service_context.h" +#include "mongo/s/local_sharding_info.h" #include "mongo/util/log.h" #include "mongo/util/stringutils.h" @@ -55,7 +56,7 @@ using std::shared_ptr; using std::string; using std::stringstream; -namespace {} // namespace +namespace { bool haveLocalShardingInfo(OperationContext* txn, const string& ns) { if (!ShardingState::get(txn)->enabled()) { @@ -75,6 +76,14 @@ bool haveLocalShardingInfo(OperationContext* txn, const string& ns) { return false; } +MONGO_INITIALIZER_WITH_PREREQUISITES(MongoDLocalShardingInfo, ("SetGlobalEnvironment")) +(InitializerContext* context) { + enableLocalShardingInfo(getGlobalServiceContext(), &haveLocalShardingInfo); + return Status::OK(); +} + +} // namespace + void usingAShardConnection(const string& addr) {} } // namespace mongo diff --git a/src/mongo/s/local_sharding_info.cpp b/src/mongo/s/local_sharding_info.cpp new file mode 100644 index 00000000000..8cea70e5032 --- /dev/null +++ b/src/mongo/s/local_sharding_info.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 10gen 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. + */ + +#include "mongo/platform/basic.h" + +#include "mongo/s/local_sharding_info.h" + +#include "mongo/db/operation_context.h" +#include "mongo/db/service_context.h" + +namespace mongo { +namespace { + +using Handler = stdx::function<bool(OperationContext*, const std::string&)>; + +class LocalShardingInfo { +public: + void registerHandler(Handler handler) { + _handler = handler; + } + + const Handler& getHandler() const { + return _handler; + } + +private: + Handler _handler; +}; + +const ServiceContext::Decoration<LocalShardingInfo> forService = + ServiceContext::declareDecoration<LocalShardingInfo>(); + +} // namespace + +void enableLocalShardingInfo(ServiceContext* context, Handler handler) { + forService(context).registerHandler(handler); +} + +bool haveLocalShardingInfo(OperationContext* txn, const std::string& ns) { + auto handler = forService(txn->getServiceContext()).getHandler(); + if (handler) + return handler(txn, ns); + return false; +} + +} // namespace mongo diff --git a/src/mongo/s/d_state.h b/src/mongo/s/local_sharding_info.h index 5583d053d55..42be1a0176b 100644 --- a/src/mongo/s/d_state.h +++ b/src/mongo/s/local_sharding_info.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 10gen Inc. + * Copyright (C) 2016 10gen 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, @@ -30,11 +30,22 @@ #include <string> +#include "mongo/stdx/functional.h" + namespace mongo { +class ServiceContext; class OperationContext; /** + * Register a hook function that will implement + * 'haveLocalShardingInfo' below. If no hook is registered, + * haveLocalShardingInfo will always return 'false'. + */ +void enableLocalShardingInfo(ServiceContext* context, + stdx::function<bool(OperationContext*, const std::string&)> handler); + +/** * @return true if we have any shard info for the ns */ bool haveLocalShardingInfo(OperationContext* txn, const std::string& ns); diff --git a/src/mongo/scripting/SConscript b/src/mongo/scripting/SConscript index 629b77c36f7..fc32d46d01a 100644 --- a/src/mongo/scripting/SConscript +++ b/src/mongo/scripting/SConscript @@ -147,11 +147,7 @@ if usemozjs: '$BUILD_DIR/third_party/shim_mozjs', '$BUILD_DIR/mongo/shell/mongojs', '$BUILD_DIR/mongo/db/service_context', - ], - LIBDEPS_TAGS=[ - # Depends on haveLocalShardingInfo, which are not - # uniquely defined. - 'incomplete', + '$BUILD_DIR/mongo/s/local_sharding_info', ], ) else: diff --git a/src/mongo/scripting/mozjs/db.cpp b/src/mongo/scripting/mozjs/db.cpp index ed8575b0f4a..7fa2f179241 100644 --- a/src/mongo/scripting/mozjs/db.cpp +++ b/src/mongo/scripting/mozjs/db.cpp @@ -32,7 +32,7 @@ #include "mongo/db/namespace_string.h" #include "mongo/db/operation_context.h" -#include "mongo/s/d_state.h" +#include "mongo/s/local_sharding_info.h" #include "mongo/scripting/mozjs/idwrapper.h" #include "mongo/scripting/mozjs/implscope.h" #include "mongo/scripting/mozjs/internedstring.h" diff --git a/src/mongo/scripting/mozjs/dbcollection.cpp b/src/mongo/scripting/mozjs/dbcollection.cpp index e7c6bb67ae0..e0f903dd1fc 100644 --- a/src/mongo/scripting/mozjs/dbcollection.cpp +++ b/src/mongo/scripting/mozjs/dbcollection.cpp @@ -32,7 +32,7 @@ #include "mongo/db/namespace_string.h" #include "mongo/db/operation_context.h" -#include "mongo/s/d_state.h" +#include "mongo/s/local_sharding_info.h" #include "mongo/scripting/mozjs/bson.h" #include "mongo/scripting/mozjs/db.h" #include "mongo/scripting/mozjs/implscope.h" diff --git a/src/mongo/shell/clientAndShell.cpp b/src/mongo/shell/clientAndShell.cpp index d4507a38d90..a910fd4e932 100644 --- a/src/mongo/shell/clientAndShell.cpp +++ b/src/mongo/shell/clientAndShell.cpp @@ -47,8 +47,4 @@ class Client; class DBClientBase; class OperationContext; -bool haveLocalShardingInfo(OperationContext* txn, const string& ns) { - return false; -} - } diff --git a/src/mongo/unittest/crutch.cpp b/src/mongo/unittest/crutch.cpp index 5f22f7cd4cc..f52bcd0219c 100644 --- a/src/mongo/unittest/crutch.cpp +++ b/src/mongo/unittest/crutch.cpp @@ -45,10 +45,6 @@ class Client; class OperationContext; -bool haveLocalShardingInfo(OperationContext* txn, const std::string& ns) { - return false; -} - #ifdef _WIN32 namespace ntservice { bool shouldStartService() { |