summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@mongodb.com>2018-04-24 18:44:21 -0400
committerAndy Schwerin <schwerin@mongodb.com>2018-05-02 17:10:28 -0400
commit970ace98679d499f8821c8775dee875d3070d79f (patch)
treee0d71af365887a0d16f0521c68d02ad908ed6000 /src
parent8ee6a9c1993acf7981d4d7a0ac8ba6f808c23b60 (diff)
downloadmongo-970ace98679d499f8821c8775dee875d3070d79f.tar.gz
SERVER-34794 Construct the global ServiceContext inside a MONGO_INITIALIZER
Diffstat (limited to 'src')
-rw-r--r--src/mongo/base/deinitializer_context.h10
-rw-r--r--src/mongo/base/initializer.cpp38
-rw-r--r--src/mongo/base/initializer.h20
-rw-r--r--src/mongo/base/initializer_context.h11
-rw-r--r--src/mongo/base/initializer_test.cpp7
-rw-r--r--src/mongo/client/embedded/embedded.cpp11
-rw-r--r--src/mongo/db/auth/authorization_manager_global.cpp59
-rw-r--r--src/mongo/db/auth/sasl_mechanism_registry.cpp5
-rw-r--r--src/mongo/db/db.cpp6
-rw-r--r--src/mongo/db/query/collation/collator_factory_icu_decoration.cpp2
-rw-r--r--src/mongo/db/query/datetime/init_timezone_data.cpp4
-rw-r--r--src/mongo/db/service_context_registrar.cpp86
-rw-r--r--src/mongo/db/service_context_registrar.h2
-rw-r--r--src/mongo/db/storage/devnull/devnull_init.cpp2
-rw-r--r--src/mongo/db/storage/encryption_hooks.cpp5
-rw-r--r--src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_init.cpp2
-rw-r--r--src/mongo/db/storage/mmap_v1/mmap_v1_init.cpp2
-rw-r--r--src/mongo/db/storage/mobile/mobile_init.cpp3
-rw-r--r--src/mongo/db/storage/wiredtiger/wiredtiger_customization_hooks.cpp2
-rw-r--r--src/mongo/db/storage/wiredtiger/wiredtiger_extensions.cpp2
-rw-r--r--src/mongo/db/storage/wiredtiger/wiredtiger_init.cpp2
-rw-r--r--src/mongo/dbtests/dbtests.cpp3
-rw-r--r--src/mongo/s/server.cpp3
-rw-r--r--src/mongo/shell/dbshell.cpp3
-rw-r--r--src/mongo/tools/bridge.cpp3
-rw-r--r--src/mongo/transport/transport_layer_egress_init.cpp2
-rw-r--r--src/mongo/unittest/SConscript4
-rw-r--r--src/mongo/unittest/benchmark_main.cpp9
-rw-r--r--src/mongo/unittest/integration_test_main.cpp9
-rw-r--r--src/mongo/unittest/unittest_main.cpp9
30 files changed, 142 insertions, 184 deletions
diff --git a/src/mongo/base/deinitializer_context.h b/src/mongo/base/deinitializer_context.h
index 0db3d773c05..f2a4c927cf2 100644
--- a/src/mongo/base/deinitializer_context.h
+++ b/src/mongo/base/deinitializer_context.h
@@ -42,18 +42,8 @@ namespace mongo {
*/
class DeinitializerContext {
public:
- explicit DeinitializerContext(ServiceContext* serviceContext)
- : _serviceContext(serviceContext) {}
-
DeinitializerContext(DeinitializerContext const&) = delete;
DeinitializerContext& operator=(DeinitializerContext const&) = delete;
-
- ServiceContext* serviceContext() const {
- return _serviceContext;
- }
-
-private:
- ServiceContext* _serviceContext;
};
} // namespace mongo
diff --git a/src/mongo/base/initializer.cpp b/src/mongo/base/initializer.cpp
index 34625b7cd65..579fb6ef6da 100644
--- a/src/mongo/base/initializer.cpp
+++ b/src/mongo/base/initializer.cpp
@@ -25,13 +25,17 @@
* then also delete it in the license file.
*/
+#include "mongo/platform/basic.h"
+
#include "mongo/base/initializer.h"
+#include <iostream>
+
#include "mongo/base/deinitializer_context.h"
#include "mongo/base/global_initializer.h"
+#include "mongo/base/initializer_context.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/quick_exit.h"
-#include <iostream>
namespace mongo {
@@ -39,14 +43,13 @@ Initializer::Initializer() {}
Initializer::~Initializer() {}
Status Initializer::executeInitializers(const InitializerContext::ArgumentVector& args,
- const InitializerContext::EnvironmentMap& env,
- ServiceContext* serviceContext) {
+ const InitializerContext::EnvironmentMap& env) {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status;
- InitializerContext context(args, env, serviceContext);
+ InitializerContext context(args, env);
for (size_t i = 0; i < sortedNodes.size(); ++i) {
InitializerDependencyNode* node = _graph.getInitializerNode(sortedNodes[i]);
@@ -76,13 +79,13 @@ Status Initializer::executeInitializers(const InitializerContext::ArgumentVector
return Status::OK();
}
-Status Initializer::executeDeinitializers(ServiceContext* serviceContext) {
+Status Initializer::executeDeinitializers() {
std::vector<std::string> sortedNodes;
Status status = _graph.topSort(&sortedNodes);
if (Status::OK() != status)
return status;
- DeinitializerContext context(serviceContext);
+ DeinitializerContext context{};
// Execute deinitialization in reverse order from initialization.
for (auto it = sortedNodes.rbegin(), end = sortedNodes.rend(); it != end; ++it) {
@@ -105,15 +108,11 @@ Status Initializer::executeDeinitializers(ServiceContext* serviceContext) {
}
Status runGlobalInitializers(const InitializerContext::ArgumentVector& args,
- const InitializerContext::EnvironmentMap& env,
- ServiceContext* serviceContext) {
- return getGlobalInitializer().executeInitializers(args, env, serviceContext);
+ const InitializerContext::EnvironmentMap& env) {
+ return getGlobalInitializer().executeInitializers(args, env);
}
-Status runGlobalInitializers(int argc,
- const char* const* argv,
- const char* const* envp,
- ServiceContext* serviceContext) {
+Status runGlobalInitializers(int argc, const char* const* argv, const char* const* envp) {
InitializerContext::ArgumentVector args(argc);
std::copy(argv, argv + argc, args.begin());
@@ -129,18 +128,15 @@ Status runGlobalInitializers(int argc,
}
}
- return runGlobalInitializers(args, env, serviceContext);
+ return runGlobalInitializers(args, env);
}
-Status runGlobalDeinitializers(ServiceContext* serviceContext) {
- return getGlobalInitializer().executeDeinitializers(serviceContext);
+Status runGlobalDeinitializers() {
+ return getGlobalInitializer().executeDeinitializers();
}
-void runGlobalInitializersOrDie(int argc,
- const char* const* argv,
- const char* const* envp,
- ServiceContext* serviceContext) {
- Status status = runGlobalInitializers(argc, argv, envp, serviceContext);
+void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp) {
+ Status status = runGlobalInitializers(argc, argv, envp);
if (!status.isOK()) {
std::cerr << "Failed global initialization: " << status << std::endl;
quickExit(1);
diff --git a/src/mongo/base/initializer.h b/src/mongo/base/initializer.h
index 924569ab138..904356fdbef 100644
--- a/src/mongo/base/initializer.h
+++ b/src/mongo/base/initializer.h
@@ -66,10 +66,9 @@ public:
* and the thing being initialized should be considered dead in the water.
*/
Status executeInitializers(const InitializerContext::ArgumentVector& args,
- const InitializerContext::EnvironmentMap& env,
- ServiceContext* serviceContext);
+ const InitializerContext::EnvironmentMap& env);
- Status executeDeinitializers(ServiceContext* serviceContext);
+ Status executeDeinitializers();
private:
InitializerDependencyGraph _graph;
@@ -85,22 +84,15 @@ private:
* should probably arrange to terminate the process themselves.
*/
Status runGlobalInitializers(const InitializerContext::ArgumentVector& args,
- const InitializerContext::EnvironmentMap& env,
- ServiceContext* serviceContext);
+ const InitializerContext::EnvironmentMap& env);
-Status runGlobalInitializers(int argc,
- const char* const* argv,
- const char* const* envp,
- ServiceContext* serviceContext);
+Status runGlobalInitializers(int argc, const char* const* argv, const char* const* envp);
/**
* Same as runGlobalInitializers(), except prints a brief message to std::cerr
* and terminates the process on failure.
*/
-void runGlobalInitializersOrDie(int argc,
- const char* const* argv,
- const char* const* envp,
- ServiceContext* serviceContext);
+void runGlobalInitializersOrDie(int argc, const char* const* argv, const char* const* envp);
/**
* Run the global deinitializers. They will execute in reverse order from initialization.
@@ -111,6 +103,6 @@ void runGlobalInitializersOrDie(int argc,
* This means that the few initializers that might want to terminate the program by failing
* should probably arrange to terminate the process themselves.
*/
-Status runGlobalDeinitializers(ServiceContext* serviceContext);
+Status runGlobalDeinitializers();
} // namespace mongo
diff --git a/src/mongo/base/initializer_context.h b/src/mongo/base/initializer_context.h
index b1a9ab329e9..ee3df192803 100644
--- a/src/mongo/base/initializer_context.h
+++ b/src/mongo/base/initializer_context.h
@@ -47,10 +47,8 @@ public:
typedef std::vector<std::string> ArgumentVector;
typedef std::map<std::string, std::string> EnvironmentMap;
- InitializerContext(const ArgumentVector& args,
- const EnvironmentMap& env,
- ServiceContext* serviceContext)
- : _args(args), _env(env), _serviceContext(serviceContext) {}
+ InitializerContext(const ArgumentVector& args, const EnvironmentMap& env)
+ : _args(args), _env(env) {}
const ArgumentVector& args() const {
return _args;
@@ -59,14 +57,9 @@ public:
return _env;
}
- ServiceContext* serviceContext() const {
- return _serviceContext;
- }
-
private:
ArgumentVector _args;
EnvironmentMap _env;
- ServiceContext* _serviceContext;
};
} // namespace mongo
diff --git a/src/mongo/base/initializer_test.cpp b/src/mongo/base/initializer_test.cpp
index 31b87f5771d..9be5f0eb6f5 100644
--- a/src/mongo/base/initializer_test.cpp
+++ b/src/mongo/base/initializer_test.cpp
@@ -160,8 +160,8 @@ TEST(InitializerTest, SuccessfulInitialization) {
set7,
set8);
clearCounts();
- ASSERT_OK(initializer.executeInitializers(
- InitializerContext::ArgumentVector(), InitializerContext::EnvironmentMap(), nullptr));
+ ASSERT_OK(initializer.executeInitializers(InitializerContext::ArgumentVector(),
+ InitializerContext::EnvironmentMap()));
for (int i = 0; i < 9; ++i)
ASSERT_EQUALS(1, globalCounts[i]);
}
@@ -181,8 +181,7 @@ TEST(InitializerTest, Step5Misimplemented) {
clearCounts();
ASSERT_EQUALS(ErrorCodes::UnknownError,
initializer.executeInitializers(InitializerContext::ArgumentVector(),
- InitializerContext::EnvironmentMap(),
- nullptr));
+ InitializerContext::EnvironmentMap()));
ASSERT_EQUALS(1, globalCounts[0]);
ASSERT_EQUALS(1, globalCounts[1]);
ASSERT_EQUALS(1, globalCounts[2]);
diff --git a/src/mongo/client/embedded/embedded.cpp b/src/mongo/client/embedded/embedded.cpp
index 580843414ea..19cfa77374b 100644
--- a/src/mongo/client/embedded/embedded.cpp
+++ b/src/mongo/client/embedded/embedded.cpp
@@ -102,7 +102,7 @@ GlobalInitializerRegisterer replicationManagerInitializer(
"CreateReplicationManager",
{"SSLManager", "default"},
[](InitializerContext* context) {
- auto serviceContext = context->serviceContext();
+ auto serviceContext = getGlobalServiceContext();
repl::StorageInterface::set(serviceContext, std::make_unique<repl::StorageInterfaceImpl>());
auto logicalClock = stdx::make_unique<LogicalClock>(serviceContext);
@@ -114,7 +114,7 @@ GlobalInitializerRegisterer replicationManagerInitializer(
return Status::OK();
},
[](DeinitializerContext* context) {
- auto serviceContext = context->serviceContext();
+ auto serviceContext = getGlobalServiceContext();
repl::ReplicationCoordinator::set(serviceContext, nullptr);
LogicalClock::set(serviceContext, nullptr);
@@ -159,7 +159,7 @@ void shutdown(ServiceContext* srvContext) {
serviceContext->shutdownGlobalStorageEngineCleanly();
}
- Status status = mongo::runGlobalDeinitializers(serviceContext);
+ Status status = mongo::runGlobalDeinitializers();
uassertStatusOKWithContext(status, "Global deinitilization failed");
}
shutdownOpCtx.reset();
@@ -176,14 +176,11 @@ void shutdown(ServiceContext* srvContext) {
ServiceContext* initialize(const char* yaml_config) {
srand(static_cast<unsigned>(curTimeMicros64()));
- setGlobalServiceContext(createServiceContext());
-
// yaml_config is passed to the options parser through the argc/argv interface that already
// existed. If it is nullptr then use 0 count which will be interpreted as empty string.
const char* argv[2] = {yaml_config, nullptr};
- Status status =
- mongo::runGlobalInitializers(yaml_config ? 1 : 0, argv, nullptr, getGlobalServiceContext());
+ Status status = mongo::runGlobalInitializers(yaml_config ? 1 : 0, argv, nullptr);
uassertStatusOKWithContext(status, "Global initilization failed");
Client::initThread("initandlisten");
diff --git a/src/mongo/db/auth/authorization_manager_global.cpp b/src/mongo/db/auth/authorization_manager_global.cpp
index 39505fab014..9738f7b2932 100644
--- a/src/mongo/db/auth/authorization_manager_global.cpp
+++ b/src/mongo/db/auth/authorization_manager_global.cpp
@@ -1,30 +1,30 @@
/**
-* Copyright (C) 2013 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.
-*/
+ * Copyright (C) 2013 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"
@@ -95,18 +95,19 @@ GlobalInitializerRegisterer authorizationManagerInitializer(
{"SetupInternalSecurityUser",
"OIDGeneration",
"CreateAuthorizationExternalStateFactory",
- "EndStartupOptionStorage"},
+ "EndStartupOptionStorage",
+ "ServiceContext"},
[](InitializerContext* context) {
auto authzManager =
stdx::make_unique<AuthorizationManager>(AuthzManagerExternalState::create());
authzManager->setAuthEnabled(serverGlobalParams.authState ==
ServerGlobalParams::AuthState::kEnabled);
authzManager->setShouldValidateAuthSchemaOnStartup(startupAuthSchemaValidation);
- AuthorizationManager::set(context->serviceContext(), std::move(authzManager));
+ AuthorizationManager::set(getGlobalServiceContext(), std::move(authzManager));
return Status::OK();
},
[](DeinitializerContext* context) {
- AuthorizationManager::set(context->serviceContext(), nullptr);
+ AuthorizationManager::set(getGlobalServiceContext(), nullptr);
return Status::OK();
});
diff --git a/src/mongo/db/auth/sasl_mechanism_registry.cpp b/src/mongo/db/auth/sasl_mechanism_registry.cpp
index 6bd5417b288..4daebdb5542 100644
--- a/src/mongo/db/auth/sasl_mechanism_registry.cpp
+++ b/src/mongo/db/auth/sasl_mechanism_registry.cpp
@@ -126,6 +126,7 @@ bool SASLServerMechanismRegistry::_mechanismSupportedByConfig(StringData mechNam
GlobalInitializerRegisterer SASLServerMechanismRegistryInitializer(
"CreateSASLServerMechanismRegistry",
+ {"ServiceContext"},
[](InitializerContext* context) {
if (saslGlobalParams.hostName.empty())
saslGlobalParams.hostName = getHostNameCached();
@@ -133,11 +134,11 @@ GlobalInitializerRegisterer SASLServerMechanismRegistryInitializer(
saslGlobalParams.serviceName = "mongodb";
auto registry = stdx::make_unique<SASLServerMechanismRegistry>();
- SASLServerMechanismRegistry::set(context->serviceContext(), std::move(registry));
+ SASLServerMechanismRegistry::set(getGlobalServiceContext(), std::move(registry));
return Status::OK();
},
[](DeinitializerContext* context) {
- SASLServerMechanismRegistry::set(context->serviceContext(), nullptr);
+ SASLServerMechanismRegistry::set(getGlobalServiceContext(), nullptr);
return Status::OK();
});
diff --git a/src/mongo/db/db.cpp b/src/mongo/db/db.cpp
index e22cf803e4b..ce59dfe90f8 100644
--- a/src/mongo/db/db.cpp
+++ b/src/mongo/db/db.cpp
@@ -772,7 +772,8 @@ auto makeReplicationExecutor(ServiceContext* serviceContext) {
executor::makeNetworkInterface("Replication", nullptr, std::move(hookList)));
}
-MONGO_INITIALIZER_WITH_PREREQUISITES(CreateReplicationManager, ("SSLManager", "default"))
+MONGO_INITIALIZER_WITH_PREREQUISITES(CreateReplicationManager,
+ ("SSLManager", "ServiceContext", "default"))
(InitializerContext* context) {
auto serviceContext = getGlobalServiceContext();
repl::StorageInterface::set(serviceContext, stdx::make_unique<repl::StorageInterfaceImpl>());
@@ -953,8 +954,7 @@ int mongoDbMain(int argc, char* argv[], char** envp) {
srand(static_cast<unsigned>(curTimeMicros64()));
- setGlobalServiceContext(createServiceContext());
- Status status = mongo::runGlobalInitializers(argc, argv, envp, getGlobalServiceContext());
+ Status status = mongo::runGlobalInitializers(argc, argv, envp);
if (!status.isOK()) {
severe(LogComponent::kControl) << "Failed global initialization: " << status;
quickExit(EXIT_FAILURE);
diff --git a/src/mongo/db/query/collation/collator_factory_icu_decoration.cpp b/src/mongo/db/query/collation/collator_factory_icu_decoration.cpp
index e37ce036511..c2a4922071f 100644
--- a/src/mongo/db/query/collation/collator_factory_icu_decoration.cpp
+++ b/src/mongo/db/query/collation/collator_factory_icu_decoration.cpp
@@ -37,7 +37,7 @@ namespace mongo {
namespace {
-MONGO_INITIALIZER_WITH_PREREQUISITES(CreateCollatorFactory, ("LoadICUData"))
+MONGO_INITIALIZER_WITH_PREREQUISITES(CreateCollatorFactory, ("ServiceContext", "LoadICUData"))
(InitializerContext* context) {
CollatorFactoryInterface::set(getGlobalServiceContext(),
stdx::make_unique<CollatorFactoryICU>());
diff --git a/src/mongo/db/query/datetime/init_timezone_data.cpp b/src/mongo/db/query/datetime/init_timezone_data.cpp
index 0c17cca0457..18a842f231c 100644
--- a/src/mongo/db/query/datetime/init_timezone_data.cpp
+++ b/src/mongo/db/query/datetime/init_timezone_data.cpp
@@ -39,8 +39,8 @@
namespace mongo {
-MONGO_INITIALIZER_WITH_PREREQUISITES(LoadTimeZoneDB,
- ("GlobalLogManager", "EndStartupOptionStorage"))
+MONGO_INITIALIZER_WITH_PREREQUISITES(
+ LoadTimeZoneDB, ("GlobalLogManager", "EndStartupOptionStorage", "ServiceContext"))
(InitializerContext* context) {
auto serviceContext = getGlobalServiceContext();
if (!serverGlobalParams.timeZoneInfoPath.empty()) {
diff --git a/src/mongo/db/service_context_registrar.cpp b/src/mongo/db/service_context_registrar.cpp
index 26aa48ac15b..061936c98ec 100644
--- a/src/mongo/db/service_context_registrar.cpp
+++ b/src/mongo/db/service_context_registrar.cpp
@@ -1,33 +1,36 @@
/**
-* Copyright (C) 2018 MongoDB 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.
-*/
+ * Copyright (C) 2018 MongoDB 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/db/service_context_registrar.h"
+#include "mongo/base/init.h"
#include "mongo/db/service_context.h"
namespace mongo {
@@ -36,19 +39,32 @@ std::function<std::unique_ptr<ServiceContext>()>& getServiceContextFactory() {
static std::function<std::unique_ptr<ServiceContext>()> factory;
return factory;
}
-}
+
+// clang-format off
+GlobalInitializerRegisterer registerCreateServiceContext{
+ "ServiceContext",
+ {"default"},
+ [](InitializerContext* context) {
+ // Set the global service context if a service context factory was previously registered.
+ if (getServiceContextFactory()) {
+ setGlobalServiceContext(getServiceContextFactory()());
+ }
+ return Status::OK();
+ },
+ [](DeinitializerContext* context) {
+ // For now, deregistration is done manually after all deinitializers run, in case any
+ // erroneously access the globalServiceContext without expressing a dependency.
+ return Status::OK();
+ }
+};
+// clang-format on
+
+} // namespace
ServiceContextRegistrar::ServiceContextRegistrar(
std::function<std::unique_ptr<ServiceContext>()> fn) {
- invariant(!hasServiceContextFactory());
+ invariant(!getServiceContextFactory());
getServiceContextFactory() = std::move(fn);
}
-bool hasServiceContextFactory() {
- return getServiceContextFactory() ? true : false;
-}
-
-std::unique_ptr<ServiceContext> createServiceContext() {
- return getServiceContextFactory()();
-}
} // namespace mongo
diff --git a/src/mongo/db/service_context_registrar.h b/src/mongo/db/service_context_registrar.h
index 2317d8862f6..72f6e6ea2dd 100644
--- a/src/mongo/db/service_context_registrar.h
+++ b/src/mongo/db/service_context_registrar.h
@@ -43,6 +43,4 @@ public:
explicit ServiceContextRegistrar(std::function<std::unique_ptr<ServiceContext>()> fn);
};
-bool hasServiceContextFactory();
-std::unique_ptr<ServiceContext> createServiceContext();
} // namespace mongo
diff --git a/src/mongo/db/storage/devnull/devnull_init.cpp b/src/mongo/db/storage/devnull/devnull_init.cpp
index dff10077427..c3df87cda3e 100644
--- a/src/mongo/db/storage/devnull/devnull_init.cpp
+++ b/src/mongo/db/storage/devnull/devnull_init.cpp
@@ -64,7 +64,7 @@ public:
};
} // namespace
-MONGO_INITIALIZER(DevNullEngineInit)
+MONGO_INITIALIZER_WITH_PREREQUISITES(DevNullEngineInit, ("ServiceContext"))
(InitializerContext* context) {
getGlobalServiceContext()->registerStorageEngine("devnull", new DevNullStorageEngineFactory());
return Status::OK();
diff --git a/src/mongo/db/storage/encryption_hooks.cpp b/src/mongo/db/storage/encryption_hooks.cpp
index bb5c60a29b8..ffa7f319a06 100644
--- a/src/mongo/db/storage/encryption_hooks.cpp
+++ b/src/mongo/db/storage/encryption_hooks.cpp
@@ -43,12 +43,13 @@ namespace mongo {
/* Make a EncryptionHooks pointer a decoration on the global ServiceContext */
GlobalInitializerRegisterer encryptionHooksInitializer(
"SetEncryptionHooks",
+ {"ServiceContext"},
[](InitializerContext* context) {
- EncryptionHooks::set(context->serviceContext(), stdx::make_unique<EncryptionHooks>());
+ EncryptionHooks::set(getGlobalServiceContext(), stdx::make_unique<EncryptionHooks>());
return Status::OK();
},
[](DeinitializerContext* context) {
- EncryptionHooks::set(context->serviceContext(), nullptr);
+ EncryptionHooks::set(getGlobalServiceContext(), nullptr);
return Status::OK();
});
diff --git a/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_init.cpp b/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_init.cpp
index 1c5fff52e8b..8922776255f 100644
--- a/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_init.cpp
+++ b/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_init.cpp
@@ -70,7 +70,7 @@ public:
} // namespace
-MONGO_INITIALIZER(EphemeralForTestEngineInit)
+MONGO_INITIALIZER_WITH_PREREQUISITES(EphemeralForTestEngineInit, ("ServiceContext"))
(InitializerContext* context) {
getGlobalServiceContext()->registerStorageEngine("ephemeralForTest",
new EphemeralForTestFactory());
diff --git a/src/mongo/db/storage/mmap_v1/mmap_v1_init.cpp b/src/mongo/db/storage/mmap_v1/mmap_v1_init.cpp
index 08f586f2035..3bce0e22fba 100644
--- a/src/mongo/db/storage/mmap_v1/mmap_v1_init.cpp
+++ b/src/mongo/db/storage/mmap_v1/mmap_v1_init.cpp
@@ -76,7 +76,7 @@ public:
} // namespace
-MONGO_INITIALIZER(MMAPV1EngineInit)
+MONGO_INITIALIZER_WITH_PREREQUISITES(MMAPV1EngineInit, ("ServiceContext"))
(InitializerContext* context) {
getGlobalServiceContext()->registerStorageEngine("mmapv1", new MMAPV1Factory());
return Status::OK();
diff --git a/src/mongo/db/storage/mobile/mobile_init.cpp b/src/mongo/db/storage/mobile/mobile_init.cpp
index c9d895d0332..edadad22887 100644
--- a/src/mongo/db/storage/mobile/mobile_init.cpp
+++ b/src/mongo/db/storage/mobile/mobile_init.cpp
@@ -72,8 +72,9 @@ public:
GlobalInitializerRegisterer mobileKVEngineInitializer(
"MobileKVEngineInit",
+ {"ServiceContext"},
[](InitializerContext* context) {
- context->serviceContext()->registerStorageEngine("mobile", new MobileFactory());
+ getGlobalServiceContext()->registerStorageEngine("mobile", new MobileFactory());
return Status::OK();
},
[](DeinitializerContext* const) { return Status::OK(); });
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_customization_hooks.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_customization_hooks.cpp
index dfc59b26f3f..3d4d7ccb3cf 100644
--- a/src/mongo/db/storage/wiredtiger/wiredtiger_customization_hooks.cpp
+++ b/src/mongo/db/storage/wiredtiger/wiredtiger_customization_hooks.cpp
@@ -39,7 +39,7 @@
namespace mongo {
/* Make a WiredTigerCustomizationHooks pointer a decoration on the global ServiceContext */
-MONGO_INITIALIZER(SetWiredTigerCustomizationHooks)
+MONGO_INITIALIZER_WITH_PREREQUISITES(SetWiredTigerCustomizationHooks, ("ServiceContext"))
(InitializerContext* context) {
auto customizationHooks = stdx::make_unique<WiredTigerCustomizationHooks>();
WiredTigerCustomizationHooks::set(getGlobalServiceContext(), std::move(customizationHooks));
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_extensions.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_extensions.cpp
index b0149c0b5aa..b2df3f8c99c 100644
--- a/src/mongo/db/storage/wiredtiger/wiredtiger_extensions.cpp
+++ b/src/mongo/db/storage/wiredtiger/wiredtiger_extensions.cpp
@@ -38,7 +38,7 @@
namespace mongo {
-MONGO_INITIALIZER(SetWiredTigerExtensions)
+MONGO_INITIALIZER_WITH_PREREQUISITES(SetWiredTigerExtensions, ("ServiceContext"))
(InitializerContext* context) {
auto configHooks = stdx::make_unique<WiredTigerExtensions>();
WiredTigerExtensions::set(getGlobalServiceContext(), std::move(configHooks));
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_init.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_init.cpp
index 3419b3a93e3..26ce1200659 100644
--- a/src/mongo/db/storage/wiredtiger/wiredtiger_init.cpp
+++ b/src/mongo/db/storage/wiredtiger/wiredtiger_init.cpp
@@ -180,7 +180,7 @@ public:
};
} // namespace
-MONGO_INITIALIZER(WiredTigerEngineInit)
+MONGO_INITIALIZER_WITH_PREREQUISITES(WiredTigerEngineInit, ("ServiceContext"))
(InitializerContext* context) {
getGlobalServiceContext()->registerStorageEngine(kWiredTigerEngineName,
new WiredTigerFactory());
diff --git a/src/mongo/dbtests/dbtests.cpp b/src/mongo/dbtests/dbtests.cpp
index 53ecaaa74e9..91d32f7e032 100644
--- a/src/mongo/dbtests/dbtests.cpp
+++ b/src/mongo/dbtests/dbtests.cpp
@@ -132,8 +132,7 @@ int dbtestsMain(int argc, char** argv, char** envp) {
::mongo::setupSynchronousSignalHandlers();
mongo::dbtests::initWireSpec();
- setGlobalServiceContext(createServiceContext());
- mongo::runGlobalInitializersOrDie(argc, argv, envp, getGlobalServiceContext());
+ mongo::runGlobalInitializersOrDie(argc, argv, envp);
serverGlobalParams.featureCompatibility.setVersion(
ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo40);
repl::ReplSettings replSettings;
diff --git a/src/mongo/s/server.cpp b/src/mongo/s/server.cpp
index b35b3ed28ab..627cdf44a3c 100644
--- a/src/mongo/s/server.cpp
+++ b/src/mongo/s/server.cpp
@@ -581,8 +581,7 @@ ExitCode mongoSMain(int argc, char* argv[], char** envp) {
setupSignalHandlers();
- setGlobalServiceContext(createServiceContext());
- Status status = runGlobalInitializers(argc, argv, envp, getGlobalServiceContext());
+ Status status = runGlobalInitializers(argc, argv, envp);
if (!status.isOK()) {
severe(LogComponent::kDefault) << "Failed global initialization: " << status;
return EXIT_ABRUPT;
diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp
index 81d4ecdc85d..9155154a4dd 100644
--- a/src/mongo/shell/dbshell.cpp
+++ b/src/mongo/shell/dbshell.cpp
@@ -741,8 +741,7 @@ int _main(int argc, char* argv[], char** envp) {
mongo::shell_utils::RecordMyLocation(argv[0]);
- setGlobalServiceContext(createServiceContext());
- mongo::runGlobalInitializersOrDie(argc, argv, envp, getGlobalServiceContext());
+ mongo::runGlobalInitializersOrDie(argc, argv, envp);
// TODO This should use a TransportLayerManager or TransportLayerFactory
auto serviceContext = getGlobalServiceContext();
diff --git a/src/mongo/tools/bridge.cpp b/src/mongo/tools/bridge.cpp
index 7d4a1f10e8c..876f178fd40 100644
--- a/src/mongo/tools/bridge.cpp
+++ b/src/mongo/tools/bridge.cpp
@@ -413,8 +413,7 @@ int bridgeMain(int argc, char** argv, char** envp) {
});
setupSignalHandlers();
- setGlobalServiceContext(createServiceContext());
- runGlobalInitializersOrDie(argc, argv, envp, getGlobalServiceContext());
+ runGlobalInitializersOrDie(argc, argv, envp);
startSignalProcessingThread(LogFileStatus::kNoLogFileToRotate);
auto serviceContext = getGlobalServiceContext();
diff --git a/src/mongo/transport/transport_layer_egress_init.cpp b/src/mongo/transport/transport_layer_egress_init.cpp
index 279bd05b6ce..628cf6c2dfc 100644
--- a/src/mongo/transport/transport_layer_egress_init.cpp
+++ b/src/mongo/transport/transport_layer_egress_init.cpp
@@ -38,7 +38,7 @@ namespace mongo {
namespace {
// Linking with this file will configure an egress-only TransportLayer on a ServiceContextNoop.
// Use this for unit/integration tests that require only egress networking.
-MONGO_INITIALIZER(ConfigureEgressTransportLayer)
+MONGO_INITIALIZER_WITH_PREREQUISITES(ConfigureEgressTransportLayer, ("ServiceContext"))
(InitializerContext* context) {
auto sc = getGlobalServiceContext();
invariant(!sc->getTransportLayer());
diff --git a/src/mongo/unittest/SConscript b/src/mongo/unittest/SConscript
index dbc75f2f62f..dbc359e6787 100644
--- a/src/mongo/unittest/SConscript
+++ b/src/mongo/unittest/SConscript
@@ -21,8 +21,7 @@ env.Library(target="unittest",
env.Library("unittest_main", ['unittest_main.cpp'],
LIBDEPS=[
'unittest',
- '$BUILD_DIR/mongo/db/service_context',
- ])
+ ])
env.Library(target="integration_test_main",
source=[
@@ -48,7 +47,6 @@ bmEnv.Library(
],
LIBDEPS=[
'$BUILD_DIR/mongo/base',
- '$BUILD_DIR/mongo/db/service_context',
'$BUILD_DIR/third_party/shim_benchmark',
],
)
diff --git a/src/mongo/unittest/benchmark_main.cpp b/src/mongo/unittest/benchmark_main.cpp
index 3d43c5d6ab4..a29168ad5d5 100644
--- a/src/mongo/unittest/benchmark_main.cpp
+++ b/src/mongo/unittest/benchmark_main.cpp
@@ -34,8 +34,6 @@
#include "mongo/base/initializer.h"
#include "mongo/config.h"
-#include "mongo/db/service_context.h"
-#include "mongo/db/service_context_registrar.h"
#include "mongo/util/log.h"
#include "mongo/util/signal_handlers_synchronous.h"
@@ -44,12 +42,7 @@ int main(int argc, char** argv, char** envp) {
::mongo::clearSignalMask();
::mongo::setupSynchronousSignalHandlers();
- ::mongo::ServiceContext* serviceContext = nullptr;
- if (::mongo::hasServiceContextFactory()) {
- ::mongo::setGlobalServiceContext(::mongo::createServiceContext());
- serviceContext = ::mongo::getGlobalServiceContext();
- }
- ::mongo::runGlobalInitializersOrDie(argc, argv, envp, serviceContext);
+ ::mongo::runGlobalInitializersOrDie(argc, argv, envp);
// Copied from the BENCHMARK_MAIN macro.
::benchmark::Initialize(&argc, argv);
diff --git a/src/mongo/unittest/integration_test_main.cpp b/src/mongo/unittest/integration_test_main.cpp
index 382ea69b087..b4087176542 100644
--- a/src/mongo/unittest/integration_test_main.cpp
+++ b/src/mongo/unittest/integration_test_main.cpp
@@ -71,14 +71,7 @@ ConnectionString getFixtureConnectionString() {
int main(int argc, char** argv, char** envp) {
setupSynchronousSignalHandlers();
-
- ::mongo::ServiceContext* serviceContext = nullptr;
- if (::mongo::hasServiceContextFactory()) {
- ::mongo::setGlobalServiceContext(::mongo::createServiceContext());
- serviceContext = ::mongo::getGlobalServiceContext();
- }
- runGlobalInitializersOrDie(argc, argv, envp, serviceContext);
-
+ runGlobalInitializersOrDie(argc, argv, envp);
quickExit(unittest::Suite::run(std::vector<std::string>(), "", 1));
}
diff --git a/src/mongo/unittest/unittest_main.cpp b/src/mongo/unittest/unittest_main.cpp
index 496221a87f9..f3f0e63754d 100644
--- a/src/mongo/unittest/unittest_main.cpp
+++ b/src/mongo/unittest/unittest_main.cpp
@@ -33,8 +33,6 @@
#include "mongo/base/initializer.h"
#include "mongo/base/status.h"
-#include "mongo/db/service_context.h"
-#include "mongo/db/service_context_registrar.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/options_parser/environment.h"
#include "mongo/util/options_parser/option_section.h"
@@ -47,12 +45,7 @@ int main(int argc, char** argv, char** envp) {
::mongo::clearSignalMask();
::mongo::setupSynchronousSignalHandlers();
- ::mongo::ServiceContext* serviceContext = nullptr;
- if (::mongo::hasServiceContextFactory()) {
- ::mongo::setGlobalServiceContext(::mongo::createServiceContext());
- serviceContext = ::mongo::getGlobalServiceContext();
- }
- ::mongo::runGlobalInitializersOrDie(argc, argv, envp, serviceContext);
+ ::mongo::runGlobalInitializersOrDie(argc, argv, envp);
namespace moe = ::mongo::optionenvironment;
moe::OptionsParser parser;