ClickHouse/src/Coordination/SummingStateMachine.h

79 lines
2.0 KiB
C++
Raw Normal View History

2021-01-14 16:20:33 +00:00
#pragma once
2021-10-27 23:10:39 +00:00
#include <libnuraft/nuraft.hxx>
2021-01-14 16:20:33 +00:00
#include <Core/Types.h>
#include <atomic>
#include <map>
#include <mutex>
namespace DB
{
2021-01-25 14:47:03 +00:00
/// Example trivial state machine.
2021-01-14 16:20:33 +00:00
class SummingStateMachine : public nuraft::state_machine
{
public:
SummingStateMachine();
2021-04-08 14:17:57 +00:00
nuraft::ptr<nuraft::buffer> pre_commit(const uint64_t /*log_idx*/, nuraft::buffer & /*data*/) override { return nullptr; }
2021-01-14 16:20:33 +00:00
2021-04-08 14:17:57 +00:00
nuraft::ptr<nuraft::buffer> commit(const uint64_t log_idx, nuraft::buffer & data) override;
2021-01-14 16:20:33 +00:00
2021-04-08 14:17:57 +00:00
void rollback(const uint64_t /*log_idx*/, nuraft::buffer & /*data*/) override {}
2021-01-14 16:20:33 +00:00
2021-04-08 14:17:57 +00:00
uint64_t last_commit_index() override { return last_committed_idx; }
2021-01-14 16:20:33 +00:00
bool apply_snapshot(nuraft::snapshot & s) override;
nuraft::ptr<nuraft::snapshot> last_snapshot() override;
void create_snapshot(
nuraft::snapshot & s,
nuraft::async_result<bool>::handler_type & when_done) override;
void save_logical_snp_obj(
nuraft::snapshot & s,
2021-04-08 14:17:57 +00:00
uint64_t & obj_id,
2021-01-14 16:20:33 +00:00
nuraft::buffer & data,
bool is_first_obj,
bool is_last_obj) override;
int read_logical_snp_obj(
nuraft::snapshot & s,
void* & user_snp_ctx,
2021-04-08 14:17:57 +00:00
uint64_t obj_id,
2021-01-14 16:20:33 +00:00
nuraft::ptr<nuraft::buffer> & data_out,
bool & is_last_obj) override;
int64_t getValue() const { return value; }
private:
struct SingleValueSnapshotContext
{
SingleValueSnapshotContext(nuraft::ptr<nuraft::snapshot> & s, int64_t v)
: snapshot(s)
, value(v)
{}
nuraft::ptr<nuraft::snapshot> snapshot;
int64_t value;
};
void createSnapshotInternal(nuraft::snapshot & s);
// State machine's current value.
std::atomic<int64_t> value;
// Last committed Raft log number.
std::atomic<uint64_t> last_committed_idx;
// Keeps the last 3 snapshots, by their Raft log numbers.
std::map<uint64_t, nuraft::ptr<SingleValueSnapshotContext>> snapshots;
// Mutex for `snapshots_`.
std::mutex snapshots_lock;
};
}