YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/restart_safe_clock.h
Line
Count
Source (jump to first uncovered line)
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_RESTART_SAFE_CLOCK_H
15
#define YB_UTIL_RESTART_SAFE_CLOCK_H
16
17
#include "yb/util/monotime.h"
18
19
namespace yb {
20
21
class RestartSafeCoarseMonoClock;
22
typedef CoarseDuration RestartSafeCoarseDuration;
23
24
class RestartSafeCoarseTimePoint {
25
 public:
26
46.0M
  RestartSafeCoarseTimePoint() = default;
27
28
  static RestartSafeCoarseTimePoint FromCoarseTimePointAndDelta(
29
107M
      CoarseTimePoint time_point, CoarseDuration delta) {
30
107M
    return RestartSafeCoarseTimePoint(time_point + delta);
31
107M
  }
32
33
2.28M
  static RestartSafeCoarseTimePoint FromUInt64(uint64_t value) {
34
2.28M
    return RestartSafeCoarseTimePoint(CoarseTimePoint() + CoarseDuration(value));
35
2.28M
  }
36
37
51.4M
  uint64_t ToUInt64() const {
38
51.4M
    return (value_ - CoarseTimePoint()).count();
39
51.4M
  }
40
41
22.3k
  std::string ToString() const {
42
22.3k
    return yb::ToString(value_);
43
22.3k
  }
44
45
 private:
46
  friend class RestartSafeCoarseMonoClock;
47
48
109M
  explicit RestartSafeCoarseTimePoint(CoarseTimePoint value) : value_(value) {}
49
50
  CoarseTimePoint value_;
51
52
  friend inline bool operator==(
53
35.3M
      const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
54
35.3M
    return lhs.value_ == rhs.value_;
55
35.3M
  }
56
57
  friend inline bool operator<(
58
6.93M
      const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
59
6.93M
    return lhs.value_ < rhs.value_;
60
6.93M
  }
61
62
  friend inline RestartSafeCoarseTimePoint& operator-=(
63
50.1M
      RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseDuration& rhs) { // NOLINT
64
50.1M
    lhs.value_ -= rhs;
65
50.1M
    return lhs;
66
50.1M
  }
67
68
  friend inline RestartSafeCoarseTimePoint& operator+=(
69
656k
      RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseDuration& rhs) { // NOLINT
70
656k
    lhs.value_ += rhs;
71
656k
    return lhs;
72
656k
  }
73
};
74
75
inline bool operator!=(
76
26.4M
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
77
26.4M
  return !(lhs == rhs);
78
26.4M
}
79
80
inline bool operator>(
81
656k
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
82
656k
  return rhs < lhs;
83
656k
}
84
85
inline bool operator<=(
86
0
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
87
0
  return !(rhs < lhs);
88
0
}
89
90
inline bool operator>=(
91
0
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
92
0
  return !(lhs < rhs);
93
0
}
94
95
inline RestartSafeCoarseTimePoint operator-(
96
50.1M
    RestartSafeCoarseTimePoint lhs, const RestartSafeCoarseDuration& rhs) {
97
50.1M
  return lhs -= rhs;
98
50.1M
}
99
100
inline RestartSafeCoarseTimePoint operator+(
101
656k
    RestartSafeCoarseTimePoint lhs, const RestartSafeCoarseDuration& rhs) {
102
656k
  return lhs += rhs;
103
656k
}
104
105
class RestartSafeCoarseMonoClock {
106
 public:
107
  RestartSafeCoarseMonoClock() = default;
108
0
  explicit RestartSafeCoarseMonoClock(CoarseDuration delta) : delta_(delta) {}
109
110
  // Returns now of coarse mono time clock, the value of this clock persists between restarts.
111
  // So first time after restart will be greater than or equal to last time before restart.
112
107M
  RestartSafeCoarseTimePoint Now() const {
113
107M
    return RestartSafeCoarseTimePoint::FromCoarseTimePointAndDelta(CoarseMonoClock::Now(), delta_);
114
107M
  }
115
116
  // Adjust clocks so specified point is treated as now.
117
2.29k
  void Adjust(RestartSafeCoarseTimePoint mark_as_now) {
118
2.29k
    delta_ = mark_as_now.value_ - CoarseMonoClock::Now();
119
2.29k
  }
120
 private:
121
  CoarseDuration delta_;
122
};
123
124
} // namespace yb
125
126
#endif // YB_UTIL_RESTART_SAFE_CLOCK_H