From 701cf71ba3e8ffa706bfb49d3ea2d44dc1654126 Mon Sep 17 00:00:00 2001 From: Tyler Kaye Date: Mon, 22 Oct 2018 17:23:11 -0400 Subject: SERVER-34422-ThreadMetrics: ServerStatus now returns the number of active client operations (cherry picked from commit cdf319123d8e5d3cd169e2a11aec6aea0b951bf1) --- src/mongo/db/service_context.cpp | 13 +++++++++++++ src/mongo/db/service_context.h | 5 +++++ src/mongo/transport/service_entry_point_impl.cpp | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/src/mongo/db/service_context.cpp b/src/mongo/db/service_context.cpp index d1089916327..0930c9c9a61 100644 --- a/src/mongo/db/service_context.cpp +++ b/src/mongo/db/service_context.cpp @@ -59,6 +59,8 @@ using ConstructorActionList = stdx::list _numCurrentOps{0}; + } // namespace bool hasGlobalServiceContext() { @@ -239,6 +241,10 @@ void ServiceContext::ClientDeleter::operator()(Client* client) const { ServiceContext::UniqueOperationContext ServiceContext::makeOperationContext(Client* client) { auto opCtx = std::make_unique(client, _nextOpId.fetchAndAdd(1)); + if (client && client->session()) { + _numCurrentOps.addAndFetch(1); + } + onCreate(opCtx.get(), _clientObservers); if (!opCtx->lockState()) { opCtx->setLockState(std::make_unique()); @@ -256,6 +262,9 @@ ServiceContext::UniqueOperationContext ServiceContext::makeOperationContext(Clie void ServiceContext::OperationContextDeleter::operator()(OperationContext* opCtx) const { auto client = opCtx->getClient(); + if (client && client->session()) { + _numCurrentOps.subtractAndFetch(1); + } auto service = client->getServiceContext(); { stdx::lock_guard lk(*client); @@ -356,6 +365,10 @@ void ServiceContext::notifyStartupComplete() { _startupCompleteCondVar.notify_all(); } +int ServiceContext::getActiveClientOperations() { + return _numCurrentOps.load(); +} + namespace { /** diff --git a/src/mongo/db/service_context.h b/src/mongo/db/service_context.h index 96e965d53c7..55b0545171e 100644 --- a/src/mongo/db/service_context.h +++ b/src/mongo/db/service_context.h @@ -432,6 +432,11 @@ public: */ void notifyStartupComplete(); + /* + * Returns the number of active client operations + */ + int getActiveClientOperations(); + /** * Set the OpObserver. */ diff --git a/src/mongo/transport/service_entry_point_impl.cpp b/src/mongo/transport/service_entry_point_impl.cpp index a4935cb0fdc..2dc1562ffdb 100644 --- a/src/mongo/transport/service_entry_point_impl.cpp +++ b/src/mongo/transport/service_entry_point_impl.cpp @@ -37,6 +37,7 @@ #include #include "mongo/db/auth/restriction_environment.h" +#include "mongo/db/service_context.h" #include "mongo/transport/service_state_machine.h" #include "mongo/transport/session.h" #include "mongo/util/log.h" @@ -244,6 +245,9 @@ void ServiceEntryPointImpl::appendStats(BSONObjBuilder* bob) const { bob->append("current", static_cast(sessionCount)); bob->append("available", static_cast(_maxNumConnections - sessionCount)); bob->append("totalCreated", static_cast(_createdConnections.load())); + if (auto sc = getGlobalServiceContext()) { + bob->append("active", static_cast(sc->getActiveClientOperations())); + } if (_adminInternalPool) { BSONObjBuilder section(bob->subobjStart("adminConnections")); -- cgit v1.2.1