From a8eea00f9532b9db700f11f203b690e023b9f206 Mon Sep 17 00:00:00 2001 From: Han Fei Date: Thu, 8 Aug 2019 10:50:50 +0800 Subject: [PATCH] [flash-408] implement get-all-stores --- contrib/client-c/include/pd/Client.h | 2 +- contrib/client-c/include/pd/IClient.h | 2 +- contrib/client-c/include/pd/MockPDClient.h | 2 ++ contrib/client-c/src/pd/Client.cc | 26 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/contrib/client-c/include/pd/Client.h b/contrib/client-c/include/pd/Client.h index 42503c37816..edf096cb161 100644 --- a/contrib/client-c/include/pd/Client.h +++ b/contrib/client-c/include/pd/Client.h @@ -49,7 +49,7 @@ class Client : public IClient metapb::Store getStore(uint64_t store_id) override; - //std::vector getAllStores() override; + std::vector getAllStores() override; uint64_t getGCSafePoint() override; diff --git a/contrib/client-c/include/pd/IClient.h b/contrib/client-c/include/pd/IClient.h index 20d46dd181e..57b7520fb78 100644 --- a/contrib/client-c/include/pd/IClient.h +++ b/contrib/client-c/include/pd/IClient.h @@ -30,7 +30,7 @@ class IClient virtual metapb::Store getStore(uint64_t store_id) = 0; - // virtual std::vector getAllStores() = 0; + virtual std::vector getAllStores() = 0; virtual uint64_t getGCSafePoint() = 0; diff --git a/contrib/client-c/include/pd/MockPDClient.h b/contrib/client-c/include/pd/MockPDClient.h index d91c0946cc5..bc878c602cd 100644 --- a/contrib/client-c/include/pd/MockPDClient.h +++ b/contrib/client-c/include/pd/MockPDClient.h @@ -17,6 +17,8 @@ class MockPDClient : public IClient ~MockPDClient() override {} + std::vector getAllStores() override { throw "not implemented"; }; + uint64_t getGCSafePoint() override { return 10000000; } uint64_t getTS() override { return Clock::now().time_since_epoch().count(); } diff --git a/contrib/client-c/src/pd/Client.cc b/contrib/client-c/src/pd/Client.cc index 6853aee8a43..d479286a281 100644 --- a/contrib/client-c/src/pd/Client.cc +++ b/contrib/client-c/src/pd/Client.cc @@ -350,5 +350,31 @@ metapb::Store Client::getStore(uint64_t store_id) return response.store(); } +std::vector Client::getAllStores() +{ + pdpb::GetAllStoresRequest request{}; + pdpb::GetAllStoresResponse response{}; + + request.set_allocated_header(requestHeader()); + + grpc::ClientContext context; + + context.set_deadline(std::chrono::system_clock::now() + pd_timeout); + + auto status = leaderStub()->GetAllStores(&context, request, &response); + if (!status.ok()) + { + std::string err_msg = ("get all stores failed: " + std::to_string(status.error_code()) + ": " + status.error_message()); + log->error(err_msg); + check_leader.store(true); + throw Exception(err_msg, GRPCErrorCode); + } + std::vector stores; + int size = response.stores_size(); + for (int i = 0; i < size; i++) + stores.push_back(response.stores(i)); + return stores; +} + } // namespace pd } // namespace pingcap