yt_poller.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "yt_poller.h"
  2. #include <yt/cpp/mapreduce/http_client/raw_batch_request.h>
  3. #include <yt/cpp/mapreduce/common/debug_metrics.h>
  4. #include <yt/cpp/mapreduce/common/retry_lib.h>
  5. #include <yt/cpp/mapreduce/common/wait_proxy.h>
  6. #include <yt/cpp/mapreduce/http/retry_request.h>
  7. #include <yt/cpp/mapreduce/interface/config.h>
  8. #include <yt/cpp/mapreduce/interface/raw_client.h>
  9. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  10. namespace NYT {
  11. namespace NDetail {
  12. using namespace NRawClient;
  13. ////////////////////////////////////////////////////////////////////////////////
  14. TYtPoller::TYtPoller(
  15. IRawClientPtr rawClient,
  16. const TConfigPtr& config,
  17. const IClientRetryPolicyPtr& retryPolicy)
  18. : RawClient_(std::move(rawClient))
  19. , Config_(config)
  20. , ClientRetryPolicy_(retryPolicy)
  21. , WaiterThread_(&TYtPoller::WatchLoopProc, this)
  22. {
  23. WaiterThread_.Start();
  24. }
  25. TYtPoller::~TYtPoller()
  26. {
  27. Stop();
  28. }
  29. void TYtPoller::Watch(IYtPollerItemPtr item)
  30. {
  31. auto g = Guard(Lock_);
  32. Pending_.emplace_back(std::move(item));
  33. HasData_.Signal();
  34. }
  35. void TYtPoller::Stop()
  36. {
  37. {
  38. auto g = Guard(Lock_);
  39. if (!IsRunning_) {
  40. return;
  41. }
  42. IsRunning_ = false;
  43. HasData_.Signal();
  44. }
  45. WaiterThread_.Join();
  46. }
  47. void TYtPoller::DiscardQueuedItems()
  48. {
  49. for (auto& item : Pending_) {
  50. item->OnItemDiscarded();
  51. }
  52. for (auto& item : InProgress_) {
  53. item->OnItemDiscarded();
  54. }
  55. }
  56. void TYtPoller::WatchLoop()
  57. {
  58. TInstant nextRequest = TInstant::Zero();
  59. while (true) {
  60. {
  61. auto g = Guard(Lock_);
  62. if (IsRunning_ && Pending_.empty() && InProgress_.empty()) {
  63. TWaitProxy::Get()->WaitCondVar(HasData_, Lock_);
  64. }
  65. if (!IsRunning_) {
  66. DiscardQueuedItems();
  67. return;
  68. }
  69. {
  70. auto ug = Unguard(Lock_); // allow adding new items into Pending_
  71. TWaitProxy::Get()->SleepUntil(nextRequest);
  72. nextRequest = TInstant::Now() + Config_->WaitLockPollInterval;
  73. }
  74. if (!Pending_.empty()) {
  75. InProgress_.splice(InProgress_.end(), Pending_);
  76. }
  77. Y_ABORT_UNLESS(!InProgress_.empty());
  78. }
  79. auto rawBatchRequest = RawClient_->CreateRawBatchRequest();
  80. for (auto& item : InProgress_) {
  81. item->PrepareRequest(rawBatchRequest.Get());
  82. }
  83. try {
  84. rawBatchRequest->ExecuteBatch();
  85. } catch (const std::exception& ex) {
  86. YT_LOG_ERROR("Exception while executing batch request: %v", ex.what());
  87. }
  88. for (auto it = InProgress_.begin(); it != InProgress_.end();) {
  89. auto& item = *it;
  90. IYtPollerItem::EStatus status = item->OnRequestExecuted();
  91. if (status == IYtPollerItem::PollBreak) {
  92. it = InProgress_.erase(it);
  93. } else {
  94. ++it;
  95. }
  96. }
  97. IncDebugMetric(TStringBuf("yt_poller_top_loop_repeat_count"));
  98. }
  99. }
  100. void* TYtPoller::WatchLoopProc(void* data)
  101. {
  102. static_cast<TYtPoller*>(data)->WatchLoop();
  103. return nullptr;
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////
  106. } // namespace NDetail
  107. } // namespace NYT