diff options
author | max42 <max42@yandex-team.com> | 2023-07-29 00:02:16 +0300 |
---|---|---|
committer | max42 <max42@yandex-team.com> | 2023-07-29 00:02:16 +0300 |
commit | 73b89de71748a21e102d27b9f3ed1bf658766cb5 (patch) | |
tree | 188bbd2d622fa91cdcbb1b6d6d77fbc84a0646f5 /yt/cpp/mapreduce/client/lock.cpp | |
parent | 528e321bcc2a2b67b53aeba58c3bd88305a141ee (diff) | |
download | ydb-73b89de71748a21e102d27b9f3ed1bf658766cb5.tar.gz |
YT-19210: expose YQL shared library for YT.
After this, a new target libyqlplugin.so appears. in open-source cmake build.
Diff in open-source YDB repo looks like the following: https://paste.yandex-team.ru/f302bdb4-7ef2-4362-91c7-6ca45f329264
Diffstat (limited to 'yt/cpp/mapreduce/client/lock.cpp')
-rw-r--r-- | yt/cpp/mapreduce/client/lock.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/yt/cpp/mapreduce/client/lock.cpp b/yt/cpp/mapreduce/client/lock.cpp new file mode 100644 index 0000000000..88110f9266 --- /dev/null +++ b/yt/cpp/mapreduce/client/lock.cpp @@ -0,0 +1,105 @@ +#include "lock.h" + +#include "yt_poller.h" + +#include <yt/cpp/mapreduce/http/retry_request.h> + +#include <yt/cpp/mapreduce/common/retry_lib.h> + +#include <yt/cpp/mapreduce/raw_client/raw_batch_request.h> + +#include <util/string/builder.h> + +namespace NYT { +namespace NDetail { + +using namespace NRawClient; + +//////////////////////////////////////////////////////////////////////////////// + +class TLockPollerItem + : public IYtPollerItem +{ +public: + TLockPollerItem(const TLockId& lockId, ::NThreading::TPromise<void> acquired) + : LockStateYPath_("#" + GetGuidAsString(lockId) + "/@state") + , Acquired_(acquired) + { } + + void PrepareRequest(TRawBatchRequest* batchRequest) override + { + LockState_ = batchRequest->Get(TTransactionId(), LockStateYPath_, TGetOptions()); + } + + EStatus OnRequestExecuted() override + { + try { + const auto& state = LockState_.GetValue().AsString(); + if (state == "acquired") { + Acquired_.SetValue(); + return PollBreak; + } + } catch (const TErrorResponse& e) { + if (!IsRetriable(e)) { + Acquired_.SetException(std::current_exception()); + return PollBreak; + } + } catch (const std::exception& e) { + if (!IsRetriable(e)) { + Acquired_.SetException(std::current_exception()); + return PollBreak; + } + } + return PollContinue; + } + + void OnItemDiscarded() override + { + Acquired_.SetException(std::make_exception_ptr(yexception() << "Operation cancelled")); + } + +private: + const TString LockStateYPath_; + ::NThreading::TPromise<void> Acquired_; + + ::NThreading::TFuture<TNode> LockState_; +}; + +//////////////////////////////////////////////////////////////////////////////// + +TLock::TLock(const TLockId& lockId, TClientPtr client, bool waitable) + : LockId_(lockId) + , Client_(std::move(client)) +{ + if (!waitable) { + Acquired_ = ::NThreading::MakeFuture(); + } +} + +const TLockId& TLock::GetId() const +{ + return LockId_; +} + +TNodeId TLock::GetLockedNodeId() const +{ + auto nodeIdNode = Client_->Get( + ::TStringBuilder() << '#' << GetGuidAsString(LockId_) << "/@node_id", + TGetOptions()); + return GetGuid(nodeIdNode.AsString()); +} + +const ::NThreading::TFuture<void>& TLock::GetAcquiredFuture() const +{ + if (!Acquired_) { + auto promise = ::NThreading::NewPromise<void>(); + Client_->GetYtPoller().Watch(::MakeIntrusive<TLockPollerItem>(LockId_, promise)); + Acquired_ = promise.GetFuture(); + } + return *Acquired_; +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NDetail +} // namespace NYT |