/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 | 1 | std::string LeaderElectionData::ToString() const { |
48 | 1 | return YB_STRUCT_TO_STRING(mode, originator_uuid, pending_commit, must_be_committed_opid); |
49 | 1 | } |
50 | | |
51 | | ConsensusBootstrapInfo::ConsensusBootstrapInfo() |
52 | | : last_id(MinimumOpId()), |
53 | 150k | last_committed_id(MinimumOpId()) { |
54 | 150k | } |
55 | | |
56 | 32.1M | LeaderStatus Consensus::GetLeaderStatus(bool allow_stale) const { |
57 | 32.1M | return GetLeaderState(allow_stale).status; |
58 | 32.1M | } |
59 | | |
60 | 7.45M | int64_t Consensus::LeaderTerm() const { |
61 | 7.45M | return GetLeaderState().term; |
62 | 7.45M | } |
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 | 61.4M | Status Consensus::ExecuteHook(HookPoint point) { |
73 | 61.4M | if (PREDICT_FALSE(fault_hooks_.get() != nullptr)) { |
74 | 34 | 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 | 13 | case Consensus::PRE_UPDATE: return fault_hooks_->PreUpdate(); |
82 | 13 | 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 | 34 | } |
87 | 34 | } |
88 | 61.4M | return Status::OK(); |
89 | 61.4M | } |
90 | | |
91 | 556 | Result<OpId> Consensus::GetLastOpId(OpIdType type) { |
92 | 556 | switch (type) { |
93 | 340 | case OpIdType::RECEIVED_OPID: |
94 | 340 | return GetLastReceivedOpId(); |
95 | 216 | case OpIdType::COMMITTED_OPID: |
96 | 216 | return GetLastCommittedOpId(); |
97 | 0 | default: |
98 | 0 | break; |
99 | 556 | } |
100 | 0 | return STATUS(InvalidArgument, "Unsupported OpIdType", OpIdType_Name(type)); |
101 | 556 | } |
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 | 41.4M | LeaderState& LeaderState::MakeNotReadyLeader(LeaderStatus status_) { |
154 | 41.4M | status = status_; |
155 | 41.4M | term = yb::OpId::kUnknownTerm; |
156 | 41.4M | return *this; |
157 | 41.4M | } |
158 | | |
159 | 13.3M | Status LeaderState::CreateStatus() const { |
160 | 13.3M | switch (status) { |
161 | 81.2k | case consensus::LeaderStatus::NOT_LEADER: |
162 | 81.2k | return STATUS(IllegalState, "Not the leader"); |
163 | | |
164 | 7.18k | case consensus::LeaderStatus::LEADER_BUT_NO_OP_NOT_COMMITTED: |
165 | 7.18k | return STATUS(LeaderNotReadyToServe, |
166 | 0 | "Leader not yet replicated NoOp to be ready to serve requests"); |
167 | | |
168 | 3.16k | case consensus::LeaderStatus::LEADER_BUT_OLD_LEADER_MAY_HAVE_LEASE: |
169 | 3.16k | return STATUS_FORMAT(LeaderNotReadyToServe, |
170 | 0 | "Previous leader's lease might still be active ($0 remaining).", |
171 | 0 | remaining_old_leader_lease); |
172 | | |
173 | 15.2k | case consensus::LeaderStatus::LEADER_BUT_NO_MAJORITY_REPLICATED_LEASE: |
174 | 15.2k | return STATUS(LeaderHasNoLease, "This leader has not yet acquired a lease."); |
175 | | |
176 | 13.2M | case consensus::LeaderStatus::LEADER_AND_READY: |
177 | 13.2M | return Status::OK(); |
178 | 13.3M | } |
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 |