diff options
author | Blake Oler <blake.oler@mongodb.com> | 2022-11-17 20:07:09 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-11-17 21:39:08 +0000 |
commit | e377b81cc3be98a4aeb9081c2110a6a52694a396 (patch) | |
tree | 1adc8cfca8c128827eecf17c7d825bfc4c3f0238 /src/mongo/logv2 | |
parent | 669df50d87d20b0190dd272a51eb256944f1a480 (diff) | |
download | mongo-e377b81cc3be98a4aeb9081c2110a6a52694a396.tar.gz |
SERVER-71410 Fix SeveritySuppressor so that quiet/normal log intervals are correct
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r-- | src/mongo/logv2/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/logv2/log_severity_suppressor.h | 22 | ||||
-rw-r--r-- | src/mongo/logv2/log_severity_suppressor_test.cpp | 97 |
3 files changed, 116 insertions, 5 deletions
diff --git a/src/mongo/logv2/SConscript b/src/mongo/logv2/SConscript index af44c667c3d..34f91574b7f 100644 --- a/src/mongo/logv2/SConscript +++ b/src/mongo/logv2/SConscript @@ -9,11 +9,13 @@ env.CppUnitTest( source=[ 'logv2_component_test.cpp', 'logv2_test.cpp', + 'log_severity_suppressor_test.cpp', 'redaction_test.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/db/auth/security_token', '$BUILD_DIR/mongo/db/server_base', + '$BUILD_DIR/mongo/util/clock_source_mock', ], ) diff --git a/src/mongo/logv2/log_severity_suppressor.h b/src/mongo/logv2/log_severity_suppressor.h index 6795efcd715..d5e489e315d 100644 --- a/src/mongo/logv2/log_severity_suppressor.h +++ b/src/mongo/logv2/log_severity_suppressor.h @@ -40,6 +40,7 @@ #include "mongo/logv2/log_severity.h" #include "mongo/platform/mutex.h" #include "mongo/stdx/unordered_map.h" +#include "mongo/util/clock_source.h" #include "mongo/util/time_support.h" namespace mongo::logv2 { @@ -115,20 +116,31 @@ private: class SeveritySuppressor { public: + SeveritySuppressor(ClockSource* cs, Milliseconds period, LogSeverity normal, LogSeverity quiet) + : _clock(cs), _period{period}, _normal{normal}, _quiet{quiet} {} + SeveritySuppressor(Milliseconds period, LogSeverity normal, LogSeverity quiet) - : _period{period}, _normal{normal}, _quiet{quiet} {} + : SeveritySuppressor(nullptr, period, normal, quiet) {} LogSeverity operator()() { - auto now = Date_t::now(); + auto now = _now(); auto lg = stdx::lock_guard(_mutex); if (_expire <= now) { - _expire = now + Seconds{1}; - return _quiet; + _expire = now + _period; + return _normal; } - return _normal; + return _quiet; } private: + Date_t _now() { + if (_clock) { + return _clock->now(); + } + return Date_t::now(); + } + + ClockSource* _clock; Milliseconds _period; LogSeverity _normal; LogSeverity _quiet; diff --git a/src/mongo/logv2/log_severity_suppressor_test.cpp b/src/mongo/logv2/log_severity_suppressor_test.cpp new file mode 100644 index 00000000000..f8b3d94e117 --- /dev/null +++ b/src/mongo/logv2/log_severity_suppressor_test.cpp @@ -0,0 +1,97 @@ +/** + * Copyright (C) 2022-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/logv2/log_severity_suppressor.h" + +#include "mongo/unittest/unittest.h" +#include "mongo/util/clock_source_mock.h" +#include "mongo/util/duration.h" + +namespace mongo::logv2 { +namespace { + +TEST(LogSeveritySuppressorTest, SuppressorWorksCorrectly) { + LogSeverity normalSeverity = LogSeverity::Info(); + LogSeverity quietSeverity = LogSeverity::Debug(2); + int quiesceMs = 1000; + Milliseconds quiescePeriod{quiesceMs}; + + ClockSourceMock clockSource; + + for (int i = 0; i < quiesceMs; ++i) { + SeveritySuppressor suppressor(&clockSource, quiescePeriod, normalSeverity, quietSeverity); + ASSERT_EQ(suppressor(), normalSeverity); + clockSource.advance(Milliseconds{i}); + ASSERT_EQ(suppressor(), quietSeverity); + ASSERT_EQ(suppressor(), quietSeverity); + } + + SeveritySuppressor suppressor(&clockSource, quiescePeriod, normalSeverity, quietSeverity); + ASSERT_EQ(suppressor(), normalSeverity); + + clockSource.advance(quiescePeriod); + ASSERT_EQ(suppressor(), normalSeverity); + + clockSource.advance(quiescePeriod * 2); + ASSERT_EQ(suppressor(), normalSeverity); +} + +TEST(LogSeveritySuppressorTest, BackwardsClockMovementRetainsQuietness) { + LogSeverity normalSeverity = LogSeverity::Info(); + LogSeverity quietSeverity = LogSeverity::Debug(2); + int quiesceMs = 1000; + Milliseconds quiescePeriod{quiesceMs}; + + Date_t timeOne = Date_t::fromMillisSinceEpoch(0); + Date_t timeTwo = Date_t::fromMillisSinceEpoch(quiesceMs); + Date_t timeThree = Date_t::fromMillisSinceEpoch(quiesceMs + (quiesceMs / 2)); + + ClockSourceMock clockSource; + clockSource.reset(timeTwo); + + SeveritySuppressor suppressor(&clockSource, quiescePeriod, normalSeverity, quietSeverity); + ASSERT_EQ(suppressor(), normalSeverity); + ASSERT_EQ(suppressor(), quietSeverity); + + clockSource.reset(timeThree); + ASSERT_EQ(suppressor(), quietSeverity); + + clockSource.reset(timeTwo); + ASSERT_EQ(suppressor(), quietSeverity); + + clockSource.reset(timeOne); + ASSERT_EQ(suppressor(), quietSeverity); + + clockSource.reset(timeThree); + ASSERT_EQ(suppressor(), quietSeverity); +} + +} // namespace + +} // namespace mongo::logv2 |