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;
23
24 using ReplyCallback = std::function<bool(
25 void* owner,
27 int status,
28 std::vector<uint8_t>&& data)>;
29
30 static constexpr size_t starting_view_change = 2;
31
32 class Store
33 {
34 public:
35 virtual ~Store() {}
36 virtual void compact(Index v) = 0;
37 virtual void rollback(
38 const ccf::kv::TxID& tx_id, Term term_of_next_version) = 0;
39 virtual void initialise_term(Term t) = 0;
40 virtual std::unique_ptr<ccf::kv::AbstractExecutionWrapper> deserialize(
41 const std::vector<uint8_t> data,
42 bool public_only = false,
43 const std::optional<ccf::kv::TxID>& expected_txid = std::nullopt) = 0;
44 };
45
46 template <typename T>
47 class Adaptor : public Store
48 {
49 private:
50 std::weak_ptr<T> x;
51
52 public:
53 Adaptor(std::shared_ptr<T> x) : x(x) {}
54
55 void compact(Index v) override
56 {
57 auto p = x.lock();
58 if (p)
59 {
60 p->compact(v);
61 }
62 }
63
65 const ccf::kv::TxID& tx_id, Term term_of_next_version) override
66 {
67 auto p = x.lock();
68 if (p)
69 {
70 p->rollback(tx_id, term_of_next_version);
71 }
72 }
73
74 void initialise_term(Term t) override
75 {
76 auto p = x.lock();
77 if (p)
78 {
79 p->initialise_term(t);
80 }
81 }
82
83 std::unique_ptr<ccf::kv::AbstractExecutionWrapper> deserialize(
84 const std::vector<uint8_t> data,
85 bool public_only = false,
86 const std::optional<ccf::kv::TxID>& expected_txid = std::nullopt) override
87 {
88 auto p = x.lock();
89 if (p)
90 {
91 return p->deserialize(data, public_only, expected_txid);
92 }
93 return nullptr;
94 }
95 };
96
108 {
109 {RaftMsgType::raft_append_entries, "raft_append_entries"},
111 "raft_append_entries_response"},
113 "raft_append_entries_signed_response"},
114 {RaftMsgType::raft_request_vote, "raft_request_vote"},
115 {RaftMsgType::raft_request_vote_response, "raft_request_vote_response"},
116 {RaftMsgType::raft_propose_request_vote, "raft_propose_request_vote"},
117 });
118
119#pragma pack(push, 1)
120 template <RaftMsgType M>
122 {
124 };
125
129 ::consensus::AppendEntriesIndex
130 {
134 // An AppendEntries now contains entries for a single term. So this
135 // describes the term of all entries in the range, and if this is different
136 // from prev_term, then the first entry is the first transaction in that new
137 // term
139 // This is unused by the current implementation, but is kept to preserve
140 // wire compatibility with previous versions of the code.
141 bool contains_new_view = false;
142 };
149 term,
150 prev_term,
151 leader_commit_idx,
152 term_of_idx,
153 contains_new_view);
154
155 enum class AppendEntriesResponseType : uint8_t
156 {
157 OK = 0,
158 FAIL = 1
159 };
164
165 DECLARE_JSON_TYPE(RaftHeader<raft_append_entries_response>)
166 DECLARE_JSON_REQUIRED_FIELDS(RaftHeader<raft_append_entries_response>, msg)
168 {
169 // This term and idx usually refer to the tail of the sender's log. The
170 // exception is in a rejection because of a mismatching suffix, in which
171 // case this describes the latest point that the local node believes may
172 // still match the leader's (which may be from an old term!). In either case
173 // this can be treated as the _latest possible_ matching index for this
174 // follower. Note this may still be higher than the true match index, so
175 // should not affect the leader's opinion of how matched the follower is.
176 // Only a positive response should modify match_idx.
180 };
184 AppendEntriesResponse, term, last_log_idx, success);
185
196 RequestVote, term, last_committable_idx, term_of_last_committable_idx);
197
208
212 {
213 // A node sends this to nudge another node to begin an election, for
214 // instance because the sender is a retiring primary
216 };
220
221#pragma pack(pop)
222}
Definition raft_types.h:48
Adaptor(std::shared_ptr< T > x)
Definition raft_types.h:53
void rollback(const ccf::kv::TxID &tx_id, Term term_of_next_version) override
Definition raft_types.h:64
std::unique_ptr< ccf::kv::AbstractExecutionWrapper > deserialize(const std::vector< uint8_t > data, bool public_only=false, const std::optional< ccf::kv::TxID > &expected_txid=std::nullopt) override
Definition raft_types.h:83
void initialise_term(Term t) override
Definition raft_types.h:74
void compact(Index v) override
Definition raft_types.h:55
Definition raft_types.h:33
virtual void rollback(const ccf::kv::TxID &tx_id, Term term_of_next_version)=0
virtual void compact(Index v)=0
virtual ~Store()
Definition raft_types.h:35
virtual std::unique_ptr< ccf::kv::AbstractExecutionWrapper > deserialize(const std::vector< uint8_t > data, bool public_only=false, const std::optional< ccf::kv::TxID > &expected_txid=std::nullopt)=0
virtual void initialise_term(Term t)=0
Definition sha256_hash.h:16
std::tuple< size_t, size_t > RequestID
Definition kv_types.h:372
#define DECLARE_JSON_TYPE_WITH_BASE(TYPE, BASE)
Definition json.h:665
#define DECLARE_JSON_TYPE_WITH_2BASES(TYPE, BASE1, BASE2)
Definition json.h:676
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:714
#define DECLARE_JSON_TYPE(TYPE)
Definition json.h:663
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:837
Definition state.h:18
AppendEntriesResponseType
Definition raft_types.h:156
std::function< bool(void *owner, ccf::kv::TxHistory::RequestID caller_rid, int status, std::vector< uint8_t > &&data)> ReplyCallback
Definition raft_types.h:28
uint64_t Node2NodeMsg
Definition raft_types.h:21
uint64_t Term
Definition raft_types.h:20
RaftMsgType
Definition raft_types.h:98
@ raft_append_entries_response
Definition raft_types.h:100
@ raft_request_vote
Definition raft_types.h:102
@ raft_request_vote_response
Definition raft_types.h:103
@ raft_append_entries
Definition raft_types.h:99
@ raft_propose_request_vote
Definition raft_types.h:104
@ raft_append_entries_signed_response
Definition raft_types.h:101
uint64_t Index
Definition raft_types.h:19
Definition consensus_types.h:23
Definition raft_types.h:168
Term term
Definition raft_types.h:177
AppendEntriesResponseType success
Definition raft_types.h:179
Index last_log_idx
Definition raft_types.h:178
Definition raft_types.h:130
Index leader_commit_idx
Definition raft_types.h:133
Term prev_term
Definition raft_types.h:132
Term term
Definition raft_types.h:131
Term term_of_idx
Definition raft_types.h:138
Definition raft_types.h:212
Term term
Definition raft_types.h:215
Definition raft_types.h:122
RaftMsgType msg
Definition raft_types.h:123
Definition raft_types.h:201
Term term
Definition raft_types.h:202
bool vote_granted
Definition raft_types.h:203
Definition raft_types.h:189
Index last_committable_idx
Definition raft_types.h:191
Term term
Definition raft_types.h:190
Term term_of_last_committable_idx
Definition raft_types.h:192
Definition kv_types.h:52
Definition consensus_types.h:35