#pragma once #include #include #include #include #include namespace DB { class MergeTreeData; /** 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) */ class MergeTreeWriteAheadLog { public: /// Append-only enum. It is serialized to WAL enum class ActionType : UInt8 { ADD_PART = 0, DROP_PART = 1, }; constexpr static auto WAL_FILE_NAME = "wal"; constexpr static auto WAL_FILE_EXTENSION = ".bin"; constexpr static auto DEFAULT_WAL_FILE_NAME = "wal.bin"; MergeTreeWriteAheadLog(MergeTreeData & storage_, const DiskPtr & disk_, const String & name = DEFAULT_WAL_FILE_NAME); void addPart(const Block & block, const String & part_name); void dropPart(const String & part_name); std::vector restore(const StorageMetadataPtr & metadata_snapshot); using MinMaxBlockNumber = std::pair; static std::optional tryParseMinMaxBlockNumber(const String & filename); private: void init(); void rotate(const std::lock_guard & lock); void sync(const std::lock_guard & lock); const MergeTreeData & storage; DiskPtr disk; String name; String path; std::unique_ptr out; std::unique_ptr block_out; Int64 min_block_number = std::numeric_limits::max(); Int64 max_block_number = -1; BackgroundSchedulePool & pool; BackgroundSchedulePoolTaskHolder sync_task; size_t bytes_at_last_sync = 0; bool sync_scheduled = false; mutable std::mutex write_mutex; }; }