ClickHouse/src/Storages/HDFS/HDFSCommon.h

105 lines
2.5 KiB
C++
Raw Normal View History

2020-10-10 18:37:02 +00:00
#pragma once
2021-04-04 07:33:07 +00:00
#if !defined(ARCADIA_BUILD)
2019-01-19 20:17:19 +00:00
#include <Common/config.h>
2021-04-04 07:33:07 +00:00
#endif
#if USE_HDFS
2019-01-19 20:17:19 +00:00
#include <memory>
#include <type_traits>
2020-09-09 12:13:20 +00:00
#include <vector>
2019-01-19 20:17:19 +00:00
#include <hdfs/hdfs.h> // Y_IGNORE
#include <common/types.h>
#include <mutex>
#include <Interpreters/Context.h>
#include <Poco/Util/AbstractConfiguration.h>
2019-01-19 20:17:19 +00:00
namespace DB
{
2021-04-20 07:53:55 +00:00
2019-01-19 20:17:19 +00:00
namespace detail
{
struct HDFSFsDeleter
2019-01-19 20:17:19 +00:00
{
void operator()(hdfsFS fs_ptr)
{
hdfsDisconnect(fs_ptr);
}
};
2019-01-19 20:17:19 +00:00
}
2021-04-20 07:53:55 +00:00
2019-07-24 09:51:02 +00:00
struct HDFSFileInfo
{
hdfsFileInfo * file_info;
int length;
2021-04-20 07:53:55 +00:00
HDFSFileInfo() : file_info(nullptr) , length(0) {}
2019-07-24 09:51:02 +00:00
HDFSFileInfo(const HDFSFileInfo & other) = delete;
HDFSFileInfo(HDFSFileInfo && other) = default;
HDFSFileInfo & operator=(const HDFSFileInfo & other) = delete;
HDFSFileInfo & operator=(HDFSFileInfo && other) = default;
~HDFSFileInfo()
{
hdfsFreeFileInfo(file_info, length);
}
};
2020-09-09 12:13:20 +00:00
2021-04-20 07:53:55 +00:00
2020-09-09 12:13:20 +00:00
class HDFSBuilderWrapper
{
2021-04-04 09:08:09 +00:00
friend HDFSBuilderWrapper createHDFSBuilder(const String & uri_str, const Poco::Util::AbstractConfiguration &);
2021-04-20 07:53:55 +00:00
static const String CONFIG_PREFIX;
2020-09-09 12:13:20 +00:00
2021-04-04 09:08:09 +00:00
public:
HDFSBuilderWrapper() : hdfs_builder(hdfsNewBuilder()) {}
2021-04-20 07:53:55 +00:00
~HDFSBuilderWrapper() { hdfsFreeBuilder(hdfs_builder); }
2021-04-04 09:08:09 +00:00
HDFSBuilderWrapper(const HDFSBuilderWrapper &) = delete;
HDFSBuilderWrapper(HDFSBuilderWrapper &&) = default;
2021-04-20 07:53:55 +00:00
hdfsBuilder * get() { return hdfs_builder; }
2021-04-04 09:08:09 +00:00
private:
void loadFromConfig(const Poco::Util::AbstractConfiguration & config, const String & config_path, bool isUser = false);
String getKinitCmd();
2020-09-09 12:13:20 +00:00
2020-10-29 20:40:47 +00:00
void runKinit();
2020-09-09 12:13:20 +00:00
// hdfs builder relies on an external config data storage
2020-09-09 12:13:20 +00:00
std::pair<String, String>& keep(const String & k, const String & v)
{
return config_stor.emplace_back(std::make_pair(k, v));
}
2021-04-20 07:53:55 +00:00
hdfsBuilder * hdfs_builder;
String hadoop_kerberos_keytab;
String hadoop_kerberos_principal;
String hadoop_kerberos_kinit_command = "kinit";
String hadoop_security_kerberos_ticket_cache_path;
2021-04-20 07:53:55 +00:00
static std::mutex kinit_mtx;
std::vector<std::pair<String, String>> config_stor;
bool need_kinit{false};
2020-09-09 12:13:20 +00:00
};
2019-01-19 20:17:19 +00:00
using HDFSFSPtr = std::unique_ptr<std::remove_pointer_t<hdfsFS>, detail::HDFSFsDeleter>;
2021-04-20 07:53:55 +00:00
2019-01-19 20:17:19 +00:00
// set read/connect timeout, default value in libhdfs3 is about 1 hour, and too large
/// TODO Allow to tune from query Settings.
HDFSBuilderWrapper createHDFSBuilder(const String & uri_str, const Poco::Util::AbstractConfiguration &);
2019-01-19 20:17:19 +00:00
HDFSFSPtr createHDFSFS(hdfsBuilder * builder);
2021-04-20 07:53:55 +00:00
2019-01-19 20:17:19 +00:00
}
#endif