2021-02-15 15:36:25 +00:00
|
|
|
#pragma once
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <libnuraft/log_store.hxx>
|
2021-02-15 15:36:25 +00:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Coordination/Changelog.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2022-06-14 22:35:55 +00:00
|
|
|
#include <base/defines.h>
|
2021-02-15 15:36:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Wrapper around Changelog class. Implements RAFT log storage.
|
2021-03-29 08:24:56 +00:00
|
|
|
class KeeperLogStore : public nuraft::log_store
|
2021-02-15 15:36:25 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-09-22 10:38:06 +00:00
|
|
|
KeeperLogStore(const std::string & changelogs_path, uint64_t rotate_interval_, bool force_sync_, bool compress_logs_);
|
2021-02-15 15:36:25 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Read log storage from filesystem starting from last_commited_log_index
|
2021-04-08 14:17:57 +00:00
|
|
|
void init(uint64_t last_commited_log_index, uint64_t logs_to_keep);
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-04-08 14:17:57 +00:00
|
|
|
uint64_t start_index() const override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-04-08 14:17:57 +00:00
|
|
|
uint64_t next_slot() const override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// return last entry from log
|
2021-02-15 17:59:40 +00:00
|
|
|
nuraft::ptr<nuraft::log_entry> last_entry() const override;
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Append new entry to log
|
2021-04-08 14:17:57 +00:00
|
|
|
uint64_t append(nuraft::ptr<nuraft::log_entry> & entry) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Remove all entries starting from index and write entry into index position
|
2021-04-08 14:17:57 +00:00
|
|
|
void write_at(uint64_t index, nuraft::ptr<nuraft::log_entry> & entry) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Return entries between [start, end)
|
2021-04-08 14:17:57 +00:00
|
|
|
nuraft::ptr<std::vector<nuraft::ptr<nuraft::log_entry>>> log_entries(uint64_t start, uint64_t end) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Return entry at index
|
2021-04-08 14:17:57 +00:00
|
|
|
nuraft::ptr<nuraft::log_entry> entry_at(uint64_t index) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Term if the index
|
2021-04-08 14:17:57 +00:00
|
|
|
uint64_t term_at(uint64_t index) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Serialize entries in interval [index, index + cnt)
|
2021-04-08 14:17:57 +00:00
|
|
|
nuraft::ptr<nuraft::buffer> pack(uint64_t index, int32_t cnt) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Apply serialized entries starting from index
|
2021-04-08 14:17:57 +00:00
|
|
|
void apply_pack(uint64_t index, nuraft::buffer & pack) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Entries from last_log_index can be removed from memory and from disk
|
2021-04-08 14:17:57 +00:00
|
|
|
bool compact(uint64_t last_log_index) override;
|
2021-02-15 17:59:40 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Call fsync to the stored data
|
2021-02-15 17:59:40 +00:00
|
|
|
bool flush() override;
|
2021-02-15 15:36:25 +00:00
|
|
|
|
2022-05-02 22:45:13 +00:00
|
|
|
/// Stop background cleanup thread in change
|
|
|
|
void shutdownChangelog();
|
|
|
|
|
|
|
|
/// Flush logstore and call shutdown of background thread in changelog
|
|
|
|
bool flushChangelogAndShutdown();
|
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Current log storage size
|
2021-04-08 14:17:57 +00:00
|
|
|
uint64_t size() const;
|
2021-02-17 08:00:17 +00:00
|
|
|
|
2021-08-24 12:30:31 +00:00
|
|
|
/// Flush batch of appended entries
|
2021-04-16 13:50:09 +00:00
|
|
|
void end_of_append_batch(uint64_t start_index, uint64_t count) override;
|
|
|
|
|
2021-10-19 14:10:09 +00:00
|
|
|
/// Get entry with latest config in logstore
|
2021-10-18 15:27:51 +00:00
|
|
|
nuraft::ptr<nuraft::log_entry> getLatestConfigChange() const;
|
|
|
|
|
2021-02-15 15:36:25 +00:00
|
|
|
private:
|
2021-02-15 17:59:40 +00:00
|
|
|
mutable std::mutex changelog_lock;
|
2021-02-20 11:04:38 +00:00
|
|
|
Poco::Logger * log;
|
2022-06-14 22:35:55 +00:00
|
|
|
Changelog changelog TSA_GUARDED_BY(changelog_lock);
|
2021-02-15 15:36:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|