YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/consensus/opid_util.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include "yb/consensus/opid_util.h"
33
34
#include <limits>
35
36
#include <glog/logging.h>
37
38
#include "yb/consensus/consensus.pb.h"
39
#include "yb/gutil/port.h"
40
#include "yb/gutil/strings/substitute.h"
41
#include "yb/util/opid.h"
42
43
namespace yb {
44
namespace consensus {
45
46
const int64_t kMinimumTerm = 0;
47
const int64_t kMinimumOpIdIndex = 0;
48
const int64_t kInvalidOpIdIndex = -1;
49
50
50
int OpIdCompare(const OpIdPB& first, const OpIdPB& second) {
51
50
  DCHECK(first.IsInitialized());
52
50
  DCHECK(second.IsInitialized());
53
50
  if (PREDICT_TRUE(first.term() == second.term())) {
54
46
    return first.index() < second.index() ? 
-120
:
first.index() == second.index()26
?
026
:
10
;
55
46
  }
56
4
  return first.term() < second.term() ? -1 : 
10
;
57
50
}
58
59
339
bool OpIdEquals(const OpIdPB& left, const OpIdPB& right) {
60
339
  DCHECK(left.IsInitialized());
61
339
  DCHECK(right.IsInitialized());
62
339
  return left.term() == right.term() && 
left.index() == right.index()152
;
63
339
}
64
65
1.13M
bool OpIdLessThan(const OpIdPB& left, const OpIdPB& right) {
66
1.13M
  DCHECK(left.IsInitialized());
67
1.13M
  DCHECK(right.IsInitialized());
68
1.13M
  if (left.term() < right.term()) 
return true157
;
69
1.13M
  if (left.term() > right.term()) 
return false98
;
70
1.13M
  return left.index() < right.index();
71
1.13M
}
72
73
0
bool OpIdBiggerThan(const OpIdPB& left, const OpIdPB& right) {
74
0
  DCHECK(left.IsInitialized());
75
0
  DCHECK(right.IsInitialized());
76
0
  if (left.term() > right.term()) return true;
77
0
  if (left.term() < right.term()) return false;
78
0
  return left.index() > right.index();
79
0
}
80
81
0
bool CopyIfOpIdLessThan(const OpIdPB& to_compare, OpIdPB* target) {
82
0
  if (to_compare.IsInitialized() &&
83
0
      (!target->IsInitialized() || OpIdLessThan(to_compare, *target))) {
84
0
    target->CopyFrom(to_compare);
85
0
    return true;
86
0
  }
87
0
  return false;
88
0
}
89
90
318
size_t OpIdHashFunctor::operator() (const OpIdPB& id) const {
91
318
  return (id.term() + 31) ^ id.index();
92
318
}
93
94
142
bool OpIdEqualsFunctor::operator() (const OpIdPB& left, const OpIdPB& right) const {
95
142
  return OpIdEquals(left, right);
96
142
}
97
98
0
bool OpIdLessThanPtrFunctor::operator() (const OpIdPB* left, const OpIdPB* right) const {
99
0
  return OpIdLessThan(*left, *right);
100
0
}
101
102
0
bool OpIdIndexLessThanPtrFunctor::operator() (const OpIdPB* left, const OpIdPB* right) const {
103
0
  return left->index() < right->index();
104
0
}
105
106
0
bool OpIdCompareFunctor::operator() (const OpIdPB& left, const OpIdPB& right) const {
107
0
  return OpIdLessThan(left, right);
108
0
}
109
110
0
bool OpIdBiggerThanFunctor::operator() (const OpIdPB& left, const OpIdPB& right) const {
111
0
  return OpIdBiggerThan(left, right);
112
0
}
113
114
751k
OpIdPB MinimumOpId() {
115
751k
  OpIdPB op_id;
116
751k
  op_id.set_term(0);
117
751k
  op_id.set_index(0);
118
751k
  return op_id;
119
751k
}
120
121
34.7M
OpIdPB MaximumOpId() {
122
34.7M
  OpIdPB op_id;
123
34.7M
  op_id.set_term(std::numeric_limits<int64_t>::max());
124
34.7M
  op_id.set_index(std::numeric_limits<int64_t>::max());
125
34.7M
  return op_id;
126
34.7M
}
127
128
// helper hash functor for delta store ids
129
struct DeltaIdHashFunction {
130
0
  size_t operator()(const std::pair<int64_t, int64_t >& id) const {
131
0
    return (id.first + 31) ^ id.second;
132
0
  }
133
};
134
135
// helper equals functor for delta store ids
136
struct DeltaIdEqualsTo {
137
  bool operator()(const std::pair<int64_t, int64_t >& left,
138
0
                  const std::pair<int64_t, int64_t >& right) const {
139
0
    return left.first == right.first && left.second == right.second;
140
0
  }
141
};
142
143
32.5k
std::ostream& operator<<(std::ostream& os, const OpIdPB& op_id) {
144
32.5k
  os << OpIdToString(op_id);
145
32.5k
  return os;
146
32.5k
}
147
148
36.3k
std::string OpIdToString(const OpIdPB& op_id) {
149
36.3k
  if (!op_id.IsInitialized()) {
150
0
    return "<uninitialized op>";
151
0
  }
152
36.3k
  return strings::Substitute("$0.$1", op_id.term(), op_id.index());
153
36.3k
}
154
155
1.59k
std::string OpsRangeString(const ConsensusRequestPB& req) {
156
1.59k
  std::string ret;
157
1.59k
  ret.reserve(100);
158
1.59k
  ret.push_back('[');
159
1.59k
  if (req.ops_size() > 0) {
160
34
    const OpIdPB& first_op = req.ops(0).id();
161
34
    const OpIdPB& last_op = req.ops(req.ops_size() - 1).id();
162
34
    strings::SubstituteAndAppend(&ret, "$0.$1-$2.$3",
163
34
                                 first_op.term(), first_op.index(),
164
34
                                 last_op.term(), last_op.index());
165
34
  }
166
1.59k
  ret.push_back(']');
167
1.59k
  return ret;
168
1.59k
}
169
170
1.20M
OpIdPB MakeOpId(int64_t term, int64_t index) {
171
1.20M
  OpIdPB ret;
172
1.20M
  ret.set_index(index);
173
1.20M
  ret.set_term(term);
174
18.4E
  LOG_IF(DFATAL, term < 0 || index < 0) << "MakeOpId: negative term/index: " << OpIdToString(ret);
175
1.20M
  return ret;
176
1.20M
}
177
178
1.20M
OpIdPB MakeOpIdPB(const yb::OpId& op_id) {
179
1.20M
  return MakeOpId(op_id.term, op_id.index);
180
1.20M
}
181
182
} // namespace consensus
183
}  // namespace yb