diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-09-19 16:59:09 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-09-19 17:27:22 +0300 |
commit | befec05e336f383b229bcafff2bfa2452966cb16 (patch) | |
tree | 8c48b16aecb51f977c7b0b4f14f3f4f431bbd96f | |
parent | 38ceb38a3317511ee3fb6d25bfea9025adc09d91 (diff) | |
download | ydb-befec05e336f383b229bcafff2bfa2452966cb16.tar.gz |
Made ApplyJitter a separate function in jitter.h. Reimplemented BackoffStrategy with it
-rw-r--r-- | yt/yt/core/misc/backoff_strategy.cpp | 11 | ||||
-rw-r--r-- | yt/yt/core/misc/jitter.h | 28 | ||||
-rw-r--r-- | yt/yt/core/misc/public.h | 8 |
3 files changed, 43 insertions, 4 deletions
diff --git a/yt/yt/core/misc/backoff_strategy.cpp b/yt/yt/core/misc/backoff_strategy.cpp index c902ca5b5d6..4b9e5871338 100644 --- a/yt/yt/core/misc/backoff_strategy.cpp +++ b/yt/yt/core/misc/backoff_strategy.cpp @@ -1,4 +1,5 @@ #include "backoff_strategy.h" +#include "jitter.h" #include <util/random/normal.h> @@ -58,10 +59,12 @@ TDuration TBackoffStrategy::GetBackoff() const void TBackoffStrategy::ApplyJitter() { - auto rnd = StdNormalRandom<double>(); - bool isNegative = rnd < 0; - auto jitter = std::abs(rnd) * Options_.BackoffJitter * Backoff_; - BackoffWithJitter_ = isNegative ? Backoff_ - jitter : Backoff_ + jitter; + BackoffWithJitter_ = ::NYT::ApplyJitter(Backoff_, Options_.BackoffJitter, +[]{ + //! StdNormalRandom produces [-6.660, 6.660] according to Wiki + const double StdNormalRandomMaxValue = 7.0; + + return StdNormalRandom<double>() / StdNormalRandomMaxValue; + }); } diff --git a/yt/yt/core/misc/jitter.h b/yt/yt/core/misc/jitter.h new file mode 100644 index 00000000000..7d3935b0d63 --- /dev/null +++ b/yt/yt/core/misc/jitter.h @@ -0,0 +1,28 @@ +#pragma once + +#include "public.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +//! TRandomGenerator is required to produce numbers only in range [-1, 1]. +//! Will crash otherwise +template <CScalable<double> TValue, class TRandomGenerator> + requires std::is_invocable_r<double, TRandomGenerator>::value +constexpr inline TValue ApplyJitter(TValue average, double jitter, const TRandomGenerator& randomGenerator) +{ + YT_VERIFY(jitter >= 0 && jitter <= 1); + + double rnd = randomGenerator(); + + YT_VERIFY(std::abs(rnd) <= 1); + + double multiplier = static_cast<double>(1) + jitter * rnd; + + return average * multiplier; +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/yt/yt/core/misc/public.h b/yt/yt/core/misc/public.h index 556fb71a029..a7ee5641b52 100644 --- a/yt/yt/core/misc/public.h +++ b/yt/yt/core/misc/public.h @@ -172,4 +172,12 @@ DECLARE_REFCOUNTED_STRUCT(IMemoryReferenceTracker) //////////////////////////////////////////////////////////////////////////////// +template <class TObject, class TScalar> +concept CScalable = requires (TObject object, TScalar scalar) +{ + { object * scalar } -> std::same_as<TObject>; +}; + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT |