Skip to content
Navigation Menu
{{ message }}
forked from apache/cassandra-cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.cpp
More file actions
323 lines (282 loc) · 9.35 KB
/
Copy pathpool.cpp
File metadata and controls
323 lines (282 loc) · 9.35 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "pool.hpp"
#include "session.hpp"
#include "timer.hpp"
#include "prepare_handler.hpp"
#include "logger.hpp"
#include "set_keyspace_handler.hpp"
#include "request_handler.hpp"
#include "connection.hpp"
#include "result_response.hpp"
#include "error_response.hpp"
#include "third_party/boost/boost/bind.hpp"
namespace cass {
static bool least_busy_comp(Connection* a, Connection* b) {
return a->pending_request_count() < b->pending_request_count();
}
Pool::PoolHandler::PoolHandler(Pool* pool, Connection* connection,
RequestHandler* request_handler)
: pool_(pool)
, connection_(connection)
, request_handler_(request_handler) {}
void Pool::PoolHandler::on_set(ResponseMessage* response) {
switch (response->opcode()) {
case CQL_OPCODE_RESULT:
on_result_response(response);
break;
case CQL_OPCODE_ERROR:
on_error_response(response);
break;
default:
// TODO(mpenick): Log this
request_handler_->on_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE,
"Unexpected response");
connection_->defunct();
}
finish_request();
}
void Pool::PoolHandler::on_error(CassError code, const std::string& message) {
if (code == CASS_ERROR_LIB_WRITE_ERROR ||
code == CASS_ERROR_LIB_UNABLE_TO_SET_KEYSPACE) {
request_handler_.release()->retry(RETRY_WITH_NEXT_HOST);
} else {
request_handler_->on_error(code, message);
}
finish_request();
}
void Pool::PoolHandler::on_timeout() {
request_handler_->on_timeout();
finish_request();
}
void Pool::PoolHandler::finish_request() {
if (connection_->is_ready()) {
pool_->execute_pending_request(connection_);
}
}
void Pool::PoolHandler::on_result_response(ResponseMessage* response) {
ResultResponse* result =
static_cast<ResultResponse*>(response->response_body().get());
switch (result->kind()) {
case CASS_RESULT_KIND_SET_KEYSPACE:
if (pool_->keyspace_callback_) {
pool_->keyspace_callback_(result->keyspace());
}
break;
}
request_handler_->on_set(response);
}
void Pool::PoolHandler::on_error_response(ResponseMessage* response) {
ErrorResponse* error =
static_cast<ErrorResponse*>(response->response_body().get());
switch (error->code()) {
case CQL_ERROR_UNPREPARED: {
RequestHandler* request_handler = request_handler_.release();
ScopedPtr<PrepareHandler> prepare_handler(
new PrepareHandler(request_handler));
if (prepare_handler->init(error->prepared_id())) {
connection_->execute(prepare_handler.release());
} else {
request_handler->on_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE,
"Received unprepared error for invalid "
"request type or invalid prepared id");
}
break;
}
default:
request_handler_->on_set(response);
break;
}
}
Pool::Pool(const Host& host, uv_loop_t* loop,
Logger* logger, const Config& config)
: state_(POOL_STATE_NEW)
, host_(host)
, loop_(loop)
, logger_(logger)
, config_(config)
, protocol_version_(config.protocol_version())
, is_defunct_(false) {
}
Pool::~Pool() {
for (RequestHandlerList::iterator it = pending_request_queue_.begin(),
end = pending_request_queue_.end();
it != end; ++it) {
RequestHandler* request_handler = *it;
if (request_handler->timer != NULL) {
Timer::stop(request_handler->timer);
request_handler->timer = NULL;
request_handler->retry(RETRY_WITH_NEXT_HOST);
}
}
pending_request_queue_.clear();
}
void Pool::connect(const std::string& keyspace) {
if (state_ == POOL_STATE_NEW) {
for (unsigned i = 0; i < config_.core_connections_per_host(); ++i) {
spawn_connection(keyspace);
}
state_ = POOL_STATE_CONNECTING;
}
}
void Pool::close() {
if (state_ != POOL_STATE_CLOSING && state_ != POOL_STATE_CLOSED) {
// We're closing before we've connected (likely beause of an error), we need
// to notify we're "ready"
if (state_ == POOL_STATE_CONNECTING) {
if (ready_callback_) {
ready_callback_(this);
}
}
state_ = POOL_STATE_CLOSING;
for (ConnectionVec::iterator it = connections_.begin(),
end = connections_.end();
it != end; ++it) {
(*it)->close();
}
for (ConnectionSet::iterator it = connections_pending_.begin(),
end = connections_pending_.end();
it != end; ++it) {
(*it)->close();
}
maybe_close();
}
}
Connection* Pool::borrow_connection(const std::string& keyspace) {
if (connections_.empty()) {
for (unsigned i = 0; i < config_.core_connections_per_host(); ++i) {
spawn_connection(keyspace);
}
return NULL;
}
Connection* connection = find_least_busy();
if (connection == NULL ||
connection->pending_request_count() >=
config_.max_simultaneous_requests_threshold()) {
maybe_spawn_connection(keyspace);
}
return connection;
}
bool Pool::execute(Connection* connection, RequestHandler* request_handler) {
PoolHandler* pool_handler =
new PoolHandler(this, connection, request_handler);
if (request_handler->keyspace == connection->keyspace()) {
return connection->execute(pool_handler);
} else {
return connection->execute(new SetKeyspaceHandler(
request_handler->keyspace, connection, pool_handler));
}
}
void Pool::defunct() {
is_defunct_ = true;
close();
}
void Pool::maybe_notify_ready(Connection* connection) {
// Currently, this will notify ready even if all the connections fail.
// This might use some improvement.
// We won't notify until we've tried all valid protocol versions
if (!connection->is_invalid_protocol() ||
connection->protocol_version() <= 1) {
if (state_ == POOL_STATE_CONNECTING && connections_pending_.empty()) {
state_ = POOL_STATE_READY;
if (ready_callback_) {
ready_callback_(this);
}
}
}
}
void Pool::maybe_close() {
if (state_ == POOL_STATE_CLOSING && connections_.empty() &&
connections_pending_.empty()) {
state_ = POOL_STATE_CLOSED;
if (closed_callback_) {
closed_callback_(this);
}
}
}
void Pool::spawn_connection(const std::string& keyspace) {
if (state_ != POOL_STATE_CLOSING && state_ != POOL_STATE_CLOSED) {
Connection* connection =
new Connection(loop_, host_, logger_, config_,
keyspace, protocol_version_);
connection->set_ready_callback(
boost::bind(&Pool::on_connection_ready, this, _1));
connection->set_close_callback(
boost::bind(&Pool::on_connection_closed, this, _1));
connection->connect();
connections_pending_.insert(connection);
}
}
void Pool::maybe_spawn_connection(const std::string& keyspace) {
if (connections_pending_.size() >= config_.max_simultaneous_creation()) {
return;
}
if (connections_.size() + connections_pending_.size() >=
config_.max_connections_per_host()) {
return;
}
spawn_connection(keyspace);
}
Connection* Pool::find_least_busy() {
ConnectionVec::iterator it = std::min_element(
connections_.begin(), connections_.end(), least_busy_comp);
if ((*it)->is_ready() && (*it)->available_streams() > 0) {
return *it;
}
return NULL;
}
void Pool::execute_pending_request(Connection* connection) {
if (!pending_request_queue_.empty()) {
RequestHandler* request_handler = pending_request_queue_.front();
pending_request_queue_.pop_front();
if (request_handler->timer != NULL) {
Timer::stop(request_handler->timer);
request_handler->timer = NULL;
}
if (!execute(connection, request_handler)) {
request_handler->retry(RETRY_WITH_NEXT_HOST);
}
}
}
void Pool::on_connection_ready(Connection* connection) {
connections_pending_.erase(connection);
maybe_notify_ready(connection);
connections_.push_back(connection);
execute_pending_request(connection);
}
void Pool::on_connection_closed(Connection* connection) {
connections_pending_.erase(connection);
maybe_notify_ready(connection);
ConnectionVec::iterator it =
std::find(connections_.begin(), connections_.end(), connection);
if (it != connections_.end()) {
connections_.erase(it);
}
if (connection->is_invalid_protocol() &&
connection->protocol_version() > 1) {
protocol_version_ = connection->protocol_version() - 1;
spawn_connection(connection->keyspace());
} else if (connection->is_defunct()) {
// TODO(mpenick): Conviction policy
defunct();
}
maybe_close();
}
void Pool::on_timeout(Timer* timer) {
RequestHandler* request_handler = static_cast<RequestHandler*>(timer->data());
pending_request_queue_.remove(request_handler);
request_handler->retry(RETRY_WITH_NEXT_HOST);
maybe_close();
}
bool Pool::wait_for_connection(RequestHandler* request_handler) {
if (pending_request_queue_.size() + 1 > config_.max_pending_requests()) {
logger_->warn("Exceeded the max pending requests setting of %u on '%s'",
config_.max_pending_requests(),
host_.address.to_string().c_str());
return false;
}
request_handler->timer =
Timer::start(loop_, config_.connect_timeout(), request_handler,
boost::bind(&Pool::on_timeout, this, _1));
pending_request_queue_.push_back(request_handler);
return true;
}
} // namespace cass
You can’t perform that action at this time.
