#pragma once #include #include #include #include #include namespace DB { /// Example trivial state machine. class SummingStateMachine : public nuraft::state_machine { public: SummingStateMachine(); nuraft::ptr pre_commit(const uint64_t /*log_idx*/, nuraft::buffer & /*data*/) override { return nullptr; } nuraft::ptr commit(const uint64_t log_idx, nuraft::buffer & data) override; /// NOLINT void rollback(const uint64_t /*log_idx*/, nuraft::buffer & /*data*/) override {} /// NOLINT uint64_t last_commit_index() override { return last_committed_idx; } bool apply_snapshot(nuraft::snapshot & s) override; nuraft::ptr last_snapshot() override; void create_snapshot( nuraft::snapshot & s, nuraft::async_result::handler_type & when_done) override; void save_logical_snp_obj( nuraft::snapshot & s, uint64_t & obj_id, nuraft::buffer & data, bool is_first_obj, bool is_last_obj) override; int read_logical_snp_obj( nuraft::snapshot & s, void* & user_snp_ctx, uint64_t obj_id, nuraft::ptr & data_out, bool & is_last_obj) override; int64_t getValue() const { return value; } private: struct SingleValueSnapshotContext { SingleValueSnapshotContext(nuraft::ptr & s, int64_t v) : snapshot(s) , value(v) {} nuraft::ptr snapshot; int64_t value; }; void createSnapshotInternal(nuraft::snapshot & s); // State machine's current value. std::atomic value; // Last committed Raft log number. std::atomic last_committed_idx; // Keeps the last 3 snapshots, by their Raft log numbers. std::map> snapshots; // Mutex for `snapshots_`. std::mutex snapshots_lock; }; }