summaryrefslogtreecommitdiff
path: root/src/mongo/db/service_context.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/service_context.cpp')
-rw-r--r--src/mongo/db/service_context.cpp347
1 files changed, 171 insertions, 176 deletions
diff --git a/src/mongo/db/service_context.cpp b/src/mongo/db/service_context.cpp
index 745cb2f9d76..8dfa7bfdc98 100644
--- a/src/mongo/db/service_context.cpp
+++ b/src/mongo/db/service_context.cpp
@@ -39,222 +39,217 @@
namespace mongo {
- namespace {
+namespace {
- ServiceContext* globalServiceContext = NULL;
+ServiceContext* globalServiceContext = NULL;
- } // namespace
+} // namespace
- bool hasGlobalServiceContext() { return globalServiceContext; }
+bool hasGlobalServiceContext() {
+ return globalServiceContext;
+}
- ServiceContext* getGlobalServiceContext() {
- fassert(17508, globalServiceContext);
- return globalServiceContext;
- }
-
- void setGlobalServiceContext(std::unique_ptr<ServiceContext>&& serviceContext) {
- fassert(17509, serviceContext.get());
+ServiceContext* getGlobalServiceContext() {
+ fassert(17508, globalServiceContext);
+ return globalServiceContext;
+}
- delete globalServiceContext;
+void setGlobalServiceContext(std::unique_ptr<ServiceContext>&& serviceContext) {
+ fassert(17509, serviceContext.get());
- globalServiceContext = serviceContext.release();
- }
+ delete globalServiceContext;
- bool _supportsDocLocking = false;
+ globalServiceContext = serviceContext.release();
+}
- bool supportsDocLocking() {
- return _supportsDocLocking;
- }
+bool _supportsDocLocking = false;
- bool isMMAPV1() {
- StorageEngine* globalStorageEngine = getGlobalServiceContext()->getGlobalStorageEngine();
+bool supportsDocLocking() {
+ return _supportsDocLocking;
+}
- invariant(globalStorageEngine);
- return globalStorageEngine->isMmapV1();
- }
+bool isMMAPV1() {
+ StorageEngine* globalStorageEngine = getGlobalServiceContext()->getGlobalStorageEngine();
- Status validateStorageOptions(const BSONObj& storageEngineOptions,
- stdx::function<Status (const StorageEngine::Factory* const, const BSONObj&)> validateFunc) {
-
- BSONObjIterator storageIt(storageEngineOptions);
- while (storageIt.more()) {
- BSONElement storageElement = storageIt.next();
- StringData storageEngineName = storageElement.fieldNameStringData();
- if (storageElement.type() != mongo::Object) {
- return Status(ErrorCodes::BadValue, str::stream()
- << "'storageEngine." << storageElement.fieldNameStringData()
- << "' has to be an embedded document.");
- }
+ invariant(globalStorageEngine);
+ return globalStorageEngine->isMmapV1();
+}
- std::unique_ptr<StorageFactoriesIterator> sfi(getGlobalServiceContext()->
- makeStorageFactoriesIterator());
- invariant(sfi);
- bool found = false;
- while (sfi->more()) {
- const StorageEngine::Factory* const& factory = sfi->next();
- if (storageEngineName != factory->getCanonicalName()) {
- continue;
- }
- Status status = validateFunc(factory, storageElement.Obj());
- if ( !status.isOK() ) {
- return status;
- }
- found = true;
- }
- if (!found) {
- return Status(ErrorCodes::InvalidOptions, str::stream() << storageEngineName <<
- " is not a registered storage engine for this server");
- }
+Status validateStorageOptions(
+ const BSONObj& storageEngineOptions,
+ stdx::function<Status(const StorageEngine::Factory* const, const BSONObj&)> validateFunc) {
+ BSONObjIterator storageIt(storageEngineOptions);
+ while (storageIt.more()) {
+ BSONElement storageElement = storageIt.next();
+ StringData storageEngineName = storageElement.fieldNameStringData();
+ if (storageElement.type() != mongo::Object) {
+ return Status(ErrorCodes::BadValue,
+ str::stream() << "'storageEngine." << storageElement.fieldNameStringData()
+ << "' has to be an embedded document.");
}
- return Status::OK();
- }
-
- ServiceContext::~ServiceContext() {
- stdx::lock_guard<stdx::mutex> lk(_mutex);
- invariant(_clients.empty());
- }
- ServiceContext::UniqueClient ServiceContext::makeClient(std::string desc,
- AbstractMessagingPort* p) {
- std::unique_ptr<Client> client(new Client(std::move(desc), this, p));
- auto observer = _clientObservers.cbegin();
- try {
- for (; observer != _clientObservers.cend(); ++observer) {
- observer->get()->onCreateClient(client.get());
- }
- }
- catch (...) {
- try {
- while (observer != _clientObservers.cbegin()) {
- --observer;
- observer->get()->onDestroyClient(client.get());
- }
+ std::unique_ptr<StorageFactoriesIterator> sfi(
+ getGlobalServiceContext()->makeStorageFactoriesIterator());
+ invariant(sfi);
+ bool found = false;
+ while (sfi->more()) {
+ const StorageEngine::Factory* const& factory = sfi->next();
+ if (storageEngineName != factory->getCanonicalName()) {
+ continue;
}
- catch (...) {
- std::terminate();
+ Status status = validateFunc(factory, storageElement.Obj());
+ if (!status.isOK()) {
+ return status;
}
- throw;
+ found = true;
}
- {
- stdx::lock_guard<stdx::mutex> lk(_mutex);
- invariant(_clients.insert(client.get()).second);
+ if (!found) {
+ return Status(ErrorCodes::InvalidOptions,
+ str::stream() << storageEngineName
+ << " is not a registered storage engine for this server");
}
- return UniqueClient(client.release());
- }
-
- TickSource* ServiceContext::getTickSource() const {
- return _tickSource.get();
- }
-
- void ServiceContext::setTickSource(std::unique_ptr<TickSource> newSource) {
- _tickSource = std::move(newSource);
}
-
- void ServiceContext::ClientDeleter::operator()(Client* client) const {
- ServiceContext* const service = client->getServiceContext();
- {
- stdx::lock_guard<stdx::mutex> lk(service->_mutex);
- invariant(service->_clients.erase(client));
+ return Status::OK();
+}
+
+ServiceContext::~ServiceContext() {
+ stdx::lock_guard<stdx::mutex> lk(_mutex);
+ invariant(_clients.empty());
+}
+
+ServiceContext::UniqueClient ServiceContext::makeClient(std::string desc,
+ AbstractMessagingPort* p) {
+ std::unique_ptr<Client> client(new Client(std::move(desc), this, p));
+ auto observer = _clientObservers.cbegin();
+ try {
+ for (; observer != _clientObservers.cend(); ++observer) {
+ observer->get()->onCreateClient(client.get());
}
+ } catch (...) {
try {
- for (const auto& observer : service->_clientObservers) {
- observer->onDestroyClient(client);
+ while (observer != _clientObservers.cbegin()) {
+ --observer;
+ observer->get()->onDestroyClient(client.get());
}
- }
- catch (...) {
+ } catch (...) {
std::terminate();
}
- delete client;
+ throw;
}
-
- ServiceContext::UniqueOperationContext ServiceContext::makeOperationContext(Client* client) {
- auto opCtx = _newOpCtx(client);
- auto observer = _clientObservers.begin();
- try {
- for (; observer != _clientObservers.cend(); ++observer) {
- observer->get()->onCreateOperationContext(opCtx.get());
- }
+ {
+ stdx::lock_guard<stdx::mutex> lk(_mutex);
+ invariant(_clients.insert(client.get()).second);
+ }
+ return UniqueClient(client.release());
+}
+
+TickSource* ServiceContext::getTickSource() const {
+ return _tickSource.get();
+}
+
+void ServiceContext::setTickSource(std::unique_ptr<TickSource> newSource) {
+ _tickSource = std::move(newSource);
+}
+
+void ServiceContext::ClientDeleter::operator()(Client* client) const {
+ ServiceContext* const service = client->getServiceContext();
+ {
+ stdx::lock_guard<stdx::mutex> lk(service->_mutex);
+ invariant(service->_clients.erase(client));
+ }
+ try {
+ for (const auto& observer : service->_clientObservers) {
+ observer->onDestroyClient(client);
}
- catch (...) {
- try {
- while (observer != _clientObservers.cbegin()) {
- --observer;
- observer->get()->onDestroyOperationContext(opCtx.get());
- }
- }
- catch (...) {
- std::terminate();
- }
- throw;
+ } catch (...) {
+ std::terminate();
+ }
+ delete client;
+}
+
+ServiceContext::UniqueOperationContext ServiceContext::makeOperationContext(Client* client) {
+ auto opCtx = _newOpCtx(client);
+ auto observer = _clientObservers.begin();
+ try {
+ for (; observer != _clientObservers.cend(); ++observer) {
+ observer->get()->onCreateOperationContext(opCtx.get());
}
- // // TODO(schwerin): When callers no longer construct their own OperationContexts directly,
- // // but only through the ServiceContext, uncomment the following. Until then, it must
- // // be done in the operation context destructors, which introduces a potential race.
- // {
- // stdx::lock_guard<Client> lk(*client);
- // client->setOperationContext(opCtx.get());
- // }
- return UniqueOperationContext(opCtx.release());
- };
-
- void ServiceContext::OperationContextDeleter::operator()(OperationContext* opCtx) const {
- auto client = opCtx->getClient();
- auto service = client->getServiceContext();
- // // TODO(schwerin): When callers no longer construct their own OperationContexts directly,
- // // but only through the ServiceContext, uncomment the following. Until then, it must
- // // be done in the operation context destructors, which introduces a potential race.
- // {
- // stdx::lock_guard<Client> lk(*client);
- // client->resetOperationContext();
- // }
+ } catch (...) {
try {
- for (const auto& observer : service->_clientObservers) {
- observer->onDestroyOperationContext(opCtx);
+ while (observer != _clientObservers.cbegin()) {
+ --observer;
+ observer->get()->onDestroyOperationContext(opCtx.get());
}
- }
- catch (...) {
+ } catch (...) {
std::terminate();
}
- delete opCtx;
+ throw;
}
-
- void ServiceContext::registerClientObserver(std::unique_ptr<ClientObserver> observer) {
- _clientObservers.push_back(std::move(observer));
+ // // TODO(schwerin): When callers no longer construct their own OperationContexts directly,
+ // // but only through the ServiceContext, uncomment the following. Until then, it must
+ // // be done in the operation context destructors, which introduces a potential race.
+ // {
+ // stdx::lock_guard<Client> lk(*client);
+ // client->setOperationContext(opCtx.get());
+ // }
+ return UniqueOperationContext(opCtx.release());
+};
+
+void ServiceContext::OperationContextDeleter::operator()(OperationContext* opCtx) const {
+ auto client = opCtx->getClient();
+ auto service = client->getServiceContext();
+ // // TODO(schwerin): When callers no longer construct their own OperationContexts directly,
+ // // but only through the ServiceContext, uncomment the following. Until then, it must
+ // // be done in the operation context destructors, which introduces a potential race.
+ // {
+ // stdx::lock_guard<Client> lk(*client);
+ // client->resetOperationContext();
+ // }
+ try {
+ for (const auto& observer : service->_clientObservers) {
+ observer->onDestroyOperationContext(opCtx);
+ }
+ } catch (...) {
+ std::terminate();
}
+ delete opCtx;
+}
- ServiceContext::LockedClientsCursor::LockedClientsCursor(ServiceContext* service)
- : _lock(service->_mutex),
- _curr(service->_clients.cbegin()),
- _end(service->_clients.cend()) {}
-
- Client* ServiceContext::LockedClientsCursor::next() {
- if (_curr == _end)
- return nullptr;
- Client* result = *_curr;
- ++_curr;
- return result;
- }
+void ServiceContext::registerClientObserver(std::unique_ptr<ClientObserver> observer) {
+ _clientObservers.push_back(std::move(observer));
+}
- BSONArray storageEngineList() {
- if (!hasGlobalServiceContext())
- return BSONArray();
+ServiceContext::LockedClientsCursor::LockedClientsCursor(ServiceContext* service)
+ : _lock(service->_mutex), _curr(service->_clients.cbegin()), _end(service->_clients.cend()) {}
- std::unique_ptr<StorageFactoriesIterator> sfi(
- getGlobalServiceContext()->makeStorageFactoriesIterator());
+Client* ServiceContext::LockedClientsCursor::next() {
+ if (_curr == _end)
+ return nullptr;
+ Client* result = *_curr;
+ ++_curr;
+ return result;
+}
- if (!sfi)
- return BSONArray();
+BSONArray storageEngineList() {
+ if (!hasGlobalServiceContext())
+ return BSONArray();
- BSONArrayBuilder engineArrayBuilder;
+ std::unique_ptr<StorageFactoriesIterator> sfi(
+ getGlobalServiceContext()->makeStorageFactoriesIterator());
- while (sfi->more()) {
- engineArrayBuilder.append(sfi->next()->getCanonicalName());
- }
+ if (!sfi)
+ return BSONArray();
- return engineArrayBuilder.arr();
- }
+ BSONArrayBuilder engineArrayBuilder;
- void appendStorageEngineList(BSONObjBuilder* result) {
- result->append("storageEngines", storageEngineList());
+ while (sfi->more()) {
+ engineArrayBuilder.append(sfi->next()->getCanonicalName());
}
+
+ return engineArrayBuilder.arr();
+}
+
+void appendStorageEngineList(BSONObjBuilder* result) {
+ result->append("storageEngines", storageEngineList());
+}
} // namespace mongo