YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/consensus/consensus.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/consensus.h"
33
34
#include <set>
35
36
#include "yb/consensus/opid_util.h"
37
#include "yb/gutil/strings/substitute.h"
38
#include "yb/util/result.h"
39
#include "yb/util/status_format.h"
40
41
namespace yb {
42
namespace consensus {
43
44
using std::shared_ptr;
45
using strings::Substitute;
46
47
0
std::string LeaderElectionData::ToString() const {
48
0
  return YB_STRUCT_TO_STRING(mode, originator_uuid, pending_commit, must_be_committed_opid);
49
0
}
50
51
ConsensusBootstrapInfo::ConsensusBootstrapInfo()
52
  : last_id(MinimumOpId()),
53
89.2k
    last_committed_id(MinimumOpId()) {
54
89.2k
}
55
56
11.4M
LeaderStatus Consensus::GetLeaderStatus(bool allow_stale) const {
57
11.4M
  return GetLeaderState(allow_stale).status;
58
11.4M
}
59
60
1.65M
int64_t Consensus::LeaderTerm() const {
61
1.65M
  return GetLeaderState().term;
62
1.65M
}
63
64
2
void Consensus::SetFaultHooks(const shared_ptr<ConsensusFaultHooks>& hooks) {
65
2
  fault_hooks_ = hooks;
66
2
}
67
68
2
const shared_ptr<Consensus::ConsensusFaultHooks>& Consensus::GetFaultHooks() const {
69
2
  return fault_hooks_;
70
2
}
71
72
26.0M
Status Consensus::ExecuteHook(HookPoint point) {
73
26.0M
  if (PREDICT_FALSE(fault_hooks_.get() != nullptr)) {
74
35
    switch (point) {
75
2
      case Consensus::PRE_START: return fault_hooks_->PreStart();
76
2
      case Consensus::POST_START: return fault_hooks_->PostStart();
77
0
      case Consensus::PRE_CONFIG_CHANGE: return fault_hooks_->PreConfigChange();
78
0
      case Consensus::POST_CONFIG_CHANGE: return fault_hooks_->PostConfigChange();
79
0
      case Consensus::PRE_REPLICATE: return fault_hooks_->PreReplicate();
80
0
      case Consensus::POST_REPLICATE: return fault_hooks_->PostReplicate();
81
14
      case Consensus::PRE_UPDATE: return fault_hooks_->PreUpdate();
82
14
      case Consensus::POST_UPDATE: return fault_hooks_->PostUpdate();
83
2
      case Consensus::PRE_SHUTDOWN: return fault_hooks_->PreShutdown();
84
2
      case Consensus::POST_SHUTDOWN: return fault_hooks_->PostShutdown();
85
0
      default: LOG(FATAL) << "Unknown fault hook.";
86
35
    }
87
35
  }
88
26.0M
  return Status::OK();
89
26.0M
}
90
91
557
Result<OpId> Consensus::GetLastOpId(OpIdType type) {
92
557
  switch (type) {
93
339
    case OpIdType::RECEIVED_OPID:
94
339
      return GetLastReceivedOpId();
95
218
    case OpIdType::COMMITTED_OPID:
96
218
      return GetLastCommittedOpId();
97
0
    default:
98
0
      break;
99
0
  }
100
0
  return STATUS(InvalidArgument, "Unsupported OpIdType", OpIdType_Name(type));
101
0
}
102
103
0
Status Consensus::StepDown(const LeaderStepDownRequestPB* req, LeaderStepDownResponsePB* resp) {
104
0
  return STATUS(NotSupported, "Not implemented.");
105
0
}
106
107
Status Consensus::ChangeConfig(const ChangeConfigRequestPB& req,
108
                               const StdStatusCallback& client_cb,
109
0
                               boost::optional<tserver::TabletServerErrorPB::Code>* error) {
110
0
  return STATUS(NotSupported, "Not implemented.");
111
0
}
112
113
0
Status Consensus::ConsensusFaultHooks::PreStart() {
114
0
  return Status::OK();
115
0
}
116
117
0
Status Consensus::ConsensusFaultHooks::PostStart() {
118
0
  return Status::OK();
119
0
}
120
121
0
Status Consensus::ConsensusFaultHooks::PreConfigChange() {
122
0
  return Status::OK();
123
0
}
124
125
0
Status Consensus::ConsensusFaultHooks::PostConfigChange() {
126
0
  return Status::OK();
127
0
}
128
129
0
Status Consensus::ConsensusFaultHooks::PreReplicate() {
130
0
  return Status::OK();
131
0
}
132
133
0
Status Consensus::ConsensusFaultHooks::PostReplicate() {
134
0
  return Status::OK();
135
0
}
136
137
0
Status Consensus::ConsensusFaultHooks::PreUpdate() {
138
0
  return Status::OK();
139
0
}
140
141
0
Status Consensus::ConsensusFaultHooks::PostUpdate() {
142
0
  return Status::OK();
143
0
}
144
145
0
Status Consensus::ConsensusFaultHooks::PreShutdown() {
146
0
  return Status::OK();
147
0
}
148
149
0
Status Consensus::ConsensusFaultHooks::PostShutdown() {
150
0
  return Status::OK();
151
0
}
152
153
17.4M
LeaderState& LeaderState::MakeNotReadyLeader(LeaderStatus status_) {
154
17.4M
  status = status_;
155
17.4M
  term = yb::OpId::kUnknownTerm;
156
17.4M
  return *this;
157
17.4M
}
158
159
5.77M
Status LeaderState::CreateStatus() const {
160
5.77M
  switch (status) {
161
76.9k
    case consensus::LeaderStatus::NOT_LEADER:
162
76.9k
      return STATUS(IllegalState, "Not the leader");
163
164
3.88k
    case consensus::LeaderStatus::LEADER_BUT_NO_OP_NOT_COMMITTED:
165
3.88k
        return STATUS(LeaderNotReadyToServe,
166
0
                      "Leader not yet replicated NoOp to be ready to serve requests");
167
168
2.62k
    case consensus::LeaderStatus::LEADER_BUT_OLD_LEADER_MAY_HAVE_LEASE:
169
2.62k
        return STATUS_FORMAT(LeaderNotReadyToServe,
170
0
                             "Previous leader's lease might still be active ($0 remaining).",
171
0
                             remaining_old_leader_lease);
172
173
1.95k
    case consensus::LeaderStatus::LEADER_BUT_NO_MAJORITY_REPLICATED_LEASE:
174
1.95k
        return STATUS(LeaderHasNoLease, "This leader has not yet acquired a lease.");
175
176
5.69M
    case consensus::LeaderStatus::LEADER_AND_READY:
177
5.69M
      return Status::OK();
178
0
  }
179
180
0
  FATAL_INVALID_ENUM_VALUE(consensus::LeaderStatus, status);
181
0
}
182
183
0
Status MoveStatus(LeaderState&& state) {
184
0
  return state.CreateStatus();
185
0
}
186
187
} // namespace consensus
188
} // namespace yb