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