From a376d366cf8f00d92f35b2e657f8cb38399e0571 Mon Sep 17 00:00:00 2001 From: Alex Konradi Date: Wed, 13 Jan 2021 14:20:53 -0500 Subject: [PATCH] refactor: use unitfloat in more places (#14396) Commit Message: Use UnitFloat in place of float in more locations Additional Description: UnitFloat represents a floating point value that is guaranteed to be in the range [0, 1]. Use it in place of floats that also have the same expectation in OverloadActionState and connection listeners. This PR introduces no functional changes. Risk Level: low Testing: ran affected tests Docs Changes: n/a Release Notes: n/a Platform Specific Features: n/a Signed-off-by: Alex Konradi --- include/envoy/common/BUILD | 1 + include/envoy/common/random_generator.h | 10 ++++++---- .../envoy/event/scaled_range_timer_manager.h | 4 +++- include/envoy/network/BUILD | 2 ++ include/envoy/network/connection_handler.h | 4 +++- include/envoy/network/listener.h | 4 +++- .../overload/thread_local_overload_state.h | 2 +- source/common/common/interval_value.h | 20 +++++++++++++++++++ .../event/scaled_range_timer_manager_impl.cc | 4 ++-- .../event/scaled_range_timer_manager_impl.h | 4 +++- source/common/network/tcp_listener_impl.cc | 3 +-- source/common/network/tcp_listener_impl.h | 6 ++++-- source/common/network/udp_listener_impl.h | 2 +- source/server/connection_handler_impl.cc | 2 +- source/server/connection_handler_impl.h | 4 ++-- source/server/overload_manager_impl.cc | 13 ++++++++++-- source/server/worker_impl.cc | 2 +- test/common/common/BUILD | 1 + test/common/common/random_generator_test.cc | 9 ++++----- .../scaled_range_timer_manager_impl_test.cc | 20 +++++++++---------- test/common/network/listener_impl_test.cc | 6 +++--- test/mocks/event/mocks.h | 2 +- test/mocks/network/mocks.h | 6 +++--- test/server/connection_handler_test.cc | 10 +++++----- test/server/overload_manager_impl_test.cc | 19 +++++++++--------- 25 files changed, 102 insertions(+), 58 deletions(-) diff --git a/include/envoy/common/BUILD b/include/envoy/common/BUILD index d120b7c0cc54..f831dcd62708 100644 --- a/include/envoy/common/BUILD +++ b/include/envoy/common/BUILD @@ -38,6 +38,7 @@ envoy_cc_library( envoy_cc_library( name = "random_generator_interface", hdrs = ["random_generator.h"], + deps = ["//source/common/common:interval_value"], ) envoy_cc_library( diff --git a/include/envoy/common/random_generator.h b/include/envoy/common/random_generator.h index f778d909a49b..ed4c079d2fb0 100644 --- a/include/envoy/common/random_generator.h +++ b/include/envoy/common/random_generator.h @@ -6,6 +6,8 @@ #include "envoy/common/pure.h" +#include "common/common/interval_value.h" + namespace Envoy { namespace Random { @@ -50,13 +52,13 @@ class RandomGenerator { /** * @return a random boolean value, with probability `p` equaling true. */ - bool bernoulli(float p) { - if (p <= 0) { + bool bernoulli(UnitFloat p) { + if (p == UnitFloat::min()) { return false; - } else if (p >= 1) { + } else if (p == UnitFloat::max()) { return true; } - return random() < static_cast(p * static_cast(max())); + return random() < static_cast(p.value() * static_cast(max())); } }; diff --git a/include/envoy/event/scaled_range_timer_manager.h b/include/envoy/event/scaled_range_timer_manager.h index 7defc1b45be7..9cf477b692c9 100644 --- a/include/envoy/event/scaled_range_timer_manager.h +++ b/include/envoy/event/scaled_range_timer_manager.h @@ -4,6 +4,8 @@ #include "envoy/event/scaled_timer_minimum.h" #include "envoy/event/timer.h" +#include "common/common/interval_value.h" + #include "absl/types/variant.h" namespace Envoy { @@ -36,7 +38,7 @@ class ScaledRangeTimerManager { * factor of 0.5 causes firing halfway between min and max, and a factor of 1 causes firing at * max. */ - virtual void setScaleFactor(double scale_factor) PURE; + virtual void setScaleFactor(UnitFloat scale_factor) PURE; }; using ScaledRangeTimerManagerPtr = std::unique_ptr; diff --git a/include/envoy/network/BUILD b/include/envoy/network/BUILD index 64cdece4ab9f..ccb27c6eb794 100644 --- a/include/envoy/network/BUILD +++ b/include/envoy/network/BUILD @@ -38,6 +38,7 @@ envoy_cc_library( ":listen_socket_interface", ":listener_interface", "//include/envoy/ssl:context_interface", + "//source/common/common:interval_value", ], ) @@ -173,6 +174,7 @@ envoy_cc_library( "//include/envoy/common:resource_interface", "//include/envoy/init:manager_interface", "//include/envoy/stats:stats_interface", + "//source/common/common:interval_value", "@envoy_api//envoy/config/core/v3:pkg_cc_proto", ], ) diff --git a/include/envoy/network/connection_handler.h b/include/envoy/network/connection_handler.h index c42cc290cd61..a7afc67b20c8 100644 --- a/include/envoy/network/connection_handler.h +++ b/include/envoy/network/connection_handler.h @@ -9,6 +9,8 @@ #include "envoy/network/listener.h" #include "envoy/ssl/context.h" +#include "common/common/interval_value.h" + namespace Envoy { namespace Network { @@ -98,7 +100,7 @@ class ConnectionHandler { * Set the fraction of connections the listeners should reject. * @param reject_fraction a value between 0 (reject none) and 1 (reject all). */ - virtual void setListenerRejectFraction(float reject_fraction) PURE; + virtual void setListenerRejectFraction(UnitFloat reject_fraction) PURE; /** * @return the stat prefix used for per-handler stats. diff --git a/include/envoy/network/listener.h b/include/envoy/network/listener.h index 73edb4e7e9d5..5a182c0f44a7 100644 --- a/include/envoy/network/listener.h +++ b/include/envoy/network/listener.h @@ -16,6 +16,8 @@ #include "envoy/network/udp_packet_writer_handler.h" #include "envoy/stats/scope.h" +#include "common/common/interval_value.h" + namespace Envoy { namespace Network { @@ -332,7 +334,7 @@ class Listener { * Set the fraction of incoming connections that will be closed immediately * after being opened. */ - virtual void setRejectFraction(float reject_fraction) PURE; + virtual void setRejectFraction(UnitFloat reject_fraction) PURE; }; using ListenerPtr = std::unique_ptr; diff --git a/include/envoy/server/overload/thread_local_overload_state.h b/include/envoy/server/overload/thread_local_overload_state.h index ef7a701618d9..e7531253d31a 100644 --- a/include/envoy/server/overload/thread_local_overload_state.h +++ b/include/envoy/server/overload/thread_local_overload_state.h @@ -28,7 +28,7 @@ class OverloadActionState { explicit constexpr OverloadActionState(UnitFloat value) : action_value_(value) {} - float value() const { return action_value_.value(); } + UnitFloat value() const { return action_value_; } bool isSaturated() const { return action_value_.value() == UnitFloat::max().value(); } private: diff --git a/source/common/common/interval_value.h b/source/common/common/interval_value.h index 454c493e0d25..3a058eaae4b5 100644 --- a/source/common/common/interval_value.h +++ b/source/common/common/interval_value.h @@ -22,6 +22,26 @@ template class ClosedIntervalValue { T value() const { return value_; } + // Returns a value that is as far from max as the original value is from min. + // This guarantees that max().invert() == min() and min().invert() == max(). + ClosedIntervalValue invert() const { + return ClosedIntervalValue(value_ == Interval::max_value + ? Interval::min_value + : value_ == Interval::min_value + ? Interval::max_value + : Interval::max_value - (value_ - Interval::min_value)); + } + + // Comparisons are performed using the same operators on the underlying value + // type, with the same exactness guarantees. + + bool operator==(ClosedIntervalValue other) const { return value_ == other.value(); } + bool operator!=(ClosedIntervalValue other) const { return value_ != other.value(); } + bool operator<(ClosedIntervalValue other) const { return value_ < other.value(); } + bool operator<=(ClosedIntervalValue other) const { return value_ <= other.value(); } + bool operator>=(ClosedIntervalValue other) const { return value_ >= other.value(); } + bool operator>(ClosedIntervalValue other) const { return value_ > other.value(); } + private: T value_; }; diff --git a/source/common/event/scaled_range_timer_manager_impl.cc b/source/common/event/scaled_range_timer_manager_impl.cc index 6aab81486929..5e9e1a8b8f32 100644 --- a/source/common/event/scaled_range_timer_manager_impl.cc +++ b/source/common/event/scaled_range_timer_manager_impl.cc @@ -150,9 +150,9 @@ TimerPtr ScaledRangeTimerManagerImpl::createTimer(ScaledTimerMinimum minimum, Ti return std::make_unique(minimum, callback, *this); } -void ScaledRangeTimerManagerImpl::setScaleFactor(double scale_factor) { +void ScaledRangeTimerManagerImpl::setScaleFactor(UnitFloat scale_factor) { const MonotonicTime now = dispatcher_.approximateMonotonicTime(); - scale_factor_ = UnitFloat(scale_factor); + scale_factor_ = scale_factor; for (auto& queue : queues_) { resetQueueTimer(*queue, now); } diff --git a/source/common/event/scaled_range_timer_manager_impl.h b/source/common/event/scaled_range_timer_manager_impl.h index f9bcc7242b7d..2f0a78662873 100644 --- a/source/common/event/scaled_range_timer_manager_impl.h +++ b/source/common/event/scaled_range_timer_manager_impl.h @@ -1,3 +1,5 @@ +#pragma once + #include #include @@ -26,7 +28,7 @@ class ScaledRangeTimerManagerImpl : public ScaledRangeTimerManager { // ScaledRangeTimerManager impl TimerPtr createTimer(ScaledTimerMinimum minimum, TimerCb callback) override; - void setScaleFactor(double scale_factor) override; + void setScaleFactor(UnitFloat scale_factor) override; private: class RangeTimerImpl; diff --git a/source/common/network/tcp_listener_impl.cc b/source/common/network/tcp_listener_impl.cc index 1d9b37b1a324..38c77db31e44 100644 --- a/source/common/network/tcp_listener_impl.cc +++ b/source/common/network/tcp_listener_impl.cc @@ -124,8 +124,7 @@ void TcpListenerImpl::enable() { socket_->ioHandle().enableFileEvents(Event::Fil void TcpListenerImpl::disable() { socket_->ioHandle().enableFileEvents(0); } -void TcpListenerImpl::setRejectFraction(const float reject_fraction) { - ASSERT(0 <= reject_fraction && reject_fraction <= 1); +void TcpListenerImpl::setRejectFraction(const UnitFloat reject_fraction) { reject_fraction_ = reject_fraction; } diff --git a/source/common/network/tcp_listener_impl.h b/source/common/network/tcp_listener_impl.h index d0859549ae9b..fd66ffc60dee 100644 --- a/source/common/network/tcp_listener_impl.h +++ b/source/common/network/tcp_listener_impl.h @@ -3,6 +3,8 @@ #include "envoy/common/random_generator.h" #include "envoy/runtime/runtime.h" +#include "common/common/interval_value.h" + #include "absl/strings/string_view.h" #include "base_listener_impl.h" @@ -20,7 +22,7 @@ class TcpListenerImpl : public BaseListenerImpl { ~TcpListenerImpl() override { socket_->ioHandle().resetFileEvents(); } void disable() override; void enable() override; - void setRejectFraction(float reject_fraction) override; + void setRejectFraction(UnitFloat reject_fraction) override; static const absl::string_view GlobalMaxCxRuntimeKey; @@ -38,7 +40,7 @@ class TcpListenerImpl : public BaseListenerImpl { static bool rejectCxOverGlobalLimit(); Random::RandomGenerator& random_; - float reject_fraction_; + UnitFloat reject_fraction_; }; } // namespace Network diff --git a/source/common/network/udp_listener_impl.h b/source/common/network/udp_listener_impl.h index a6b2852b3990..5e592c267276 100644 --- a/source/common/network/udp_listener_impl.h +++ b/source/common/network/udp_listener_impl.h @@ -30,7 +30,7 @@ class UdpListenerImpl : public BaseListenerImpl, // Network::Listener Interface void disable() override; void enable() override; - void setRejectFraction(float) override {} + void setRejectFraction(UnitFloat) override {} // Network::UdpListener Interface Event::Dispatcher& dispatcher() override; diff --git a/source/server/connection_handler_impl.cc b/source/server/connection_handler_impl.cc index 98191c58566a..cc9994a9b45d 100644 --- a/source/server/connection_handler_impl.cc +++ b/source/server/connection_handler_impl.cc @@ -153,7 +153,7 @@ void ConnectionHandlerImpl::enableListeners() { } } -void ConnectionHandlerImpl::setListenerRejectFraction(float reject_fraction) { +void ConnectionHandlerImpl::setListenerRejectFraction(UnitFloat reject_fraction) { listener_reject_fraction_ = reject_fraction; for (auto& listener : listeners_) { listener.second.listener_->listener()->setRejectFraction(reject_fraction); diff --git a/source/server/connection_handler_impl.h b/source/server/connection_handler_impl.h index e616294e53b8..d5524d97d74e 100644 --- a/source/server/connection_handler_impl.h +++ b/source/server/connection_handler_impl.h @@ -83,7 +83,7 @@ class ConnectionHandlerImpl : public Network::ConnectionHandler, void stopListeners() override; void disableListeners() override; void enableListeners() override; - void setListenerRejectFraction(float reject_fraction) override; + void setListenerRejectFraction(UnitFloat reject_fraction) override; const std::string& statPrefix() const override { return per_handler_stat_prefix_; } /** @@ -361,7 +361,7 @@ class ConnectionHandlerImpl : public Network::ConnectionHandler, std::list> listeners_; std::atomic num_handler_connections_{}; bool disable_listeners_; - float listener_reject_fraction_{0}; + UnitFloat listener_reject_fraction_{UnitFloat::min()}; }; class ActiveUdpListenerBase : public ConnectionHandlerImpl::ActiveListenerImplBase, diff --git a/source/server/overload_manager_impl.cc b/source/server/overload_manager_impl.cc index 07872b98442b..ebd07896f8ba 100644 --- a/source/server/overload_manager_impl.cc +++ b/source/server/overload_manager_impl.cc @@ -60,7 +60,11 @@ class ThreadLocalOverloadStateImpl : public ThreadLocalOverloadState { void setState(NamedOverloadActionSymbolTable::Symbol action, OverloadActionState state) { actions_[action.index()] = state; if (scaled_timer_action_.has_value() && scaled_timer_action_.value() == action) { - scaled_timer_manager_->setScaleFactor(1 - state.value()); + scaled_timer_manager_->setScaleFactor( + // The action state is 0 for no overload up to 1 for maximal overload, + // but the scale factor for timers is 1 for no scaling and 0 for + // maximal scaling, so invert the value to pass in (1-value). + state.value().invert()); } } @@ -86,6 +90,8 @@ class ThresholdTriggerImpl final : public OverloadAction::Trigger { const OverloadActionState state = actionState(); state_ = value >= threshold_ ? OverloadActionState::saturated() : OverloadActionState::inactive(); + // This is a floating point comparison, though state_ is always either + // saturated or inactive so there's no risk due to floating point precision. return state.value() != actionState().value(); } @@ -117,6 +123,9 @@ class ScaledTriggerImpl final : public OverloadAction::Trigger { state_ = OverloadActionState( UnitFloat((value - scaling_threshold_) / (saturated_threshold_ - scaling_threshold_))); } + // All values of state_ are produced via this same code path. Even if + // old_state and state_ should be approximately equal, there's no harm in + // signaling for a small change if they're not float::operator== equal. return state_.value() != old_state.value(); } @@ -258,7 +267,7 @@ bool OverloadAction::updateResourcePressure(const std::string& name, double pres } const auto trigger_new_state = it->second->actionState(); active_gauge_.set(trigger_new_state.isSaturated() ? 1 : 0); - scale_percent_gauge_.set(trigger_new_state.value() * 100); + scale_percent_gauge_.set(trigger_new_state.value().value() * 100); { // Compute the new state as the maximum over all trigger states. diff --git a/source/server/worker_impl.cc b/source/server/worker_impl.cc index 760b7ca630bc..47a5d59e9881 100644 --- a/source/server/worker_impl.cc +++ b/source/server/worker_impl.cc @@ -152,7 +152,7 @@ void WorkerImpl::stopAcceptingConnectionsCb(OverloadActionState state) { } void WorkerImpl::rejectIncomingConnectionsCb(OverloadActionState state) { - handler_->setListenerRejectFraction(static_cast(state.value())); + handler_->setListenerRejectFraction(state.value()); } } // namespace Server diff --git a/test/common/common/BUILD b/test/common/common/BUILD index f0e0b68986ca..d348049664fd 100644 --- a/test/common/common/BUILD +++ b/test/common/common/BUILD @@ -196,6 +196,7 @@ envoy_cc_test( name = "random_generator_test", srcs = ["random_generator_test.cc"], deps = [ + "//source/common/common:interval_value", "//source/common/common:random_generator_lib", "//test/mocks/runtime:runtime_mocks", "//test/test_common:environment_lib", diff --git a/test/common/common/random_generator_test.cc b/test/common/common/random_generator_test.cc index 71bf25624740..7658080b116b 100644 --- a/test/common/common/random_generator_test.cc +++ b/test/common/common/random_generator_test.cc @@ -1,5 +1,6 @@ #include +#include "common/common/interval_value.h" #include "common/common/random_generator.h" #include "gmock/gmock.h" @@ -70,15 +71,13 @@ TEST(UUID, SanityCheckOfUniqueness) { TEST(Random, Bernoilli) { Random::RandomGeneratorImpl random; - EXPECT_FALSE(random.bernoulli(0)); - EXPECT_FALSE(random.bernoulli(-1)); - EXPECT_TRUE(random.bernoulli(1)); - EXPECT_TRUE(random.bernoulli(2)); + EXPECT_FALSE(random.bernoulli(UnitFloat(0.0f))); + EXPECT_TRUE(random.bernoulli(UnitFloat(1.0f))); int true_count = 0; static const auto num_rolls = 100000; for (size_t i = 0; i < num_rolls; ++i) { - if (random.bernoulli(0.4)) { + if (random.bernoulli(UnitFloat(0.4f))) { ++true_count; } } diff --git a/test/common/event/scaled_range_timer_manager_impl_test.cc b/test/common/event/scaled_range_timer_manager_impl_test.cc index 824a285ee4f6..5aa5e786e224 100644 --- a/test/common/event/scaled_range_timer_manager_impl_test.cc +++ b/test/common/event/scaled_range_timer_manager_impl_test.cc @@ -280,7 +280,7 @@ TEST_P(ScaledRangeTimerManagerTestWithScope, ScheduleWithScalingFactorZero) { MockFunction callback; auto timer = manager.createTimer(AbsoluteMinimum(std::chrono::seconds(0)), callback.AsStdFunction()); - manager.setScaleFactor(0); + manager.setScaleFactor(UnitFloat(0)); EXPECT_CALL(callback, Call).WillOnce([&] { EXPECT_EQ(dispatcher_.scope_, getScope()); }); @@ -394,7 +394,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithScaling) { timers[1].timer->enableTimer(std::chrono::seconds(6)); timers[2].timer->enableTimer(std::chrono::seconds(10)); - manager.setScaleFactor(0.5); + manager.setScaleFactor(UnitFloat(0.5)); // Advance time to start = 1 second, so timers[0] hits its min. simTime().advanceTimeAndRun(std::chrono::seconds(1), dispatcher_, Dispatcher::RunType::Block); @@ -403,7 +403,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithScaling) { simTime().advanceTimeAndRun(std::chrono::seconds(1), dispatcher_, Dispatcher::RunType::Block); // At 4x speed, timers[1] will fire in only 1 second. - manager.setScaleFactor(0.25); + manager.setScaleFactor(UnitFloat(0.25)); // Advance time to start = 3, which should make timers[1] hit its scaled max. simTime().advanceTimeAndRun(std::chrono::seconds(1), dispatcher_, Dispatcher::RunType::Block); @@ -411,7 +411,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithScaling) { // Advance time to start = 6, which is the minimum required for timers[2] to fire. simTime().advanceTimeAndRun(std::chrono::seconds(3), dispatcher_, Dispatcher::RunType::Block); - manager.setScaleFactor(0); + manager.setScaleFactor(UnitFloat(0)); // With a scale factor of 0, timers[2] should be ready to be fired immediately. dispatcher_.run(Dispatcher::RunType::Block); @@ -464,7 +464,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersSameTimesFastClock) { TEST_F(ScaledRangeTimerManagerTest, ScheduledWithScalingFactorZero) { ScaledRangeTimerManagerImpl manager(dispatcher_); - manager.setScaleFactor(0); + manager.setScaleFactor(UnitFloat(0)); TrackedRangeTimer timer(AbsoluteMinimum(std::chrono::seconds(4)), manager, simTime()); @@ -566,7 +566,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithChangeInScalingFactor) { timers[0].timer->enableTimer(std::chrono::seconds(15)); timers[1].timer->enableTimer(std::chrono::seconds(14)); - manager.setScaleFactor(0.1); + manager.setScaleFactor(UnitFloat(0.1)); timers[2].timer->enableTimer(std::chrono::seconds(21)); timers[3].timer->enableTimer(std::chrono::seconds(16)); @@ -574,7 +574,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithChangeInScalingFactor) { // Advance to timer 0's min. simTime().advanceTimeAndRun(std::chrono::seconds(5), dispatcher_, Dispatcher::RunType::Block); - manager.setScaleFactor(0.5); + manager.setScaleFactor(UnitFloat(0.5)); // Now that the scale factor is 0.5, fire times are 0: start+10, 1: start+13, 2: start+14, 3: // start+13. Advance to timer 2's min. @@ -583,7 +583,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithChangeInScalingFactor) { // Advance to time start+9. simTime().advanceTimeAndRun(std::chrono::seconds(2), dispatcher_, Dispatcher::RunType::Block); - manager.setScaleFactor(0.1); + manager.setScaleFactor(UnitFloat(0.1)); // Now that the scale factor is reduced, fire times are 0: start+6, 1: start+12.2, // 2: start+8.4, 3: start+10.6. Timers 0 and 2 should fire immediately since their // trigger times are in the past. @@ -598,7 +598,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithChangeInScalingFactor) { timers[0].timer->enableTimer(std::chrono::seconds(13)); // Fire times are now 0: start+19, 1: start+13, 2: none, 3: start+13. - manager.setScaleFactor(0.5); + manager.setScaleFactor(UnitFloat(0.5)); // Advance to timer 1's min. simTime().advanceTimeAndRun(std::chrono::seconds(2), dispatcher_, Dispatcher::RunType::Block); @@ -611,7 +611,7 @@ TEST_F(ScaledRangeTimerManagerTest, MultipleTimersWithChangeInScalingFactor) { simTime().advanceTimeAndRun(std::chrono::seconds(3), dispatcher_, Dispatcher::RunType::Block); // The time is now start+16. Setting the scale factor to 0 should make timer 0 fire immediately. - manager.setScaleFactor(0); + manager.setScaleFactor(UnitFloat(0)); dispatcher_.run(Dispatcher::RunType::Block); EXPECT_THAT(*timers[0].trigger_times, ElementsAre(start + std::chrono::seconds(9), start + std::chrono::seconds(16))); diff --git a/test/common/network/listener_impl_test.cc b/test/common/network/listener_impl_test.cc index 38dc232a4b7f..e6a882a35ddd 100644 --- a/test/common/network/listener_impl_test.cc +++ b/test/common/network/listener_impl_test.cc @@ -358,7 +358,7 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionZero) { TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks, true); - listener.setRejectFraction(0); + listener.setRejectFraction(UnitFloat(0)); // This connection will be accepted and not rejected. { @@ -389,7 +389,7 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionIntermediate) { TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks, true); - listener.setRejectFraction(0.5f); + listener.setRejectFraction(UnitFloat(0.5f)); // The first connection will be rejected because the random value is too small. { @@ -452,7 +452,7 @@ TEST_P(TcpListenerImplTest, SetListenerRejectFractionAll) { TestTcpListenerImpl listener(dispatcherImpl(), random_generator, socket, listener_callbacks, true); - listener.setRejectFraction(1); + listener.setRejectFraction(UnitFloat(1)); { testing::InSequence s1; diff --git a/test/mocks/event/mocks.h b/test/mocks/event/mocks.h index 8e29e84c3b32..a38dd15cc8f8 100644 --- a/test/mocks/event/mocks.h +++ b/test/mocks/event/mocks.h @@ -185,7 +185,7 @@ class MockScaledRangeTimerManager : public ScaledRangeTimerManager { return TimerPtr{createTimer_(minimum, std::move(callback))}; } MOCK_METHOD(Timer*, createTimer_, (ScaledTimerMinimum, TimerCb)); - MOCK_METHOD(void, setScaleFactor, (double), (override)); + MOCK_METHOD(void, setScaleFactor, (UnitFloat), (override)); }; class MockSchedulableCallback : public SchedulableCallback { diff --git a/test/mocks/network/mocks.h b/test/mocks/network/mocks.h index c30f97d8a1a3..d2f862c7501d 100644 --- a/test/mocks/network/mocks.h +++ b/test/mocks/network/mocks.h @@ -397,7 +397,7 @@ class MockListener : public Listener { MOCK_METHOD(void, onDestroy, ()); MOCK_METHOD(void, enable, ()); MOCK_METHOD(void, disable, ()); - MOCK_METHOD(void, setRejectFraction, (float)); + MOCK_METHOD(void, setRejectFraction, (UnitFloat)); }; class MockConnectionHandler : public ConnectionHandler { @@ -419,7 +419,7 @@ class MockConnectionHandler : public ConnectionHandler { MOCK_METHOD(void, stopListeners, ()); MOCK_METHOD(void, disableListeners, ()); MOCK_METHOD(void, enableListeners, ()); - MOCK_METHOD(void, setListenerRejectFraction, (float), (override)); + MOCK_METHOD(void, setListenerRejectFraction, (UnitFloat), (override)); MOCK_METHOD(const std::string&, statPrefix, (), (const)); }; @@ -509,7 +509,7 @@ class MockUdpListener : public UdpListener { MOCK_METHOD(void, onDestroy, ()); MOCK_METHOD(void, enable, ()); MOCK_METHOD(void, disable, ()); - MOCK_METHOD(void, setRejectFraction, (float), (override)); + MOCK_METHOD(void, setRejectFraction, (UnitFloat), (override)); MOCK_METHOD(Event::Dispatcher&, dispatcher, ()); MOCK_METHOD(Address::InstanceConstSharedPtr&, localAddress, (), (const)); MOCK_METHOD(Api::IoCallUint64Result, send, (const UdpSendData&)); diff --git a/test/server/connection_handler_test.cc b/test/server/connection_handler_test.cc index 17fe5e69682b..dd55fea07a17 100644 --- a/test/server/connection_handler_test.cc +++ b/test/server/connection_handler_test.cc @@ -182,7 +182,7 @@ class ConnectionHandlerTest : public testing::Test, protected Logger::LoggableaddListener(absl::nullopt, *test_listener); - EXPECT_CALL(*listener, setRejectFraction(0.1234f)); + EXPECT_CALL(*listener, setRejectFraction(UnitFloat(0.1234f))); EXPECT_CALL(*listener, onDestroy()); - handler_->setListenerRejectFraction(0.1234f); + handler_->setListenerRejectFraction(UnitFloat(0.1234f)); } TEST_F(ConnectionHandlerTest, AddListenerSetRejectFraction) { @@ -481,11 +481,11 @@ TEST_F(ConnectionHandlerTest, AddListenerSetRejectFraction) { auto listener = new NiceMock(); TestListener* test_listener = addListener(1, false, false, "test_listener", listener, &listener_callbacks); - EXPECT_CALL(*listener, setRejectFraction(0.12345f)); + EXPECT_CALL(*listener, setRejectFraction(UnitFloat(0.12345f))); EXPECT_CALL(*socket_factory_, localAddress()).WillOnce(ReturnRef(local_address_)); EXPECT_CALL(*listener, onDestroy()); - handler_->setListenerRejectFraction(0.12345f); + handler_->setListenerRejectFraction(UnitFloat(0.12345f)); handler_->addListener(absl::nullopt, *test_listener); } diff --git a/test/server/overload_manager_impl_test.cc b/test/server/overload_manager_impl_test.cc index d160b42ceae8..e73f91b3834d 100644 --- a/test/server/overload_manager_impl_test.cc +++ b/test/server/overload_manager_impl_test.cc @@ -25,7 +25,7 @@ using testing::_; using testing::AllOf; using testing::AnyNumber; using testing::ByMove; -using testing::DoubleNear; +using testing::FloatNear; using testing::Invoke; using testing::MockFunction; using testing::NiceMock; @@ -236,7 +236,7 @@ TEST_F(OverloadManagerImplTest, CallbackOnlyFiresWhenStateChanges) { timer_cb_(); EXPECT_FALSE(is_active); EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false), - Property(&OverloadActionState::value, 0))); + Property(&OverloadActionState::value, UnitFloat::min()))); EXPECT_EQ(0, cb_count); EXPECT_EQ(0, active_gauge.value()); EXPECT_EQ(0, scale_percent_gauge.value()); @@ -282,7 +282,7 @@ TEST_F(OverloadManagerImplTest, CallbackOnlyFiresWhenStateChanges) { timer_cb_(); EXPECT_FALSE(is_active); EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false), - Property(&OverloadActionState::value, 0))); + Property(&OverloadActionState::value, UnitFloat::min()))); EXPECT_EQ(2, cb_count); EXPECT_EQ(30, pressure_gauge2.value()); @@ -302,7 +302,7 @@ TEST_F(OverloadManagerImplTest, CallbackOnlyFiresWhenStateChanges) { timer_cb_(); EXPECT_FALSE(is_active); EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false), - Property(&OverloadActionState::value, 0))); + Property(&OverloadActionState::value, UnitFloat::min()))); EXPECT_EQ(4, cb_count); EXPECT_EQ(41, pressure_gauge1.value()); EXPECT_EQ(42, pressure_gauge2.value()); @@ -327,7 +327,7 @@ TEST_F(OverloadManagerImplTest, ScaledTrigger) { timer_cb_(); EXPECT_THAT(action_state, AllOf(Property(&OverloadActionState::isSaturated, false), - Property(&OverloadActionState::value, 0))); + Property(&OverloadActionState::value, UnitFloat::min()))); EXPECT_EQ(0, active_gauge.value()); EXPECT_EQ(0, scale_percent_gauge.value()); @@ -336,7 +336,7 @@ TEST_F(OverloadManagerImplTest, ScaledTrigger) { factory3_.monitor_->setPressure(0.65); timer_cb_(); - EXPECT_EQ(action_state.value(), 0.5 /* = 0.65 / (0.8 - 0.5) */); + EXPECT_EQ(action_state.value(), UnitFloat(0.5) /* = 0.65 / (0.8 - 0.5) */); EXPECT_EQ(0, active_gauge.value()); EXPECT_EQ(50, scale_percent_gauge.value()); @@ -408,13 +408,13 @@ TEST_F(OverloadManagerImplTest, DelayedUpdatesAreCoalesced) { // When monitor 3 publishes its update, the action won't be visible to the thread-local state factory3_.monitor_->setPressure(0.6); factory3_.monitor_->publishUpdate(); - EXPECT_EQ(action_state.value(), 0.0); + EXPECT_EQ(action_state.value(), UnitFloat::min()); // Now when monitor 4 publishes a larger value, the update from monitor 3 is skipped. EXPECT_FALSE(action_state.isSaturated()); factory4_.monitor_->setPressure(0.65); factory4_.monitor_->publishUpdate(); - EXPECT_EQ(action_state.value(), 0.5 /* = (0.65 - 0.5) / (0.8 - 0.5) */); + EXPECT_EQ(action_state.value(), UnitFloat(0.5) /* = (0.65 - 0.5) / (0.8 - 0.5) */); } TEST_F(OverloadManagerImplTest, FlushesUpdatesEvenWithOneUnresponsive) { @@ -504,7 +504,8 @@ TEST_F(OverloadManagerImplTest, AdjustScaleFactor) { // The scaled trigger has range [0.5, 1.0] so 0.6 should map to a scale value of 0.2, which means // a timer scale factor of 0.8 (1 - 0.2). - EXPECT_CALL(*scaled_timer_manager, setScaleFactor(DoubleNear(0.8, 0.00001))); + EXPECT_CALL(*scaled_timer_manager, + setScaleFactor(Property(&UnitFloat::value, FloatNear(0.8, 0.00001)))); factory1_.monitor_->setPressure(0.6); timer_cb_();