Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat etcd: Init etcd client #837

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ option(USERVER_FEATURE_MYSQL "Provide asynchronous driver for MariaDB/MySQL" "${
option(USERVER_FEATURE_ROCKS "Provide asynchronous driver for Rocks" "${USERVER_LIB_ENABLED_DEFAULT}")
option(USERVER_FEATURE_YDB "Provide asynchronous driver for YDB" "${USERVER_YDB_DEFAULT}")
option(USERVER_FEATURE_OTLP "Provide asynchronous OTLP exporters" "${USERVER_LIB_ENABLED_DEFAULT}")
option(USERVER_FEATURE_ETCD "Provide asynchronous driver for etcd" "${USERVER_LIB_ENABLED_DEFAULT}")

set(CMAKE_DEBUG_POSTFIX d)

Expand Down Expand Up @@ -277,6 +278,11 @@ if (USERVER_FEATURE_YDB)
list(APPEND USERVER_AVAILABLE_COMPONENTS ydb)
endif()

if (USERVER_FEATURE_ETCD)
_require_userver_core("USERVER_FEATURE_ETCD")
add_subdirectory(etcd)
endif()

add_subdirectory(libraries)

if (USERVER_BUILD_TESTS)
Expand Down
11 changes: 11 additions & 0 deletions cmake/install/userver-etcd-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include_guard(GLOBAL)

if(userver_etcd_FOUND)
return()
endif()

find_package(userver REQUIRED COMPONENTS
core
)

set(userver_etcd_FOUND TRUE)
6 changes: 6 additions & 0 deletions etcd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project(userver-etcd CXX)

userver_module(etcd
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
)
39 changes: 39 additions & 0 deletions etcd/include/userver/etcd/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include <userver/clients/http/component.hpp>
#include <userver/concurrent/queue.hpp>
#include <userver/engine/task/task_with_result.hpp>
#include <userver/etcd/settings.hpp>
#include <userver/etcd/watch_listener.hpp>
#include <userver/yaml_config/fwd.hpp>

USERVER_NAMESPACE_BEGIN

namespace etcd {

class Client {
public:
virtual ~Client() = default;

virtual void Put(const std::string& key, const std::string& value) = 0;

[[nodiscard]] virtual std::optional<std::string> Get(const std::string& key) = 0;

[[nodiscard]] virtual std::vector<std::string> Range(const std::string& key) = 0;

virtual void Delete(const std::string& key) = 0;

virtual WatchListener StartWatch(const std::string& key) = 0;
};

using ClientPtr = std::shared_ptr<Client>;

} // namespace etcd

USERVER_NAMESPACE_END
28 changes: 28 additions & 0 deletions etcd/include/userver/etcd/component.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <userver/components/component_base.hpp>
#include <userver/components/component_config.hpp>
#include <userver/components/component_context.hpp>
#include <userver/etcd/client.hpp>

USERVER_NAMESPACE_BEGIN

namespace etcd {

class Component final : public components::ComponentBase {
public:
static constexpr std::string_view kName = "etcd-сlient";

Component(const components::ComponentConfig&, const components::ComponentContext&);

static yaml_config::Schema GetStaticConfigSchema();

ClientPtr GetClient();

private:
const ClientPtr etcd_client_ptr_;
};

} // namespace etcd

USERVER_NAMESPACE_END
19 changes: 19 additions & 0 deletions etcd/include/userver/etcd/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <string>

#include <stdexcept>

USERVER_NAMESPACE_BEGIN

namespace etcd {

/// @brief Base class for all etcd client exceptions
class EtcdError : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};

} // namespace etcd

USERVER_NAMESPACE_END
27 changes: 27 additions & 0 deletions etcd/include/userver/etcd/settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <chrono>
#include <string>
#include <vector>

#include <userver/yaml_config/yaml_config.hpp>

USERVER_NAMESPACE_BEGIN

namespace etcd {

struct ClientSettings final {
const std::vector<std::string> endpoints;
const std::uint32_t attempts;
const std::chrono::microseconds request_timeout_ms;
};

} // namespace etcd

namespace formats::parse {

etcd::ClientSettings Parse(const yaml_config::YamlConfig& value, To<etcd::ClientSettings>);

}

USERVER_NAMESPACE_END
32 changes: 32 additions & 0 deletions etcd/include/userver/etcd/watch_listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <string>

#include <userver/concurrent/queue.hpp>
#include <userver/formats/json/value.hpp>

USERVER_NAMESPACE_BEGIN

namespace etcd {

struct KeyValueEvent final {
std::string key;
std::string value;
std::int32_t version;
};

struct WatchListener final {
concurrent::SpscQueue<KeyValueEvent>::Consumer consumer;

KeyValueEvent GetEvent();
};

} // namespace etcd

namespace formats::parse {

etcd::KeyValueEvent Parse(const formats::json::Value& value, To<etcd::KeyValueEvent>);

}

USERVER_NAMESPACE_END
9 changes: 9 additions & 0 deletions etcd/library.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project-name: userver-etcd
project-alt-names:
- yandex-userver-etcd
maintainers:
- Common components
description: Etcd driver

libraries:
- userver-core
Loading