diff options
| author | Shin Yee Tan <shinyee.tan@mongodb.com> | 2024-09-17 12:15:05 -0700 |
|---|---|---|
| committer | MongoDB Bot <mongo-bot@mongodb.com> | 2024-09-17 19:59:17 +0000 |
| commit | 905bbffe727ccb124631843c95a6c0b8678730bf (patch) | |
| tree | 2ff35ea8e70ed93d44a8b5f4ab3f2f2608b86c6a | |
| parent | 09fe0ee4f033e1b7d26c9b40c82cbdf16928c813 (diff) | |
SERVER-94846 Move WiredTigerEventHandler out of wiredtiger_util files (#27126)
GitOrigin-RevId: 3caf18a9e418919dcc4e844ad9e68d57b33b1b43
5 files changed, 195 insertions, 118 deletions
diff --git a/src/mongo/db/storage/wiredtiger/SConscript b/src/mongo/db/storage/wiredtiger/SConscript index 743491f1897..05571287841 100644 --- a/src/mongo/db/storage/wiredtiger/SConscript +++ b/src/mongo/db/storage/wiredtiger/SConscript @@ -23,6 +23,7 @@ wtEnv.Library( "wiredtiger_compiled_configuration.cpp", "wiredtiger_cursor.cpp", "wiredtiger_cursor_helpers.cpp", + "wiredtiger_event_handler.cpp", "wiredtiger_global_options.cpp", "wiredtiger_index.cpp", "wiredtiger_index_util.cpp", diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.cpp new file mode 100644 index 00000000000..0f236d67383 --- /dev/null +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.cpp @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2024-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * 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 + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * 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 Server Side 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/db/storage/wiredtiger/wiredtiger_event_handler.h" +#include "mongo/logv2/log.h" + +namespace mongo { + +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kWiredTiger + +void WiredTigerEventHandler::setWtConnReady(WT_CONNECTION* conn) { + stdx::unique_lock<mongo::Mutex> lock(_mutex); + _wtConn = conn; + if (_activeReaders == 0 || conn) { + return; + } + LOGV2(7003100, + "WiredTiger connection close is waiting for active statistics readers to finish", + "activeReaders"_attr = _activeReaders); + _idleCondition.wait(lock, [this]() { return _activeReaders == 0; }); +} + +WT_CONNECTION* WiredTigerEventHandler::getStatsCollectionPermit() { + stdx::lock_guard<mongo::Mutex> lock(_mutex); + if (_wtConn) { + _activeReaders++; + return _wtConn; + } + return nullptr; +} + +void WiredTigerEventHandler::releaseStatsCollectionPermit() { + stdx::unique_lock<mongo::Mutex> lock(_mutex); + _activeReaders--; + if (_activeReaders == 0 && !_wtConn) { + _idleCondition.notify_all(); + return; + } +} +} // namespace mongo diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.h b/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.h new file mode 100644 index 00000000000..891f0cbe480 --- /dev/null +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_event_handler.h @@ -0,0 +1,127 @@ +/** + * Copyright (C) 2024-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * 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 + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * 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 Server Side 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 <boost/optional/optional.hpp> +#include <cstdint> +#include <wiredtiger.h> + +#include "mongo/platform/mutex.h" +#include "mongo/stdx/condition_variable.h" + +namespace mongo { +/** + * Returns a WT_EVENT_HANDLER with MongoDB's default handlers. + * The default handlers just log so it is recommended that you consider calling them even if + * you are capturing the output. + * + * There is no default "close" handler. You only need to provide one if you need to call a + * destructor. + */ +class WiredTigerEventHandler : private WT_EVENT_HANDLER { +public: + WiredTigerEventHandler(); + + WT_EVENT_HANDLER* getWtEventHandler(); + + bool wasStartupSuccessful() { + return _startupSuccessful; + } + + void setStartupSuccessful() { + _startupSuccessful = true; + } + + bool isWtIncompatible() { + return _wtIncompatible; + } + + void setWtIncompatible() { + _wtIncompatible = true; + } + + /** + * Updates the current WT connection usable for safe statistics collection, or nullptr if + * statistics collection is no longer safe. + * + * If the WT_CONNECTION is non-null or there are no active statistics readers, the following + * function returns immediately. Otherwise, if there are active statistics readers and a + * WT_CONN_CLOSE event is setting this connection to be unavailable for statistics collection, + * this function waits until all active readers release their collection permits. + */ + void setWtConnReady(WT_CONNECTION* conn); + void setWtConnNotReady() { + setWtConnReady(nullptr); + } + + /** + * This function obtains one permit to safely use WiredTiger statistics cursors. When this + * function returns a non-null connection pointer, the caller may safely collect metrics using + * this connection, but storage engine shutdown is blocked. The caller *must* make a subsequent + * call to `releaseStatsCollectionPermit` to allow the storage engine to shut down. + * + * If this function returns nullptr, statistics collection is not available due to startup or + * shutdown. + */ + WT_CONNECTION* getStatsCollectionPermit(); + + /** + * The following call releases one statistics collection permit. When there are no outstanding + * permits, the WT connection is allowed to shut down cleanly. + */ + void releaseStatsCollectionPermit(); + + /** + * Returns the number of outstanding, active statistics collection permits. + */ + int32_t getActiveStatsReaders() const { + stdx::lock_guard<mongo::Mutex> lock(_mutex); + return _activeReaders; + } + + /** + * Returns true if the WT connection is ready for statistics cursors to be used. This is unsafe + * because the connection can become invalid immediately after returning. To ensure the + * connection stays valid, a permit must be obtained with getStatsCollectionPermit(). + */ + bool isWtConnReadyForStatsCollection() const { + stdx::lock_guard<mongo::Mutex> lock(_mutex); + return _wtConn != nullptr; + } + +private: + bool _startupSuccessful = false; + bool _wtIncompatible = false; + mutable mongo::Mutex _mutex = MONGO_MAKE_LATCH("mongo::WiredTigerEventHandler::_mutex"); + WT_CONNECTION* _wtConn = nullptr; + stdx::condition_variable _idleCondition; + int32_t _activeReaders{0}; +}; +} // namespace mongo diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp index 9c163400f22..5c8aa8db1a2 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp @@ -93,36 +93,6 @@ "transaction is too large and will not fit in the storage engine cache" namespace mongo { -void WiredTigerEventHandler::setWtConnReady(WT_CONNECTION* conn) { - stdx::unique_lock<mongo::Mutex> lock(_mutex); - _wtConn = conn; - if (_activeReaders == 0 || conn) { - return; - } - LOGV2(7003100, - "WiredTiger connection close is waiting for active statistics readers to finish", - "activeReaders"_attr = _activeReaders); - _idleCondition.wait(lock, [this]() { return _activeReaders == 0; }); -} - -WT_CONNECTION* WiredTigerEventHandler::getStatsCollectionPermit() { - stdx::lock_guard<mongo::Mutex> lock(_mutex); - if (_wtConn) { - _activeReaders++; - return _wtConn; - } - return nullptr; -} - -void WiredTigerEventHandler::releaseStatsCollectionPermit() { - stdx::unique_lock<mongo::Mutex> lock(_mutex); - _activeReaders--; - if (_activeReaders == 0 && !_wtConn) { - _idleCondition.notify_all(); - return; - } -} - namespace { // TODO SERVER-81069: Remove this. diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_util.h b/src/mongo/db/storage/wiredtiger/wiredtiger_util.h index 14f5f6c6da9..b937ca0b309 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_util.h +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_util.h @@ -48,6 +48,7 @@ #include "mongo/db/catalog/validate_results.h" #include "mongo/db/namespace_string.h" #include "mongo/db/storage/durable_catalog.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_event_handler.h" #include "mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h" #include "mongo/platform/compiler.h" #include "mongo/platform/mutex.h" @@ -137,94 +138,6 @@ struct WiredTigerItem : public WT_ITEM { } }; -/** - * Returns a WT_EVENT_HANDLER with MongoDB's default handlers. - * The default handlers just log so it is recommended that you consider calling them even if - * you are capturing the output. - * - * There is no default "close" handler. You only need to provide one if you need to call a - * destructor. - */ -class WiredTigerEventHandler : private WT_EVENT_HANDLER { -public: - WiredTigerEventHandler(); - - WT_EVENT_HANDLER* getWtEventHandler(); - - bool wasStartupSuccessful() { - return _startupSuccessful; - } - - void setStartupSuccessful() { - _startupSuccessful = true; - } - - bool isWtIncompatible() { - return _wtIncompatible; - } - - void setWtIncompatible() { - _wtIncompatible = true; - } - - /** - * Updates the current WT connection usable for safe statistics collection, or nullptr if - * statistics collection is no longer safe. - * - * If the WT_CONNECTION is non-null or there are no active statistics readers, the following - * function returns immediately. Otherwise, if there are active statistics readers and a - * WT_CONN_CLOSE event is setting this connection to be unavailable for statistics collection, - * this function waits until all active readers release their collection permits. - */ - void setWtConnReady(WT_CONNECTION* conn); - void setWtConnNotReady() { - setWtConnReady(nullptr); - } - - /** - * This function obtains one permit to safely use WiredTiger statistics cursors. When this - * function returns a non-null connection pointer, the caller may safely collect metrics using - * this connection, but storage engine shutdown is blocked. The caller *must* make a subsequent - * call to `releaseStatsCollectionPermit` to allow the storage engine to shut down. - * - * If this function returns nullptr, statistics collection is not available due to startup or - * shutdown. - */ - WT_CONNECTION* getStatsCollectionPermit(); - - /** - * The following call releases one statistics collection permit. When there are no outstanding - * permits, the WT connection is allowed to shut down cleanly. - */ - void releaseStatsCollectionPermit(); - - /** - * Returns the number of outstanding, active statistics collection permits. - */ - int32_t getActiveStatsReaders() const { - stdx::lock_guard<mongo::Mutex> lock(_mutex); - return _activeReaders; - } - - /** - * Returns true if the WT connection is ready for statistics cursors to be used. This is unsafe - * because the connection can become invalid immediately after returning. To ensure the - * connection stays valid, a permit must be obtained with getStatsCollectionPermit(). - */ - bool isWtConnReadyForStatsCollection() const { - stdx::lock_guard<mongo::Mutex> lock(_mutex); - return _wtConn != nullptr; - } - -private: - bool _startupSuccessful = false; - bool _wtIncompatible = false; - mutable mongo::Mutex _mutex = MONGO_MAKE_LATCH("mongo::WiredTigerEventHandler::_mutex"); - WT_CONNECTION* _wtConn = nullptr; - stdx::condition_variable _idleCondition; - int32_t _activeReaders{0}; -}; - class WiredTigerUtil { WiredTigerUtil(const WiredTigerUtil&) = delete; WiredTigerUtil& operator=(const WiredTigerUtil&) = delete; |
