2020-04-14 19:47:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DataStreams/NativeBlockInputStream.h>
|
|
|
|
#include <DataStreams/NativeBlockOutputStream.h>
|
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPart.h>
|
2020-09-01 22:25:10 +00:00
|
|
|
#include <Core/BackgroundSchedulePool.h>
|
2020-04-14 19:47:19 +00:00
|
|
|
#include <Disks/IDisk.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class MergeTreeData;
|
|
|
|
|
2020-06-22 18:56:53 +00:00
|
|
|
/** WAL stores addditions and removals of data parts in in-memory format.
|
|
|
|
* Format of data in WAL:
|
|
|
|
* - version
|
|
|
|
* - type of action (ADD or DROP)
|
|
|
|
* - part name
|
|
|
|
* - part's block in Native format. (for ADD action)
|
|
|
|
*/
|
2020-04-14 19:47:19 +00:00
|
|
|
class MergeTreeWriteAheadLog
|
|
|
|
{
|
|
|
|
public:
|
2020-05-29 15:02:12 +00:00
|
|
|
/// Append-only enum. It is serialized to WAL
|
|
|
|
enum class ActionType : UInt8
|
|
|
|
{
|
|
|
|
ADD_PART = 0,
|
|
|
|
DROP_PART = 1,
|
|
|
|
};
|
|
|
|
|
2020-04-14 19:47:19 +00:00
|
|
|
constexpr static auto WAL_FILE_NAME = "wal";
|
|
|
|
constexpr static auto WAL_FILE_EXTENSION = ".bin";
|
2020-06-22 18:56:53 +00:00
|
|
|
constexpr static auto DEFAULT_WAL_FILE_NAME = "wal.bin";
|
2020-04-14 19:47:19 +00:00
|
|
|
|
2020-09-01 22:25:10 +00:00
|
|
|
MergeTreeWriteAheadLog(MergeTreeData & storage_, const DiskPtr & disk_,
|
2020-06-22 18:56:53 +00:00
|
|
|
const String & name = DEFAULT_WAL_FILE_NAME);
|
2020-04-14 19:47:19 +00:00
|
|
|
|
2020-05-29 15:02:12 +00:00
|
|
|
void addPart(const Block & block, const String & part_name);
|
|
|
|
void dropPart(const String & part_name);
|
2020-06-26 11:30:23 +00:00
|
|
|
std::vector<MergeTreeMutableDataPartPtr> restore(const StorageMetadataPtr & metadata_snapshot);
|
2020-04-14 19:47:19 +00:00
|
|
|
|
2020-05-27 20:05:55 +00:00
|
|
|
using MinMaxBlockNumber = std::pair<Int64, Int64>;
|
|
|
|
static std::optional<MinMaxBlockNumber> tryParseMinMaxBlockNumber(const String & filename);
|
|
|
|
|
2020-04-14 19:47:19 +00:00
|
|
|
private:
|
2020-04-30 15:27:39 +00:00
|
|
|
void init();
|
2020-06-22 18:56:53 +00:00
|
|
|
void rotate(const std::lock_guard<std::mutex> & lock);
|
2020-09-01 22:25:10 +00:00
|
|
|
void sync(const std::lock_guard<std::mutex> & lock);
|
2020-04-14 19:47:19 +00:00
|
|
|
|
|
|
|
const MergeTreeData & storage;
|
|
|
|
DiskPtr disk;
|
2020-05-14 20:08:15 +00:00
|
|
|
String name;
|
2020-04-14 19:47:19 +00:00
|
|
|
String path;
|
|
|
|
|
|
|
|
std::unique_ptr<WriteBuffer> out;
|
|
|
|
std::unique_ptr<NativeBlockOutputStream> block_out;
|
|
|
|
|
|
|
|
Int64 min_block_number = std::numeric_limits<Int64>::max();
|
2020-05-14 20:08:15 +00:00
|
|
|
Int64 max_block_number = -1;
|
2020-04-14 19:47:19 +00:00
|
|
|
|
2020-09-01 22:25:10 +00:00
|
|
|
BackgroundSchedulePool & pool;
|
|
|
|
BackgroundSchedulePoolTaskHolder sync_task;
|
|
|
|
|
|
|
|
size_t bytes_at_last_sync = 0;
|
|
|
|
bool sync_scheduled = false;
|
|
|
|
|
2020-04-14 19:47:19 +00:00
|
|
|
mutable std::mutex write_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|