YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
51
int OpIdCompare(const OpIdPB& first, const OpIdPB& second) {
51
51
  DCHECK(first.IsInitialized());
52
51
  DCHECK(second.IsInitialized());
53
51
  if (PREDICT_TRUE(first.term() == second.term())) {
54
26
    return first.index() < second.index() ? -1 : first.index() == second.index() ? 0 : 1;
55
47
  }
56
4
  return first.term() < second.term() ? -1 : 1;
57
4
}
58
59
277
bool OpIdEquals(const OpIdPB& left, const OpIdPB& right) {
60
277
  DCHECK(left.IsInitialized());
61
277
  DCHECK(right.IsInitialized());
62
277
  return left.term() == right.term() && left.index() == right.index();
63
277
}
64
65
122k
bool OpIdLessThan(const OpIdPB& left, const OpIdPB& right) {
66
122k
  DCHECK(left.IsInitialized());
67
122k
  DCHECK(right.IsInitialized());
68
122k
  if (left.term() < right.term()) return true;
69
122k
  if (left.term() > right.term()) return false;
70
122k
  return left.index() < right.index();
71
122k
}
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
445k
OpIdPB MinimumOpId() {
115
445k
  OpIdPB op_id;
116
445k
  op_id.set_term(0);
117
445k
  op_id.set_index(0);
118
445k
  return op_id;
119
445k
}
120
121
15.1M
OpIdPB MaximumOpId() {
122
15.1M
  OpIdPB op_id;
123
15.1M
  op_id.set_term(std::numeric_limits<int64_t>::max());
124
15.1M
  op_id.set_index(std::numeric_limits<int64_t>::max());
125
15.1M
  return op_id;
126
15.1M
}
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
33.6k
std::ostream& operator<<(std::ostream& os, const OpIdPB& op_id) {
144
33.6k
  os << OpIdToString(op_id);
145
33.6k
  return os;
146
33.6k
}
147
148
37.4k
std::string OpIdToString(const OpIdPB& op_id) {
149
37.4k
  if (!op_id.IsInitialized()) {
150
0
    return "<uninitialized op>";
151
0
  }
152
37.4k
  return strings::Substitute("$0.$1", op_id.term(), op_id.index());
153
37.4k
}
154
155
101
std::string OpsRangeString(const ConsensusRequestPB& req) {
156
101
  std::string ret;
157
101
  ret.reserve(100);
158
101
  ret.push_back('[');
159
101
  if (req.ops_size() > 0) {
160
8
    const OpIdPB& first_op = req.ops(0).id();
161
8
    const OpIdPB& last_op = req.ops(req.ops_size() - 1).id();
162
8
    strings::SubstituteAndAppend(&ret, "$0.$1-$2.$3",
163
8
                                 first_op.term(), first_op.index(),
164
8
                                 last_op.term(), last_op.index());
165
8
  }
166
101
  ret.push_back(']');
167
101
  return ret;
168
101
}
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
6
  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