ClickHouse/programs/odbc-bridge/MainHandler.cpp

197 lines
6.5 KiB
C++
Raw Normal View History

#include "MainHandler.h"
#include "validateODBCConnectionString.h"
#include "ODBCBlockInputStream.h"
2020-04-28 00:56:44 +00:00
#include "ODBCBlockOutputStream.h"
2020-08-14 13:41:44 +00:00
#include "getIdentifierQuote.h"
#include <DataStreams/copyData.h>
#include <DataTypes/DataTypeFactory.h>
#include <Formats/FormatFactory.h>
#include <IO/WriteBufferFromHTTPServerResponse.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadHelpers.h>
2020-08-14 13:41:44 +00:00
#include <IO/ReadBufferFromIStream.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTMLForm.h>
2020-08-14 13:41:44 +00:00
#include <Poco/ThreadPool.h>
#include <Processors/Formats/InputStreamFromInputFormat.h>
#include <common/logger_useful.h>
2020-08-14 13:41:44 +00:00
2019-04-17 17:36:58 +00:00
#include <mutex>
2020-08-14 13:41:44 +00:00
#include <memory>
2020-05-14 21:51:07 +00:00
2020-05-15 11:26:51 +00:00
#if USE_ODBC
2020-05-14 21:51:07 +00:00
#include <Poco/Data/ODBC/SessionImpl.h>
#define POCO_SQL_ODBC_CLASS Poco::Data::ODBC
2020-05-15 11:26:51 +00:00
#endif
namespace DB
{
namespace
{
std::unique_ptr<Block> parseColumns(std::string && column_string)
{
std::unique_ptr<Block> sample_block = std::make_unique<Block>();
auto names_and_types = NamesAndTypesList::parse(column_string);
for (const NameAndTypePair & column_data : names_and_types)
sample_block->insert({column_data.type, column_data.name});
return sample_block;
}
}
2019-04-17 17:36:58 +00:00
using PocoSessionPoolConstructor = std::function<std::shared_ptr<Poco::Data::SessionPool>()>;
/** Is used to adjust max size of default Poco thread pool. See issue #750
* Acquire the lock, resize pool and construct new Session.
*/
2019-12-15 06:34:43 +00:00
static std::shared_ptr<Poco::Data::SessionPool> createAndCheckResizePocoSessionPool(PocoSessionPoolConstructor pool_constr)
2019-04-17 17:36:58 +00:00
{
static std::mutex mutex;
Poco::ThreadPool & pool = Poco::ThreadPool::defaultPool();
/// NOTE: The lock don't guarantee that external users of the pool don't change its capacity
std::unique_lock lock(mutex);
if (pool.available() == 0)
pool.addCapacity(2 * std::max(pool.capacity(), 1));
return pool_constr();
}
ODBCHandler::PoolPtr ODBCHandler::getPool(const std::string & connection_str)
{
std::lock_guard lock(mutex);
if (!pool_map->count(connection_str))
{
2019-02-22 17:16:11 +00:00
pool_map->emplace(connection_str, createAndCheckResizePocoSessionPool([connection_str]
{
return std::make_shared<Poco::Data::SessionPool>("ODBC", validateODBCConnectionString(connection_str));
}));
}
return pool_map->at(connection_str);
}
2020-04-28 00:56:44 +00:00
void ODBCHandler::processError(Poco::Net::HTTPServerResponse & response, const std::string & message)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
if (!response.sent())
response.send() << message << std::endl;
2020-05-23 22:24:01 +00:00
LOG_WARNING(log, message);
2020-04-28 00:56:44 +00:00
}
void ODBCHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
{
2020-04-28 00:56:44 +00:00
Poco::Net::HTMLForm params(request);
if (mode == "read")
params.read(request.stream());
2020-05-23 22:24:01 +00:00
LOG_TRACE(log, "Request URI: {}", request.getURI());
2020-05-14 21:51:07 +00:00
if (mode == "read" && !params.has("query"))
{
processError(response, "No 'query' in request body");
return;
}
if (!params.has("columns"))
{
2020-04-28 00:56:44 +00:00
processError(response, "No 'columns' in request URL");
return;
}
if (!params.has("connection_string"))
{
2020-04-28 00:56:44 +00:00
processError(response, "No 'connection_string' in request URL");
return;
}
2020-05-14 21:51:07 +00:00
UInt64 max_block_size = DEFAULT_BLOCK_SIZE;
if (params.has("max_block_size"))
{
std::string max_block_size_str = params.get("max_block_size", "");
if (max_block_size_str.empty())
{
processError(response, "Empty max_block_size specified");
return;
}
max_block_size = parse<size_t>(max_block_size_str);
}
std::string columns = params.get("columns");
std::unique_ptr<Block> sample_block;
try
{
sample_block = parseColumns(std::move(columns));
}
catch (const Exception & ex)
{
2020-04-28 00:56:44 +00:00
processError(response, "Invalid 'columns' parameter in request body '" + ex.message() + "'");
2020-05-23 22:24:01 +00:00
LOG_WARNING(log, ex.getStackTraceString());
return;
}
std::string format = params.get("format", "RowBinary");
std::string connection_string = params.get("connection_string");
2020-05-23 22:24:01 +00:00
LOG_TRACE(log, "Connection string: '{}'", connection_string);
WriteBufferFromHTTPServerResponse out(request, response, keep_alive_timeout);
2020-05-14 21:51:07 +00:00
try
{
2020-05-14 21:51:07 +00:00
if (mode == "write")
2020-04-28 00:56:44 +00:00
{
if (!params.has("db_name"))
{
processError(response, "No 'db_name' in request URL");
return;
}
if (!params.has("table_name"))
{
processError(response, "No 'table_name' in request URL");
return;
}
std::string db_name = params.get("db_name");
std::string table_name = params.get("table_name");
2020-05-23 22:24:01 +00:00
LOG_TRACE(log, "DB name: '{}', table name: '{}'", db_name, table_name);
2020-05-14 21:51:07 +00:00
2020-05-15 11:26:51 +00:00
auto quoting_style = IdentifierQuotingStyle::None;
#if USE_ODBC
2020-05-14 21:51:07 +00:00
POCO_SQL_ODBC_CLASS::SessionImpl session(validateODBCConnectionString(connection_string), DBMS_DEFAULT_CONNECT_TIMEOUT_SEC);
2020-05-15 11:26:51 +00:00
quoting_style = getQuotingStyle(session.dbc().handle());
#endif
2020-04-28 00:56:44 +00:00
auto pool = getPool(connection_string);
ReadBufferFromIStream read_buf(request.stream());
2020-06-12 12:14:21 +00:00
auto input_format = FormatFactory::instance().getInput(format, read_buf, *sample_block,
context, max_block_size);
auto input_stream = std::make_shared<InputStreamFromInputFormat>(input_format);
2020-05-14 21:51:07 +00:00
ODBCBlockOutputStream output_stream(pool->get(), db_name, table_name, *sample_block, quoting_style);
2020-04-28 00:56:44 +00:00
copyData(*input_stream, output_stream);
writeStringBinary("Ok.", out);
}
2020-05-14 21:51:07 +00:00
else
2020-04-28 00:56:44 +00:00
{
2020-05-14 21:51:07 +00:00
std::string query = params.get("query");
2020-05-23 22:24:01 +00:00
LOG_TRACE(log, "Query: {}", query);
2020-04-28 00:56:44 +00:00
2020-12-30 03:07:30 +00:00
BlockOutputStreamPtr writer = FormatFactory::instance().getOutputStream(format, out, *sample_block, context);
2020-04-28 00:56:44 +00:00
auto pool = getPool(connection_string);
ODBCBlockInputStream inp(pool->get(), query, *sample_block, max_block_size);
copyData(inp, *writer);
}
2020-05-14 21:51:07 +00:00
}
catch (...)
{
auto message = getCurrentExceptionMessage(true);
response.setStatusAndReason(
Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); // can't call process_error, because of too soon response sending
writeStringBinary(message, out);
tryLogCurrentException(log);
2020-04-28 00:56:44 +00:00
}
}
2020-04-28 00:56:44 +00:00
}