ClickHouse/src/Storages/FileLog/StorageFileLog.h

167 lines
4.2 KiB
C++
Raw Normal View History

2021-06-09 02:03:36 +00:00
#pragma once
#include <Storages/FileLog/Buffer_fwd.h>
2021-09-10 17:21:03 +00:00
#include <Storages/FileLog/FileLogDirectoryWatcher.h>
2021-09-04 17:04:35 +00:00
#include <Storages/FileLog/FileLogSettings.h>
2021-06-09 02:03:36 +00:00
#include <Core/BackgroundSchedulePool.h>
#include <Storages/IStorage.h>
#include <Common/SettingsChanges.h>
#include <Poco/File.h>
#include <Poco/Semaphore.h>
2021-07-04 08:52:05 +00:00
#include <common/shared_ptr_helper.h>
2021-06-09 02:03:36 +00:00
#include <mutex>
#include <atomic>
2021-09-04 17:04:35 +00:00
#include <fstream>
2021-06-09 02:03:36 +00:00
namespace DB
{
2021-07-04 08:52:05 +00:00
class StorageFileLog final : public shared_ptr_helper<StorageFileLog>, public IStorage, WithContext
2021-06-09 02:03:36 +00:00
{
2021-07-04 08:52:05 +00:00
friend struct shared_ptr_helper<StorageFileLog>;
2021-06-09 02:03:36 +00:00
public:
2021-09-05 06:32:32 +00:00
2021-06-09 02:03:36 +00:00
using Files = std::vector<String>;
std::string getName() const override { return "FileLog"; }
bool noPushingToViews() const override { return true; }
void startup() override;
void shutdown() override;
Pipe read(
const Names & column_names,
const StorageMetadataPtr & /*metadata_snapshot*/,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
unsigned num_streams) override;
2021-09-26 07:22:45 +00:00
void drop() override;
bool dropTableImmediately() const override { return true; }
2021-06-09 02:03:36 +00:00
const auto & getFormatName() const { return format_name; }
2021-09-26 07:22:45 +00:00
enum class FileStatus
{
OPEN, /// first time open file after table start up
NO_CHANGE,
UPDATED,
REMOVED,
};
struct FileContext
{
FileStatus status = FileStatus::OPEN;
std::ifstream reader;
};
struct FileMeta
{
String file_name;
UInt64 last_writen_position = 0;
2021-09-27 08:44:48 +00:00
UInt64 last_open_end = 0;
2021-09-26 07:22:45 +00:00
};
using FileNameToInode = std::unordered_map<String, UInt64>;
using InodeToFileMeta = std::unordered_map<UInt64, FileMeta>;
using FileNameToContext = std::unordered_map<String, FileContext>;
struct FileInfos
{
FileNameToInode inode_by_name;
InodeToFileMeta meta_by_inode;
FileNameToContext context_by_name;
/// file names without path
Names file_names;
};
auto & getFileInfos() { return file_infos; }
auto getFullMetaPath(const String & file_name) const { return root_meta_path + "/" + file_name; }
auto getFullDataPath(const String & file_name) const { return root_data_path + "/" + file_name; }
2021-06-09 02:03:36 +00:00
2021-09-24 16:44:22 +00:00
NamesAndTypesList getVirtuals() const override;
static Names getVirtualColumnNames();
2021-09-26 07:22:45 +00:00
static UInt64 getInode(const String & file_name);
2021-09-27 04:39:50 +00:00
void openFilesAndSetPos();
void closeFilesAndStoreMeta();
2021-06-09 02:03:36 +00:00
protected:
StorageFileLog(
const StorageID & table_id_,
ContextPtr context_,
const ColumnsDescription & columns_,
2021-09-13 03:44:12 +00:00
const String & relative_path_,
2021-09-04 17:04:35 +00:00
const String & format_name_,
2021-09-26 07:22:45 +00:00
std::unique_ptr<FileLogSettings> settings,
bool attach);
2021-06-09 02:03:36 +00:00
private:
2021-09-04 17:04:35 +00:00
std::unique_ptr<FileLogSettings> filelog_settings;
2021-09-26 07:22:45 +00:00
/// user_files_path/ + path_argument/
const String path;
bool path_is_directory = true;
2021-06-09 02:03:36 +00:00
2021-09-26 07:22:45 +00:00
/// If path argument of the table is a regular file, it equals to user_files_path
/// otherwise, it equals to user_files_path/ + path_argument/, e.g. path
String root_data_path;
/// Database meta_path/ + .table_name/
String root_meta_path;
2021-09-04 17:04:35 +00:00
2021-09-26 07:22:45 +00:00
FileInfos file_infos;
2021-09-04 17:04:35 +00:00
2021-09-26 07:22:45 +00:00
const String format_name;
Poco::Logger * log;
2021-09-04 17:04:35 +00:00
std::mutex status_mutex;
2021-06-09 02:03:36 +00:00
2021-09-10 17:21:03 +00:00
std::unique_ptr<FileLogDirectoryWatcher> directory_watch = nullptr;
2021-09-21 16:11:35 +00:00
uint64_t milliseconds_to_wait;
2021-06-09 02:03:36 +00:00
struct TaskContext
{
BackgroundSchedulePool::TaskHolder holder;
std::atomic<bool> stream_cancelled {false};
explicit TaskContext(BackgroundSchedulePool::TaskHolder&& task_) : holder(std::move(task_))
{
}
};
std::shared_ptr<TaskContext> task;
2021-09-04 17:04:35 +00:00
using TaskThread = BackgroundSchedulePool::TaskHolder;
2021-09-26 07:22:45 +00:00
void loadFiles();
void loadMetaFiles(bool attach);
2021-09-22 02:07:59 +00:00
2021-06-09 02:03:36 +00:00
void threadFunc();
size_t getPollMaxBatchSize() const;
size_t getMaxBlockSize() const;
size_t getPollTimeoutMillisecond() const;
bool streamToViews();
bool checkDependencies(const StorageID & table_id);
2021-09-04 17:04:35 +00:00
2021-09-26 07:22:45 +00:00
bool updateFileInfos();
/// Serialize all file meta
void serialize(bool with_end_pos = false) const;
void deserialize();
2021-06-09 02:03:36 +00:00
};
}