ClickHouse/src/Dictionaries/XDBCDictionarySource.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

291 lines
11 KiB
C++
Raw Normal View History

#include "XDBCDictionarySource.h"
2018-06-05 19:46:49 +00:00
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <IO/ReadWriteBufferFromHTTP.h>
#include <IO/WriteHelpers.h>
Fix terribly broken, fragile and potentially cyclic linking Sorry for the clickbaity title. This is about static method ConnectionTimeouts::getHTTPTimeouts(). It was be declared in header IO/ConnectionTimeouts.h, and defined in header IO/ConnectionTimeoutsContext.h (!). This is weird and caused issues with linking on s390x (##45520). There was an attempt to fix some inconsistencies (#45848) but neither did @Algunenano nor me at first really understand why the definition is in the header. Turns out that ConnectionTimeoutsContext.h is only #include'd from source files which are part of the normal server build BUT NOT part of the keeper standalone build (which must be enabled via CMake -DBUILD_STANDALONE_KEEPER=1). This dependency was not documented and as a result, some misguided workarounds were introduced earlier, e.g. https://github.com/ClickHouse/ClickHouse/pull/38475/commits/0341c6c54bd7ac77200b4ca123208b195514ef20 The deeper cause was that getHTTPTimeouts() is passed a "Context". This class is part of the "dbms" libary which is deliberately not linked by the standalone build of clickhouse-keeper. The context is only used to read the settings and the "Settings" class is part of the clickhouse_common library which is linked by clickhouse-keeper already. To resolve this mess, this PR - creates source file IO/ConnectionTimeouts.cpp and moves all ConnectionTimeouts definitions into it, including getHTTPTimeouts(). - breaks the wrong dependency by passing "Settings" instead of "Context" into getHTTPTimeouts(). - resolves the previous hacks
2023-02-03 10:54:49 +00:00
#include <IO/ConnectionTimeouts.h>
#include <Interpreters/Context.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Util/AbstractConfiguration.h>
#include <Common/LocalDateTime.h>
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
#include "DictionarySourceFactory.h"
#include "DictionaryStructure.h"
#include "readInvalidateQuery.h"
2019-12-15 06:34:43 +00:00
#include "registerDictionaries.h"
2021-04-17 08:09:22 +00:00
#include <Common/escapeForFileName.h>
2021-10-16 14:03:50 +00:00
#include <QueryPipeline/QueryPipeline.h>
2021-10-13 18:22:02 +00:00
#include <Processors/Formats/IInputFormat.h>
#include "config.h"
2019-12-15 06:34:43 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int SUPPORT_IS_DISABLED;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
namespace
{
ExternalQueryBuilder makeExternalQueryBuilder(const DictionaryStructure & dict_struct_,
const std::string & db_,
const std::string & schema_,
const std::string & table_,
const std::string & query_,
const std::string & where_,
IXDBCBridgeHelper & bridge_)
{
2021-09-13 19:11:16 +00:00
QualifiedTableName qualified_name{schema_, table_};
if (bridge_.isSchemaAllowed())
{
2021-09-13 19:11:16 +00:00
if (qualified_name.database.empty())
qualified_name = QualifiedTableName::parseFromString(qualified_name.table);
}
else
{
2021-09-13 19:11:16 +00:00
if (!qualified_name.database.empty())
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Dictionary source specifies a schema but schema is not supported by {}-driver",
2021-04-10 18:48:36 +00:00
bridge_.getName());
}
2021-09-13 19:11:16 +00:00
return {dict_struct_, db_, qualified_name.database, qualified_name.table, query_, where_, bridge_.getIdentifierQuotingStyle()};
}
}
2019-02-10 16:55:12 +00:00
static const UInt64 max_block_size = 8192;
XDBCDictionarySource::XDBCDictionarySource(
const DictionaryStructure & dict_struct_,
const Configuration & configuration_,
const Block & sample_block_,
2021-06-01 12:20:52 +00:00
ContextPtr context_,
const BridgeHelperPtr bridge_)
: WithContext(context_->getGlobalContext())
, log(&Poco::Logger::get(bridge_->getName() + "DictionarySource"))
, update_time(std::chrono::system_clock::from_time_t(0))
, dict_struct(dict_struct_)
, configuration(configuration_)
, sample_block(sample_block_)
, query_builder(makeExternalQueryBuilder(dict_struct, configuration.db, configuration.schema, configuration.table, configuration.query, configuration.where, *bridge_))
, load_all_query(query_builder.composeLoadAllQuery())
, bridge_helper(bridge_)
, bridge_url(bridge_helper->getMainURI())
{
Fix terribly broken, fragile and potentially cyclic linking Sorry for the clickbaity title. This is about static method ConnectionTimeouts::getHTTPTimeouts(). It was be declared in header IO/ConnectionTimeouts.h, and defined in header IO/ConnectionTimeoutsContext.h (!). This is weird and caused issues with linking on s390x (##45520). There was an attempt to fix some inconsistencies (#45848) but neither did @Algunenano nor me at first really understand why the definition is in the header. Turns out that ConnectionTimeoutsContext.h is only #include'd from source files which are part of the normal server build BUT NOT part of the keeper standalone build (which must be enabled via CMake -DBUILD_STANDALONE_KEEPER=1). This dependency was not documented and as a result, some misguided workarounds were introduced earlier, e.g. https://github.com/ClickHouse/ClickHouse/pull/38475/commits/0341c6c54bd7ac77200b4ca123208b195514ef20 The deeper cause was that getHTTPTimeouts() is passed a "Context". This class is part of the "dbms" libary which is deliberately not linked by the standalone build of clickhouse-keeper. The context is only used to read the settings and the "Settings" class is part of the clickhouse_common library which is linked by clickhouse-keeper already. To resolve this mess, this PR - creates source file IO/ConnectionTimeouts.cpp and moves all ConnectionTimeouts definitions into it, including getHTTPTimeouts(). - breaks the wrong dependency by passing "Settings" instead of "Context" into getHTTPTimeouts(). - resolves the previous hacks
2023-02-03 10:54:49 +00:00
const auto & settings = context_->getSettingsRef();
const auto & config = context_->getConfigRef();
Poco::Timespan http_keep_alive_timeout{config.getUInt("keep_alive_timeout", 10), 0};
timeouts = ConnectionTimeouts::getHTTPTimeouts(settings, http_keep_alive_timeout);
2021-04-17 08:09:22 +00:00
auto url_params = bridge_helper->getURLParams(max_block_size);
for (const auto & [name, value] : url_params)
bridge_url.addQueryParameter(name, value);
}
/// copy-constructor is provided in order to support cloneability
XDBCDictionarySource::XDBCDictionarySource(const XDBCDictionarySource & other)
: WithContext(other.getContext())
, log(&Poco::Logger::get(other.bridge_helper->getName() + "DictionarySource"))
, update_time(other.update_time)
, dict_struct(other.dict_struct)
, configuration(other.configuration)
, sample_block(other.sample_block)
, query_builder(other.query_builder)
, load_all_query(other.load_all_query)
, invalidate_query_response(other.invalidate_query_response)
, bridge_helper(other.bridge_helper)
, bridge_url(other.bridge_url)
, timeouts(other.timeouts)
{
}
2021-04-17 08:09:22 +00:00
std::string XDBCDictionarySource::getUpdateFieldAndDate()
{
if (update_time != std::chrono::system_clock::from_time_t(0))
{
time_t hr_time = std::chrono::system_clock::to_time_t(update_time) - configuration.update_lag;
2021-03-15 19:23:27 +00:00
std::string str_time = DateLUT::instance().timeToString(hr_time);
update_time = std::chrono::system_clock::now();
return query_builder.composeUpdateQuery(configuration.update_field, str_time);
}
else
{
update_time = std::chrono::system_clock::now();
return load_all_query;
}
}
2021-04-17 08:09:22 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline XDBCDictionarySource::loadAll()
{
LOG_TRACE(log, fmt::runtime(load_all_query));
2021-04-17 08:09:22 +00:00
return loadFromQuery(bridge_url, sample_block, load_all_query);
}
2021-04-17 08:09:22 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline XDBCDictionarySource::loadUpdatedAll()
{
std::string load_query_update = getUpdateFieldAndDate();
LOG_TRACE(log, fmt::runtime(load_query_update));
2021-04-17 08:09:22 +00:00
return loadFromQuery(bridge_url, sample_block, load_query_update);
}
2021-04-17 08:09:22 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline XDBCDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
const auto query = query_builder.composeLoadIdsQuery(ids);
2021-04-17 08:09:22 +00:00
return loadFromQuery(bridge_url, sample_block, query);
}
2021-04-17 08:09:22 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline XDBCDictionarySource::loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows)
{
const auto query = query_builder.composeLoadKeysQuery(key_columns, requested_rows, ExternalQueryBuilder::AND_OR_CHAIN);
2021-04-17 08:09:22 +00:00
return loadFromQuery(bridge_url, sample_block, query);
}
2021-04-17 08:09:22 +00:00
bool XDBCDictionarySource::supportsSelectiveLoad() const
{
return true;
}
2021-04-17 08:09:22 +00:00
bool XDBCDictionarySource::hasUpdateField() const
{
return !configuration.update_field.empty();
}
2021-04-17 08:09:22 +00:00
DictionarySourcePtr XDBCDictionarySource::clone() const
{
return std::make_shared<XDBCDictionarySource>(*this);
}
2021-04-17 08:09:22 +00:00
std::string XDBCDictionarySource::toString() const
{
const auto & where = configuration.where;
return bridge_helper->getName() + ": " + configuration.db + '.' + configuration.table + (where.empty() ? "" : ", where: " + where);
}
2021-04-17 08:09:22 +00:00
bool XDBCDictionarySource::isModified() const
{
if (!configuration.invalidate_query.empty())
{
auto response = doInvalidateQuery(configuration.invalidate_query);
if (invalidate_query_response == response) //-V1051
return false;
invalidate_query_response = response;
}
return true;
}
std::string XDBCDictionarySource::doInvalidateQuery(const std::string & request) const
{
Block invalidate_sample_block;
ColumnPtr column(ColumnString::create());
invalidate_sample_block.insert(ColumnWithTypeAndName(column, std::make_shared<DataTypeString>(), "Sample Block"));
2018-09-12 21:34:48 +00:00
bridge_helper->startBridgeSync();
auto invalidate_url = bridge_helper->getMainURI();
2021-04-17 08:09:22 +00:00
auto url_params = bridge_helper->getURLParams(max_block_size);
2018-09-12 21:34:48 +00:00
for (const auto & [name, value] : url_params)
invalidate_url.addQueryParameter(name, value);
2021-09-16 17:40:42 +00:00
return readInvalidateQuery(QueryPipeline(loadFromQuery(invalidate_url, invalidate_sample_block, request)));
}
2021-04-17 08:09:22 +00:00
2022-05-20 19:49:31 +00:00
QueryPipeline XDBCDictionarySource::loadFromQuery(const Poco::URI & url, const Block & required_sample_block, const std::string & query) const
{
bridge_helper->startBridgeSync();
2021-04-17 08:09:22 +00:00
auto write_body_callback = [required_sample_block, query](std::ostream & os)
{
os << "sample_block=" << escapeForFileName(required_sample_block.getNamesAndTypesList().toString());
os << "&";
os << "query=" << escapeForFileName(query);
};
2021-10-28 10:28:05 +00:00
auto read_buf = std::make_unique<ReadWriteBufferFromHTTP>(
url, Poco::Net::HTTPRequest::HTTP_POST, write_body_callback, timeouts, credentials);
2021-10-11 16:11:50 +00:00
auto format = getContext()->getInputFormat(IXDBCBridgeHelper::DEFAULT_FORMAT, *read_buf, required_sample_block, max_block_size);
format->addBuffer(std::move(read_buf));
2022-05-20 19:49:31 +00:00
return QueryPipeline(std::move(format));
}
2021-04-17 08:09:22 +00:00
void registerDictionarySourceXDBC(DictionarySourceFactory & factory)
{
auto create_table_source = [=](const DictionaryStructure & dict_struct,
const Poco::Util::AbstractConfiguration & config,
const std::string & config_prefix,
Block & sample_block,
2021-08-12 15:16:55 +00:00
ContextPtr global_context,
2020-08-15 03:10:57 +00:00
const std::string & /* default_database */,
bool /* check_config */) -> DictionarySourcePtr {
#if USE_ODBC
BridgeHelperPtr bridge = std::make_shared<XDBCBridgeHelper<ODBCBridgeMixin>>(
2023-01-19 00:36:25 +00:00
global_context,
global_context->getSettings().http_receive_timeout,
config.getString(config_prefix + ".odbc.connection_string"),
config.getBool(config_prefix + ".settings.odbc_bridge_use_connection_pooling",
global_context->getSettingsRef().odbc_bridge_use_connection_pooling));
std::string settings_config_prefix = config_prefix + ".odbc";
XDBCDictionarySource::Configuration configuration
{
.db = config.getString(settings_config_prefix + ".db", ""),
.schema = config.getString(settings_config_prefix + ".schema", ""),
.table = config.getString(settings_config_prefix + ".table", ""),
.query = config.getString(settings_config_prefix + ".query", ""),
.where = config.getString(settings_config_prefix + ".where", ""),
.invalidate_query = config.getString(settings_config_prefix + ".invalidate_query", ""),
.update_field = config.getString(settings_config_prefix + ".update_field", ""),
.update_lag = config.getUInt64(settings_config_prefix + ".update_lag", 1)
};
2021-08-12 15:16:55 +00:00
return std::make_unique<XDBCDictionarySource>(dict_struct, configuration, sample_block, global_context, bridge);
#else
2018-11-29 14:37:56 +00:00
(void)dict_struct;
(void)config;
(void)config_prefix;
(void)sample_block;
2021-08-12 15:16:55 +00:00
(void)global_context;
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
"Dictionary source of type `odbc` is disabled because poco library was built without ODBC support.");
#endif
};
factory.registerSource("odbc", create_table_source);
}
2021-04-17 08:09:22 +00:00
void registerDictionarySourceJDBC(DictionarySourceFactory & factory)
{
auto create_table_source = [=](const DictionaryStructure & /* dict_struct */,
const Poco::Util::AbstractConfiguration & /* config */,
const std::string & /* config_prefix */,
Block & /* sample_block */,
2021-08-12 15:16:55 +00:00
ContextPtr /* global_context */,
2020-08-15 03:10:57 +00:00
const std::string & /* default_database */,
bool /* created_from_ddl */) -> DictionarySourcePtr {
2021-04-10 18:48:36 +00:00
throw Exception(ErrorCodes::SUPPORT_IS_DISABLED,
"Dictionary source of type `jdbc` is disabled until consistent support for nullable fields.");
};
factory.registerSource("jdbc", create_table_source);
}
}