Derecho  0.9
Distributed systems toolkit for RDMA
group.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <cstdint>
5 #include <ctime>
6 #include <exception>
7 #include <iostream>
8 #include <list>
9 #include <map>
10 #include <mutex>
11 #include <queue>
12 #include <string>
13 #include <type_traits>
14 #include <typeindex>
15 #include <utility>
16 #include <vector>
17 
18 #include <derecho/tcp/tcp.hpp>
19 
20 #include "derecho_exception.hpp"
23 #include "detail/rpc_manager.hpp"
24 #include "detail/view_manager.hpp"
25 #include "replicated.hpp"
26 #include "subgroup_info.hpp"
27 
28 #include <derecho/conf/conf.hpp>
29 #include <mutils-containers/KindMap.hpp>
30 #include <mutils-containers/TypeMap2.hpp>
31 
32 namespace derecho {
38 template <uint32_t counter, typename TargetType, typename FirstType, typename... RestTypes>
39 constexpr uint32_t index_of_type_impl() {
40  if constexpr(std::is_same<TargetType, FirstType>::value)
41  return counter;
42  else
43  return index_of_type_impl<counter + 1, TargetType, RestTypes...>();
44 }
45 
54 template <typename TargetType, typename... TypePack>
55 constexpr inline uint32_t index_of_type = index_of_type_impl<0, TargetType, TypePack...>();
56 
62 template <typename TargetType, typename... TypePack>
63 using contains = std::integral_constant<bool, (std::is_same<TargetType, TypePack>::value || ...)>;
64 
65 //Type alias for a sparse-vector of Replicated, otherwise KindMap can't understand it's a template
66 template <typename T>
67 using replicated_index_map = std::map<uint32_t, Replicated<T>>;
68 
69 class _Group {
70 private:
71 protected:
72  virtual uint32_t get_index_of_type(const std::type_info&) = 0;
73 
74 public:
75  virtual ~_Group() = default;
76 
77  template <typename SubgroupType>
78  auto& get_subgroup(uint32_t subgroup_num = 0);
79 
80  template <typename SubgroupType>
81  auto& get_nonmember_subgroup(uint32_t subgroup_num = 0);
82 
83  template <typename SubgroupType>
84  std::size_t get_number_of_shards(uint32_t subgroup_index = 0);
85 
86  template <typename SubgroupType>
87  std::vector<std::vector<node_id_t>> get_subgroup_members(uint32_t subgroup_index = 0);
88 };
89 
90 template <typename ReplicatedType>
91 class GroupProjection : public virtual _Group {
92 protected:
93  virtual void set_replicated_pointer(std::type_index, uint32_t, void**) = 0;
94  virtual void set_external_caller_pointer(std::type_index, uint32_t, void**) = 0;
95  virtual ViewManager& get_view_manager() = 0;
96 
97 public:
98  Replicated<ReplicatedType>& get_subgroup(uint32_t subgroup_num = 0);
99  ExternalCaller<ReplicatedType>& get_nonmember_subgroup(uint32_t subgroup_index = 0);
100  std::vector<std::vector<node_id_t>> get_subgroup_members(uint32_t subgroup_index = 0);
101  std::size_t get_number_of_shards(uint32_t subgroup_index = 0);
102 };
103 
105 public:
107  uint32_t subgroup_index;
108  void set_group_pointers(_Group* group, uint32_t subgroup_index) {
109  this->group = group;
110  this->subgroup_index = subgroup_index;
111  }
112 };
113 
121 template <typename... ReplicatedTypes>
122 class Group : public virtual _Group, public GroupProjection<ReplicatedTypes>... {
123 public:
124  void set_replicated_pointer(std::type_index type, uint32_t subgroup_num, void** ret);
125  void set_external_caller_pointer(std::type_index type, uint32_t subgroup_num, void** ret);
126 
127 protected:
128  uint32_t get_index_of_type(const std::type_info&) override;
129  ViewManager& get_view_manager() override;
130 
131 private:
133 
134  //Same thing for a sparse-vector of ExternalCaller
135  template <typename T>
136  using external_caller_index_map = std::map<uint32_t, ExternalCaller<T>>;
137 
140  std::optional<tcp::socket> leader_connection;
149  // std::shared_ptr<IDeserializationContext> user_deserialization_context;
151 
158  std::shared_ptr<tcp::tcp_connections> tcp_sockets;
166  mutils::KindMap<Factory, ReplicatedTypes...> factories;
175  mutils::KindMap<replicated_index_map, ReplicatedTypes...> replicated_objects;
182  mutils::KindMap<external_caller_index_map, ReplicatedTypes...> external_callers;
195  std::map<subgroup_id_t, std::reference_wrapper<ReplicatedObject>> objects_by_subgroup_id;
196 
204  void receive_objects(const std::set<std::pair<subgroup_id_t, node_id_t>>& subgroups_and_leaders);
205 
207  void set_up_components();
208 
216  template <typename... Empty>
217  std::enable_if_t<0 == sizeof...(Empty),
218  std::set<std::pair<subgroup_id_t, node_id_t>>>
220  return std::set<std::pair<subgroup_id_t, node_id_t>>();
221  }
222 
241  template <typename FirstType, typename... RestTypes>
242  std::set<std::pair<subgroup_id_t, node_id_t>> construct_objects(
243  const View& curr_view, const vector_int64_2d& old_shard_leaders);
244 
245 public:
263  Group(const CallbackSet& callbacks,
264  const SubgroupInfo& subgroup_info,
265  IDeserializationContext* deserialization_context,
266  std::vector<view_upcall_t> _view_upcalls = {},
267  Factory<ReplicatedTypes>... factories);
268 
279  Group(const SubgroupInfo& subgroup_info, Factory<ReplicatedTypes>... factories);
280 
281  ~Group();
282 
301  template <typename SubgroupType>
302  Replicated<SubgroupType>& get_subgroup(uint32_t subgroup_index = 0);
303 
317  template <typename SubgroupType>
318  ExternalCaller<SubgroupType>& get_nonmember_subgroup(uint32_t subgroup_index = 0);
319 
320  template <typename SubgroupType>
321  ShardIterator<SubgroupType> get_shard_iterator(uint32_t subgroup_index = 0);
322 
326  void leave(bool group_shutdown = true);
327 
329  std::vector<node_id_t> get_members();
336  template <typename SubgroupType>
337  std::vector<std::vector<node_id_t>> get_subgroup_members(uint32_t subgroup_index = 0);
339  std::int32_t get_my_rank();
341  node_id_t get_my_id();
345  template <typename SubgroupType>
346  std::int32_t get_my_shard(uint32_t subgroup_index = 0);
348  void report_failure(const node_id_t who);
350  void barrier_sync();
351  void debug_print_status() const;
352 };
353 
354 } /* namespace derecho */
355 
356 #include "detail/group_impl.hpp"
The Deserialization Interface to be implemented by user applications.
Definition: rpc_manager.hpp:36
auto & get_subgroup(uint32_t subgroup_num=0)
Definition: group_impl.hpp:21
PersistenceManager persistence_manager
Persist the objects.
Definition: group.hpp:154
The top-level object for creating a Derecho group.
Definition: group.hpp:122
std::vector< std::vector< int64_t > > vector_int64_2d
Type of a 2-dimensional vector used to store potential node IDs, or -1.
mutils::KindMap< Factory, ReplicatedTypes... > factories
Maps a type to the Factory for that type.
Definition: group.hpp:166
IDeserializationContext * user_deserialization_context
The shared pointer holding deserialization context is obsolete.
Definition: group.hpp:150
rpc::RPCManager rpc_manager
Contains all state related to receiving and handling RPC function calls for any Replicated objects im...
Definition: group.hpp:164
std::shared_ptr< tcp::tcp_connections > tcp_sockets
Contains a TCP connection to each member of the group, for the purpose of transferring state informat...
Definition: group.hpp:158
ViewManager view_manager
Contains all state related to managing Views, including the MulticastGroup and SST (since those chang...
Definition: group.hpp:161
bool is_starting_leader
Definition: group.hpp:139
const node_id_t my_id
Definition: group.hpp:138
mutils::KindMap< external_caller_index_map, ReplicatedTypes... > external_callers
Maps each type T to a map of (index -> ExternalCaller<T>) for the subgroup(s) of that type that this ...
Definition: group.hpp:182
virtual ~_Group()=default
constexpr uint32_t index_of_type_impl()
The function that implements index_of_type, which is separate to hide the "counter" template paramete...
Definition: group.hpp:39
std::size_t get_number_of_shards(uint32_t subgroup_index=0)
Definition: group_impl.hpp:45
auto & get_nonmember_subgroup(uint32_t subgroup_num=0)
Definition: group_impl.hpp:29
void set(volatile Elem &e, const Elem &value)
Thread-safe setter for DerechoSST members; ensures there is a std::atomic_signal_fence after writing ...
std::optional< tcp::socket > leader_connection
Definition: group.hpp:140
constexpr uint32_t index_of_type
A compile-time "function" that computes the index of a type within a template parameter pack of types...
Definition: group.hpp:55
std::integral_constant< bool,(std::is_same< TargetType, TypePack >::value||...)> contains
A type-trait-like template that provides a True member "value" if TargetType matches some type in Typ...
Definition: group.hpp:63
PersistenceManager is responsible for persisting all the data in a group.
virtual uint32_t get_index_of_type(const std::type_info &)=0
std::function< std::unique_ptr< T >(persistent::PersistentRegistry *)> Factory
The type of factory function the user must provide to the Group constructor, to construct each Replic...
Bundles together a set of callback functions for message delivery events.
std::enable_if_t< 0==sizeof...(Empty), std::set< std::pair< subgroup_id_t, node_id_t > > > construct_objects(const View &, const vector_int64_2d &)
Base case for the construct_objects template.
Definition: group.hpp:219
uint32_t node_id_t
Type alias for Node IDs in a Derecho group.
std::vector< std::vector< node_id_t > > get_subgroup_members(uint32_t subgroup_index=0)
Definition: group_impl.hpp:37
std::map< uint32_t, Replicated< T > > replicated_index_map
Definition: group.hpp:67
Container for whatever information is needed to describe a Group&#39;s subgroups and shards.
std::map< subgroup_id_t, std::reference_wrapper< ReplicatedObject > > objects_by_subgroup_id
Alternate view of the Replicated<T>s, indexed by subgroup ID.
Definition: group.hpp:195
void set_group_pointers(_Group *group, uint32_t subgroup_index)
Definition: group.hpp:108
uint32_t subgroup_index
Definition: group.hpp:107
mutils::KindMap< replicated_index_map, ReplicatedTypes... > replicated_objects
Maps each type T to a map of (index -> Replicated<T>) for that type&#39;s subgroup(s).
Definition: group.hpp:175