YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
25.5M
  RestartSafeCoarseTimePoint() = default;
27
28
  static RestartSafeCoarseTimePoint FromCoarseTimePointAndDelta(
29
37.6M
      CoarseTimePoint time_point, CoarseDuration delta) {
30
37.6M
    return RestartSafeCoarseTimePoint(time_point + delta);
31
37.6M
  }
32
33
1.63M
  static RestartSafeCoarseTimePoint FromUInt64(uint64_t value) {
34
1.63M
    return RestartSafeCoarseTimePoint(CoarseTimePoint() + CoarseDuration(value));
35
1.63M
  }
36
37
28.7M
  uint64_t ToUInt64() const {
38
28.7M
    return (value_ - CoarseTimePoint()).count();
39
28.7M
  }
40
41
18.0k
  std::string ToString() const {
42
18.0k
    return yb::ToString(value_);
43
18.0k
  }
44
45
 private:
46
  friend class RestartSafeCoarseMonoClock;
47
48
39.3M
  explicit RestartSafeCoarseTimePoint(CoarseTimePoint value) : value_(value) {}
49
50
  CoarseTimePoint value_;
51
52
  friend inline bool operator==(
53
19.6M
      const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
54
19.6M
    return lhs.value_ == rhs.value_;
55
19.6M
  }
56
57
  friend inline bool operator<(
58
2.64M
      const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
59
2.64M
    return lhs.value_ < rhs.value_;
60
2.64M
  }
61
62
  friend inline RestartSafeCoarseTimePoint& operator-=(
63
6.40M
      RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseDuration& rhs) { // NOLINT
64
6.40M
    lhs.value_ -= rhs;
65
6.40M
    return lhs;
66
6.40M
  }
67
68
  friend inline RestartSafeCoarseTimePoint& operator+=(
69
297k
      RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseDuration& rhs) { // NOLINT
70
297k
    lhs.value_ += rhs;
71
297k
    return lhs;
72
297k
  }
73
};
74
75
inline bool operator!=(
76
14.8M
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
77
14.8M
  return !(lhs == rhs);
78
14.8M
}
79
80
inline bool operator>(
81
297k
    const RestartSafeCoarseTimePoint& lhs, const RestartSafeCoarseTimePoint& rhs) {
82
297k
  return rhs < lhs;
83
297k
}
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
6.40M
    RestartSafeCoarseTimePoint lhs, const RestartSafeCoarseDuration& rhs) {
97
6.40M
  return lhs -= rhs;
98
6.40M
}
99
100
inline RestartSafeCoarseTimePoint operator+(
101
297k
    RestartSafeCoarseTimePoint lhs, const RestartSafeCoarseDuration& rhs) {
102
297k
  return lhs += rhs;
103
297k
}
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
37.6M
  RestartSafeCoarseTimePoint Now() const {
113
37.6M
    return RestartSafeCoarseTimePoint::FromCoarseTimePointAndDelta(CoarseMonoClock::Now(), delta_);
114
37.6M
  }
115
116
  // Adjust clocks so specified point is treated as now.
117
1.25k
  void Adjust(RestartSafeCoarseTimePoint mark_as_now) {
118
1.25k
    delta_ = mark_as_now.value_ - CoarseMonoClock::Now();
119
1.25k
  }
120
 private:
121
  CoarseDuration delta_;
122
};
123
124
} // namespace yb
125
126
#endif // YB_UTIL_RESTART_SAFE_CLOCK_H