Derecho  0.9
Distributed systems toolkit for RDMA
replicated.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <functional>
10 #include <memory>
11 #include <tuple>
12 #include <type_traits>
13 
14 #include "derecho_exception.hpp"
18 #include "detail/rpc_manager.hpp"
19 #include "detail/rpc_utils.hpp"
20 
22 #include <derecho/tcp/tcp.hpp>
23 
24 namespace derecho {
25 
26 class _Group;
27 class GroupReference;
28 
35 class PersistsFields {};
36 
42 template <typename T>
43 using has_persistent_fields = std::is_base_of<PersistsFields, T>;
44 
51 struct RawObject {
52  static auto register_functions() { return std::tuple<>(); };
53 };
54 
59 inline std::unique_ptr<RawObject> raw_object_factory(persistent::PersistentRegistry*) {
60  return std::make_unique<RawObject>();
61 }
62 
63 template <typename T>
65 private:
68  std::unique_ptr<persistent::PersistentRegistry> persistent_registry_ptr;
69 #if defined(_PERFORMANCE_DEBUG) || !defined(NDEBUG)
70 public:
71 #endif
72 
77  std::unique_ptr<std::unique_ptr<T>> user_object_ptr;
78 #if defined(_PERFORMANCE_DEBUG) || !defined(NDEBUG)
79 private:
80 #endif
81 
85  const uint32_t subgroup_index;
91  const uint32_t shard_num;
95  std::unique_ptr<rpc::RemoteInvocableOf<T>> wrapped_this;
99  uint64_t next_timestamp_us = 0;
100 
101 public:
119  Replicated(subgroup_type_id_t type_id, node_id_t nid, subgroup_id_t subgroup_id, uint32_t subgroup_index,
120  uint32_t shard_num, rpc::RPCManager& group_rpc_manager,
121  Factory<T> client_object_factory, _Group* group);
122 
141  Replicated(subgroup_type_id_t type_id, node_id_t nid, subgroup_id_t subgroup_id, uint32_t subgroup_index,
142  uint32_t shard_num, rpc::RPCManager& group_rpc_manager, _Group* group);
143 
144  Replicated(Replicated&& rhs);
145  Replicated(const Replicated&) = delete;
146  virtual ~Replicated();
147 
153  virtual bool is_persistent() const {
155  }
156 
162  bool is_valid() const {
163  return *user_object_ptr && true;
164  }
165 
170  uint32_t get_shard_num() const {
171  return shard_num;
172  }
173 
183  template <rpc::FunctionTag tag, typename... Args>
184  auto p2p_send(node_id_t dest_node, Args&&... args);
185 
195  template <rpc::FunctionTag tag, typename... Args>
196  auto ordered_send(Args&&... args);
197 
203  void send(unsigned long long int payload_size, const std::function<void(char* buf)>& msg_generator);
204 
209  std::size_t object_size() const;
210 
218  void send_object(tcp::socket& receiver_socket) const;
219 
228  void send_object_raw(tcp::socket& receiver_socket) const;
229 
238  std::size_t receive_object(char* buffer);
239 
240  const uint64_t compute_global_stability_frontier();
241 
242  inline const HLC getFrontier() {
243  // transform from ns to us:
244  HLC hlc(this->compute_global_stability_frontier() / 1e3, 0);
245  return hlc;
246  }
247 
253  const persistent::version_t get_minimum_latest_persisted_version() noexcept(false);
254 
259  virtual void make_version(const persistent::version_t& ver, const HLC& hlc) noexcept(false) {
260  persistent_registry_ptr->makeVersion(ver, hlc);
261  };
262 
266  virtual void persist(const persistent::version_t version) noexcept(false);
267 
273  virtual void trim(const persistent::version_t& earliest_version) noexcept(false) {
274  persistent_registry_ptr->trim(earliest_version);
275  };
276 
283  virtual void truncate(const persistent::version_t& latest_version) {
284  persistent_registry_ptr->truncate(latest_version);
285  }
286 
290  virtual void post_next_version(const persistent::version_t& version, const uint64_t & ts_us) {
291  next_version = version;
292  next_timestamp_us = ts_us;
293  }
294 
298  virtual std::tuple<persistent::version_t,uint64_t> get_next_version() {
299  return std::tie(next_version,next_timestamp_us);
300  }
301 
308  virtual void register_persistent_member(const char* object_name,
309  const persistent::VersionFunc& vf,
310  const persistent::PersistFunc& pf,
311  const persistent::TrimFunc& tf,
313  persistent::TruncateFunc tcf) noexcept(false) {
314  this->persistent_registry_ptr->registerPersist(object_name, vf, pf, tf, gf, tcf);
315  }
316 };
317 
318 template <typename T>
319 class ExternalCaller {
320 private:
328  std::unique_ptr<rpc::RemoteInvokerFor<T>> wrapped_this;
329 
330 public:
331  ExternalCaller(uint32_t type_id, node_id_t nid, subgroup_id_t subgroup_id, rpc::RPCManager& group_rpc_manager);
332 
333  ExternalCaller(ExternalCaller&&) = default;
334  ExternalCaller(const ExternalCaller&) = delete;
335 
345  template <rpc::FunctionTag tag, typename... Args>
346  auto p2p_send(node_id_t dest_node, Args&&... args);
347 
348  bool is_valid() const { return true; }
349 };
350 
351 template <typename T>
353 private:
355  const std::vector<node_id_t> shard_reps;
356 
357 public:
358  ShardIterator(ExternalCaller<T>& EC, std::vector<node_id_t> shard_reps)
359  : EC(EC),
360  shard_reps(shard_reps) {}
361  template <rpc::FunctionTag tag, typename... Args>
362  auto p2p_send(Args&&... args);
363 };
364 } // namespace derecho
365 
uint32_t subgroup_id_t
Type alias for the internal Subgroup IDs generated by ViewManager.
const uint32_t shard_num
The index, within the subgroup, of the shard that replicates this object.
Definition: replicated.hpp:91
partial_wrapped< Tag, Ret, NewClass, Args... > tag(Ret(NewClass::*fun)(Args...))
User-facing entry point for the series of functions that binds a FunctionTag to a class&#39;s member func...
unsigned long long FunctionTag
Definition: rpc_utils.hpp:54
ShardIterator(ExternalCaller< T > &EC, std::vector< node_id_t > shard_reps)
Definition: replicated.hpp:358
void trim(const int64_t &earliest_version) noexcept(false)
Trims the log of all versions earlier than the argument.
Definition: Persistent.cpp:34
uint32_t subgroup_type_id_t
Type of the numeric ID used to refer to subgroup types within a Group; this is currently computed as ...
virtual void truncate(const persistent::version_t &latest_version)
Truncate the logs of all Persistent<T> members back to the version specified.
Definition: replicated.hpp:283
const subgroup_id_t subgroup_id
The internally-generated subgroup ID of the subgroup that replicates this object. ...
Definition: replicated.hpp:84
virtual void make_version(const persistent::version_t &ver, const HLC &hlc) noexcept(false)
make a version for all the persistent<T> members.
Definition: replicated.hpp:259
std::is_base_of< PersistsFields, T > has_persistent_fields
A template whose member field "value" will be true if type T inherits from PersistsFields, and false otherwise.
Definition: replicated.hpp:43
std::function< void(const version_t &, const HLC &)> VersionFunc
const node_id_t node_id
The ID of this node.
Definition: replicated.hpp:322
std::unique_ptr< rpc::RemoteInvokerFor< T > > wrapped_this
The actual implementation of ExternalCaller, which has lots of ugly template parameters.
Definition: replicated.hpp:328
subgroup_id_t subgroup_id
The internally-generated subgroup ID of the subgroup that this ExternalCaller will contact...
Definition: replicated.hpp:324
uint32_t get_shard_num() const
Definition: replicated.hpp:170
std::function< const version_t(void)> PersistFunc
bool send(uint16_t group_number, std::shared_ptr< rdma::memory_region > mr, size_t offset, size_t length) __attribute__((warn_unused_result))
ExternalCaller< T > & EC
Definition: replicated.hpp:354
std::unique_ptr< persistent::PersistentRegistry > persistent_registry_ptr
persistent registry for persistent<t>
Definition: replicated.hpp:68
const uint32_t subgroup_index
Definition: replicated.hpp:85
void truncate(const int64_t &last_version)
Truncates the log, deleting all versions newer than the provided argument.
Definition: Persistent.cpp:54
virtual bool is_persistent() const
Definition: replicated.hpp:153
virtual void trim(const persistent::version_t &earliest_version) noexcept(false)
trim the logs to a version, inclusively.
Definition: replicated.hpp:273
rpc::RPCManager & group_rpc_manager
Reference to the RPCManager for the Group this Replicated is in.
Definition: replicated.hpp:93
std::unique_ptr< RawObject > raw_object_factory(persistent::PersistentRegistry *)
An implementation of Factory<T> for RawObject, which is trivial because RawObjects have no state...
Definition: replicated.hpp:59
Common interface for all types of Replicated<T>, specifying some methods for state transfer and persi...
virtual void register_persistent_member(const char *object_name, const persistent::VersionFunc &vf, const persistent::PersistFunc &pf, const persistent::TrimFunc &tf, const persistent::LatestPersistedGetterFunc &gf, persistent::TruncateFunc tcf) noexcept(false)
Register a persistent member.
Definition: replicated.hpp:308
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...
bool is_valid() const
Definition: replicated.hpp:162
#define INVALID_VERSION
Definition: PersistLog.hpp:28
const HLC getFrontier()
Definition: replicated.hpp:242
void makeVersion(const int64_t &ver, const HLC &mhlc) noexcept(false)
Make a new version capturing the current state of the object.
Definition: Persistent.cpp:26
This is a marker interface for user-defined Replicated Objects (i.e.
Definition: replicated.hpp:35
std::function< void(const int64_t &)> TruncateFunc
virtual std::tuple< persistent::version_t, uint64_t > get_next_version()
Get the next version to be handled.
Definition: replicated.hpp:298
std::function< void(const version_t &)> TrimFunc
Definition: HLC.hpp:7
uint32_t node_id_t
Type alias for Node IDs in a Derecho group.
std::unique_ptr< rpc::RemoteInvocableOf< T > > wrapped_this
The actual implementation of Replicated<T>, hiding its ugly template parameters.
Definition: replicated.hpp:95
std::function< const version_t(void)> LatestPersistedGetterFunc
An empty class to be used as the "replicated type" for a subgroup that doesn&#39;t implement a Replicated...
Definition: replicated.hpp:51
std::unique_ptr< std::unique_ptr< T > > user_object_ptr
The user-provided state object with some RPC methods.
Definition: replicated.hpp:77
void registerPersist(const char *obj_name, const VersionFunc &vf, const PersistFunc &pf, const TrimFunc &tf, const LatestPersistedGetterFunc &lpgf, const TruncateFunc &tcf) noexcept(false)
set the latest version for serialization register a Persistent<T> along with its lambda ...
Definition: Persistent.cpp:58
const std::vector< node_id_t > shard_reps
Definition: replicated.hpp:355
static auto register_functions()
Definition: replicated.hpp:52
virtual void post_next_version(const persistent::version_t &version, const uint64_t &ts_us)
Post the next version to be handled.
Definition: replicated.hpp:290
const node_id_t node_id
The ID of this node.
Definition: replicated.hpp:82
rpc::RPCManager & group_rpc_manager
Reference to the RPCManager for the Group this ExternalCaller is in.
Definition: replicated.hpp:326
PersistentRegistry is a book for all the Persistent<T> or Volatile<T> variables.
Definition: Persistent.hpp:81