From 6b1cfa6bb096ddcc83e8c02ba9deea5242435487 Mon Sep 17 00:00:00 2001 From: linkmyth Date: Tue, 16 Jul 2019 16:23:27 +0800 Subject: [PATCH 1/2] add backoff to get gc safe point --- contrib/client-c/include/pd/Client.h | 3 +++ contrib/client-c/src/pd/Client.cc | 11 ++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/contrib/client-c/include/pd/Client.h b/contrib/client-c/include/pd/Client.h index f93bfd4a022..1ee04bc8d65 100644 --- a/contrib/client-c/include/pd/Client.h +++ b/contrib/client-c/include/pd/Client.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "IClient.h" namespace pingcap{ @@ -27,6 +28,8 @@ class Client : public IClient { const std::chrono::seconds update_leader_interval; + const size_t get_gc_safe_point_timeout; // ms + public: Client(const std::vector & addrs); diff --git a/contrib/client-c/src/pd/Client.cc b/contrib/client-c/src/pd/Client.cc index f09293d3145..51a2c235c6e 100644 --- a/contrib/client-c/src/pd/Client.cc +++ b/contrib/client-c/src/pd/Client.cc @@ -25,6 +25,7 @@ Client::Client(const std::vector & addrs) pd_timeout(3), loop_interval(100), update_leader_interval(60), + get_gc_safe_point_timeout(20000), urls(addrsToUrls(addrs)), log(&Logger::get("pingcap.pd")) { @@ -187,11 +188,12 @@ uint64_t Client::getGCSafePoint() { pdpb::GetGCSafePointRequest request{}; pdpb::GetGCSafePointResponse response{}; request.set_allocated_header(requestHeader()); -; + + kv::Backoffer bo(get_gc_safe_point_timeout); ::grpc::Status status; std::string err_msg; - for (int i = 0; i < max_init_cluster_retries; i++) { + while (true) { grpc::ClientContext context; context.set_deadline(std::chrono::system_clock::now() + pd_timeout); @@ -202,10 +204,9 @@ uint64_t Client::getGCSafePoint() { err_msg = "get safe point failed: " + std::to_string(status.error_code()) + ": " + status.error_message(); log->error(err_msg); check_leader.store(true); - usleep(100000); - // TODO retry outside. + bo.backoff(kv::BackoffType::boPDRPC, Exception(err_msg, status.error_code())); } - throw Exception(err_msg, status.error_code()); + } std::tuple> Client::getRegion(std::string key) { From 953392fefa8a68965e810c150d1245f5ed325703 Mon Sep 17 00:00:00 2001 From: linkmyth Date: Thu, 18 Jul 2019 13:03:59 +0800 Subject: [PATCH 2/2] move backoff to common namespace --- .../client-c/include/{tikv => common}/Backoff.h | 2 +- contrib/client-c/include/pd/Client.h | 2 +- contrib/client-c/include/tikv/Region.h | 3 ++- contrib/client-c/include/tikv/RegionClient.h | 14 +++++++------- contrib/client-c/src/CMakeLists.txt | 2 +- contrib/client-c/src/{tikv => common}/Backoff.cc | 4 ++-- contrib/client-c/src/pd/Client.cc | 4 ++-- contrib/client-c/src/tikv/Region.cc | 6 +++--- 8 files changed, 19 insertions(+), 18 deletions(-) rename contrib/client-c/include/{tikv => common}/Backoff.h (99%) rename contrib/client-c/src/{tikv => common}/Backoff.cc (97%) diff --git a/contrib/client-c/include/tikv/Backoff.h b/contrib/client-c/include/common/Backoff.h similarity index 99% rename from contrib/client-c/include/tikv/Backoff.h rename to contrib/client-c/include/common/Backoff.h index be7675ae156..ceef1e99a4a 100644 --- a/contrib/client-c/include/tikv/Backoff.h +++ b/contrib/client-c/include/common/Backoff.h @@ -9,7 +9,7 @@ #include namespace pingcap { -namespace kv { +namespace common { enum Jitter { NoJitter = 1, diff --git a/contrib/client-c/include/pd/Client.h b/contrib/client-c/include/pd/Client.h index 1ee04bc8d65..d3dac128ab2 100644 --- a/contrib/client-c/include/pd/Client.h +++ b/contrib/client-c/include/pd/Client.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "IClient.h" namespace pingcap{ diff --git a/contrib/client-c/include/tikv/Region.h b/contrib/client-c/include/tikv/Region.h index 835de437d34..6f6b4f7aa19 100644 --- a/contrib/client-c/include/tikv/Region.h +++ b/contrib/client-c/include/tikv/Region.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -123,6 +123,7 @@ struct RPCContext { }; using RPCContextPtr = std::shared_ptr; +using Backoffer = common::Backoffer; class RegionCache { public: diff --git a/contrib/client-c/include/tikv/RegionClient.h b/contrib/client-c/include/tikv/RegionClient.h index a7426609285..08e5ca20563 100644 --- a/contrib/client-c/include/tikv/RegionClient.h +++ b/contrib/client-c/include/tikv/RegionClient.h @@ -2,7 +2,7 @@ #include #include -#include +#include namespace pingcap { namespace kv { @@ -19,7 +19,7 @@ struct RegionClient { int64_t getReadIndex() { auto request = new kvrpcpb::ReadIndexRequest(); - Backoffer bo(readIndexMaxBackoff); + Backoffer bo(common::readIndexMaxBackoff); auto rpc_call = std::make_shared>(request); sendReqToRegion(bo, rpc_call, true); return rpc_call -> getResp() -> read_index(); @@ -57,10 +57,10 @@ struct RegionClient { auto not_leader = err.not_leader(); if (not_leader.has_leader()) { cache -> updateLeader(bo, rpc_ctx->region, not_leader.leader().store_id()); - bo.backoff(boUpdateLeader, Exception("not leader")); + bo.backoff(common::boUpdateLeader, Exception("not leader")); } else { cache -> dropRegion(rpc_ctx->region); - bo.backoff(boRegionMiss, Exception("not leader")); + bo.backoff(common::boRegionMiss, Exception("not leader")); } return; } @@ -76,7 +76,7 @@ struct RegionClient { } if (err.has_server_is_busy()) { - bo.backoff(boServerBusy, Exception("server busy")); + bo.backoff(common::boServerBusy, Exception("server busy")); return; } @@ -93,12 +93,12 @@ struct RegionClient { void onGetLearnerFail(Backoffer & bo, const Exception & e) { log -> error("error found, retrying. The error msg is: "+ e.message()); - bo.backoff(boTiKVRPC, e); + bo.backoff(common::boTiKVRPC, e); } void onSendFail(Backoffer & bo, const Exception & e, RPCContextPtr rpc_ctx) { cache->dropStoreOnSendReqFail(rpc_ctx, e); - bo.backoff(boTiKVRPC, e); + bo.backoff(common::boTiKVRPC, e); } }; diff --git a/contrib/client-c/src/CMakeLists.txt b/contrib/client-c/src/CMakeLists.txt index 948558ae324..930af3b58ea 100644 --- a/contrib/client-c/src/CMakeLists.txt +++ b/contrib/client-c/src/CMakeLists.txt @@ -3,7 +3,7 @@ set(kvClient_sources) list(APPEND kvClient_sources pd/Client.cc) list(APPEND kvClient_sources tikv/Region.cc) list(APPEND kvClient_sources tikv/RegionClient.cc) -list(APPEND kvClient_sources tikv/Backoff.cc) +list(APPEND kvClient_sources common/Backoff.cc) list(APPEND kvClient_sources tikv/Rpc.cc) set(kvClient_INCLUDE_DIR ${kvClient_SOURCE_DIR}/include) diff --git a/contrib/client-c/src/tikv/Backoff.cc b/contrib/client-c/src/common/Backoff.cc similarity index 97% rename from contrib/client-c/src/tikv/Backoff.cc rename to contrib/client-c/src/common/Backoff.cc index 4cf0c04cad4..e1cf0507a13 100644 --- a/contrib/client-c/src/tikv/Backoff.cc +++ b/contrib/client-c/src/common/Backoff.cc @@ -1,8 +1,8 @@ -#include +#include #include namespace pingcap { -namespace kv { +namespace common { BackoffPtr newBackoff(BackoffType tp) { switch(tp) { diff --git a/contrib/client-c/src/pd/Client.cc b/contrib/client-c/src/pd/Client.cc index 51a2c235c6e..0e029b1baca 100644 --- a/contrib/client-c/src/pd/Client.cc +++ b/contrib/client-c/src/pd/Client.cc @@ -189,7 +189,7 @@ uint64_t Client::getGCSafePoint() { pdpb::GetGCSafePointResponse response{}; request.set_allocated_header(requestHeader()); - kv::Backoffer bo(get_gc_safe_point_timeout); + common::Backoffer bo(get_gc_safe_point_timeout); ::grpc::Status status; std::string err_msg; @@ -204,7 +204,7 @@ uint64_t Client::getGCSafePoint() { err_msg = "get safe point failed: " + std::to_string(status.error_code()) + ": " + status.error_message(); log->error(err_msg); check_leader.store(true); - bo.backoff(kv::BackoffType::boPDRPC, Exception(err_msg, status.error_code())); + bo.backoff(common::BackoffType::boPDRPC, Exception(err_msg, status.error_code())); } } diff --git a/contrib/client-c/src/tikv/Region.cc b/contrib/client-c/src/tikv/Region.cc index 68c552ca8b9..0ecd60fb333 100644 --- a/contrib/client-c/src/tikv/Region.cc +++ b/contrib/client-c/src/tikv/Region.cc @@ -70,7 +70,7 @@ RegionPtr RegionCache::loadRegionByID(Backoffer & bo, uint64_t region_id) { } return region; } catch (const Exception & e) { - bo.backoff(boPDRPC, e); + bo.backoff(common::boPDRPC, e); } } } @@ -103,7 +103,7 @@ RegionPtr RegionCache::loadRegion(Backoffer & bo, std::string key) { } return region; } catch (const Exception & e) { - bo.backoff(boPDRPC, e); + bo.backoff(common::boPDRPC, e); } } } @@ -114,7 +114,7 @@ metapb::Store RegionCache::loadStore(Backoffer & bo, uint64_t id) { const auto & store = pdClient->getStore(id); return store; } catch (Exception & e) { - bo.backoff(boPDRPC, e); + bo.backoff(common::boPDRPC, e); } } }