2021-02-01 14:14:59 +00:00
|
|
|
#pragma once
|
2021-03-01 13:33:34 +00:00
|
|
|
#include <libnuraft/nuraft.hxx> // Y_IGNORE
|
2021-02-01 14:14:59 +00:00
|
|
|
#include <Coordination/NuKeeperStorage.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-03-02 14:30:56 +00:00
|
|
|
using SnapshotMetadata = nuraft::snapshot;
|
|
|
|
using SnapshotMetadataPtr = std::shared_ptr<SnapshotMetadata>;
|
|
|
|
|
2021-03-01 13:33:34 +00:00
|
|
|
enum SnapshotVersion : uint8_t
|
|
|
|
{
|
|
|
|
V0 = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NuKeeperStorageSnapshot
|
2021-02-01 14:14:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-03-01 13:33:34 +00:00
|
|
|
NuKeeperStorageSnapshot(NuKeeperStorage * storage_, size_t up_to_log_idx_);
|
2021-03-02 14:30:56 +00:00
|
|
|
|
|
|
|
NuKeeperStorageSnapshot(NuKeeperStorage * storage_, const SnapshotMetadataPtr & snapshot_meta_);
|
2021-03-01 13:33:34 +00:00
|
|
|
~NuKeeperStorageSnapshot();
|
|
|
|
|
|
|
|
static void serialize(const NuKeeperStorageSnapshot & snapshot, WriteBuffer & out);
|
2021-02-01 14:14:59 +00:00
|
|
|
|
2021-03-02 14:30:56 +00:00
|
|
|
static SnapshotMetadataPtr deserialize(NuKeeperStorage & storage, ReadBuffer & in);
|
2021-03-01 13:33:34 +00:00
|
|
|
|
|
|
|
NuKeeperStorage * storage;
|
|
|
|
|
|
|
|
SnapshotVersion version = SnapshotVersion::V0;
|
2021-03-02 14:30:56 +00:00
|
|
|
SnapshotMetadataPtr snapshot_meta;
|
2021-03-01 13:33:34 +00:00
|
|
|
int64_t session_id;
|
|
|
|
size_t snapshot_container_size;
|
|
|
|
NuKeeperStorage::Container::const_iterator begin;
|
|
|
|
SessionAndTimeout session_and_timeout;
|
|
|
|
};
|
|
|
|
|
2021-03-05 10:40:24 +00:00
|
|
|
using NuKeeperStorageSnapshotPtr = std::shared_ptr<NuKeeperStorageSnapshot>;
|
2021-03-07 21:40:32 +00:00
|
|
|
using CreateSnapshotCallback = std::function<void(NuKeeperStorageSnapshotPtr &&)>;
|
2021-03-05 10:40:24 +00:00
|
|
|
|
2021-03-01 13:33:34 +00:00
|
|
|
class NuKeeperSnapshotManager
|
|
|
|
{
|
|
|
|
public:
|
2021-03-01 14:54:08 +00:00
|
|
|
NuKeeperSnapshotManager(const std::string & snapshots_path_, size_t snapshots_to_keep_);
|
2021-03-01 13:33:34 +00:00
|
|
|
|
2021-03-03 11:10:24 +00:00
|
|
|
SnapshotMetadataPtr restoreFromLatestSnapshot(NuKeeperStorage * storage);
|
2021-03-01 13:33:34 +00:00
|
|
|
|
2021-03-01 14:40:32 +00:00
|
|
|
static nuraft::ptr<nuraft::buffer> serializeSnapshotToBuffer(const NuKeeperStorageSnapshot & snapshot);
|
2021-03-01 13:33:34 +00:00
|
|
|
std::string serializeSnapshotBufferToDisk(nuraft::buffer & buffer, size_t up_to_log_idx);
|
|
|
|
|
2021-03-02 14:30:56 +00:00
|
|
|
static SnapshotMetadataPtr deserializeSnapshotFromBuffer(NuKeeperStorage * storage, nuraft::ptr<nuraft::buffer> buffer);
|
|
|
|
|
2021-03-01 14:40:32 +00:00
|
|
|
nuraft::ptr<nuraft::buffer> deserializeSnapshotBufferFromDisk(size_t up_to_log_idx) const;
|
2021-03-03 11:10:24 +00:00
|
|
|
nuraft::ptr<nuraft::buffer> deserializeLatestSnapshotBufferFromDisk();
|
|
|
|
|
|
|
|
void removeSnapshot(size_t log_idx);
|
|
|
|
|
|
|
|
size_t totalSnapshots() const
|
|
|
|
{
|
|
|
|
return existing_snapshots.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getLatestSnapshotIndex() const
|
|
|
|
{
|
|
|
|
if (!existing_snapshots.empty())
|
|
|
|
return existing_snapshots.rbegin()->first;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-01 13:33:34 +00:00
|
|
|
|
|
|
|
private:
|
2021-03-01 14:54:08 +00:00
|
|
|
void removeOutdatedSnapshotsIfNeeded();
|
2021-03-01 13:33:34 +00:00
|
|
|
const std::string snapshots_path;
|
2021-03-01 14:54:08 +00:00
|
|
|
const size_t snapshots_to_keep;
|
2021-03-01 13:33:34 +00:00
|
|
|
std::map<size_t, std::string> existing_snapshots;
|
2021-02-01 14:14:59 +00:00
|
|
|
};
|
|
|
|
|
2021-03-05 10:40:24 +00:00
|
|
|
struct CreateSnapshotTask
|
|
|
|
{
|
|
|
|
NuKeeperStorageSnapshotPtr snapshot;
|
|
|
|
CreateSnapshotCallback create_snapshot;
|
|
|
|
};
|
|
|
|
|
2021-02-01 14:14:59 +00:00
|
|
|
}
|