ClickHouse/programs/odbc-bridge/ODBCConnectionFactory.h

164 lines
4.5 KiB
C++
Raw Normal View History

2021-03-31 12:41:12 +00:00
#pragma once
2021-10-02 07:13:14 +00:00
#include <base/logger_useful.h>
2021-03-31 12:41:12 +00:00
#include <nanodbc/nanodbc.h>
#include <mutex>
2021-10-02 07:13:14 +00:00
#include <base/BorrowedObjectPool.h>
2021-03-31 12:41:12 +00:00
#include <unordered_map>
2021-05-07 10:37:11 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NO_FREE_CONNECTION;
}
}
2021-03-31 12:41:12 +00:00
namespace nanodbc
{
2021-05-07 10:37:11 +00:00
using ConnectionPtr = std::unique_ptr<nanodbc::connection>;
2021-03-31 12:41:12 +00:00
using Pool = BorrowedObjectPool<ConnectionPtr>;
using PoolPtr = std::shared_ptr<Pool>;
2021-06-07 18:09:16 +00:00
static constexpr inline auto ODBC_CONNECT_TIMEOUT = 100;
2021-03-31 12:41:12 +00:00
class ConnectionHolder
{
public:
2021-06-07 18:09:16 +00:00
ConnectionHolder(PoolPtr pool_,
ConnectionPtr connection_,
const String & connection_string_)
2021-06-07 18:09:16 +00:00
: pool(pool_)
, connection(std::move(connection_))
, connection_string(connection_string_)
{
}
2021-03-31 12:41:12 +00:00
2021-05-07 10:37:11 +00:00
ConnectionHolder(const ConnectionHolder & other) = delete;
2021-03-31 12:41:12 +00:00
2021-06-07 12:15:24 +00:00
~ConnectionHolder()
{
pool->returnObject(std::move(connection));
}
2021-03-31 12:41:12 +00:00
2021-05-07 10:37:11 +00:00
nanodbc::connection & get() const
{
assert(connection != nullptr);
2021-03-31 12:41:12 +00:00
return *connection;
}
2021-06-07 18:09:16 +00:00
void updateConnection()
{
connection = std::make_unique<nanodbc::connection>(connection_string, ODBC_CONNECT_TIMEOUT);
}
2021-03-31 12:41:12 +00:00
private:
PoolPtr pool;
ConnectionPtr connection;
String connection_string;
2021-03-31 12:41:12 +00:00
};
2021-05-07 11:18:49 +00:00
2021-06-07 18:09:16 +00:00
using ConnectionHolderPtr = std::shared_ptr<ConnectionHolder>;
2021-03-31 12:41:12 +00:00
}
namespace DB
{
2021-05-07 10:37:11 +00:00
static constexpr inline auto ODBC_CONNECT_TIMEOUT = 100;
static constexpr inline auto ODBC_POOL_WAIT_TIMEOUT = 10000;
2021-06-07 18:09:16 +00:00
template <typename T>
T execute(nanodbc::ConnectionHolderPtr connection_holder, std::function<T(nanodbc::connection &)> query_func)
2021-03-31 12:41:12 +00:00
{
2021-06-07 18:09:16 +00:00
try
2021-03-31 12:41:12 +00:00
{
2021-06-07 18:09:16 +00:00
return query_func(connection_holder->get());
2021-03-31 12:41:12 +00:00
}
2021-06-07 18:09:16 +00:00
catch (const nanodbc::database_error & e)
2021-06-07 12:15:24 +00:00
{
2021-09-29 10:23:44 +00:00
tryLogCurrentException(__PRETTY_FUNCTION__);
2021-09-29 09:43:58 +00:00
/// SQLState, connection related errors start with 08 (main: 08S01), cursor invalid state is 24000.
/// Invalid cursor state is a retriable error.
2021-09-29 13:18:24 +00:00
/// Invalid transaction state 25000. Truncate to 2 letters on purpose.
2021-09-29 09:43:58 +00:00
/// https://docs.microsoft.com/ru-ru/sql/odbc/reference/appendixes/appendix-a-odbc-error-codes?view=sql-server-ver15
2021-09-29 13:18:24 +00:00
if (e.state().starts_with("08") || e.state().starts_with("24") || e.state().starts_with("25"))
2021-06-07 12:15:24 +00:00
{
2021-06-07 18:09:16 +00:00
connection_holder->updateConnection();
return query_func(connection_holder->get());
2021-06-07 12:15:24 +00:00
}
/// psqlodbc driver error handling is incomplete and under some scenarious
/// it doesn't propagate correct errors to the caller.
/// As a quick workaround we run a quick "ping" query over the connection
/// on generic errors.
/// If "ping" fails, recycle the connection and try the query once more.
if (e.state().starts_with("HY00"))
{
try
{
just_execute(connection_holder->get(), "SELECT 1");
}
catch (...)
{
connection_holder->updateConnection();
return query_func(connection_holder->get());
}
}
2021-06-07 18:09:16 +00:00
throw;
}
}
2021-06-07 12:15:24 +00:00
2021-06-07 18:09:16 +00:00
class ODBCConnectionFactory final : private boost::noncopyable
{
public:
static ODBCConnectionFactory & instance()
{
static ODBCConnectionFactory ret;
return ret;
2021-06-07 12:15:24 +00:00
}
2021-05-07 11:18:49 +00:00
nanodbc::ConnectionHolderPtr get(const std::string & connection_string, size_t pool_size)
2021-03-31 12:41:12 +00:00
{
std::lock_guard lock(mutex);
if (!factory.count(connection_string))
factory.emplace(std::make_pair(connection_string, std::make_shared<nanodbc::Pool>(pool_size)));
2021-05-07 10:37:11 +00:00
auto & pool = factory[connection_string];
nanodbc::ConnectionPtr connection;
auto connection_available = pool->tryBorrowObject(connection, []() { return nullptr; }, ODBC_POOL_WAIT_TIMEOUT);
if (!connection_available)
throw Exception("Unable to fetch connection within the timeout", ErrorCodes::NO_FREE_CONNECTION);
try
{
2021-06-07 18:09:16 +00:00
if (!connection)
2021-05-07 10:37:11 +00:00
connection = std::make_unique<nanodbc::connection>(connection_string, ODBC_CONNECT_TIMEOUT);
}
catch (...)
{
pool->returnObject(std::move(connection));
2021-06-07 12:15:24 +00:00
throw;
2021-05-07 10:37:11 +00:00
}
2021-06-07 18:09:16 +00:00
return std::make_unique<nanodbc::ConnectionHolder>(factory[connection_string], std::move(connection), connection_string);
2021-03-31 12:41:12 +00:00
}
private:
2021-04-06 18:59:34 +00:00
/// [connection_settings_string] -> [connection_pool]
2021-03-31 12:41:12 +00:00
using PoolFactory = std::unordered_map<std::string, nanodbc::PoolPtr>;
PoolFactory factory;
std::mutex mutex;
};
}