YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/common/read_hybrid_time.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_COMMON_READ_HYBRID_TIME_H
15
#define YB_COMMON_READ_HYBRID_TIME_H
16
17
#include <functional>
18
19
#include "yb/common/clock.h"
20
#include "yb/common/hybrid_time.h"
21
22
#include "yb/util/compare_util.h"
23
#include "yb/util/tostring.h"
24
25
namespace yb {
26
27
// Hybrid time range used for read.
28
// Limit is the maximum time that could have existed on any server at the time the read operation
29
// was initiated, and is used to decide whether the read operation need to be restarted at a higher
30
// hybrid time than `read`.
31
struct ReadHybridTime {
32
  // Hybrid time of read operation.
33
  HybridTime read;
34
35
  // Read time limit, that is used for local records of requested tablet.
36
  HybridTime local_limit;
37
38
  // Read time limit, that is used for global entries, for instance transactions.
39
  HybridTime global_limit;
40
41
  // Read time limit for intents from the same transaction.
42
  HybridTime in_txn_limit;
43
44
  // Serial no of request that uses this read hybrid time.
45
  int64_t serial_no = 0;
46
47
1.30M
  static ReadHybridTime Max() {
48
1.30M
    return SingleTime(HybridTime::kMax);
49
1.30M
  }
50
51
4.51M
  static ReadHybridTime SingleTime(HybridTime value) {
52
4.51M
    return {value, value, value, HybridTime::kMax, 0};
53
4.51M
  }
54
55
  static ReadHybridTime FromMicros(MicrosTime micros) {
56
    return SingleTime(HybridTime::FromMicros(micros));
57
  }
58
59
2.26k
  static ReadHybridTime FromUint64(uint64_t value) {
60
2.26k
    return SingleTime(HybridTime(value));
61
2.26k
  }
62
63
10.1M
  static ReadHybridTime FromHybridTimeRange(const HybridTimeRange& range) {
64
10.1M
    return {range.first, range.second, range.second, HybridTime::kMax, 0};
65
10.1M
  }
66
67
  template <class PB>
68
12.5M
  static ReadHybridTime FromReadTimePB(const PB& pb) {
69
12.5M
    if (!pb.has_read_time()) {
70
10.2M
      return ReadHybridTime();
71
10.2M
    }
72
2.23M
    return FromPB(pb.read_time());
73
12.5M
  }
yb::ReadHybridTime yb::ReadHybridTime::FromReadTimePB<yb::tserver::ReadRequestPB>(yb::tserver::ReadRequestPB const&)
Line
Count
Source
68
9.54M
  static ReadHybridTime FromReadTimePB(const PB& pb) {
69
9.54M
    if (!pb.has_read_time()) {
70
7.99M
      return ReadHybridTime();
71
7.99M
    }
72
1.55M
    return FromPB(pb.read_time());
73
9.54M
  }
yb::ReadHybridTime yb::ReadHybridTime::FromReadTimePB<yb::tserver::WriteRequestPB>(yb::tserver::WriteRequestPB const&)
Line
Count
Source
68
2.94M
  static ReadHybridTime FromReadTimePB(const PB& pb) {
69
2.94M
    if (!pb.has_read_time()) {
70
2.27M
      return ReadHybridTime();
71
2.27M
    }
72
667k
    return FromPB(pb.read_time());
73
2.94M
  }
yb::ReadHybridTime yb::ReadHybridTime::FromReadTimePB<yb::ChildTransactionDataPB>(yb::ChildTransactionDataPB const&)
Line
Count
Source
68
13.8k
  static ReadHybridTime FromReadTimePB(const PB& pb) {
69
13.8k
    if (!pb.has_read_time()) {
70
0
      return ReadHybridTime();
71
0
    }
72
13.8k
    return FromPB(pb.read_time());
73
13.8k
  }
yb::ReadHybridTime yb::ReadHybridTime::FromReadTimePB<yb::QLPagingStatePB>(yb::QLPagingStatePB const&)
Line
Count
Source
68
281
  static ReadHybridTime FromReadTimePB(const PB& pb) {
69
281
    if (!pb.has_read_time()) {
70
5
      return ReadHybridTime();
71
5
    }
72
276
    return FromPB(pb.read_time());
73
281
  }
74
75
  template <class PB>
76
11.8M
  static ReadHybridTime FromRestartReadTimePB(const PB& pb) {
77
11.8M
    if (!pb.has_restart_read_time()) {
78
11.8M
      return ReadHybridTime();
79
11.8M
    }
80
26.2k
    return FromPB(pb.restart_read_time());
81
11.8M
  }
yb::ReadHybridTime yb::ReadHybridTime::FromRestartReadTimePB<yb::tserver::WriteResponsePB>(yb::tserver::WriteResponsePB const&)
Line
Count
Source
76
2.45M
  static ReadHybridTime FromRestartReadTimePB(const PB& pb) {
77
2.45M
    if (!pb.has_restart_read_time()) {
78
2.45M
      return ReadHybridTime();
79
2.45M
    }
80
1.83k
    return FromPB(pb.restart_read_time());
81
2.45M
  }
yb::ReadHybridTime yb::ReadHybridTime::FromRestartReadTimePB<yb::tserver::ReadResponsePB>(yb::tserver::ReadResponsePB const&)
Line
Count
Source
76
9.44M
  static ReadHybridTime FromRestartReadTimePB(const PB& pb) {
77
9.44M
    if (!pb.has_restart_read_time()) {
78
9.41M
      return ReadHybridTime();
79
9.41M
    }
80
24.3k
    return FromPB(pb.restart_read_time());
81
9.44M
  }
82
83
  template <class PB>
84
13.0M
  static ReadHybridTime FromPB(const PB& read_time) {
85
13.0M
    return {
86
13.0M
      .read = HybridTime(read_time.read_ht()),
87
13.0M
      .local_limit = HybridTime(read_time.has_local_limit_ht()
88
13.0M
          ? 
read_time.local_limit_ht()12.9M
89
13.0M
          : 
read_time.deprecated_max_of_read_time_and_local_limit_ht()56.9k
),
90
13.0M
      .global_limit = HybridTime(read_time.global_limit_ht()),
91
      // Use max hybrid time for backward compatibility.
92
13.0M
      .in_txn_limit = read_time.in_txn_limit_ht()
93
13.0M
          ? 
HybridTime(read_time.in_txn_limit_ht())12.9M
94
13.0M
          : 
HybridTime::kMax47.0k
,
95
13.0M
      .serial_no = 0,
96
13.0M
    };
97
13.0M
  }
98
99
  template <class PB>
100
11.1M
  void ToPB(PB* out) const {
101
11.1M
    out->set_read_ht(read.ToUint64());
102
11.1M
    out->set_local_limit_ht(local_limit.ToUint64());
103
11.1M
    out->set_global_limit_ht(global_limit.ToUint64());
104
11.1M
    out->set_in_txn_limit_ht(
105
11.1M
        in_txn_limit.is_valid() ? 
in_txn_limit.ToUint64()3.24M
:
HybridTime::kMax.ToUint64()7.88M
);
106
11.1M
    out->set_deprecated_max_of_read_time_and_local_limit_ht(std::max(local_limit, read).ToUint64());
107
11.1M
  }
108
109
  template <class PB>
110
2.39M
  void AddToPB(PB* pb) const {
111
2.39M
    if (read.is_valid()) {
112
2.39M
      ToPB(pb->mutable_read_time());
113
2.39M
    } else {
114
87
      pb->clear_read_time();
115
87
    }
116
2.39M
  }
void yb::ReadHybridTime::AddToPB<yb::tserver::ReadRequestPB>(yb::tserver::ReadRequestPB*) const
Line
Count
Source
110
1.54M
  void AddToPB(PB* pb) const {
111
1.54M
    if (read.is_valid()) {
112
1.54M
      ToPB(pb->mutable_read_time());
113
1.54M
    } else {
114
30
      pb->clear_read_time();
115
30
    }
116
1.54M
  }
void yb::ReadHybridTime::AddToPB<yb::tserver::WriteRequestPB>(yb::tserver::WriteRequestPB*) const
Line
Count
Source
110
668k
  void AddToPB(PB* pb) const {
111
668k
    if (read.is_valid()) {
112
668k
      ToPB(pb->mutable_read_time());
113
668k
    } else {
114
6
      pb->clear_read_time();
115
6
    }
116
668k
  }
void yb::ReadHybridTime::AddToPB<yb::tserver::PgPerformOptionsPB>(yb::tserver::PgPerformOptionsPB*) const
Line
Count
Source
110
48.5k
  void AddToPB(PB* pb) const {
111
48.5k
    if (
read.is_valid()48.5k
) {
112
48.5k
      ToPB(pb->mutable_read_time());
113
18.4E
    } else {
114
18.4E
      pb->clear_read_time();
115
18.4E
    }
116
48.5k
  }
void yb::ReadHybridTime::AddToPB<yb::ChildTransactionDataPB>(yb::ChildTransactionDataPB*) const
Line
Count
Source
110
66.6k
  void AddToPB(PB* pb) const {
111
66.6k
    if (read.is_valid()) {
112
66.6k
      ToPB(pb->mutable_read_time());
113
66.6k
    } else {
114
12
      pb->clear_read_time();
115
12
    }
116
66.6k
  }
void yb::ReadHybridTime::AddToPB<yb::QLPagingStatePB>(yb::QLPagingStatePB*) const
Line
Count
Source
110
2.39k
  void AddToPB(PB* pb) const {
111
2.39k
    if (read.is_valid()) {
112
2.39k
      ToPB(pb->mutable_read_time());
113
2.39k
    } else {
114
0
      pb->clear_read_time();
115
0
    }
116
2.39k
  }
void yb::ReadHybridTime::AddToPB<yb::PgsqlPagingStatePB>(yb::PgsqlPagingStatePB*) const
Line
Count
Source
110
62.8k
  void AddToPB(PB* pb) const {
111
62.8k
    if (read.is_valid()) {
112
62.8k
      ToPB(pb->mutable_read_time());
113
62.8k
    } else {
114
42
      pb->clear_read_time();
115
42
    }
116
62.8k
  }
117
118
61.6M
  explicit operator bool() const {
119
61.6M
    return read.is_valid();
120
61.6M
  }
121
122
43.5M
  bool operator!() const {
123
43.5M
    return !read.is_valid();
124
43.5M
  }
125
126
#define YB_READ_HYBRID_TIME_FIELDS read, local_limit, global_limit, in_txn_limit, serial_no
127
128
8.59k
  std::string ToString() const {
129
8.59k
    return YB_STRUCT_TO_STRING(YB_READ_HYBRID_TIME_FIELDS);
130
8.59k
  }
131
};
132
133
6.72k
inline std::ostream& operator<<(std::ostream& out, const ReadHybridTime& read_time) {
134
6.72k
  return out << read_time.ToString();
135
6.72k
}
136
137
7.90M
inline bool operator==(const ReadHybridTime& lhs, const ReadHybridTime& rhs) {
138
7.90M
  return YB_STRUCT_EQUALS(YB_READ_HYBRID_TIME_FIELDS);
139
7.90M
}
140
141
} // namespace yb
142
143
#endif // YB_COMMON_READ_HYBRID_TIME_H