diff options
author | Andy Schwerin <schwerin@mongodb.com> | 2018-05-01 14:21:45 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@mongodb.com> | 2018-05-02 11:19:46 -0400 |
commit | 3e25e21a342566799516f1a23b7fcbeb49fd5d8d (patch) | |
tree | 587beaa2b5b4a1c14230bec8e751d6da2f5c06f4 /src | |
parent | d54b9f51949186e0f99e60f41e9b2b9a7f3802c4 (diff) | |
download | mongo-3e25e21a342566799516f1a23b7fcbeb49fd5d8d.tar.gz |
SERVER-34751 Move StorageEngine ownership to ServiceContext from subclasses
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/client/embedded/service_context_embedded.cpp | 22 | ||||
-rw-r--r-- | src/mongo/client/embedded/service_context_embedded.h | 4 | ||||
-rw-r--r-- | src/mongo/db/service_context.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/service_context.h | 21 | ||||
-rw-r--r-- | src/mongo/db/service_context_d.cpp | 22 | ||||
-rw-r--r-- | src/mongo/db/service_context_d.h | 5 | ||||
-rw-r--r-- | src/mongo/db/service_context_noop.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/service_context_noop.h | 2 |
8 files changed, 42 insertions, 44 deletions
diff --git a/src/mongo/client/embedded/service_context_embedded.cpp b/src/mongo/client/embedded/service_context_embedded.cpp index 6a1c1ff72b7..7719cc80127 100644 --- a/src/mongo/client/embedded/service_context_embedded.cpp +++ b/src/mongo/client/embedded/service_context_embedded.cpp @@ -68,13 +68,6 @@ ServiceContextMongoEmbedded::ServiceContextMongoEmbedded() = default; ServiceContextMongoEmbedded::~ServiceContextMongoEmbedded() = default; -StorageEngine* ServiceContextMongoEmbedded::getGlobalStorageEngine() { - // We don't check that globalStorageEngine is not-NULL here intentionally. We can encounter - // an error before it's initialized and proceed to exitCleanly which is equipped to deal - // with a NULL storage engine. - return _storageEngine.get(); -} - void ServiceContextMongoEmbedded::createLockFile() { try { _lockFile = stdx::make_unique<StorageEngineLockFile>(storageGlobalParams.dbpath); @@ -106,7 +99,7 @@ void ServiceContextMongoEmbedded::createLockFile() { void ServiceContextMongoEmbedded::initializeGlobalStorageEngine() { // This should be set once. - invariant(!_storageEngine); + invariant(!getStorageEngine()); // We should have a _lockFile or be in read-only mode. Confusingly, we can still have a lockFile // if we are in read-only mode. This can happen if the server is started in read-only mode on a @@ -217,8 +210,9 @@ void ServiceContextMongoEmbedded::initializeGlobalStorageEngine() { } }); - _storageEngine.reset(factory->create(storageGlobalParams, _lockFile.get())); - _storageEngine->finishInit(); + setStorageEngine( + std::unique_ptr<StorageEngine>(factory->create(storageGlobalParams, _lockFile.get()))); + getStorageEngine()->finishInit(); if (_lockFile) { uassertStatusOK(_lockFile->writePid()); @@ -235,12 +229,12 @@ void ServiceContextMongoEmbedded::initializeGlobalStorageEngine() { guard.Dismiss(); - _supportsDocLocking = _storageEngine->supportsDocLocking(); + _supportsDocLocking = getStorageEngine()->supportsDocLocking(); } void ServiceContextMongoEmbedded::shutdownGlobalStorageEngineCleanly() { - invariant(_storageEngine); - _storageEngine->cleanShutdown(); + invariant(getStorageEngine()); + getStorageEngine()->cleanShutdown(); if (_lockFile) { _lockFile->clearPidAndUnlock(); } @@ -255,7 +249,7 @@ void ServiceContextMongoEmbedded::registerStorageEngine(const std::string& name, invariant(factory); // and all factories should be added before we pick a storage engine. - invariant(NULL == _storageEngine); + invariant(!getStorageEngine()); _storageFactories[name].reset(factory); } diff --git a/src/mongo/client/embedded/service_context_embedded.h b/src/mongo/client/embedded/service_context_embedded.h index 5954060cd0e..873cb89b24e 100644 --- a/src/mongo/client/embedded/service_context_embedded.h +++ b/src/mongo/client/embedded/service_context_embedded.h @@ -45,8 +45,6 @@ public: ~ServiceContextMongoEmbedded(); - StorageEngine* getGlobalStorageEngine() override; - void createLockFile(); void initializeGlobalStorageEngine() override; @@ -65,8 +63,6 @@ private: std::unique_ptr<StorageEngineLockFile> _lockFile; - std::unique_ptr<StorageEngine> _storageEngine; - // All possible storage engines are registered here through MONGO_INIT. FactoryMap _storageFactories; }; diff --git a/src/mongo/db/service_context.cpp b/src/mongo/db/service_context.cpp index b3392b684bc..a61734e8cff 100644 --- a/src/mongo/db/service_context.cpp +++ b/src/mongo/db/service_context.cpp @@ -193,6 +193,12 @@ transport::ServiceExecutor* ServiceContext::getServiceExecutor() const { return _serviceExecutor.get(); } +void ServiceContext::setStorageEngine(std::unique_ptr<StorageEngine> engine) { + invariant(engine); + invariant(!_storageEngine); + _storageEngine = std::move(engine); +} + void ServiceContext::setOpObserver(std::unique_ptr<OpObserver> opObserver) { _opObserver = std::move(opObserver); } diff --git a/src/mongo/db/service_context.h b/src/mongo/db/service_context.h index 7ef549cac20..305a85c226f 100644 --- a/src/mongo/db/service_context.h +++ b/src/mongo/db/service_context.h @@ -267,9 +267,23 @@ public: virtual void shutdownGlobalStorageEngineCleanly() = 0; /** + * Sets the storage engine for this instance. May be called up to once per instance. + */ + void setStorageEngine(std::unique_ptr<StorageEngine> engine); + + /** + * Return the storage engine instance we're using. + */ + StorageEngine* getStorageEngine() { + return _storageEngine.get(); + } + + /** * Return the storage engine instance we're using. */ - virtual StorageEngine* getGlobalStorageEngine() = 0; + StorageEngine* getGlobalStorageEngine() { + return getStorageEngine(); + } // // Global operation management. This may not belong here and there may be too many methods @@ -461,6 +475,11 @@ private: virtual std::unique_ptr<OperationContext> _newOpCtx(Client* client, unsigned opId) = 0; /** + * The storage engine, if any. + */ + std::unique_ptr<StorageEngine> _storageEngine; + + /** * The periodic runner. */ std::unique_ptr<PeriodicRunner> _runner; diff --git a/src/mongo/db/service_context_d.cpp b/src/mongo/db/service_context_d.cpp index 3f8f1ef0961..4162f7150ca 100644 --- a/src/mongo/db/service_context_d.cpp +++ b/src/mongo/db/service_context_d.cpp @@ -70,13 +70,6 @@ ServiceContextMongoD::ServiceContextMongoD() = default; ServiceContextMongoD::~ServiceContextMongoD() = default; -StorageEngine* ServiceContextMongoD::getGlobalStorageEngine() { - // We don't check that globalStorageEngine is not-NULL here intentionally. We can encounter - // an error before it's initialized and proceed to exitCleanly which is equipped to deal - // with a NULL storage engine. - return _storageEngine; -} - void ServiceContextMongoD::createLockFile() { try { _lockFile.reset(new StorageEngineLockFile(storageGlobalParams.dbpath)); @@ -109,7 +102,7 @@ void ServiceContextMongoD::createLockFile() { void ServiceContextMongoD::initializeGlobalStorageEngine() { // This should be set once. - invariant(!_storageEngine); + invariant(!getStorageEngine()); // We should have a _lockFile or be in read-only mode. Confusingly, we can still have a lockFile // if we are in read-only mode. This can happen if the server is started in read-only mode on a @@ -220,8 +213,9 @@ void ServiceContextMongoD::initializeGlobalStorageEngine() { } }); - _storageEngine = factory->create(storageGlobalParams, _lockFile.get()); - _storageEngine->finishInit(); + setStorageEngine( + std::unique_ptr<StorageEngine>(factory->create(storageGlobalParams, _lockFile.get()))); + getStorageEngine()->finishInit(); if (_lockFile) { uassertStatusOK(_lockFile->writePid()); @@ -238,12 +232,12 @@ void ServiceContextMongoD::initializeGlobalStorageEngine() { guard.Dismiss(); - _supportsDocLocking = _storageEngine->supportsDocLocking(); + _supportsDocLocking = getStorageEngine()->supportsDocLocking(); } void ServiceContextMongoD::shutdownGlobalStorageEngineCleanly() { - invariant(_storageEngine); - _storageEngine->cleanShutdown(); + invariant(getStorageEngine()); + getStorageEngine()->cleanShutdown(); if (_lockFile) { _lockFile->clearPidAndUnlock(); } @@ -258,7 +252,7 @@ void ServiceContextMongoD::registerStorageEngine(const std::string& name, invariant(factory); // and all factories should be added before we pick a storage engine. - invariant(NULL == _storageEngine); + invariant(!getStorageEngine()); _storageFactories[name] = factory; } diff --git a/src/mongo/db/service_context_d.h b/src/mongo/db/service_context_d.h index 79b2126831a..9074cb07e51 100644 --- a/src/mongo/db/service_context_d.h +++ b/src/mongo/db/service_context_d.h @@ -45,8 +45,6 @@ public: ~ServiceContextMongoD(); - StorageEngine* getGlobalStorageEngine() override; - void createLockFile(); void initializeGlobalStorageEngine() override; @@ -65,9 +63,6 @@ private: std::unique_ptr<StorageEngineLockFile> _lockFile; - // logically owned here, but never deleted by anyone. - StorageEngine* _storageEngine = nullptr; - // All possible storage engines are registered here through MONGO_INIT. FactoryMap _storageFactories; }; diff --git a/src/mongo/db/service_context_noop.cpp b/src/mongo/db/service_context_noop.cpp index 59f40dfa710..59d0e696fb6 100644 --- a/src/mongo/db/service_context_noop.cpp +++ b/src/mongo/db/service_context_noop.cpp @@ -35,10 +35,6 @@ namespace mongo { -StorageEngine* ServiceContextNoop::getGlobalStorageEngine() { - return nullptr; -} - void ServiceContextNoop::initializeGlobalStorageEngine() {} void ServiceContextNoop::shutdownGlobalStorageEngineCleanly() {} diff --git a/src/mongo/db/service_context_noop.h b/src/mongo/db/service_context_noop.h index 251cd43310a..3bf4829cffe 100644 --- a/src/mongo/db/service_context_noop.h +++ b/src/mongo/db/service_context_noop.h @@ -34,8 +34,6 @@ namespace mongo { class ServiceContextNoop : public ServiceContext { public: - StorageEngine* getGlobalStorageEngine() override; - void initializeGlobalStorageEngine() override; void shutdownGlobalStorageEngineCleanly() override; |