diff options
author | Andrew Shuvalov <andrew.shuvalov@mongodb.com> | 2021-08-23 15:49:20 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-08-23 16:24:22 +0000 |
commit | 57ccc00599906803b361b40f30898e6dab1936d5 (patch) | |
tree | 9c26a2fdd9025c7f20e96c0c327abab8b92673ba /src/mongo/db/process_health | |
parent | 6144e4b699aa0a68f47348d869a3d5875fa09f8f (diff) | |
download | mongo-57ccc00599906803b361b40f30898e6dab1936d5.tar.gz |
SERVER-59357 Initial scaffolding of the Fault class
Diffstat (limited to 'src/mongo/db/process_health')
-rw-r--r-- | src/mongo/db/process_health/SConscript | 15 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault.h | 90 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault_impl.cpp | 59 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault_impl.h | 70 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault_impl_test.cpp | 71 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault_manager.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/process_health/fault_manager.h | 13 |
7 files changed, 301 insertions, 19 deletions
diff --git a/src/mongo/db/process_health/SConscript b/src/mongo/db/process_health/SConscript index f59342d05bf..4758adbd836 100644 --- a/src/mongo/db/process_health/SConscript +++ b/src/mongo/db/process_health/SConscript @@ -1,16 +1,17 @@ # -*- mode: python -*- -Import("env") +Import('env') env = env.Clone() env.Library( target='fault_manager', source=[ - "fault_manager.cpp", + 'fault_impl.cpp', + 'fault_manager.cpp', ], LIBDEPS=[ - "$BUILD_DIR/mongo/base", + '$BUILD_DIR/mongo/base', '$BUILD_DIR/mongo/db/service_context', ], LIBDEPS_PRIVATE=[ @@ -18,11 +19,13 @@ env.Library( ) env.CppUnitTest( - target="fault_manager_test", + target='fault_base_classes_test', source=[ - "fault_manager_test.cpp", + 'fault_impl_test.cpp', + 'fault_manager_test.cpp', ], LIBDEPS=[ - "fault_manager", + '$BUILD_DIR/mongo/util/clock_source_mock', + 'fault_manager', ], ) diff --git a/src/mongo/db/process_health/fault.h b/src/mongo/db/process_health/fault.h new file mode 100644 index 00000000000..6dbb2358772 --- /dev/null +++ b/src/mongo/db/process_health/fault.h @@ -0,0 +1,90 @@ +/** + * Copyright (C) 2021-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 <memory> + +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/util/duration.h" +#include "mongo/util/uuid.h" + +namespace mongo { +namespace process_health { + +/** + * Detailed description of the current fault. + * @see FaultManager for more details. + */ +class Fault { + Fault(const Fault&) = delete; + Fault& operator=(const Fault&) = delete; + +public: + Fault() = default; + virtual ~Fault() = default; + + virtual UUID getId() const = 0; + + /** + * The fault severity value is an aggregate severity calculated + * from all facets currently owned by this instance. + * + * @return Current fault severity. The expected values: + * 0: Ok + * (0, 1.0): Transient fault condition + * [1.0, Inf): Active fault condition + */ + virtual double getSeverity() const = 0; + + /** + * Gets the duration of an active fault, if any. + * This is the time from the moment the severity reached the 1.0 value + * and stayed on or above 1.0. + * + * Note: each time the severity drops below 1.0 the duration is reset. + */ + virtual Milliseconds getActiveFaultDuration() const = 0; + + /** + * @return The lifetime of this fault from the moment it was created. + * Invariant: getDuration() >= getActiveFaultDuration() + */ + virtual Milliseconds getDuration() const = 0; + + /** + * Describes the current fault. + */ + virtual void appendDescription(BSONObjBuilder* builder) const = 0; +}; + +using FaultConstPtr = std::shared_ptr<const Fault>; + + +} // namespace process_health +} // namespace mongo diff --git a/src/mongo/db/process_health/fault_impl.cpp b/src/mongo/db/process_health/fault_impl.cpp new file mode 100644 index 00000000000..1a0a9db9f32 --- /dev/null +++ b/src/mongo/db/process_health/fault_impl.cpp @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2021-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/process_health/fault_impl.h" + +namespace mongo { +namespace process_health { + +FaultImpl::FaultImpl(ServiceContext* svcCtx) + : _svcCtx(svcCtx), _startTime(_svcCtx->getFastClockSource()->now()) { + invariant(svcCtx); // Will crash before this line, just for readability. +} + +UUID FaultImpl::getId() const { + return _id; +} + +double FaultImpl::getSeverity() const { + return 0; +} + +Milliseconds FaultImpl::getActiveFaultDuration() const { + return Milliseconds(0); +} + +Milliseconds FaultImpl::getDuration() const { + return Milliseconds(_svcCtx->getFastClockSource()->now() - _startTime); +} + +void FaultImpl::appendDescription(BSONObjBuilder* builder) const {} + +} // namespace process_health +} // namespace mongo diff --git a/src/mongo/db/process_health/fault_impl.h b/src/mongo/db/process_health/fault_impl.h new file mode 100644 index 00000000000..83deb49158a --- /dev/null +++ b/src/mongo/db/process_health/fault_impl.h @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2021-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 "mongo/db/process_health/fault.h" + +#include "mongo/db/service_context.h" +#include "mongo/util/clock_source.h" +#include "mongo/util/system_clock_source.h" +#include "mongo/util/timer.h" + +namespace mongo { +namespace process_health { + +/** + * Internal implementation of the Fault class. + * @see Fault + */ +class FaultImpl : public Fault { +public: + explicit FaultImpl(ServiceContext* svcCtx); + + ~FaultImpl() override = default; + + // Fault interface. + + UUID getId() const override; + + double getSeverity() const override; + + Milliseconds getActiveFaultDuration() const override; + + Milliseconds getDuration() const override; + + void appendDescription(BSONObjBuilder* builder) const override; + +private: + ServiceContext* const _svcCtx; + const UUID _id = UUID::gen(); + const Date_t _startTime; +}; + +} // namespace process_health +} // namespace mongo diff --git a/src/mongo/db/process_health/fault_impl_test.cpp b/src/mongo/db/process_health/fault_impl_test.cpp new file mode 100644 index 00000000000..e46cb5c4310 --- /dev/null +++ b/src/mongo/db/process_health/fault_impl_test.cpp @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2021-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/process_health/fault_impl.h" + +#include "mongo/unittest/unittest.h" +#include "mongo/util/clock_source_mock.h" + +namespace mongo { +namespace process_health { +namespace { + +class FaultImplTest : public unittest::Test { +public: + void setUp() override { + _svcCtx = ServiceContext::make(); + _svcCtx->setFastClockSource(std::make_unique<ClockSourceMock>()); + _faultImpl = std::make_unique<FaultImpl>(_svcCtx.get()); + } + + ClockSourceMock& clockSource() { + return *static_cast<ClockSourceMock*>(_svcCtx->getFastClockSource()); + } + + FaultImpl& fault() { + return *_faultImpl; + } + +private: + ServiceContext::UniqueServiceContext _svcCtx; + + std::unique_ptr<FaultImpl> _faultImpl; +}; + + +TEST_F(FaultImplTest, TimeSourceWorks) { + // Fault was just created, duration should be zero. + ASSERT_EQ(Milliseconds(0), fault().getDuration()); + clockSource().advance(Milliseconds(1)); + ASSERT_EQ(Milliseconds(1), fault().getDuration()); +} + +} // namespace +} // namespace process_health +} // namespace mongo diff --git a/src/mongo/db/process_health/fault_manager.cpp b/src/mongo/db/process_health/fault_manager.cpp index 385721b8106..b7400c373e8 100644 --- a/src/mongo/db/process_health/fault_manager.cpp +++ b/src/mongo/db/process_health/fault_manager.cpp @@ -66,7 +66,7 @@ FaultState FaultManager::getFaultState() const { return FaultState::kOk; } -boost::optional<FaultManager::FaultConstPtr> FaultManager::activeFault() const { +boost::optional<FaultConstPtr> FaultManager::activeFault() const { return {}; } diff --git a/src/mongo/db/process_health/fault_manager.h b/src/mongo/db/process_health/fault_manager.h index d886bf43917..ccdf8cf96eb 100644 --- a/src/mongo/db/process_health/fault_manager.h +++ b/src/mongo/db/process_health/fault_manager.h @@ -30,6 +30,7 @@ #include <memory> +#include "mongo/db/process_health/fault.h" #include "mongo/db/service_context.h" namespace mongo { @@ -68,9 +69,6 @@ class FaultManager { FaultManager& operator=(const FaultManager&) = delete; public: - class Fault; - using FaultConstPtr = std::shared_ptr<const Fault>; - FaultManager() = default; virtual ~FaultManager(); @@ -84,15 +82,6 @@ public: // Returns the current fault, if any. virtual boost::optional<FaultConstPtr> activeFault() const; - /** - * Detailed description of the current fault. - */ - class Fault { - public: - Fault() = default; - virtual ~Fault() {} - }; - protected: // Starts the health check sequence and updates the internal state on completion. // This is invoked by the internal timer. |