Derecho  0.9
Distributed systems toolkit for RDMA
restart_state.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <map>
5 #include <memory>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 
10 #include "../subgroup_info.hpp"
11 #include "../view.hpp"
12 #include "derecho_internal.hpp"
13 
15 #include <spdlog/spdlog.h>
16 
17 namespace derecho {
25  uint32_t shard_num;
26  int vid;
27  int32_t leader_id; //Signed instead of unsigned so it can have the special value -1
28  std::vector<int32_t> max_received_by_sender;
29  RaggedTrim(subgroup_id_t subgroup_id, uint32_t shard_num, int vid,
30  int32_t leader_id, std::vector<int32_t> max_received_by_sender)
31  : subgroup_id(subgroup_id), shard_num(shard_num), vid(vid), leader_id(leader_id), max_received_by_sender(max_received_by_sender) {}
32  DEFAULT_SERIALIZATION_SUPPORT(RaggedTrim, subgroup_id, shard_num, vid, leader_id, max_received_by_sender);
33 };
34 
38 enum class CommitMessage {
39  PREPARE,
40  COMMIT,
41  ABORT,
42  ACK
43 };
44 
48 inline std::string ragged_trim_filename(subgroup_id_t subgroup_num, uint32_t shard_num) {
49  std::ostringstream string_builder;
50  string_builder << "RaggedTrim_" << subgroup_num << "_" << shard_num;
51  return string_builder.str();
52 }
53 
55 using ragged_trim_map_t = std::map<subgroup_id_t, std::map<uint32_t, std::unique_ptr<RaggedTrim>>>;
56 
57 struct RestartState {
65  std::vector<std::vector<int64_t>> restart_shard_leaders;
66  void load_ragged_trim(const View& curr_view);
76  static persistent::version_t ragged_trim_to_latest_version(const int32_t view_id,
77  const std::vector<int32_t>& max_received_by_sender);
78 };
79 
81 private:
85  std::unique_ptr<View> curr_view;
90 
91  std::unique_ptr<View> restart_view;
92  std::map<node_id_t, tcp::socket> waiting_join_sockets;
93  std::map<node_id_t, std::tuple<ip_addr_t, uint16_t, uint16_t, uint16_t, uint16_t>> rejoined_node_ips_and_ports;
94  std::set<node_id_t> members_sent_restart_view;
95  std::set<node_id_t> rejoined_node_ids;
96  std::set<node_id_t> last_known_view_members;
97  std::vector<std::vector<persistent::version_t>> longest_log_versions;
98  std::vector<std::vector<int64_t>> nodes_with_longest_log;
100 
108  void receive_joiner_logs(const node_id_t& joiner_id, tcp::socket& client_socket);
109 
117  bool compute_restart_view();
118 
124  std::unique_ptr<View> update_curr_and_next_restart_view();
125 
126 public:
127  static const int RESTART_LEADER_TIMEOUT = 2000;
128  RestartLeaderState(std::unique_ptr<View> _curr_view, RestartState& restart_state,
129  const SubgroupInfo& subgroup_info,
130  const node_id_t my_id);
138  void await_quorum(tcp::connection_listener& server_socket);
139 
146  bool has_restart_quorum();
147 
154  bool resend_view_until_quorum_lost();
155 
163  int64_t send_restart_view();
164 
169  void send_abort();
170 
177  int64_t send_prepare();
178 
183  void send_commit();
185  const View& get_curr_view() const { return *curr_view; }
187  const View& get_restart_view() const { return *restart_view; }
190  std::unique_ptr<View> take_restart_view() { return std::move(restart_view); }
191 
192  void print_longest_logs() const;
193 
207  static std::unique_ptr<View> make_next_view(const std::unique_ptr<View>& curr_view,
208  const std::vector<node_id_t>& joiner_ids,
209  const std::vector<std::tuple<ip_addr_t, uint16_t, uint16_t, uint16_t, uint16_t>>& joiner_ips_and_ports);
214  static bool contains_at_least_one_member_per_subgroup(std::set<node_id_t> rejoined_node_ids,
215  const View& last_view);
216 };
217 
218 } /* namespace derecho */
uint32_t subgroup_id_t
Type alias for the internal Subgroup IDs generated by ViewManager.
std::vector< std::vector< int64_t > > restart_shard_leaders
Map from (subgroup ID, shard num) to ID of the "restart leader" for that shard, which is the node wit...
CommitMessage
A type-safe set of messages that can be sent during two-phase commit.
DEFAULT_SERIALIZATION_SUPPORT(RaggedTrim, subgroup_id, shard_num, vid, leader_id, max_received_by_sender)
const SubgroupInfo & subgroup_info
subgroup_id_t subgroup_id
RestartState & restart_state
Mutable reference to RestartState, since this class needs to update the restart state stored in ViewM...
std::vector< int32_t > max_received_by_sender
std::unique_ptr< View > restart_view
RaggedTrim(subgroup_id_t subgroup_id, uint32_t shard_num, int vid, int32_t leader_id, std::vector< int32_t > max_received_by_sender)
A non-POD type which wishes to mark itself byte representable should extend this class.
const View & get_curr_view() const
Read the curr_view (last known view) managed by RestartLeaderState.
std::set< node_id_t > rejoined_node_ids
std::map< subgroup_id_t, std::map< uint32_t, std::unique_ptr< RaggedTrim > >> ragged_trim_map_t
List of logged ragged trim states, indexed by (subgroup ID, shard num), stored by pointer...
std::set< node_id_t > last_known_view_members
std::unique_ptr< View > curr_view
Takes ownership of ViewManager&#39;s curr_view pointer, because await_quroum() might replace curr_view wi...
uint32_t node_id_t
Type alias for Node IDs in a Derecho group.
ragged_trim_map_t logged_ragged_trim
List of logged ragged trim states recovered from the last known View, either read locally from this n...
std::set< node_id_t > members_sent_restart_view
std::vector< std::vector< int64_t > > nodes_with_longest_log
Container for whatever information is needed to describe a Group&#39;s subgroups and shards.
std::vector< std::vector< persistent::version_t > > longest_log_versions
std::map< node_id_t, std::tuple< ip_addr_t, uint16_t, uint16_t, uint16_t, uint16_t > > rejoined_node_ips_and_ports
Represents the data needed to log a "ragged trim" decision to disk.
std::map< node_id_t, tcp::socket > waiting_join_sockets
std::unique_ptr< View > take_restart_view()
Remove and return the restart view managed by RestartLeaderState; this will take ownership back to th...
const View & get_restart_view() const
Read the current restart view managed by RestartLeaderState.
std::string ragged_trim_filename(subgroup_id_t subgroup_num, uint32_t shard_num)
Builds a filename to use for a RaggedTrim logged to disk using its subgroup and shard IDs...