diff options
author | samantharitter <samantha.ritter@10gen.com> | 2017-06-13 14:36:56 -0400 |
---|---|---|
committer | Jason Carey <jcarey@argv.me> | 2017-07-06 12:39:32 -0400 |
commit | 0503521c2375a2e7bfbbda314ff997c77a372d66 (patch) | |
tree | 3f2d81aa90da070bf931dfedde195602ab71f5e8 /src/mongo | |
parent | 3ea2d70f0260b1ec6ec8c60d42d6a6669a802ef2 (diff) | |
download | mongo-0503521c2375a2e7bfbbda314ff997c77a372d66.tar.gz |
SERVER-29198 Implement ServiceLiasonMongod
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/SConscript | 18 | ||||
-rw-r--r-- | src/mongo/db/logical_session_cache_factory_mongod.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/service_liason_mongod.cpp | 91 | ||||
-rw-r--r-- | src/mongo/db/service_liason_mongod.h | 62 | ||||
-rw-r--r-- | src/mongo/util/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/util/periodic_runner_asio.cpp | 3 | ||||
-rw-r--r-- | src/mongo/util/periodic_runner_asio.h | 10 |
7 files changed, 188 insertions, 5 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index fad8e64c954..b5cfd10b189 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -940,6 +940,21 @@ envWithAsio.Library( ) env.Library( + target='service_liason_mongod', + source=[ + 'service_liason_mongod.cpp', + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/db/logical_session_id', + '$BUILD_DIR/mongo/util/clock_sources', + '$BUILD_DIR/mongo/util/periodic_runner', + 'clientcursor', + 'service_context', + 'service_liason', + ], +) + +env.Library( target='sessions_collection', source=[ 'sessions_collection.cpp', @@ -979,6 +994,7 @@ envWithAsio.CppUnitTest( 'logical_session_cache_test.cpp', ], LIBDEPS=[ + '$BUILD_DIR/mongo/db/service_context_noop_init', '$BUILD_DIR/mongo/executor/async_timer_mock', 'logical_session_cache', 'service_liason_mock', @@ -993,7 +1009,7 @@ envWithAsio.Library( ], LIBDEPS=[ 'logical_session_cache', - 'service_liason_mock', # TODO SERVER-29198 replace with service_liason_mongod + 'service_liason_mongod', 'sessions_collection_mock', # TODO SERVER-29201, SERVER-29202, SERVER-29203 ], ) diff --git a/src/mongo/db/logical_session_cache_factory_mongod.cpp b/src/mongo/db/logical_session_cache_factory_mongod.cpp index fcaa6937e68..6f253ca5116 100644 --- a/src/mongo/db/logical_session_cache_factory_mongod.cpp +++ b/src/mongo/db/logical_session_cache_factory_mongod.cpp @@ -32,7 +32,7 @@ #include "mongo/db/logical_session_cache_factory_mongod.h" -#include "mongo/db/service_liason_mock.h" +#include "mongo/db/service_liason_mongod.h" #include "mongo/db/sessions_collection_mock.h" #include "mongo/stdx/memory.h" @@ -62,8 +62,7 @@ std::unique_ptr<SessionsCollection> makeSessionsCollection(LogicalSessionCacheSe } // namespace std::unique_ptr<LogicalSessionCache> makeLogicalSessionCacheD(LogicalSessionCacheServer state) { - // TODO SERVER-29198 replace with ServiceLiasonMongod - auto liason = stdx::make_unique<MockServiceLiason>(std::make_shared<MockServiceLiasonImpl>()); + auto liason = stdx::make_unique<ServiceLiasonMongod>(); // Set up the logical session cache auto sessionsColl = makeSessionsCollection(state); diff --git a/src/mongo/db/service_liason_mongod.cpp b/src/mongo/db/service_liason_mongod.cpp new file mode 100644 index 00000000000..fdbf811de18 --- /dev/null +++ b/src/mongo/db/service_liason_mongod.cpp @@ -0,0 +1,91 @@ +/** + * Copyright (C) 2017 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_liason_mongod.h" + +#include "mongo/db/client.h" +#include "mongo/db/cursor_manager.h" +#include "mongo/db/operation_context.h" +#include "mongo/db/service_context.h" +#include "mongo/stdx/mutex.h" +#include "mongo/util/clock_source.h" +#include "mongo/util/periodic_runner.h" + +namespace mongo { + +LogicalSessionIdSet ServiceLiasonMongod::getActiveSessions() const { + LogicalSessionIdSet activeSessions; + + invariant(hasGlobalServiceContext()); + + // Walk through the service context and append lsids for all currently-running ops. + for (ServiceContext::LockedClientsCursor cursor(getGlobalServiceContext()); + Client* client = cursor.next();) { + + stdx::lock_guard<Client> lk(*client); + auto clientOpCtx = client->getOperationContext(); + + // Ignore clients without currently-running operations + if (!clientOpCtx) + continue; + + // Append this op ctx's session to our list, if it has one + auto lsid = clientOpCtx->getLogicalSessionId(); + if (lsid) { + activeSessions.insert(*lsid); + } + } + + // Append any in-use session ids from the global and collection-level cursor managers + { + auto client = Client::getCurrent(); + auto opCtx = client->makeOperationContext(); + CursorManager::appendAllActiveSessions(opCtx.get(), &activeSessions); + } + + return activeSessions; +} + +void ServiceLiasonMongod::scheduleJob(PeriodicRunner::PeriodicJob job) { + invariant(hasGlobalServiceContext()); + getGlobalServiceContext()->getPeriodicRunner()->scheduleJob(std::move(job)); +} + +void ServiceLiasonMongod::join() { + invariant(hasGlobalServiceContext()); + getGlobalServiceContext()->getPeriodicRunner()->shutdown(); +} + +Date_t ServiceLiasonMongod::now() const { + invariant(hasGlobalServiceContext()); + return getGlobalServiceContext()->getFastClockSource()->now(); +} + +} // namespace mongo diff --git a/src/mongo/db/service_liason_mongod.h b/src/mongo/db/service_liason_mongod.h new file mode 100644 index 00000000000..21c677feb8c --- /dev/null +++ b/src/mongo/db/service_liason_mongod.h @@ -0,0 +1,62 @@ +/** + * Copyright (C) 2017 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. + */ + +#pragma once + +#include "mongo/db/logical_session_id.h" +#include "mongo/db/operation_context.h" +#include "mongo/db/service_liason.h" +#include "mongo/util/periodic_runner.h" +#include "mongo/util/time_support.h" + +namespace mongo { + +/** + * This is the service liason to mongod for the logical session cache. + * + * This class will return active sessions for cursors stored in the + * global cursor manager and cursors in per-collection managers. This + * class will also walk the service context to find all sessions for + * currently-running operations on this server. + * + * Job scheduling on this class will be handled behind the scenes by a + * periodic runner for this mongod. The time will be returned from the + * system clock. + */ +class ServiceLiasonMongod : public ServiceLiason { +public: + LogicalSessionIdSet getActiveSessions() const override; + + void scheduleJob(PeriodicRunner::PeriodicJob job) override; + + void join() override; + + Date_t now() const override; +}; + +} // namespace mongo diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript index 604743801c4..e348375d4a7 100644 --- a/src/mongo/util/SConscript +++ b/src/mongo/util/SConscript @@ -295,6 +295,7 @@ env.Library( ], LIBDEPS=[ "$BUILD_DIR/mongo/base", + "$BUILD_DIR/mongo/db/service_context", "$BUILD_DIR/third_party/shim_asio", "$BUILD_DIR/mongo/executor/async_timer_asio", "periodic_runner", @@ -307,8 +308,9 @@ env.CppUnitTest( "periodic_runner_asio_test.cpp", ], LIBDEPS=[ - "periodic_runner_asio", + '$BUILD_DIR/mongo/db/service_context_noop_init', "$BUILD_DIR/mongo/executor/async_timer_mock", + "periodic_runner_asio", ], ) diff --git a/src/mongo/util/periodic_runner_asio.cpp b/src/mongo/util/periodic_runner_asio.cpp index f1648b090cf..9fadfc67124 100644 --- a/src/mongo/util/periodic_runner_asio.cpp +++ b/src/mongo/util/periodic_runner_asio.cpp @@ -35,6 +35,7 @@ #include "mongo/util/periodic_runner_asio.h" +#include "mongo/db/client.h" #include "mongo/util/log.h" namespace mongo { @@ -109,6 +110,8 @@ Status PeriodicRunnerASIO::startup() { _state = State::kRunning; _thread = stdx::thread([this]() { try { + Client::initThread("PeriodicRunnerASIO"); + asio::io_service::work workItem(_io_service); std::error_code ec; _io_service.run(ec); diff --git a/src/mongo/util/periodic_runner_asio.h b/src/mongo/util/periodic_runner_asio.h index b35f29bd66f..e5be509f0ea 100644 --- a/src/mongo/util/periodic_runner_asio.h +++ b/src/mongo/util/periodic_runner_asio.h @@ -42,6 +42,16 @@ namespace mongo { * to schedule and run jobs at regular intervals. * * This class takes a timer factory so that it may be mocked out for testing. + * + * The runner will set up a client on its internal thread. Scheduled jobs that require + * an operation context should use Client::getCurrent()->makeOperationContext() to + * create one for themselves, and MUST clear it before they return. + * + * The thread running internally will use the thread name "PeriodicRunnerASIO" and + * anything logged from within a scheduled background task will use this thread name. + * Scheduled tasks may set the thread name to a custom value as they run. However, + * if they do this, they MUST set the thread name back to its original value before + * they return. */ class PeriodicRunnerASIO : public PeriodicRunner { public: |