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
|
|
|
enum class FileStatus
|
|
|
|
{
|
|
|
|
BEGIN,
|
|
|
|
NO_CHANGE,
|
|
|
|
UPDATED,
|
|
|
|
REMOVED
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
const auto & getFormatName() const { return format_name; }
|
|
|
|
|
2021-09-05 06:32:32 +00:00
|
|
|
auto & getFileNames() { return file_names; }
|
2021-09-21 16:11:35 +00:00
|
|
|
auto & getFileStatuses() { return file_statuses; }
|
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_,
|
|
|
|
std::unique_ptr<FileLogSettings> settings);
|
2021-06-09 02:03:36 +00:00
|
|
|
|
|
|
|
private:
|
2021-09-04 17:04:35 +00:00
|
|
|
std::unique_ptr<FileLogSettings> filelog_settings;
|
2021-06-09 02:03:36 +00:00
|
|
|
const String path;
|
2021-09-04 17:04:35 +00:00
|
|
|
bool path_is_directory = false;
|
|
|
|
|
2021-06-09 02:03:36 +00:00
|
|
|
const String format_name;
|
|
|
|
Poco::Logger * log;
|
|
|
|
|
2021-09-04 17:04:35 +00:00
|
|
|
struct FileContext
|
|
|
|
{
|
|
|
|
FileStatus status = FileStatus::BEGIN;
|
2021-09-13 14:50:27 +00:00
|
|
|
size_t last_read_position = 0;
|
2021-09-04 17:04:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using NameToFile = std::unordered_map<String, FileContext>;
|
2021-09-21 16:11:35 +00:00
|
|
|
NameToFile file_statuses;
|
2021-09-04 17:04:35 +00:00
|
|
|
|
|
|
|
std::vector<String> file_names;
|
|
|
|
|
|
|
|
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
|
|
|
// Stream thread
|
|
|
|
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-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-21 16:11:35 +00:00
|
|
|
bool updateFileStatuses();
|
2021-06-09 02:03:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|