userver: userver/ydb/coordination.hpp Source File
Loading...
Searching...
No Matches
coordination.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/coordination.hpp
4/// @brief YDB Coordination client
5
6#include <memory>
7#include <string_view>
8
9#include <ydb-cpp-sdk/client/coordination/coordination.h>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace ydb {
14
15namespace impl {
16class Driver;
17} // namespace impl
18
19/// @brief Coordination Session
20///
21/// @see https://ydb.tech/docs/ru/reference/ydb-sdk/coordination#session
22class CoordinationSession final {
23public:
24 /// @cond
25 // For internal use only.
26 explicit CoordinationSession(NYdb::NCoordination::TSession&& session);
27 /// @endcond
28
29 /// Get session id
30 std::uint64_t GetSessionId();
31
32 /// Get session state
34
35 /// Get connection state
37
38 /// Close session
39 void Close();
40
41 /// Ping
42 void Ping();
43
44 /// Reconnect session
45 void Reconnect();
46
47 /// Acquire semaphore
48 /// @warning Use `TAcquireSemaphoreSettings::OnAccepted` callback with care,
49 /// it will be executed on a non-coroutine thread
50 bool AcquireSemaphore(std::string_view name, const NYdb::NCoordination::TAcquireSemaphoreSettings& settings);
51
52 /// Release semaphore
53 bool ReleaseSemaphore(std::string_view name);
54
55 /// Describe semaphore
56 /// @warning Use `TDescribeSemaphoreSettings::OnChanged` callback with care,
57 /// it will be executed on a non-coroutine thread
60
61 /// Create semaphore
62 void CreateSemaphore(std::string_view name, std::uint64_t limit);
63
64 /// Update semaphore
65 void UpdateSemaphore(std::string_view name, std::string_view data);
66
67 /// Delete semaphore
68 void DeleteSemaphore(std::string_view name);
69
70private:
71 NYdb::NCoordination::TSession session_;
72};
73
74/// @ingroup userver_clients
75///
76/// @brief YDB Coordination Client
77///
78/// Provides access to work with Coordination Service
79/// @see https://ydb.tech/docs/ru/reference/ydb-sdk/coordination
80class CoordinationClient final {
81public:
82 /// @cond
83 // For internal use only.
84 explicit CoordinationClient(std::shared_ptr<impl::Driver> driver);
85 /// @endcond
86
87 /// Start session
88 /// @warning Use `TSessionSettings::OnStateChanged` and
89 /// `TSessionSettings::OnStopped` callbacks with care, they will be executed
90 /// on a non-coroutine thread
91 CoordinationSession StartSession(std::string_view path, const NYdb::NCoordination::TSessionSettings& settings);
92
93 /// Create coordination node
94 void CreateNode(std::string_view path, const NYdb::NCoordination::TCreateNodeSettings& settings);
95
96 /// Alter coordination node
97 void AlterNode(std::string_view path, const NYdb::NCoordination::TAlterNodeSettings& settings);
98
99 /// Drop coordination node
100 void DropNode(std::string_view path);
101
102 /// Describe coordination node
104
105 /// Get native coordination client
106 /// @warning Use with care! Facilities from
107 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
108 /// non-blocking wait operations.
110
111private:
112 std::shared_ptr<impl::Driver> driver_;
113 NYdb::NCoordination::TClient client_;
114};
115
116} // namespace ydb
117
118USERVER_NAMESPACE_END