19 #include <sys/types.h> 25 #if defined(_PERFORMANCE_DEBUG) || defined(DERECHO_DEBUG) || !defined(NDEBUG) 27 #endif //_PERFORMANCE_DEBUG 37 #define DEFINE_PERSIST_VAR(_t, _n, _s) \ 38 Persistent<_t, _s> _n(#_n) 39 #define DECLARE_PERSIST_VAR(_t, _n, _s) \ 40 extern DEFINE_PERSIST_VAR(_t, _n, _s) 57 template <
typename int_type>
70 template <
typename int_type>
86 const std::type_index& subgroup_type,
87 uint32_t subgroup_index, uint32_t shard_num);
93 void makeVersion(
const int64_t& ver,
const HLC& mhlc) noexcept(
false);
97 const int64_t persist() noexcept(
false);
100 void trim(
const int64_t& earliest_version) noexcept(
false);
112 static void setEarliestVersionToSerialize(
const int64_t& ver) noexcept(
true);
115 static void resetEarliestVersionToSerialize() noexcept(
true);
118 static int64_t getEarliestVersionToSerialize() noexcept(
true);
125 void truncate(
const int64_t& last_version);
131 void registerPersist(
const char* obj_name,
141 void unregisterPersist(
const char* obj_name) noexcept(
false);
147 if(_temporal_query_frontier_provider !=
nullptr) {
149 const HLC r = _temporal_query_frontier_provider->getFrontier();
153 return _temporal_query_frontier_provider->getFrontier();
157 clock_gettime(CLOCK_REALTIME, &t);
158 return HLC((uint64_t)(t.tv_sec * 1e6 + t.tv_nsec / 1e3), (uint64_t)0);
183 const char* get_subgroup_prefix();
192 static std::string generate_prefix(
const std::type_index& subgroup_type, uint32_t subgroup_index, uint32_t shard_num) noexcept(
true);
201 static bool match_prefix(
const std::string str,
const std::type_index& subgroup_type, uint32_t subgroup_index, uint32_t shard_num) noexcept(
true);
217 std::map<std::size_t, std::tuple<VersionFunc, PersistFunc, TrimFunc, LatestPersistedGetterFunc, TruncateFunc>>
_registry;
222 template <
int funcIdx,
typename... Args>
223 void callFunc(Args... args);
228 template <
int funcIdx,
typename ReturnType,
typename... Args>
229 ReturnType callFuncMin(Args... args);
257 template <
typename DeltaObjectType>
261 return DeltaObjectType::create(dm);
265 template <
typename ObjectType>
269 virtual void applyDelta(
char const*
const) = 0;
275 template <
typename ObjectType, StorageType storageType>
285 std::unique_ptr<std::string> make(
const char* prefix) noexcept(
false);
318 template <
typename ObjectType,
326 inline void initialize_log(
const char* object_name) noexcept(
false);
330 inline void initialize_object_from_log(
const std::function<std::unique_ptr<ObjectType>(
void)>& object_factory,
335 inline void register_callbacks() noexcept(
false);
339 inline void unregister_callbacks() noexcept(
false);
350 const std::function<std::unique_ptr<ObjectType>(
void)>& object_factory,
351 const char* object_name =
nullptr,
370 const char* object_name,
371 std::unique_ptr<ObjectType>& wrapped_obj_ptr,
372 const char* log_tail =
nullptr,
388 ObjectType& operator*();
393 ObjectType* operator->();
399 const ObjectType& getConstRef()
const;
404 const std::string& getObjectName();
411 template <
typename Func>
426 template <
typename Func>
435 std::unique_ptr<ObjectType> getByIndex(
445 template <
typename Func>
455 std::unique_ptr<ObjectType>
get(
462 template <
typename TKey>
463 void trim(
const TKey& k) noexcept(
false);
469 void truncate(
const int64_t& ver);
477 template <
typename Func>
486 std::unique_ptr<ObjectType>
get(
493 std::unique_ptr<ObjectType>
operator[](
const int64_t ver) noexcept(
false) {
494 return this->
get(ver);
501 return this->
get(hlc);
507 virtual int64_t getNumOfVersions() noexcept(
false);
512 virtual int64_t getEarliestIndex() noexcept(
false);
517 virtual int64_t getEarliestVersion() noexcept(
false);
522 virtual int64_t getLatestIndex() noexcept(
false);
527 virtual int64_t getLatestVersion() noexcept(
false);
532 virtual const int64_t getLastPersisted() noexcept(
false);
537 virtual void set(ObjectType& v,
const version_t& ver,
const HLC& mhlc) noexcept(
false);
542 virtual void set(ObjectType& v,
const version_t& ver) noexcept(
false);
547 virtual void version(
const version_t& ver) noexcept(
false);
553 virtual const int64_t persist() noexcept(
false);
578 std::size_t
to_bytes(
char* ret)
const;
580 void post_object(
const std::function<
void(
char const*
const, std::size_t)>& f)
const;
591 #if defined(_PERFORMANCE_DEBUG) || !defined(NDEBUG) 592 uint64_t ns_in_persist = 0ul;
593 uint64_t ns_in_set = 0ul;
594 uint64_t cnt_in_persist = 0ul;
595 uint64_t cnt_in_set = 0ul;
596 virtual void print_performance_stat();
597 #endif //_PERFORMANCE_DEBUG 600 template <
typename ObjectType>
611 const std::function<std::unique_ptr<ObjectType>(
void)>& object_factory,
612 const char* object_name =
nullptr,
635 const std::function<std::unique_ptr<ObjectType>(
void)>& object_factory,
636 const char* object_name,
637 std::unique_ptr<ObjectType>& wrapped_obj_ptr,
638 std::unique_ptr<PersistLog>& log_ptr,
663 template <
typename ObjectType, StorageType storageType = ST_FILE>
664 void saveObject(ObjectType& obj,
const char* object_name =
nullptr) noexcept(
false);
671 template <
typename ObjectType, StorageType storageType = ST_FILE>
672 std::unique_ptr<ObjectType>
loadObject(
const char* object_name =
nullptr) noexcept(
false);
680 template <StorageType storageType = ST_FILE>
681 const typename std::enable_if<(storageType == ST_FILE || storageType == ST_MEM), version_t>::type
getMinimumLatestPersistedVersion(
const std::type_index& subgroup_type, uint32_t subgroup_index, uint32_t shard_num);
688 #endif //PERSISTENT_HPP std::enable_if_t< std::is_base_of< ByteRepresentable CMA T >::value, std::unique_ptr< T > > from_bytes(DeserializationManager *ctx, char const *v)
Calls T::from_bytes(ctx,v) when T is a ByteRepresentable.
const char * m_sObjectTypeName
virtual void ensure_registered(mutils::DeserializationManager &)
This file include all common types internal to derecho and not necessarily being known by a client pr...
const std::string _subgroup_prefix
this appears in the first part of storage file for persistent<T>
virtual const HLC getFrontier()=0
std::pair< int_type, int_type > unpack_version(const version_t packed_int)
Helper function for unpacking a Persistent version number into two signed or unsigned int32 values...
static thread_local int64_t earliest_version_to_serialize
Set the earliest version to serialize for recovery.
This file defines some type aliases used by the Persistence library.
std::function< void(const version_t &, const HLC &)> VersionFunc
const std::enable_if<(storageType==ST_FILE||storageType==ST_MEM), version_t >::type getMinimumLatestPersistedVersion(const std::type_index &subgroup_type, uint32_t subgroup_index, uint32_t shard_num)
get the minmum latest persisted version for a Replicated<T> identified by
static std::unique_ptr< DeltaObjectType > create(mutils::DeserializationManager *dm)
std::unique_ptr< ObjectType > m_pWrappedObject
virtual ~Volatile() noexcept(true)
pthread_spinlock_t m_oLck
std::function< void(char const *const, std::size_t)> DeltaFinalizer
std::function< const version_t(void)> PersistFunc
A non-POD type which wishes to mark itself byte representable should extend this class.
auto bytes_size(const T &)
Just calls sizeof(T)
Volatile(const std::function< std::unique_ptr< ObjectType >(void)> &object_factory, const char *object_name=nullptr, PersistentRegistry *persistent_registry=nullptr, mutils::DeserializationManager dm={{}}) noexcept(false)
constructor 1 is for building a persistent<T> locally, load/create a log and register itself to a per...
The manager for any RemoteDeserializationContexts.
Volatile(Volatile &&other) noexcept(false)
constructor 2 is move constructor.
std::map< std::size_t, std::tuple< VersionFunc, PersistFunc, TrimFunc, LatestPersistedGetterFunc, TruncateFunc > > _registry
Callback registry.
Volatile(const std::function< std::unique_ptr< ObjectType >(void)> &object_factory, const char *object_name, std::unique_ptr< ObjectType > &wrapped_obj_ptr, std::unique_ptr< PersistLog > &log_ptr, PersistentRegistry *persistent_registry=nullptr, mutils::DeserializationManager dm={{}}) noexcept(false)
constructor 3 is for deserialization.
std::unique_ptr< PersistLog > m_pLog
std::unique_ptr< ObjectType > operator[](const HLC &hlc) noexcept(false)
syntax sugar: get a specified version of T without DSM
std::function< void(const int64_t &)> TruncateFunc
std::function< void(const version_t &)> TrimFunc
const HLC getFrontier()
get temporal query frontier
std::unique_ptr< ObjectType > loadObject(const char *object_name) noexcept(false)
loadObject() loads a serializable object from a persistent store
PersistentRegistry * m_pRegistry
std::function< const version_t(void)> LatestPersistedGetterFunc
void saveObject(ObjectType &obj, const char *object_name) noexcept(false)
saveObject() saves a serializable object
version_t combine_int32s(const int_type high_bits, const int_type low_bits)
Helper function for creating Persistent version numbers out of MulticastGroup sequence numbers and Vi...
std::enable_if_t< std::is_pod< BR >::value > post_object(const F &f, const BR &br, Args &&... args)
In-place serialization is also sometimes possible.
std::unique_ptr< ObjectType > operator[](const int64_t ver) noexcept(false)
syntax sugar: get a specified version of T without DSM
std::size_t to_bytes(const ByteRepresentable &b, char *v)
calls b.to_bytes(v) when b is a ByteRepresentable; calls std::memcpy() when b is POD.
If a class which implements ByteRepresentable requires a context in order to correctly deserialize...
PersistentRegistry is a book for all the Persistent<T> or Volatile<T> variables.
ITemporalQueryFrontierProvider * _temporal_query_frontier_provider
Pointer to an entity providing TemporalQueryFrontier service.
#define dbg_default_warn(...)