From e80709418bf4b8677c91ccea5e323eb6122c662b Mon Sep 17 00:00:00 2001 From: Tao He Date: Thu, 27 May 2021 22:10:24 +0800 Subject: [PATCH] Avoid including protobuf & grpc headers in our interface files. Signed-off-by: Tao He --- CMakeLists.txt | 7 ++---- etcd/Client.hpp | 5 +++- etcd/Response.hpp | 5 ++-- etcd/Value.hpp | 45 ++++++++++++++++++++++++++++++++++ src/Response.cpp | 7 +++--- src/Value.cpp | 59 +++++++++++++++++++++++++++++++++++++++++---- tst/CMakeLists.txt | 6 ++++- tst/WatcherTest.cpp | 8 ++++++ 8 files changed, 124 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 35318a8..ae5f1ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,12 +88,9 @@ endif() set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) +enable_testing() add_subdirectory(src) - -if (BUILD_ETCD_TESTS) - enable_testing() - add_subdirectory(tst) -endif () +add_subdirectory(tst) install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/etcd/Client.hpp ${CMAKE_CURRENT_SOURCE_DIR}/etcd/KeepAlive.hpp diff --git a/etcd/Client.hpp b/etcd/Client.hpp index 4879ba7..8222bff 100644 --- a/etcd/Client.hpp +++ b/etcd/Client.hpp @@ -5,7 +5,6 @@ #include #include -#include #include "pplx/pplxtasks.h" #include "etcd/Response.hpp" @@ -18,6 +17,10 @@ namespace etcdv3 { } } +namespace grpc { + class Channel; +} + namespace etcd { class KeepAlive; diff --git a/etcd/Response.hpp b/etcd/Response.hpp index a18a3bd..6095bed 100644 --- a/etcd/Response.hpp +++ b/etcd/Response.hpp @@ -8,7 +8,6 @@ #include "pplx/pplxtasks.h" #include "etcd/Value.hpp" -#include "kv.pb.h" namespace etcdv3 { class AsyncWatchAction; @@ -138,7 +137,7 @@ namespace etcd /** * Returns the watched events. */ - std::vector const & events() const; + std::vector const & events() const; /** * Returns the duration of request execution in microseconds. @@ -158,7 +157,7 @@ namespace etcd Values _values; Keys _keys; std::string _lock_key; // for lock - std::vector _events; // for watch + std::vector _events; // for watch std::chrono::microseconds _duration; // execute duration (in microseconds), during the action created and response parsed friend class SyncClient; friend class etcdv3::AsyncWatchAction; diff --git a/etcd/Value.hpp b/etcd/Value.hpp index 0d0bf66..6437b5a 100644 --- a/etcd/Value.hpp +++ b/etcd/Value.hpp @@ -8,8 +8,17 @@ namespace etcdv3 { class KeyValue; } +namespace mvccpb { + class KeyValue; + class Event; +} + namespace etcd { + class Value; + class Event; + class Response; + /** * Represents a value object received from the etcd server */ @@ -54,8 +63,12 @@ namespace etcd friend class BaseResponse; //deliberately done since Value class will be removed during full V3 friend class DeleteRpcResponse; friend class AsyncDeleteResponse; + + friend class Event; + Value(); Value(etcdv3::KeyValue const & kvs); + Value(mvccpb::KeyValue const & kvs); std::string _key; bool dir; std::string value; @@ -66,6 +79,38 @@ namespace etcd }; typedef std::vector Values; + + class Event + { + public: + enum class EventType { + PUT, + DELETE_, + INVALID, + }; + + enum EventType event_type() const; + + bool has_kv() const; + + bool has_prev_kv() const; + + const Value &kv() const; + + const Value &prev_kv() const; + + protected: + friend class Response; + + Event(mvccpb::Event const & event); + + private: + enum EventType event_type_; + Value _kv, _prev_kv; + bool _has_kv, _has_prev_kv; + }; + + typedef std::vector Events; } #endif diff --git a/src/Response.cpp b/src/Response.cpp index 57ac8b8..3052449 100644 --- a/src/Response.cpp +++ b/src/Response.cpp @@ -3,7 +3,6 @@ #include - etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseconds const& duration) { _index = reply.get_index(); @@ -27,7 +26,9 @@ etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseco _prev_value = Value(reply.get_prev_value()); _lock_key = reply.get_lock_key(); - _events = reply.get_events(); + for (auto const &ev: reply.get_events()) { + _events.emplace_back(etcd::Event(ev)); + } // duration _duration = duration; @@ -111,7 +112,7 @@ std::string const & etcd::Response::lock_key() const { return _lock_key; } -std::vector const & etcd::Response::events() const { +std::vector const & etcd::Response::events() const { return this->_events; } diff --git a/src/Value.cpp b/src/Value.cpp index 666a8c3..8e58b73 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -15,15 +15,26 @@ etcd::Value::Value() etcd::Value::Value(etcdv3::KeyValue const & kv) { - dir=false; - _key=kv.kvs.key(); - value=kv.kvs.value(); - created=kv.kvs.create_revision(); - modified=kv.kvs.mod_revision(); + dir = false; + _key = kv.kvs.key(); + value = kv.kvs.value(); + created = kv.kvs.create_revision(); + modified = kv.kvs.mod_revision(); leaseId = kv.kvs.lease(); _ttl = kv.get_ttl(); } +etcd::Value::Value(mvccpb::KeyValue const & kv) +{ + dir = false; + _key = kv.key(); + value = kv.value(); + created = kv.create_revision(); + modified = kv.mod_revision(); + leaseId = kv.lease(); + _ttl = -1; +} + std::string const & etcd::Value::key() const { return _key; @@ -58,3 +69,41 @@ int64_t etcd::Value::lease() const { return leaseId; } + +etcd::Event::Event(mvccpb::Event const & event) { + _has_kv = event.has_kv(); + _has_prev_kv = event.has_prev_kv(); + if (_has_kv) { + _kv = Value(event.kv()); + } + if (_has_prev_kv) { + _prev_kv = Value(event.prev_kv()); + } + if (event.type() == mvccpb::Event::PUT) { + event_type_ = EventType::PUT; + } else if (event.type() == mvccpb::Event::DELETE_) { + event_type_ = EventType::DELETE_; + } else { + event_type_ = EventType::INVALID; + } +} + +enum etcd::Event::EventType etcd::Event::event_type() const { + return event_type_; +} + +bool etcd::Event::has_kv() const { + return _has_kv; +} + +bool etcd::Event::has_prev_kv() const { + return _has_prev_kv; +} + +const etcd::Value &etcd::Event::kv() const { + return _kv; +} + +const etcd::Value &etcd::Event::prev_kv() const { + return _prev_kv; +} diff --git a/tst/CMakeLists.txt b/tst/CMakeLists.txt index 301302c..aa7344b 100644 --- a/tst/CMakeLists.txt +++ b/tst/CMakeLists.txt @@ -10,7 +10,11 @@ foreach(testfile ${TEST_FILES}) string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${testfile}) set(test_name ${CMAKE_MATCH_1}) message(STATUS "Found unit_test - " ${test_name}) - add_executable(${test_name} ${CMAKE_CURRENT_SOURCE_DIR}/${testfile}) + if(BUILD_ETCD_TESTS) + add_executable(${test_name} ${CMAKE_CURRENT_SOURCE_DIR}/${testfile}) + else() + add_executable(${test_name} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/${testfile}) + endif() add_test(NAME ${test_name} COMMAND $) set_property(TARGET ${test_name} PROPERTY CXX_STANDARD 11) diff --git a/tst/WatcherTest.cpp b/tst/WatcherTest.cpp index c57d4b6..6f74fd5 100644 --- a/tst/WatcherTest.cpp +++ b/tst/WatcherTest.cpp @@ -21,6 +21,14 @@ void printResponse(etcd::Response const & resp) { std::cout << resp.action() << " " << resp.value().as_string() << std::endl; std::cout << "Previous value: " << resp.prev_value().as_string() << std::endl; + + std::cout << "Events size: " << resp.events().size() << std::endl; + for (auto const &ev: resp.events()) { + std::cout << "Value change in events: " << static_cast(ev.event_type()) + << ", prev kv = " << ev.prev_kv().key() << " -> " << ev.prev_kv().as_string() + << ", kv = " << ev.kv().key() << " -> " << ev.kv().as_string() + << std::endl; + } } }