-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathConnectionTimeouts.cpp
More file actions
214 lines (182 loc) · 8.69 KB
/
Copy pathConnectionTimeouts.cpp
File metadata and controls
214 lines (182 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <Core/ServerSettings.h>
#include <Core/Settings.h>
#include <IO/ConnectionTimeouts.h>
#include <Poco/Util/AbstractConfiguration.h>
namespace DB
{
namespace Setting
{
extern const SettingsSeconds connect_timeout;
extern const SettingsSeconds send_timeout;
extern const SettingsSeconds receive_timeout;
extern const SettingsSeconds tcp_keep_alive_timeout;
extern const SettingsMilliseconds handshake_timeout_ms;
extern const SettingsMilliseconds hedged_connection_timeout_ms;
extern const SettingsMilliseconds receive_data_timeout_ms;
extern const SettingsMilliseconds connect_timeout_with_failover_ms;
extern const SettingsMilliseconds connect_timeout_with_failover_secure_ms;
extern const SettingsSeconds http_connection_timeout;
extern const SettingsSeconds http_send_timeout;
extern const SettingsSeconds http_receive_timeout;
}
namespace ServerSetting
{
extern const ServerSettingsSeconds keep_alive_timeout;
extern const ServerSettingsSeconds replicated_fetches_http_connection_timeout;
extern const ServerSettingsSeconds replicated_fetches_http_receive_timeout;
extern const ServerSettingsSeconds replicated_fetches_http_send_timeout;
}
Poco::Timespan ConnectionTimeouts::saturate(Poco::Timespan timespan, Poco::Timespan limit)
{
if (limit.totalMicroseconds() == 0)
return timespan;
return (timespan > limit) ? limit : timespan;
}
/// Timeouts for the case when we have just single attempt to connect.
ConnectionTimeouts ConnectionTimeouts::getTCPTimeoutsWithoutFailover(const Settings & settings)
{
return ConnectionTimeouts()
.withConnectionTimeout(settings[Setting::connect_timeout])
.withSendTimeout(settings[Setting::send_timeout])
.withReceiveTimeout(settings[Setting::receive_timeout])
.withTCPKeepAliveTimeout(settings[Setting::tcp_keep_alive_timeout])
.withHandshakeTimeout(settings[Setting::handshake_timeout_ms])
.withHedgedConnectionTimeout(settings[Setting::hedged_connection_timeout_ms])
.withReceiveDataTimeout(settings[Setting::receive_data_timeout_ms]);
}
/// Timeouts for the case when we will try many addresses in a loop.
ConnectionTimeouts ConnectionTimeouts::getTCPTimeoutsWithFailover(const Settings & settings)
{
return getTCPTimeoutsWithoutFailover(settings)
.withUnsecureConnectionTimeout(settings[Setting::connect_timeout_with_failover_ms])
.withSecureConnectionTimeout(settings[Setting::connect_timeout_with_failover_secure_ms]);
}
ConnectionTimeouts ConnectionTimeouts::getHTTPTimeouts(const Settings & settings, const ServerSettings & server_settings)
{
return ConnectionTimeouts()
.withConnectionTimeout(settings[Setting::http_connection_timeout])
.withSendTimeout(settings[Setting::http_send_timeout])
.withReceiveTimeout(settings[Setting::http_receive_timeout])
.withHTTPKeepAliveTimeout(server_settings[ServerSetting::keep_alive_timeout])
.withTCPKeepAliveTimeout(settings[Setting::tcp_keep_alive_timeout])
.withHandshakeTimeout(settings[Setting::handshake_timeout_ms]);
}
ConnectionTimeouts ConnectionTimeouts::getFetchPartHTTPTimeouts(const ServerSettings & server_settings, const Settings & user_settings)
{
auto timeouts = getHTTPTimeouts(user_settings, server_settings);
if (server_settings[ServerSetting::replicated_fetches_http_connection_timeout].changed)
timeouts.connection_timeout = server_settings[ServerSetting::replicated_fetches_http_connection_timeout];
if (server_settings[ServerSetting::replicated_fetches_http_send_timeout].changed)
timeouts.send_timeout = server_settings[ServerSetting::replicated_fetches_http_send_timeout];
if (server_settings[ServerSetting::replicated_fetches_http_receive_timeout].changed)
timeouts.receive_timeout = server_settings[ServerSetting::replicated_fetches_http_receive_timeout];
return timeouts;
}
class TimeoutsForFirstAttempt
{
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);
/// These timeouts are specifically important for Azure blob storage.
/// For Azure first byte latency is usually very good, but connection to Azure blob storage can take a long time.
static constexpr auto CONNECT_TIMEOUT_FOR_FIRST_ATTEMPT_MS = 500;
static constexpr auto CONNECT_TIMEOUT_FOR_N_ATTEMPT_MS = 3000;
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)
);
}
static std::pair<Poco::Timespan, Poco::Timespan> getConnectTimeouts(bool first_attempt)
{
if (first_attempt)
{
/// It is important for Azure blob storage, where connection can take a long time.
/// If the connection is not established in this time, we will try to connect to another endpoint.
return std::make_pair(
Poco::Timespan(CONNECT_TIMEOUT_FOR_FIRST_ATTEMPT_MS * 1000),
Poco::Timespan(CONNECT_TIMEOUT_FOR_FIRST_ATTEMPT_MS * 1000)
);
}
return std::make_pair(
Poco::Timespan(CONNECT_TIMEOUT_FOR_N_ATTEMPT_MS * 1000),
Poco::Timespan(CONNECT_TIMEOUT_FOR_N_ATTEMPT_MS * 1000)
);
}
};
const TimeoutsForFirstAttempt::KnownMethodsArray TimeoutsForFirstAttempt::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] = TimeoutsForFirstAttempt::getSendReceiveTimeout(method, first_byte);
auto [unsecure_connect, secure_connect] = TimeoutsForFirstAttempt::getConnectTimeouts(first_attempt);
return ConnectionTimeouts(*this)
.withConnectionTimeout(saturate(unsecure_connect, connection_timeout))
.withSecureConnectionTimeout(saturate(secure_connect, secure_connection_timeout))
.withSendTimeout(saturate(send, send_timeout))
.withReceiveTimeout(saturate(recv, receive_timeout));
}
void setTimeouts(Poco::Net::HTTPClientSession & session, const ConnectionTimeouts & timeouts)
{
session.setTimeout(timeouts.connection_timeout, timeouts.send_timeout, timeouts.receive_timeout);
/// we can not change keep alive timeout for already initiated connections
if (!session.connected())
{
session.setKeepAliveTimeout(timeouts.http_keep_alive_timeout);
session.setKeepAliveMaxRequests(int(timeouts.http_keep_alive_max_requests));
}
}
ConnectionTimeouts getTimeouts(const Poco::Net::HTTPClientSession & session)
{
return ConnectionTimeouts()
.withConnectionTimeout(session.getConnectionTimeout())
.withSendTimeout(session.getSendTimeout())
.withReceiveTimeout(session.getReceiveTimeout())
.withHTTPKeepAliveTimeout(session.getKeepAliveTimeout());
}
}