Derecho  0.9
Distributed systems toolkit for RDMA
Persistent.cpp
Go to the documentation of this file.
2 
3 namespace persistent {
4 
6 
7 #define VERSION_FUNC_IDX (0)
8 #define PERSIST_FUNC_IDX (1)
9 #define TRIM_FUNC_IDX (2)
10 #define GET_ML_PERSISTED_VER (3)
11 #define TRUNCATE_FUNC_IDX (4)
12 
15  const std::type_index& subgroup_type,
16  uint32_t subgroup_index,
17  uint32_t shard_num) :
18  _subgroup_prefix(generate_prefix(subgroup_type, subgroup_index, shard_num)),
19  _temporal_query_frontier_provider(tqfp) {
20  }
21 
23  this->_registry.clear();
24  };
25 
26  void PersistentRegistry::makeVersion(const int64_t& ver, const HLC& mhlc) noexcept(false) {
27  callFunc<VERSION_FUNC_IDX>(ver, mhlc);
28  };
29 
30  const int64_t PersistentRegistry::persist() noexcept(false) {
31  return callFuncMin<PERSIST_FUNC_IDX, int64_t>();
32  };
33 
34  void PersistentRegistry::trim(const int64_t& earliest_version) noexcept(false) {
35  callFunc<TRIM_FUNC_IDX>(earliest_version);
36  };
37 
38  const int64_t PersistentRegistry::getMinimumLatestPersistedVersion() noexcept(false) {
39  return callFuncMin<GET_ML_PERSISTED_VER, int64_t>();
40  }
41 
42  void PersistentRegistry::setEarliestVersionToSerialize(const int64_t& ver) noexcept(true) {
43  PersistentRegistry::earliest_version_to_serialize = ver;
44  }
45 
47  PersistentRegistry::earliest_version_to_serialize = INVALID_VERSION;
48  }
49 
52  }
53 
54  void PersistentRegistry::truncate(const int64_t& last_version) {
55  callFunc<TRUNCATE_FUNC_IDX>(last_version);
56  }
57 
58  void PersistentRegistry::registerPersist(const char* obj_name,
59  const VersionFunc& vf,
60  const PersistFunc& pf,
61  const TrimFunc& tf,
62  const LatestPersistedGetterFunc& lpgf,
63  const TruncateFunc& tcf) noexcept(false) {
64  //this->_registry.push_back(std::make_tuple(vf,pf,tf));
65  auto tuple_val = std::make_tuple(vf, pf, tf, lpgf, tcf);
66  std::size_t key = std::hash<std::string>{}(obj_name);
67  auto res = this->_registry.insert(std::pair<std::size_t, std::tuple<VersionFunc, PersistFunc, TrimFunc, LatestPersistedGetterFunc, TruncateFunc>>(key, tuple_val));
68  if(res.second == false) {
69  //override the previous value:
70  this->_registry.erase(res.first);
71  this->_registry.insert(std::pair<std::size_t, std::tuple<VersionFunc, PersistFunc, TrimFunc, LatestPersistedGetterFunc, TruncateFunc>>(key, tuple_val));
72  }
73  };
74 
75  void PersistentRegistry::unregisterPersist(const char* obj_name) noexcept(false) {
76  // The upcoming regsiterPersist() call will override this automatically.
77  // this->_registry.erase(std::hash<std::string>{}(obj_name));
78  }
79 
82  }
83 
85  return this->_subgroup_prefix.c_str();
86  }
87 
89  const std::type_index& subgroup_type,
90  uint32_t subgroup_index,
91  uint32_t shard_num) noexcept(true) {
92  const char* subgroup_type_name = subgroup_type.name();
93  char prefix[strlen(subgroup_type_name) * 2 + 32];
94  uint32_t i = 0;
95  for(i = 0; i < strlen(subgroup_type.name()); i++) {
96  sprintf(prefix + 2 * i, "%02x", subgroup_type_name[i]);
97  }
98  sprintf(prefix + 2 * i, "-%u-%u", subgroup_index, shard_num);
99  return std::string(prefix);
100  }
101 
103  const std::string str,
104  const std::type_index& subgroup_type,
105  uint32_t subgroup_index,
106  uint32_t shard_num) noexcept(true) {
107  std::string prefix = generate_prefix(subgroup_type, subgroup_index, shard_num);
108  try {
109  if(prefix == str.substr(0, prefix.length()))
110  return true;
111  } catch(const std::out_of_range&) {
112  // str is shorter than prefix, just return false.
113  }
114  return false;
115  }
116 
117 }
This file include all common types internal to derecho and not necessarily being known by a client pr...
void trim(const int64_t &earliest_version) noexcept(false)
Trims the log of all versions earlier than the argument.
Definition: Persistent.cpp:34
const std::string _subgroup_prefix
this appears in the first part of storage file for persistent<T>
Definition: Persistent.hpp:207
static thread_local int64_t earliest_version_to_serialize
Set the earliest version to serialize for recovery.
Definition: Persistent.hpp:234
const char * get_subgroup_prefix()
Get prefix for subgroup, this will appear in the file name of Persistent<T>
Definition: Persistent.cpp:84
std::function< void(const version_t &, const HLC &)> VersionFunc
std::function< const version_t(void)> PersistFunc
void truncate(const int64_t &last_version)
Truncates the log, deleting all versions newer than the provided argument.
Definition: Persistent.cpp:54
const int64_t getMinimumLatestPersistedVersion() noexcept(false)
Returns the minimum of the latest persisted versions among all Persistent fields. ...
Definition: Persistent.cpp:38
static void setEarliestVersionToSerialize(const int64_t &ver) noexcept(true)
Set the earliest version for serialization, exclusive.
Definition: Persistent.cpp:42
static int64_t getEarliestVersionToSerialize() noexcept(true)
Returns the earliest version for serialization.
Definition: Persistent.cpp:50
std::map< std::size_t, std::tuple< VersionFunc, PersistFunc, TrimFunc, LatestPersistedGetterFunc, TruncateFunc > > _registry
Callback registry.
Definition: Persistent.hpp:217
const int64_t persist() noexcept(false)
(attempt to) Persist all existing versions
Definition: Persistent.cpp:30
#define INVALID_VERSION
Definition: PersistLog.hpp:28
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
static bool match_prefix(const std::string str, const std::type_index &subgroup_type, uint32_t subgroup_index, uint32_t shard_num) noexcept(true)
match prefix
Definition: Persistent.cpp:102
std::function< void(const int64_t &)> TruncateFunc
PersistentRegistry(ITemporalQueryFrontierProvider *tqfp, const std::type_index &subgroup_type, uint32_t subgroup_index, uint32_t shard_num)
Definition: Persistent.cpp:13
std::function< void(const version_t &)> TrimFunc
Definition: HLC.hpp:7
std::function< const version_t(void)> LatestPersistedGetterFunc
void updateTemporalFrontierProvider(ITemporalQueryFrontierProvider *tqfp)
update temporal query frontier we didn&#39;t use a lock on this becuase we assume this is only updated ob...
Definition: Persistent.cpp:80
static void resetEarliestVersionToSerialize() noexcept(true)
Reset the earliest version for serialization to an invalid "uninitialized" state. ...
Definition: Persistent.cpp:46
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
void unregisterPersist(const char *obj_name) noexcept(false)
deregister
Definition: Persistent.cpp:75
static std::string generate_prefix(const std::type_index &subgroup_type, uint32_t subgroup_index, uint32_t shard_num) noexcept(true)
prefix generator prefix format: [hex of subgroup_type]-[subgroup_index]-[shard_num] ...
Definition: Persistent.cpp:88
ITemporalQueryFrontierProvider * _temporal_query_frontier_provider
Pointer to an entity providing TemporalQueryFrontier service.
Definition: Persistent.hpp:212