2024-07-10 16:10:19 +00:00
|
|
|
#include <Core/ServerSettings.h>
|
2023-02-03 10:54:49 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2024-07-10 16:10:19 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
|
|
|
|
2023-02-03 10:54:49 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
Poco::Timespan ConnectionTimeouts::saturate(Poco::Timespan timespan, Poco::Timespan limit)
|
|
|
|
{
|
|
|
|
if (limit.totalMicroseconds() == 0)
|
|
|
|
return timespan;
|
|
|
|
else
|
|
|
|
return (timespan > limit) ? limit : timespan;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Timeouts for the case when we have just single attempt to connect.
|
|
|
|
ConnectionTimeouts ConnectionTimeouts::getTCPTimeoutsWithoutFailover(const Settings & settings)
|
|
|
|
{
|
2024-01-22 11:03:36 +00:00
|
|
|
return ConnectionTimeouts()
|
|
|
|
.withConnectionTimeout(settings.connect_timeout)
|
|
|
|
.withSendTimeout(settings.send_timeout)
|
|
|
|
.withReceiveTimeout(settings.receive_timeout)
|
2024-02-17 01:14:15 +00:00
|
|
|
.withTCPKeepAliveTimeout(settings.tcp_keep_alive_timeout)
|
2024-01-22 11:03:36 +00:00
|
|
|
.withHandshakeTimeout(settings.handshake_timeout_ms)
|
|
|
|
.withHedgedConnectionTimeout(settings.hedged_connection_timeout_ms)
|
|
|
|
.withReceiveDataTimeout(settings.receive_data_timeout_ms);
|
2023-02-03 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Timeouts for the case when we will try many addresses in a loop.
|
|
|
|
ConnectionTimeouts ConnectionTimeouts::getTCPTimeoutsWithFailover(const Settings & settings)
|
|
|
|
{
|
2024-01-22 11:03:36 +00:00
|
|
|
return getTCPTimeoutsWithoutFailover(settings)
|
|
|
|
.withUnsecureConnectionTimeout(settings.connect_timeout_with_failover_ms)
|
|
|
|
.withSecureConnectionTimeout(settings.connect_timeout_with_failover_secure_ms);
|
2023-02-03 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 11:42:31 +00:00
|
|
|
ConnectionTimeouts ConnectionTimeouts::getHTTPTimeouts(const Settings & settings, Poco::Timespan http_keep_alive_timeout)
|
2023-02-03 10:54:49 +00:00
|
|
|
{
|
2024-01-22 11:03:36 +00:00
|
|
|
return ConnectionTimeouts()
|
|
|
|
.withConnectionTimeout(settings.http_connection_timeout)
|
|
|
|
.withSendTimeout(settings.http_send_timeout)
|
|
|
|
.withReceiveTimeout(settings.http_receive_timeout)
|
2024-02-17 01:14:15 +00:00
|
|
|
.withHTTPKeepAliveTimeout(http_keep_alive_timeout)
|
|
|
|
.withTCPKeepAliveTimeout(settings.tcp_keep_alive_timeout)
|
2024-01-22 11:03:36 +00:00
|
|
|
.withHandshakeTimeout(settings.handshake_timeout_ms);
|
2023-02-03 10:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 12:34:37 +00:00
|
|
|
ConnectionTimeouts ConnectionTimeouts::getFetchPartHTTPTimeouts(const ServerSettings & server_settings, const Settings & user_settings)
|
|
|
|
{
|
|
|
|
auto timeouts = getHTTPTimeouts(user_settings, server_settings.keep_alive_timeout);
|
|
|
|
|
|
|
|
if (server_settings.replicated_fetches_http_connection_timeout.changed)
|
|
|
|
timeouts.connection_timeout = server_settings.replicated_fetches_http_connection_timeout;
|
|
|
|
|
|
|
|
if (server_settings.replicated_fetches_http_send_timeout.changed)
|
|
|
|
timeouts.send_timeout = server_settings.replicated_fetches_http_send_timeout;
|
|
|
|
|
|
|
|
if (server_settings.replicated_fetches_http_receive_timeout.changed)
|
|
|
|
timeouts.receive_timeout = server_settings.replicated_fetches_http_receive_timeout;
|
|
|
|
|
|
|
|
return timeouts;
|
|
|
|
}
|
|
|
|
|
2023-11-20 13:53:22 +00:00
|
|
|
class SendReceiveTimeoutsForFirstAttempt
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static constexpr size_t known_methods_count = 6;
|
|
|
|
using KnownMethodsArray = std::array<String, known_methods_count>;
|
|
|
|
static const KnownMethodsArray known_methods;
|
|
|
|
|
|
|
|
/// HTTP_POST is used for CompleteMultipartUpload requests. Its latency could be high.
|
|
|
|
/// These requests need longer timeout, especially when minio is used.
|
|
|
|
/// The same assumption are made for HTTP_DELETE, HTTP_PATCH
|
|
|
|
/// That requests are more heavy that HTTP_GET, HTTP_HEAD, HTTP_PUT
|
|
|
|
|
|
|
|
static constexpr Poco::Timestamp::TimeDiff first_byte_ms[known_methods_count][2] =
|
|
|
|
{
|
|
|
|
/* GET */ {200, 200},
|
|
|
|
/* POST */ {200, 200},
|
|
|
|
/* DELETE */ {200, 200},
|
|
|
|
/* PUT */ {200, 200},
|
|
|
|
/* HEAD */ {200, 200},
|
|
|
|
/* PATCH */ {200, 200},
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr Poco::Timestamp::TimeDiff rest_bytes_ms[known_methods_count][2] =
|
|
|
|
{
|
|
|
|
/* GET */ {500, 500},
|
|
|
|
/* POST */ {1000, 30000},
|
|
|
|
/* DELETE */ {1000, 10000},
|
|
|
|
/* PUT */ {1000, 3000},
|
|
|
|
/* HEAD */ {500, 500},
|
|
|
|
/* PATCH */ {1000, 10000},
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(first_byte_ms) == sizeof(rest_bytes_ms));
|
|
|
|
static_assert(sizeof(first_byte_ms) == known_methods_count * sizeof(Poco::Timestamp::TimeDiff) * 2);
|
|
|
|
|
|
|
|
static size_t getMethodIndex(const String & method)
|
|
|
|
{
|
|
|
|
KnownMethodsArray::const_iterator it = std::find(known_methods.begin(), known_methods.end(), method);
|
|
|
|
chassert(it != known_methods.end());
|
|
|
|
if (it == known_methods.end())
|
|
|
|
return 0;
|
|
|
|
return std::distance(known_methods.begin(), it);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static std::pair<Poco::Timespan, Poco::Timespan> getSendReceiveTimeout(const String & method, bool first_byte)
|
|
|
|
{
|
|
|
|
auto idx = getMethodIndex(method);
|
|
|
|
|
|
|
|
if (first_byte)
|
|
|
|
return std::make_pair(
|
|
|
|
Poco::Timespan(first_byte_ms[idx][0] * 1000),
|
|
|
|
Poco::Timespan(first_byte_ms[idx][1] * 1000)
|
|
|
|
);
|
|
|
|
|
|
|
|
return std::make_pair(
|
|
|
|
Poco::Timespan(rest_bytes_ms[idx][0] * 1000),
|
|
|
|
Poco::Timespan(rest_bytes_ms[idx][1] * 1000)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const SendReceiveTimeoutsForFirstAttempt::KnownMethodsArray SendReceiveTimeoutsForFirstAttempt::known_methods =
|
|
|
|
{
|
|
|
|
"GET", "POST", "DELETE", "PUT", "HEAD", "PATCH"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionTimeouts ConnectionTimeouts::getAdaptiveTimeouts(const String & method, bool first_attempt, bool first_byte) const
|
|
|
|
{
|
|
|
|
if (!first_attempt)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
auto [send, recv] = SendReceiveTimeoutsForFirstAttempt::getSendReceiveTimeout(method, first_byte);
|
|
|
|
|
2024-01-22 11:03:36 +00:00
|
|
|
return ConnectionTimeouts(*this)
|
|
|
|
.withSendTimeout(saturate(send, send_timeout))
|
|
|
|
.withReceiveTimeout(saturate(recv, receive_timeout));
|
2023-11-20 13:53:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 13:22:40 +00:00
|
|
|
void setTimeouts(Poco::Net::HTTPClientSession & session, const ConnectionTimeouts & timeouts)
|
|
|
|
{
|
|
|
|
session.setTimeout(timeouts.connection_timeout, timeouts.send_timeout, timeouts.receive_timeout);
|
2024-04-03 13:13:59 +00:00
|
|
|
/// we can not change keep alive timeout for already initiated connections
|
|
|
|
if (!session.connected())
|
|
|
|
{
|
|
|
|
session.setKeepAliveTimeout(timeouts.http_keep_alive_timeout);
|
2024-04-04 20:49:52 +00:00
|
|
|
session.setKeepAliveMaxRequests(int(timeouts.http_keep_alive_max_requests));
|
2024-04-03 13:13:59 +00:00
|
|
|
}
|
2024-03-03 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionTimeouts getTimeouts(const Poco::Net::HTTPClientSession & session)
|
|
|
|
{
|
|
|
|
return ConnectionTimeouts()
|
|
|
|
.withConnectionTimeout(session.getConnectionTimeout())
|
|
|
|
.withSendTimeout(session.getSendTimeout())
|
|
|
|
.withReceiveTimeout(session.getReceiveTimeout())
|
|
|
|
.withHTTPKeepAliveTimeout(session.getKeepAliveTimeout());
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:54:49 +00:00
|
|
|
}
|