ClickHouse/src/Storages/StorageKeeperMap.h

153 lines
4.8 KiB
C++
Raw Normal View History

2022-07-26 09:08:55 +00:00
#pragma once
#include <Interpreters/Context.h>
#include <Interpreters/IKeyValueEntity.h>
2022-07-26 09:08:55 +00:00
#include <QueryPipeline/Pipe.h>
#include <Storages/IStorage.h>
2022-07-27 13:20:45 +00:00
#include <Storages/StorageInMemoryMetadata.h>
2022-08-08 14:09:37 +00:00
#include <Common/PODArray_fwd.h>
#include <Common/logger_useful.h>
2022-07-27 13:20:45 +00:00
#include <Common/ZooKeeper/ZooKeeper.h>
2023-11-08 10:22:44 +00:00
#include <Backups/IBackup.h>
#include <Backups/WithRetries.h>
2022-07-27 13:20:45 +00:00
#include <span>
2022-07-26 09:08:55 +00:00
namespace DB
{
2022-08-23 13:15:31 +00:00
namespace ErrorCodes
{
extern const int INVALID_STATE;
}
2022-07-27 13:20:45 +00:00
// KV store using (Zoo|CH)Keeper
2022-08-23 13:15:31 +00:00
class StorageKeeperMap final : public IStorage, public IKeyValueEntity, WithContext
2022-07-26 09:08:55 +00:00
{
public:
StorageKeeperMap(
2022-08-23 13:15:31 +00:00
ContextPtr context_,
2022-07-27 13:20:45 +00:00
const StorageID & table_id,
const StorageInMemoryMetadata & metadata,
bool attach,
2022-07-27 13:20:45 +00:00
std::string_view primary_key_,
2022-08-23 13:15:31 +00:00
const std::string & root_path_,
2022-08-31 08:14:28 +00:00
UInt64 keys_limit_);
2022-07-26 09:08:55 +00:00
Pipe read(
const Names & column_names,
const StorageSnapshotPtr & storage_snapshot,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
size_t num_streams) override;
2022-07-26 09:08:55 +00:00
SinkToStoragePtr write(const ASTPtr & query, const StorageMetadataPtr & metadata_snapshot, ContextPtr context, bool async_insert) override;
2022-07-26 09:08:55 +00:00
2022-08-23 13:15:31 +00:00
void truncate(const ASTPtr &, const StorageMetadataPtr &, ContextPtr, TableExclusiveLockHolder &) override;
void drop() override;
2023-03-31 13:29:20 +00:00
NamesAndTypesList getVirtuals() const override;
2022-07-27 13:20:45 +00:00
std::string getName() const override { return "KeeperMap"; }
Names getPrimaryKey() const override { return {primary_key}; }
2022-07-26 09:08:55 +00:00
Chunk getByKeys(const ColumnsWithTypeAndName & keys, PaddedPODArray<UInt8> & null_map, const Names &) const override;
2023-03-31 13:29:20 +00:00
Chunk getBySerializedKeys(std::span<const std::string> keys, PaddedPODArray<UInt8> * null_map, bool with_version) const;
2022-07-26 09:08:55 +00:00
Block getSampleBlock(const Names &) const override;
2022-09-08 12:45:36 +00:00
void checkTableCanBeRenamed(const StorageID & new_name) const override;
void rename(const String & new_path_to_table_data, const StorageID & new_table_id) override;
void checkMutationIsPossible(const MutationCommands & commands, const Settings & settings) const override;
void mutate(const MutationCommands & commands, ContextPtr context) override;
2022-07-27 13:20:45 +00:00
bool supportsParallelInsert() const override { return true; }
bool supportsDelete() const override { return true; }
2022-07-27 13:20:45 +00:00
2023-11-08 10:22:44 +00:00
void backupData(BackupEntriesCollector & backup_entries_collector, const String & data_path_in_backup, const std::optional<ASTs> & partitions) override;
void restoreDataFromBackup(RestorerFromBackup & restorer, const String & data_path_in_backup, const std::optional<ASTs> & partitions) override;
2022-08-31 08:14:28 +00:00
zkutil::ZooKeeperPtr getClient() const;
2022-09-12 14:43:52 +00:00
const std::string & dataPath() const;
2022-07-27 13:20:45 +00:00
std::string fullPathForKey(std::string_view key) const;
2022-07-26 09:08:55 +00:00
2022-08-31 08:14:28 +00:00
UInt64 keysLimit() const;
2022-08-23 13:15:31 +00:00
template <bool throw_on_error>
void checkTable() const
{
auto is_table_valid = isTableValid();
if (!is_table_valid.has_value())
{
2023-01-16 22:20:33 +00:00
static constexpr auto error_msg = "Failed to activate table because of connection issues. It will be activated "
2022-08-23 13:15:31 +00:00
"once a connection is established and metadata is verified";
if constexpr (throw_on_error)
throw Exception(ErrorCodes::INVALID_STATE, error_msg);
else
{
2023-01-16 22:20:33 +00:00
LOG_ERROR(log, error_msg);
2022-08-23 13:15:31 +00:00
return;
}
}
if (!*is_table_valid)
{
2023-01-16 22:20:33 +00:00
static constexpr auto error_msg
2022-08-23 13:15:31 +00:00
= "Failed to activate table because of invalid metadata in ZooKeeper. Please DETACH table";
if constexpr (throw_on_error)
throw Exception(ErrorCodes::INVALID_STATE, error_msg);
else
{
2023-01-16 22:20:33 +00:00
LOG_ERROR(log, error_msg);
2022-08-23 13:15:31 +00:00
return;
}
}
}
2022-08-08 09:43:29 +00:00
2022-07-27 13:20:45 +00:00
private:
2022-08-31 08:23:44 +00:00
bool dropTable(zkutil::ZooKeeperPtr zookeeper, const zkutil::EphemeralNodeHolder::Ptr & metadata_drop_lock);
2022-08-24 17:27:07 +00:00
2022-08-31 08:14:28 +00:00
std::optional<bool> isTableValid() const;
2023-11-09 15:56:57 +00:00
void restoreDataImpl(
const BackupPtr & backup,
const String & data_path_in_backup,
std::shared_ptr<WithRetries> with_retries,
bool allow_non_empty_tables,
const DiskPtr & temporary_disk);
2023-11-08 10:22:44 +00:00
std::string zk_root_path;
2022-07-27 13:20:45 +00:00
std::string primary_key;
2022-08-24 17:27:07 +00:00
2023-11-08 10:22:44 +00:00
std::string zk_data_path;
2022-09-12 14:43:52 +00:00
2023-11-08 10:22:44 +00:00
std::string zk_metadata_path;
2022-08-24 17:27:07 +00:00
2023-11-08 10:22:44 +00:00
std::string zk_tables_path;
std::string zk_table_path;
2022-08-24 17:27:07 +00:00
2023-11-08 10:22:44 +00:00
std::string zk_dropped_path;
std::string zk_dropped_lock_path;
2022-08-23 13:15:31 +00:00
std::string zookeeper_name;
2022-09-08 12:45:36 +00:00
std::string metadata_string;
2022-08-31 08:14:28 +00:00
uint64_t keys_limit{0};
2022-08-23 13:15:31 +00:00
mutable std::mutex zookeeper_mutex;
mutable zkutil::ZooKeeperPtr zookeeper_client{nullptr};
2022-07-27 13:20:45 +00:00
2022-08-23 13:15:31 +00:00
mutable std::mutex init_mutex;
mutable std::optional<bool> table_is_valid;
2022-08-10 08:52:36 +00:00
2024-01-23 17:04:50 +00:00
LoggerPtr log;
2022-07-26 09:08:55 +00:00
};
}