From a335b35b2e95538220941960764e6f60136b3973 Mon Sep 17 00:00:00 2001 From: Max Hirschhorn Date: Tue, 26 May 2015 20:27:50 -0400 Subject: SERVER-17861 Change the default storage engine to wiredTiger. WiredTiger is used as the default storage engine if the dbpath does not contain any data files. Otherwise, the storage engine specified in the storage.bson metadata file is used when the --storageEngine flag is omitted from the command line invocation. --- src/mongo/db/service_context_d.cpp | 52 +++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'src/mongo/db/service_context_d.cpp') diff --git a/src/mongo/db/service_context_d.cpp b/src/mongo/db/service_context_d.cpp index 83427b0cef2..eaef2f6f639 100644 --- a/src/mongo/db/service_context_d.cpp +++ b/src/mongo/db/service_context_d.cpp @@ -32,6 +32,8 @@ #include "mongo/db/service_context_d.h" +#include + #include "mongo/base/init.h" #include "mongo/base/initializer.h" #include "mongo/db/client.h" @@ -45,6 +47,7 @@ #include "mongo/scripting/engine.h" #include "mongo/stdx/memory.h" #include "mongo/util/log.h" +#include "mongo/util/map_util.h" #include "mongo/util/mongoutils/str.h" #include "mongo/util/scopeguard.h" @@ -72,21 +75,52 @@ namespace mongo { extern bool _supportsDocLocking; - void ServiceContextMongoD::setGlobalStorageEngine(const std::string& name) { + void ServiceContextMongoD::initializeGlobalStorageEngine() { // This should be set once. invariant(!_storageEngine); - const StorageEngine::Factory* factory = _storageFactories[name]; + const std::string dbpath = storageGlobalParams.dbpath; + if (auto existingStorageEngine = StorageEngineMetadata::getStorageEngineForPath(dbpath)) { + if (storageGlobalParams.engineSetByUser) { + // Verify that the name of the user-supplied storage engine matches the contents of + // the metadata file. + const StorageEngine::Factory* factory = mapFindWithDefault( + _storageFactories, + storageGlobalParams.engine, + static_cast(nullptr)); + + if (factory) { + uassert(28662, str::stream() + << "Cannot start server. Detected data files in " << dbpath << " created by" + << " the '" << *existingStorageEngine << "' storage engine, but the" + << " specified storage engine was '" << factory->getCanonicalName() << "'.", + factory->getCanonicalName() == *existingStorageEngine); + } + } + else { + // Otherwise set the active storage engine as the contents of the metadata file. + log() << "Detected data files in " << dbpath << " created by the '" + << *existingStorageEngine << "' storage engine, so setting the active" + << " storage engine to '" << *existingStorageEngine << "'."; + storageGlobalParams.engine = *existingStorageEngine; + } + } + else if (!storageGlobalParams.engineSetByUser) { + // Ensure the default storage engine is available with this build of mongod. + uassert(28663, str::stream() + << "Cannot start server. The default storage engine '" << storageGlobalParams.engine + << "' is not available with this build of mongod. Please specify a different" + << " storage engine explicitly, e.g. --storageEngine=mmapv1.", + isRegisteredStorageEngine(storageGlobalParams.engine)); + } + + const StorageEngine::Factory* factory = _storageFactories[storageGlobalParams.engine]; uassert(18656, str::stream() - << "Cannot start server with an unknown storage engine: " << name, + << "Cannot start server with an unknown storage engine: " << storageGlobalParams.engine, factory); - std::string canonicalName = factory->getCanonicalName().toString(); - - // Do not proceed if data directory has been used by a different storage engine previously. - std::auto_ptr metadata = - StorageEngineMetadata::validate(storageGlobalParams.dbpath, canonicalName); + std::unique_ptr metadata = StorageEngineMetadata::forPath(dbpath); // Validate options in metadata against current startup options. if (metadata.get()) { @@ -116,7 +150,7 @@ namespace mongo { // Write a new metadata file if it is not present. if (!metadata.get()) { metadata.reset(new StorageEngineMetadata(storageGlobalParams.dbpath)); - metadata->setStorageEngine(canonicalName); + metadata->setStorageEngine(factory->getCanonicalName().toString()); metadata->setStorageEngineOptions(factory->createMetadataOptions(storageGlobalParams)); uassertStatusOK(metadata->write()); } -- cgit v1.2.1