YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/backoff_waiter.h
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_UTIL_BACKOFF_WAITER_H
15
#define YB_UTIL_BACKOFF_WAITER_H
16
17
#include <chrono>
18
#include <thread>
19
20
#include "yb/util/monotime.h"
21
#include "yb/util/random_util.h"
22
23
namespace yb {
24
25
// Utility class for waiting.
26
// It tracks number of attempts and exponentially increase sleep timeout.
27
template <class Clock>
28
class GenericBackoffWaiter {
29
 public:
30
  typedef typename Clock::time_point TimePoint;
31
  typedef typename Clock::duration Duration;
32
33
  // deadline - time when waiter decides that it is expired.
34
  // max_wait - max duration for single wait.
35
  // base_delay - multiplier for wait duration.
36
  explicit GenericBackoffWaiter(
37
      TimePoint deadline, Duration max_wait = Duration::max(),
38
      Duration base_delay = std::chrono::milliseconds(1))
39
95.5k
      : deadline_(deadline), max_wait_(max_wait), base_delay_(base_delay) {}
_ZN2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEEC2ENS2_10time_pointIS3_NS2_8durationIxNS1_5ratioILl1ELl1000000000EEEEEEES9_S9_
Line
Count
Source
39
45.8k
      : deadline_(deadline), max_wait_(max_wait), base_delay_(base_delay) {}
_ZN2yb20GenericBackoffWaiterINS_15CoarseMonoClockEEC2ENSt3__16chrono10time_pointIS1_NS4_8durationIxNS3_5ratioILl1ELl1000000000EEEEEEES9_S9_
Line
Count
Source
39
49.7k
      : deadline_(deadline), max_wait_(max_wait), base_delay_(base_delay) {}
40
41
15.3k
  bool ExpiredNow() const {
42
15.3k
    return ExpiredAt(Clock::now());
43
15.3k
  }
44
45
57.3k
  bool ExpiredAt(TimePoint time) const {
46
57.3k
    return deadline_ < time + ClockResolution<Clock>();
47
57.3k
  }
_ZNK2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEE9ExpiredAtENS2_10time_pointIS3_NS2_8durationIxNS1_5ratioILl1ELl1000000000EEEEEEE
Line
Count
Source
45
18.2k
  bool ExpiredAt(TimePoint time) const {
46
18.2k
    return deadline_ < time + ClockResolution<Clock>();
47
18.2k
  }
_ZNK2yb20GenericBackoffWaiterINS_15CoarseMonoClockEE9ExpiredAtENSt3__16chrono10time_pointIS1_NS4_8durationIxNS3_5ratioILl1ELl1000000000EEEEEEE
Line
Count
Source
45
39.0k
  bool ExpiredAt(TimePoint time) const {
46
39.0k
    return deadline_ < time + ClockResolution<Clock>();
47
39.0k
  }
48
49
42.0k
  bool Wait() {
50
42.0k
    auto now = Clock::now();
51
42.0k
    if (ExpiredAt(now)) {
52
2
      return false;
53
2
    }
54
55
42.0k
    NextAttempt();
56
57
42.0k
    std::this_thread::sleep_for(DelayForTime(now));
58
42.0k
    return true;
59
42.0k
  }
_ZN2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEE4WaitEv
Line
Count
Source
49
18.2k
  bool Wait() {
50
18.2k
    auto now = Clock::now();
51
18.2k
    if (ExpiredAt(now)) {
52
0
      return false;
53
0
    }
54
55
18.2k
    NextAttempt();
56
57
18.2k
    std::this_thread::sleep_for(DelayForTime(now));
58
18.2k
    return true;
59
18.2k
  }
_ZN2yb20GenericBackoffWaiterINS_15CoarseMonoClockEE4WaitEv
Line
Count
Source
49
23.7k
  bool Wait() {
50
23.7k
    auto now = Clock::now();
51
23.7k
    if (ExpiredAt(now)) {
52
2
      return false;
53
2
    }
54
55
23.7k
    NextAttempt();
56
57
23.7k
    std::this_thread::sleep_for(DelayForTime(now));
58
23.7k
    return true;
59
23.7k
  }
60
61
42.1k
  void NextAttempt() {
62
42.1k
    ++attempt_;
63
42.1k
  }
_ZN2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEE11NextAttemptEv
Line
Count
Source
61
18.2k
  void NextAttempt() {
62
18.2k
    ++attempt_;
63
18.2k
  }
_ZN2yb20GenericBackoffWaiterINS_15CoarseMonoClockEE11NextAttemptEv
Line
Count
Source
61
23.9k
  void NextAttempt() {
62
23.9k
    ++attempt_;
63
23.9k
  }
64
65
  Duration DelayForNow() const {
66
    return DelayForTime(Clock::now());
67
  }
68
69
42.0k
  Duration DelayForTime(TimePoint now) const {
70
42.0k
    Duration max_wait = std::min(deadline_ - now, max_wait_);
71
    // 1st retry delayed 2^4 of base delays, 2nd 2^5 base delays, etc..
72
42.0k
    Duration attempt_delay =
73
42.0k
        base_delay_ *
74
41.4k
        (attempt_ >= 29 ? std::numeric_limits<int32_t>::max() : 1LL << (attempt_ + 3));
75
42.0k
    Duration jitter = std::chrono::milliseconds(RandomUniformInt(0, 50));
76
42.0k
    return std::min(attempt_delay + jitter, max_wait);
77
42.0k
  }
_ZNK2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEE12DelayForTimeENS2_10time_pointIS3_NS2_8durationIxNS1_5ratioILl1ELl1000000000EEEEEEE
Line
Count
Source
69
18.2k
  Duration DelayForTime(TimePoint now) const {
70
18.2k
    Duration max_wait = std::min(deadline_ - now, max_wait_);
71
    // 1st retry delayed 2^4 of base delays, 2nd 2^5 base delays, etc..
72
18.2k
    Duration attempt_delay =
73
18.2k
        base_delay_ *
74
18.2k
        (attempt_ >= 29 ? std::numeric_limits<int32_t>::max() : 1LL << (attempt_ + 3));
75
18.2k
    Duration jitter = std::chrono::milliseconds(RandomUniformInt(0, 50));
76
18.2k
    return std::min(attempt_delay + jitter, max_wait);
77
18.2k
  }
_ZNK2yb20GenericBackoffWaiterINS_15CoarseMonoClockEE12DelayForTimeENSt3__16chrono10time_pointIS1_NS4_8durationIxNS3_5ratioILl1ELl1000000000EEEEEEE
Line
Count
Source
69
23.7k
  Duration DelayForTime(TimePoint now) const {
70
23.7k
    Duration max_wait = std::min(deadline_ - now, max_wait_);
71
    // 1st retry delayed 2^4 of base delays, 2nd 2^5 base delays, etc..
72
23.7k
    Duration attempt_delay =
73
23.7k
        base_delay_ *
74
23.1k
        (attempt_ >= 29 ? std::numeric_limits<int32_t>::max() : 1LL << (attempt_ + 3));
75
23.7k
    Duration jitter = std::chrono::milliseconds(RandomUniformInt(0, 50));
76
23.7k
    return std::min(attempt_delay + jitter, max_wait);
77
23.7k
  }
78
79
18.1k
  size_t attempt() const {
80
18.1k
    return attempt_;
81
18.1k
  }
_ZNK2yb20GenericBackoffWaiterINSt3__16chrono12steady_clockEE7attemptEv
Line
Count
Source
79
18.1k
  size_t attempt() const {
80
18.1k
    return attempt_;
81
18.1k
  }
Unexecuted instantiation: _ZNK2yb20GenericBackoffWaiterINS_15CoarseMonoClockEE7attemptEv
82
83
  // Resets attempt counter, w/o modifying deadline.
84
  void Restart() {
85
    attempt_ = 0;
86
  }
87
88
 private:
89
  TimePoint deadline_;
90
  size_t attempt_ = 0;
91
  Duration max_wait_;
92
  Duration base_delay_;
93
};
94
95
typedef GenericBackoffWaiter<std::chrono::steady_clock> BackoffWaiter;
96
typedef GenericBackoffWaiter<CoarseMonoClock> CoarseBackoffWaiter;
97
98
} // namespace yb
99
100
#endif // YB_UTIL_BACKOFF_WAITER_H