CCF
Loading...
Searching...
No Matches
raft_types.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/crypto/ecdsa.h"
6#include "ccf/entity_id.h"
10#include "kv/kv_types.h"
11
12#include <array>
13#include <chrono>
14#include <cstdint>
15#include <limits>
16
17namespace aft
18{
19 using Index = uint64_t;
20 using Term = uint64_t;
21 using Node2NodeMsg = uint64_t;
22
23 static constexpr size_t starting_view_change = 2;
24
25 class Store
26 {
27 public:
28 virtual ~Store() = default;
29 virtual void compact(Index v) = 0;
30 virtual void rollback(
31 const ccf::TxID& tx_id, Term term_of_next_version) = 0;
32 virtual void initialise_term(Term t) = 0;
33 virtual std::unique_ptr<ccf::kv::AbstractExecutionWrapper> deserialize(
34 std::vector<uint8_t> data,
35 bool public_only = false,
36 const std::optional<ccf::TxID>& expected_txid = std::nullopt) = 0;
37 };
38
39 template <typename T>
40 class Adaptor : public Store
41 {
42 private:
43 std::weak_ptr<T> x;
44
45 public:
46 Adaptor(std::shared_ptr<T> x) : x(x) {}
47
48 void compact(Index v) override
49 {
50 auto p = x.lock();
51 if (p)
52 {
53 p->compact(v);
54 }
55 }
56
57 void rollback(const ccf::TxID& tx_id, Term term_of_next_version) override
58 {
59 auto p = x.lock();
60 if (p)
61 {
62 p->rollback(tx_id, term_of_next_version);
63 }
64 }
65
66 void initialise_term(Term t) override
67 {
68 auto p = x.lock();
69 if (p)
70 {
71 p->initialise_term(t);
72 }
73 }
74
75 std::unique_ptr<ccf::kv::AbstractExecutionWrapper> deserialize(
76 const std::vector<uint8_t> data,
77 bool public_only = false,
78 const std::optional<ccf::TxID>& expected_txid = std::nullopt) override
79 {
80 auto p = x.lock();
81 if (p)
82 {
83 return p->deserialize(data, public_only, expected_txid);
84 }
85 return nullptr;
86 }
87 };
88
89 // NOLINTNEXTLINE(performance-enum-size)
103 {
104 {RaftMsgType::raft_append_entries, "raft_append_entries"},
106 "raft_append_entries_response"},
108 "raft_append_entries_signed_response"},
109 {RaftMsgType::raft_request_vote, "raft_request_vote"},
110 {RaftMsgType::raft_request_vote_response, "raft_request_vote_response"},
111 {RaftMsgType::raft_propose_request_vote, "raft_propose_request_vote"},
112 {RaftMsgType::raft_request_pre_vote, "raft_request_pre_vote"},
114 "raft_request_pre_vote_response"},
115 });
116
117#pragma pack(push, 1)
118 template <RaftMsgType M>
120 {
122 };
123
127 ::consensus::AppendEntriesIndex
128 {
132 // An AppendEntries now contains entries for a single term. So this
133 // describes the term of all entries in the range, and if this is different
134 // from prev_term, then the first entry is the first transaction in that new
135 // term
137 // This is unused by the current implementation, but is kept to preserve
138 // wire compatibility with previous versions of the code.
139 bool contains_new_view = false;
140 };
147 term,
148 prev_term,
149 leader_commit_idx,
150 term_of_idx,
151 contains_new_view);
152
153 enum class AppendEntriesResponseType : uint8_t
154 {
155 OK = 0,
156 FAIL = 1
157 };
162
163 DECLARE_JSON_TYPE(RaftHeader<raft_append_entries_response>)
164 DECLARE_JSON_REQUIRED_FIELDS(RaftHeader<raft_append_entries_response>, msg)
166 {
167 // This term and idx usually refer to the tail of the sender's log. The
168 // exception is in a rejection because of a mismatching suffix, in which
169 // case this describes the latest point that the local node believes may
170 // still match the leader's (which may be from an old term!). In either case
171 // this can be treated as the _latest possible_ matching index for this
172 // follower. Note this may still be higher than the true match index, so
173 // should not affect the leader's opinion of how matched the follower is.
174 // Only a positive response should modify match_idx.
178 };
182 AppendEntriesResponse, term, last_log_idx, success);
183
184 enum ElectionType : std::uint8_t
185 {
187 RegularVote = 1
188 };
191 {{ElectionType::PreVote, "PreVote"},
192 {ElectionType::RegularVote, "RegularVote"}});
193
194 DECLARE_JSON_TYPE(RaftHeader<raft_request_vote>)
195 DECLARE_JSON_REQUIRED_FIELDS(RaftHeader<raft_request_vote>, msg)
204 RequestVote, term, last_committable_idx, term_of_last_committable_idx);
205
217 RequestPreVote, term, last_committable_idx, term_of_last_committable_idx);
218
229
240
244 {
245 // A node sends this to nudge another node to begin an election, for
246 // instance because the sender is a retiring primary
248 };
252
253#pragma pack(pop)
254}
255
256FMT_BEGIN_NAMESPACE
257template <>
258struct formatter<aft::RaftMsgType>
259{
260 template <typename ParseContext>
261 constexpr auto parse(ParseContext& ctx)
262 {
263 return ctx.begin();
264 }
265
266 template <typename FormatContext>
267 auto format(const aft::RaftMsgType& msg_type, FormatContext& ctx) const
268 -> decltype(ctx.out())
269 {
270 switch (msg_type)
271 {
273 {
274 return fmt::format_to(ctx.out(), "append_entries");
275 }
277 {
278 return fmt::format_to(ctx.out(), "append_entries_response");
279 }
281 {
282 return fmt::format_to(ctx.out(), "append_entries_signed_response");
283 }
285 {
286 return fmt::format_to(ctx.out(), "request_vote");
287 }
289 {
290 return fmt::format_to(ctx.out(), "request_vote_response");
291 }
293 {
294 return fmt::format_to(ctx.out(), "propose_request_vote");
295 }
297 {
298 return fmt::format_to(ctx.out(), "request_pre_vote");
299 }
301 {
302 return fmt::format_to(ctx.out(), "request_pre_vote_response");
303 }
304 default:
305 throw std::runtime_error(
306 fmt::format("Unhandled RaftMsgType: {}", uint64_t(msg_type)));
307 }
308 }
309};
310FMT_END_NAMESPACE
Definition raft_types.h:41
Adaptor(std::shared_ptr< T > x)
Definition raft_types.h:46
void rollback(const ccf::TxID &tx_id, Term term_of_next_version) override
Definition raft_types.h:57
void initialise_term(Term t) override
Definition raft_types.h:66
std::unique_ptr< ccf::kv::AbstractExecutionWrapper > deserialize(const std::vector< uint8_t > data, bool public_only=false, const std::optional< ccf::TxID > &expected_txid=std::nullopt) override
Definition raft_types.h:75
void compact(Index v) override
Definition raft_types.h:48
Definition raft_types.h:26
virtual void rollback(const ccf::TxID &tx_id, Term term_of_next_version)=0
virtual ~Store()=default
virtual std::unique_ptr< ccf::kv::AbstractExecutionWrapper > deserialize(std::vector< uint8_t > data, bool public_only=false, const std::optional< ccf::TxID > &expected_txid=std::nullopt)=0
virtual void compact(Index v)=0
virtual void initialise_term(Term t)=0
#define DECLARE_JSON_TYPE_WITH_BASE(TYPE, BASE)
Definition json.h:669
#define DECLARE_JSON_TYPE_WITH_2BASES(TYPE, BASE1, BASE2)
Definition json.h:680
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:718
#define DECLARE_JSON_TYPE(TYPE)
Definition json.h:667
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:841
Definition state.h:17
ElectionType
Definition raft_types.h:185
@ PreVote
Definition raft_types.h:186
@ RegularVote
Definition raft_types.h:187
AppendEntriesResponseType
Definition raft_types.h:154
uint64_t Node2NodeMsg
Definition raft_types.h:21
uint64_t Term
Definition raft_types.h:20
RaftMsgType
Definition raft_types.h:91
@ raft_append_entries_response
Definition raft_types.h:93
@ raft_request_pre_vote_response
Definition raft_types.h:99
@ raft_request_vote
Definition raft_types.h:95
@ raft_request_pre_vote
Definition raft_types.h:98
@ raft_request_vote_response
Definition raft_types.h:96
@ raft_append_entries
Definition raft_types.h:92
@ raft_propose_request_vote
Definition raft_types.h:97
@ raft_append_entries_signed_response
Definition raft_types.h:94
uint64_t Index
Definition raft_types.h:19
Definition consensus_types.h:23
Definition raft_types.h:166
Term term
Definition raft_types.h:175
AppendEntriesResponseType success
Definition raft_types.h:177
Index last_log_idx
Definition raft_types.h:176
Definition raft_types.h:128
Index leader_commit_idx
Definition raft_types.h:131
Term prev_term
Definition raft_types.h:130
Term term
Definition raft_types.h:129
Term term_of_idx
Definition raft_types.h:136
Definition raft_types.h:244
Term term
Definition raft_types.h:247
Definition raft_types.h:120
RaftMsgType msg
Definition raft_types.h:121
Definition raft_types.h:233
bool vote_granted
Definition raft_types.h:235
Term term
Definition raft_types.h:234
Definition raft_types.h:209
Term term_of_last_committable_idx
Definition raft_types.h:212
Index last_committable_idx
Definition raft_types.h:211
Term term
Definition raft_types.h:210
Definition raft_types.h:222
Term term
Definition raft_types.h:223
bool vote_granted
Definition raft_types.h:224
Definition raft_types.h:197
Index last_committable_idx
Definition raft_types.h:199
Term term
Definition raft_types.h:198
Term term_of_last_committable_idx
Definition raft_types.h:200
Definition tx_id.h:44
Definition consensus_types.h:35
constexpr auto parse(ParseContext &ctx)
Definition raft_types.h:261
auto format(const aft::RaftMsgType &msg_type, FormatContext &ctx) const -> decltype(ctx.out())
Definition raft_types.h:267