开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (18)
标签 (41)
master
clickhouse
clickhouse-new-tmp
clickhouse-old-2021年12月22日
suppress-msan-2
master-old-2021年12月22日
nm/fix-uninitialized-memory
cmake-3-19
clickhouse-patch-leshikus2
update
boringssl
2.15.3
clickhouse_old_2.11
ssl-default-verify-paths
CPP-747-experiment
gh-pages
2.0
1.0
2.15.2
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.0
2.11.0
2.10.0
2.10.0-beta1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
cpp-driver
/
src
/
request_handler.cpp
cpp-driver
/
src
/
request_handler.cpp
request_handler.cpp 25.29 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "request_handler.hpp"
#include "batch_request.hpp"
#include "connection.hpp"
#include "connection_pool_manager.hpp"
#include "constants.hpp"
#include "error_response.hpp"
#include "execute_request.hpp"
#include "metrics.hpp"
#include "prepare_request.hpp"
#include "protocol.hpp"
#include "response.hpp"
#include "result_response.hpp"
#include "row.hpp"
#include "session.hpp"
#include <uv.h>
using namespace datastax;
using namespace datastax::internal;
using namespace datastax::internal::core;
static String to_hex(const String& byte_id) {
static const char half_byte_to_hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
OStringStream ss;
const char* data = byte_id.data();
for (size_t i = 0; i < byte_id.length(); ++i) {
uint8_t byte = static_cast<uint8_t>(data[i]);
ss << half_byte_to_hex[(byte >> 4) & 0x0F];
ss << half_byte_to_hex[byte & 0x0F];
}
return ss.str();
}
class SingleHostQueryPlan : public QueryPlan {
public:
SingleHostQueryPlan(const Address& address)
: host_(new Host(address)) {}
virtual Host::Ptr compute_next() {
Host::Ptr temp = host_;
host_.reset(); // Only return the host once
return temp;
}
private:
Host::Ptr host_;
};
class PrepareCallback : public SimpleRequestCallback {
public:
PrepareCallback(const String& query, const String& id, RequestExecution* request_execution);
private:
class PrepareRequest : public core::PrepareRequest {
public:
PrepareRequest(const String& query, const String& keyspace, uint64_t request_timeout_ms)
: core::PrepareRequest(query) {
set_keyspace(keyspace);
set_request_timeout_ms(request_timeout_ms);
}
};
private:
virtual void on_internal_set(ResponseMessage* response);
virtual void on_internal_error(CassError code, const String& message);
virtual void on_internal_timeout();
private:
RequestExecution::Ptr request_execution_;
String id_;
};
PrepareCallback::PrepareCallback(const String& query, const String& id,
RequestExecution* request_execution)
: SimpleRequestCallback(
Request::ConstPtr(new PrepareRequest(query, request_execution->request()->keyspace(),
request_execution->request_timeout_ms())))
, request_execution_(request_execution)
, id_(id) {}
void PrepareCallback::on_internal_set(ResponseMessage* response) {
switch (response->opcode()) {
case CQL_OPCODE_RESULT: {
ResultResponse* result = static_cast<ResultResponse*>(response->response_body().get());
if (result->kind() == CASS_RESULT_KIND_PREPARED) {
String result_id = result->prepared_id().to_string();
if (id_ != result_id) {
request_execution_->notify_prepared_id_mismatch(id_, result_id);
} else {
request_execution_->notify_result_metadata_changed(request(), result);
request_execution_->on_retry_current_host();
}
} else {
request_execution_->on_retry_next_host();
}
} break;
case CQL_OPCODE_ERROR:
request_execution_->on_retry_next_host();
break;
default:
break;
}
}
void PrepareCallback::on_internal_error(CassError code, const String& message) {
request_execution_->on_retry_next_host();
}
void PrepareCallback::on_internal_timeout() { request_execution_->on_retry_next_host(); }
class NopRequestListener : public RequestListener {
public:
virtual void on_prepared_metadata_changed(const String& id,
const PreparedMetadata::Entry::Ptr& entry) {}
virtual void on_keyspace_changed(const String& keyspace, KeyspaceChangedResponse response) {}
virtual bool on_wait_for_tracing_data(const RequestHandler::Ptr& request_handler,
const Host::Ptr& current_host,
const Response::Ptr& response) {
return false;
}
virtual bool on_wait_for_schema_agreement(const RequestHandler::Ptr& request_handler,
const Host::Ptr& current_host,
const Response::Ptr& response) {
return false;
}
virtual bool on_prepare_all(const RequestHandler::Ptr& request_handler,
const Host::Ptr& current_host, const Response::Ptr& response) {
return false;
}
virtual void on_done() {}
};
static NopRequestListener nop_request_listener__;
RequestHandler::RequestHandler(const Request::ConstPtr& request, const ResponseFuture::Ptr& future,
Metrics* metrics)
: wrapper_(request)
, future_(future)
, is_done_(false)
, running_executions_(0)
, start_time_ns_(uv_hrtime())
, listener_(&nop_request_listener__)
, manager_(NULL)
, metrics_(metrics) {}
RequestHandler::~RequestHandler() {
if (Logger::log_level() >= CASS_LOG_TRACE) {
OStringStream ss;
for (RequestTryVec::const_iterator it = request_tries_.begin(), end = request_tries_.end();
it != end; ++it) {
if (it != request_tries_.begin()) ss << ", ";
ss << "(" << it->address << ", ";
if (it->error != CASS_OK) {
ss << cass_error_desc(it->error);
} else {
ss << it->latency;
}
ss << ")";
}
LOG_TRACE("Speculative execution attempts: [%s]", ss.str().c_str());
}
}
void RequestHandler::set_prepared_metadata(const PreparedMetadata::Entry::Ptr& entry) {
wrapper_.set_prepared_metadata(entry);
}
void RequestHandler::init(const ExecutionProfile& profile, ConnectionPoolManager* manager,
const TokenMap* token_map, TimestampGenerator* timestamp_generator,
RequestListener* listener) {
manager_ = manager;
listener_ = listener ? listener : &nop_request_listener__;
wrapper_.init(profile, timestamp_generator);
// Attempt to use the statement's keyspace first then if not set then use the session's keyspace
const String& keyspace(!request()->keyspace().empty() ? request()->keyspace()
: manager_->keyspace());
// If a specific host is set then bypass the load balancing policy and use a
// specialized single host query plan.
if (request()->host()) {
query_plan_.reset(new SingleHostQueryPlan(*request()->host()));
} else {
query_plan_.reset(profile.load_balancing_policy()->new_query_plan(keyspace, this, token_map));
}
execution_plan_.reset(
profile.speculative_execution_policy()->new_plan(keyspace, wrapper_.request().get()));
}
void RequestHandler::execute() {
RequestExecution::Ptr request_execution(new RequestExecution(this));
running_executions_++;
internal_retry(request_execution.get());
}
void RequestHandler::retry(RequestExecution* request_execution, Protected) {
internal_retry(request_execution);
}
void RequestHandler::start_request(uv_loop_t* loop, Protected) {
if (!timer_.is_running()) {
uint64_t request_timeout_ms = wrapper_.request_timeout_ms();
if (request_timeout_ms > 0) { // 0 means no timeout
timer_.start(loop, request_timeout_ms, bind_callback(&RequestHandler::on_timeout, this));
}
}
}
Host::Ptr RequestHandler::next_host(Protected) { return query_plan_->compute_next(); }
int64_t RequestHandler::next_execution(const Host::Ptr& current_host, Protected) {
return execution_plan_->next_execution(current_host);
}
void RequestHandler::add_attempted_address(const Address& address, Protected) {
future_->add_attempted_address(address);
}
void RequestHandler::notify_result_metadata_changed(const String& prepared_id, const String& query,
const String& keyspace,
const String& result_metadata_id,
const ResultResponse::ConstPtr& result_response,
Protected) {
PreparedMetadata::Entry::Ptr entry(
new PreparedMetadata::Entry(query, keyspace, result_metadata_id, result_response));
listener_->on_prepared_metadata_changed(prepared_id, entry);
}
void RequestHandler::notify_keyspace_changed(const String& keyspace, const Host::Ptr& current_host,
const Response::Ptr& response) {
listener_->on_keyspace_changed(
keyspace, KeyspaceChangedResponse(RequestHandler::Ptr(this), current_host, response));
}
bool RequestHandler::wait_for_tracing_data(const Host::Ptr& current_host,
const Response::Ptr& response) {
return listener_->on_wait_for_tracing_data(Ptr(this), current_host, response);
}
bool RequestHandler::wait_for_schema_agreement(const Host::Ptr& current_host,
const Response::Ptr& response) {
return listener_->on_wait_for_schema_agreement(Ptr(this), current_host, response);
}
bool RequestHandler::prepare_all(const Host::Ptr& current_host, const Response::Ptr& response) {
return listener_->on_prepare_all(Ptr(this), current_host, response);
}
void RequestHandler::set_response(const Host::Ptr& host, const Response::Ptr& response) {
stop_request();
running_executions_--;
if (future_->set_response(host->address(), response)) {
if (metrics_) {
metrics_->record_request(uv_hrtime() - start_time_ns_);
}
} else {
// This request is a speculative execution for whom we already processed
// a response (another speculative execution). So consider this one an
// aborted speculative execution.
if (metrics_) {
metrics_->record_speculative_request(uv_hrtime() - start_time_ns_);
}
}
if (Logger::log_level() >= CASS_LOG_TRACE) {
request_tries_.push_back(RequestTry(host->address(), uv_hrtime() - start_time_ns_));
}
}
void RequestHandler::set_error(CassError code, const String& message) {
stop_request();
bool skip = (code == CASS_ERROR_LIB_NO_HOSTS_AVAILABLE && --running_executions_ > 0);
if (!skip) {
future_->set_error(code, message);
}
}
void RequestHandler::set_error(const Host::Ptr& host, CassError code, const String& message) {
stop_request();
bool skip = (code == CASS_ERROR_LIB_NO_HOSTS_AVAILABLE && --running_executions_ > 0);
if (!skip) {
if (host) {
future_->set_error_with_address(host->address(), code, message);
} else {
set_error(code, message);
}
}
if (Logger::log_level() >= CASS_LOG_TRACE) {
request_tries_.push_back(RequestTry(host->address(), code));
}
}
void RequestHandler::set_error_with_error_response(const Host::Ptr& host,
const Response::Ptr& error, CassError code,
const String& message) {
stop_request();
running_executions_--;
future_->set_error_with_response(host->address(), error, code, message);
if (Logger::log_level() >= CASS_LOG_TRACE) {
request_tries_.push_back(RequestTry(host->address(), code));
}
}
void RequestHandler::stop_timer() { timer_.stop(); }
void RequestHandler::on_timeout(Timer* timer) {
if (metrics_) {
metrics_->request_timeouts.inc();
}
set_error(CASS_ERROR_LIB_REQUEST_TIMED_OUT, "Request timed out");
LOG_DEBUG("Request timed out");
}
void RequestHandler::stop_request() {
if (!is_done_) {
listener_->on_done();
is_done_ = true;
}
timer_.stop();
}
void RequestHandler::internal_retry(RequestExecution* request_execution) {
if (is_done_) {
LOG_DEBUG("Canceling speculative execution (%p) for request (%p) on host %s",
static_cast<void*>(request_execution), static_cast<void*>(this),
request_execution->current_host()
? request_execution->current_host()->address_string().c_str()
: "<no current host>");
return;
}
bool is_done = false;
while (!is_done && request_execution->current_host()) {
PooledConnection::Ptr connection =
manager_->find_least_busy(request_execution->current_host()->address());
if (connection) {
int32_t result = connection->write(request_execution);
if (result > 0) {
is_done = true;
} else {
switch (result) {
case SocketRequest::SOCKET_REQUEST_ERROR_CLOSED:
// This should never happen, but retry with next host if it does.
request_execution->next_host();
break;
case SocketRequest::SOCKET_REQUEST_ERROR_NO_HANDLER:
set_error(CASS_ERROR_LIB_WRITE_ERROR,
"Socket is not properly configured with a handler");
is_done = true;
break;
case Request::REQUEST_ERROR_NO_AVAILABLE_STREAM_IDS:
// Retry with next host
request_execution->next_host();
break;
case Request::REQUEST_ERROR_BATCH_WITH_NAMED_VALUES:
case Request::REQUEST_ERROR_PARAMETER_UNSET:
case Request::REQUEST_ERROR_UNSUPPORTED_PROTOCOL:
case Request::REQUEST_ERROR_NO_DATA_WRITTEN:
// Already handled with a specific error.
is_done = true;
break;
default:
set_error(CASS_ERROR_LIB_WRITE_ERROR, "Unspecified write error occurred");
is_done = true;
break;
}
}
} else {
// No connection available on the current host, move to the next host.
request_execution->next_host();
}
}
if (!request_execution->current_host()) {
set_error(CASS_ERROR_LIB_NO_HOSTS_AVAILABLE, "All hosts in current policy attempted "
"and were either unavailable or failed");
}
}
RequestExecution::RequestExecution(RequestHandler* request_handler)
: RequestCallback(request_handler->wrapper())
, request_handler_(request_handler)
, current_host_(request_handler->next_host(RequestHandler::Protected()))
, num_retries_(0)
, start_time_ns_(uv_hrtime()) {}
void RequestExecution::on_execute_next(Timer* timer) { request_handler_->execute(); }
void RequestExecution::on_retry_current_host() { retry_current_host(); }
void RequestExecution::on_retry_next_host() {
if (current_host_) current_host_->decrement_inflight_requests();
retry_next_host();
}
void RequestExecution::retry_current_host() {
// Reset the request so it can be executed again
set_state(REQUEST_STATE_NEW);
request_handler_->retry(this, RequestHandler::Protected());
}
void RequestExecution::retry_next_host() {
next_host();
retry_current_host();
}
void RequestExecution::on_write(Connection* connection) {
assert(current_host_ && "Tried to start on a non-existent host");
current_host_->increment_inflight_requests();
connection_ = connection;
if (request()->record_attempted_addresses()) {
request_handler_->add_attempted_address(current_host_->address(), RequestHandler::Protected());
}
request_handler_->start_request(connection->loop(), RequestHandler::Protected());
if (request()->is_idempotent()) {
int64_t timeout = request_handler_->next_execution(current_host_, RequestHandler::Protected());
if (timeout == 0) {
request_handler_->execute();
} else if (timeout > 0) {
schedule_timer_.start(connection->loop(), timeout,
bind_callback(&RequestExecution::on_execute_next, this));
}
}
}
void RequestExecution::on_set(ResponseMessage* response) {
assert(connection_ != NULL);
assert(current_host_ && "Tried to set on a non-existent host");
current_host_->decrement_inflight_requests();
Connection* connection = connection_;
switch (response->opcode()) {
case CQL_OPCODE_RESULT:
on_result_response(connection, response);
break;
case CQL_OPCODE_ERROR:
on_error_response(connection, response);
break;
default:
connection->defunct();
set_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE, "Unexpected response");
break;
}
}
void RequestExecution::on_error(CassError code, const String& message) {
if (current_host_) current_host_->decrement_inflight_requests();
set_error(code, message);
}
void RequestExecution::notify_result_metadata_changed(const Request* request,
ResultResponse* result_response) {
// Attempt to use the per-query keyspace first (v5+/DSEv2+ only) then
// the keyspace in the result metadata.
String keyspace;
if (result_response->protocol_version().supports_set_keyspace() && !request->keyspace().empty()) {
keyspace = request->keyspace();
} else {
keyspace = result_response->quoted_keyspace();
}
if (request->opcode() == CQL_OPCODE_EXECUTE && result_response->kind() == CASS_RESULT_KIND_ROWS) {
const ExecuteRequest* execute = static_cast<const ExecuteRequest*>(request);
request_handler_->notify_result_metadata_changed(
execute->prepared()->id(), execute->prepared()->query(), keyspace,
result_response->new_metadata_id().to_string(), ResultResponse::ConstPtr(result_response),
RequestHandler::Protected());
} else if (request->opcode() == CQL_OPCODE_PREPARE &&
result_response->kind() == CASS_RESULT_KIND_PREPARED) {
const PrepareRequest* prepare = static_cast<const PrepareRequest*>(request);
request_handler_->notify_result_metadata_changed(
result_response->prepared_id().to_string(), prepare->query(), keyspace,
result_response->result_metadata_id().to_string(),
ResultResponse::ConstPtr(result_response), RequestHandler::Protected());
} else {
assert(false && "Invalid response type for a result metadata change");
}
}
void RequestExecution::notify_prepared_id_mismatch(const String& expected_id,
const String& received_id) {
OStringStream ss;
ss << "ID mismatch while trying to prepare query (expected ID " << to_hex(expected_id)
<< ", received ID " << to_hex(received_id)
<< "). This prepared statement won't work anymore. This usually happens when you run a "
"'USE...' query after the statement was prepared.";
String message = ss.str();
request_handler_->set_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE, message);
}
void RequestExecution::on_result_response(Connection* connection, ResponseMessage* response) {
ResultResponse* result = static_cast<ResultResponse*>(response->response_body().get());
switch (result->kind()) {
case CASS_RESULT_KIND_ROWS:
current_host_->update_latency(uv_hrtime() - start_time_ns_);
// Execute statements with no metadata get their metadata from
// result_metadata() returned when the statement was prepared.
if (request()->opcode() == CQL_OPCODE_EXECUTE) {
if (result->no_metadata()) {
if (!skip_metadata()) {
// Caused by a race condition in C* 2.1.0
on_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE,
"Expected metadata but no metadata in response (see CASSANDRA-8054)");
return;
}
result->set_metadata(prepared_metadata_entry()->result()->result_metadata());
} else if (result->metadata_changed()) {
notify_result_metadata_changed(request(), result);
}
}
if (!response->response_body()->has_tracing_id() ||
!request_handler_->wait_for_tracing_data(current_host(), response->response_body())) {
set_response(response->response_body());
}
break;
case CASS_RESULT_KIND_SCHEMA_CHANGE: {
if (!request_handler_->wait_for_schema_agreement(current_host(), response->response_body())) {
set_response(response->response_body());
}
break;
}
case CASS_RESULT_KIND_SET_KEYSPACE:
// The response is set after the keyspace is propagated to all threads.
request_handler_->notify_keyspace_changed(result->quoted_keyspace(), current_host_,
response->response_body());
break;
case CASS_RESULT_KIND_PREPARED:
notify_result_metadata_changed(request(), result);
if (!request_handler_->prepare_all(current_host(), response->response_body())) {
set_response(response->response_body());
}
break;
default:
set_response(response->response_body());
break;
}
}
void RequestExecution::on_error_response(Connection* connection, ResponseMessage* response) {
ErrorResponse* error = static_cast<ErrorResponse*>(response->response_body().get());
RetryPolicy::RetryDecision decision = RetryPolicy::RetryDecision::return_error();
switch (error->code()) {
case CQL_ERROR_READ_TIMEOUT:
if (retry_policy()) {
decision = retry_policy()->on_read_timeout(request(), error->consistency(),
error->received(), error->required(),
error->data_present() > 0, num_retries_);
}
break;
case CQL_ERROR_WRITE_TIMEOUT:
if (retry_policy() && request()->is_idempotent()) {
decision =
retry_policy()->on_write_timeout(request(), error->consistency(), error->received(),
error->required(), error->write_type(), num_retries_);
}
break;
case CQL_ERROR_UNAVAILABLE:
if (retry_policy()) {
decision = retry_policy()->on_unavailable(
request(), error->consistency(), error->required(), error->received(), num_retries_);
}
break;
case CQL_ERROR_OVERLOADED:
LOG_WARN("Host %s is overloaded.", connection->address_string().c_str());
if (retry_policy() && request()->is_idempotent()) {
decision = retry_policy()->on_request_error(request(), consistency(), error, num_retries_);
}
break;
case CQL_ERROR_SERVER_ERROR:
LOG_WARN("Received server error '%s' from host %s. Defuncting the connection...",
error->message().to_string().c_str(), connection->address_string().c_str());
connection->defunct();
if (retry_policy() && request()->is_idempotent()) {
decision = retry_policy()->on_request_error(request(), consistency(), error, num_retries_);
}
break;
case CQL_ERROR_IS_BOOTSTRAPPING:
LOG_ERROR("Query sent to bootstrapping host %s. Retrying on the next host...",
connection->address_string().c_str());
retry_next_host();
return; // Done
case CQL_ERROR_UNPREPARED:
on_error_unprepared(connection, error);
return; // Done
default:
// Return the error response
break;
}
// Process retry decision
switch (decision.type()) {
case RetryPolicy::RetryDecision::RETURN_ERROR:
set_error_with_error_response(
response->response_body(),
static_cast<CassError>(CASS_ERROR(CASS_ERROR_SOURCE_SERVER, error->code())),
error->message().to_string());
break;
case RetryPolicy::RetryDecision::RETRY:
set_retry_consistency(decision.retry_consistency());
if (decision.retry_current_host()) {
retry_current_host();
} else {
retry_next_host();
}
num_retries_++;
break;
case RetryPolicy::RetryDecision::IGNORE:
set_response(Response::Ptr(new ResultResponse()));
break;
}
}
void RequestExecution::on_error_unprepared(Connection* connection, ErrorResponse* error) {
LOG_DEBUG("Unprepared error response returned for request: %s",
error->message().to_string().c_str());
String query;
String id = error->prepared_id().to_string();
if (request()->opcode() == CQL_OPCODE_EXECUTE) {
const ExecuteRequest* execute = static_cast<const ExecuteRequest*>(request());
query = execute->prepared()->query();
} else if (request()->opcode() == CQL_OPCODE_BATCH) {
const BatchRequest* batch = static_cast<const BatchRequest*>(request());
if (!batch->find_prepared_query(id, &query)) {
set_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE,
"Unable to find prepared statement in batch statement");
return;
}
} else {
connection->defunct();
set_error(CASS_ERROR_LIB_UNEXPECTED_RESPONSE, "Received unprepared error for invalid "
"request type or invalid prepared id");
return;
}
RequestCallback::Ptr callback(new PrepareCallback(query, id, this));
if (connection->write_and_flush(callback) < 0) {
// Try to prepare on the same host but on a different connection
retry_current_host();
}
}
void RequestExecution::set_response(const Response::Ptr& response) {
request_handler_->set_response(current_host_, response);
}
void RequestExecution::set_error(CassError code, const String& message) {
request_handler_->set_error(current_host_, code, message);
}
void RequestExecution::set_error_with_error_response(const Response::Ptr& error, CassError code,
const String& message) {
request_handler_->set_error_with_error_response(current_host_, error, code, message);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp-driver加速
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cyys/cpp-driver.git
git@gitee.com:cyys/cpp-driver.git
cyys
cpp-driver
cpp-driver
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /