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 /// @param data user-defined data attached to the semaphore
65 void CreateSemaphore(std::string_view name, std::uint64_t limit, std::string_view data = {});
66
67 /// Update semaphore
68 void UpdateSemaphore(std::string_view name, std::string_view data);
69
70 /// Semaphore deletion mode
71 enum class Mode {
72 kNormal, ///< Fail if the semaphore is currently acquired
73 kForce, ///< Delete even if currently acquired by sessions
74 };
75
76 /// Delete semaphore
77 /// @param mode deletion mode; use `Mode::kForce` to delete even if currently acquired
78 void DeleteSemaphore(std::string_view name, Mode mode = Mode::kNormal);
79
80private:
81 NYdb::NCoordination::TSession session_;
82};
83
84/// @ingroup userver_clients
85///
86/// @brief YDB Coordination Client
87///
88/// Provides access to work with Coordination Service
89/// @see https://ydb.tech/docs/ru/reference/ydb-sdk/coordination
90class CoordinationClient final {
91public:
92 /// @cond
93 // For internal use only.
94 explicit CoordinationClient(std::shared_ptr<impl::Driver> driver);
95 /// @endcond
96
97 /// Start session
98 /// @warning Use `TSessionSettings::OnStateChanged` and
99 /// `TSessionSettings::OnStopped` callbacks with care, they will be executed
100 /// on a non-coroutine thread
101 CoordinationSession StartSession(std::string_view path, const NYdb::NCoordination::TSessionSettings& settings);
102
103 /// Create coordination node
104 void CreateNode(std::string_view path, const NYdb::NCoordination::TCreateNodeSettings& settings);
105
106 /// Alter coordination node
107 void AlterNode(std::string_view path, const NYdb::NCoordination::TAlterNodeSettings& settings);
108
109 /// Drop coordination node
110 void DropNode(std::string_view path);
111
112 /// Describe coordination node
113 NYdb::NCoordination::TNodeDescription DescribeNode(std::string_view path);
114
115 /// Get native coordination client
116 /// @warning Use with care! Facilities from
117 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
118 /// non-blocking wait operations.
119 NYdb::NCoordination::TClient& GetNativeCoordinationClient();
120
121private:
122 std::shared_ptr<impl::Driver> driver_;
123 NYdb::NCoordination::TClient client_;
124};
125
126} // namespace ydb
127
128USERVER_NAMESPACE_END