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 f93bfd4a022..d3dac128ab2 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/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 f09293d3145..0e029b1baca 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()); -; + + common::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(common::BackoffType::boPDRPC, Exception(err_msg, status.error_code())); } - throw Exception(err_msg, status.error_code()); + } std::tuple> Client::getRegion(std::string key) { 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); } } }