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
31
32 /// Get session state
33 NYdb::NCoordination::ESessionState GetSessionState();
34
35 /// Get connection state
36 NYdb::NCoordination::EConnectionState GetConnectionState();
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
58 NYdb::NCoordination::TSemaphoreDescription DescribeSemaphore(
59 std::string_view name,
60 const NYdb::NCoordination::TDescribeSemaphoreSettings& settings
61 );
62
63 /// Create semaphore
64 void CreateSemaphore(std::string_view name, std::uint64_t limit);
65
66 /// Update semaphore
67 void UpdateSemaphore(std::string_view name, std::string_view data);
68
69 /// Delete semaphore
70 void DeleteSemaphore(std::string_view name);
71
72private:
73 NYdb::NCoordination::TSession session_;
74};
75
76/// @ingroup userver_clients
77///
78/// @brief YDB Coordination Client
79///
80/// Provides access to work with Coordination Service
81/// @see https://ydb.tech/docs/ru/reference/ydb-sdk/coordination
82class CoordinationClient final {
83public:
84 /// @cond
85 // For internal use only.
86 explicit CoordinationClient(std::shared_ptr<impl::Driver> driver);
87 /// @endcond
88
89 /// Start session
90 /// @warning Use `TSessionSettings::OnStateChanged` and
91 /// `TSessionSettings::OnStopped` callbacks with care, they will be executed
92 /// on a non-coroutine thread
93 CoordinationSession StartSession(std::string_view path, const NYdb::NCoordination::TSessionSettings& settings);
94
95 /// Create coordination node
96 void CreateNode(std::string_view path, const NYdb::NCoordination::TCreateNodeSettings& settings);
97
98 /// Alter coordination node
99 void AlterNode(std::string_view path, const NYdb::NCoordination::TAlterNodeSettings& settings);
100
101 /// Drop coordination node
102 void DropNode(std::string_view path);
103
104 /// Describe coordination node
105 NYdb::NCoordination::TNodeDescription DescribeNode(std::string_view path);
106
107 /// Get native coordination client
108 /// @warning Use with care! Facilities from
109 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
110 /// non-blocking wait operations.
111 NYdb::NCoordination::TClient& GetNativeCoordinationClient();
112
113private:
114 std::shared_ptr<impl::Driver> driver_;
115 NYdb::NCoordination::TClient client_;
116};
117
118} // namespace ydb
119
120USERVER_NAMESPACE_END