Skip to content

Commit f779f74

Browse files
Ilya-Repinapolukhin
authored andcommitted
feat ydb: extend ydb::CoordinationSession semaphore methods
Updated `ydb::CoordinationSession` semaphore methods (`CreateSemaphore`, `DeleteSemaphore`) 1) **CreateSemaphore**: Added `std::string_view data` argument (default is empty). Allows attaching [user-defined metadata](https://github.com/ydb-platform/ydb/blob/093a5c2d32357ce9957a4cf16c13cc36eb2b6c60/ydb/public/api/protos/ydb_coordination.proto#L264-L265) to the semaphore upon creation. [SDK method](https://github.com/ydb-platform/ydb-cpp-sdk/blob/e091a10085dff2c96f3c99641db83498e7fa2a09/include/ydb-cpp-sdk/client/coordination/coordination.h#L354-L355) 2) **DeleteSemaphore**: Added argument that enables [force deletion](https://github.com/ydb-platform/ydb/blob/093a5c2d32357ce9957a4cf16c13cc36eb2b6c60/ydb/public/api/protos/ydb_coordination.proto#L292-L293) of the semaphore. [SDK method](https://github.com/ydb-platform/ydb-cpp-sdk/blob/e091a10085dff2c96f3c99641db83498e7fa2a09/include/ydb-cpp-sdk/client/coordination/coordination.h#L360-L361) --- Pull Request resolved: #1114 Co-authored-by: antoshkka <antoshkka@userver.tech> commit_hash:a1f3a46cb7fa679dcda147f7e50f61c8cbf185e0
1 parent acafc42 commit f779f74

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

ydb/include/userver/ydb/coordination.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ class CoordinationSession final {
6161
);
6262

6363
/// Create semaphore
64-
void CreateSemaphore(std::string_view name, std::uint64_t limit);
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 = {});
6566

6667
/// Update semaphore
6768
void UpdateSemaphore(std::string_view name, std::string_view data);
6869

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+
6976
/// Delete semaphore
70-
void DeleteSemaphore(std::string_view name);
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);
7179

7280
private:
7381
NYdb::NCoordination::TSession session_;

ydb/src/ydb/coordination.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ NYdb::NCoordination::TSemaphoreDescription CoordinationSession::DescribeSemaphor
5656
return ExtractResult(session_.DescribeSemaphore(impl::ToString(name), settings), "DescribeSemaphore");
5757
}
5858

59-
void CoordinationSession::CreateSemaphore(std::string_view name, std::uint64_t limit) {
60-
ExtractResult(session_.CreateSemaphore(impl::ToString(name), limit), "CreateSemaphore");
59+
void CoordinationSession::CreateSemaphore(std::string_view name, std::uint64_t limit, std::string_view data) {
60+
ExtractResult(session_.CreateSemaphore(impl::ToString(name), limit, impl::ToString(data)), "CreateSemaphore");
6161
}
6262

6363
void CoordinationSession::UpdateSemaphore(std::string_view name, std::string_view data) {
6464
ExtractResult(session_.UpdateSemaphore(impl::ToString(name), impl::ToString(data)), "UpdateSemaphore");
6565
}
6666

67-
void CoordinationSession::DeleteSemaphore(std::string_view name) {
68-
ExtractResult(session_.DeleteSemaphore(impl::ToString(name)), "DeleteSemaphore");
67+
void CoordinationSession::DeleteSemaphore(std::string_view name, Mode mode) {
68+
ExtractResult(session_.DeleteSemaphore(impl::ToString(name), mode == Mode::kForce), "DeleteSemaphore");
6969
}
7070

7171
CoordinationClient::CoordinationClient(std::shared_ptr<impl::Driver> driver)

ydb/tests/coordination_test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ UTEST_F(YdbCoordinationFixture, CreateSemaphore) {
7373
UASSERT_THROW(session.DescribeSemaphore(kSemaphoreName, {}), ydb::YdbResponseError);
7474
}
7575

76+
UTEST_F(YdbCoordinationFixture, CreateSemaphoreWithData) {
77+
auto session = StartSession(kCoordinationNode);
78+
79+
constexpr std::string_view semaphore_data = "test_semaphore_data";
80+
UASSERT_NO_THROW(session.CreateSemaphore(kSemaphoreName, kSemaphoreLimit, semaphore_data));
81+
82+
NYdb::NCoordination::TSemaphoreDescription desc;
83+
UASSERT_NO_THROW(desc = session.DescribeSemaphore(kSemaphoreName, {}));
84+
EXPECT_EQ(kSemaphoreName, desc.GetName());
85+
EXPECT_EQ(semaphore_data, desc.GetData());
86+
EXPECT_EQ(kSemaphoreLimit, desc.GetLimit());
87+
88+
UASSERT_NO_THROW(session.DeleteSemaphore(kSemaphoreName));
89+
}
90+
91+
UTEST_F(YdbCoordinationFixture, DeleteSemaphoreForce) {
92+
auto session = StartSession(kCoordinationNode);
93+
UASSERT_NO_THROW(session.CreateSemaphore(kSemaphoreName, kSemaphoreLimit));
94+
95+
ASSERT_TRUE(session.AcquireSemaphore(
96+
kSemaphoreName,
97+
NYdb::NCoordination::TAcquireSemaphoreSettings{}.Count(kSemaphoreLimit)
98+
));
99+
100+
UASSERT_THROW(
101+
session.DeleteSemaphore(kSemaphoreName, ydb::CoordinationSession::Mode::kNormal),
102+
ydb::YdbResponseError
103+
);
104+
UASSERT_NO_THROW(session.DeleteSemaphore(kSemaphoreName, ydb::CoordinationSession::Mode::kForce));
105+
UASSERT_THROW(session.DescribeSemaphore(kSemaphoreName, {}), ydb::YdbResponseError);
106+
}
107+
76108
UTEST_F(YdbCoordinationFixture, UpdateSemaphore) {
77109
auto session = StartSession(kCoordinationNode);
78110
UASSERT_NO_THROW(session.CreateSemaphore(kSemaphoreName, kSemaphoreLimit));

0 commit comments

Comments
 (0)