retry_heavy_write_request.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "retry_heavy_write_request.h"
  2. #include "transaction.h"
  3. #include "transaction_pinger.h"
  4. #include <yt/cpp/mapreduce/common/retry_lib.h>
  5. #include <yt/cpp/mapreduce/common/wait_proxy.h>
  6. #include <yt/cpp/mapreduce/interface/config.h>
  7. #include <yt/cpp/mapreduce/interface/tvm.h>
  8. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  9. #include <yt/cpp/mapreduce/http/helpers.h>
  10. #include <yt/cpp/mapreduce/http/http_client.h>
  11. #include <yt/cpp/mapreduce/http/requests.h>
  12. #include <yt/cpp/mapreduce/http/retry_request.h>
  13. #include <util/stream/null.h>
  14. namespace NYT {
  15. using ::ToString;
  16. ////////////////////////////////////////////////////////////////////////////////
  17. void RetryHeavyWriteRequest(
  18. const IClientRetryPolicyPtr& clientRetryPolicy,
  19. const ITransactionPingerPtr& transactionPinger,
  20. const TClientContext& context,
  21. const TTransactionId& parentId,
  22. THttpHeader& header,
  23. std::function<THolder<IInputStream>()> streamMaker)
  24. {
  25. int retryCount = context.Config->RetryCount;
  26. if (context.ServiceTicketAuth) {
  27. header.SetServiceTicket(context.ServiceTicketAuth->Ptr->IssueServiceTicket());
  28. } else {
  29. header.SetToken(context.Token);
  30. }
  31. if (context.ImpersonationUser) {
  32. header.SetImpersonationUser(*context.ImpersonationUser);
  33. }
  34. for (int attempt = 0; attempt < retryCount; ++attempt) {
  35. TPingableTransaction attemptTx(clientRetryPolicy, context, parentId, transactionPinger->GetChildTxPinger(), TStartTransactionOptions());
  36. auto input = streamMaker();
  37. TString requestId;
  38. try {
  39. auto hostName = GetProxyForHeavyRequest(context);
  40. requestId = CreateGuidAsString();
  41. UpdateHeaderForProxyIfNeed(hostName, context, header);
  42. header.AddTransactionId(attemptTx.GetId(), /* overwrite = */ true);
  43. header.SetRequestCompression(ToString(context.Config->ContentEncoding));
  44. auto request = context.HttpClient->StartRequest(
  45. GetFullUrlForProxy(hostName, context, header),
  46. requestId,
  47. header);
  48. TransferData(input.Get(), request->GetStream());
  49. request->Finish()->GetResponse();
  50. } catch (TErrorResponse& e) {
  51. YT_LOG_ERROR("RSP %v - attempt %v failed",
  52. requestId,
  53. attempt);
  54. if (!IsRetriable(e) || attempt + 1 == retryCount) {
  55. throw;
  56. }
  57. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  58. continue;
  59. } catch (std::exception& e) {
  60. YT_LOG_ERROR("RSP %v - %v - attempt %v failed",
  61. requestId,
  62. e.what(),
  63. attempt);
  64. if (attempt + 1 == retryCount) {
  65. throw;
  66. }
  67. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  68. continue;
  69. }
  70. attemptTx.Commit();
  71. return;
  72. }
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. THeavyRequestRetrier::THeavyRequestRetrier(TParameters parameters)
  76. : Parameters_(std::move(parameters))
  77. , RequestRetryPolicy_(Parameters_.ClientRetryPolicy->CreatePolicyForGenericRequest())
  78. , StreamFactory_([] {
  79. return MakeHolder<TNullInput>();
  80. })
  81. {
  82. Retry([] { });
  83. }
  84. THeavyRequestRetrier::~THeavyRequestRetrier() = default;
  85. void THeavyRequestRetrier::Update(THeavyRequestRetrier::TStreamFactory streamFactory)
  86. {
  87. StreamFactory_ = streamFactory;
  88. Retry([this] {
  89. auto stream = StreamFactory_();
  90. stream->Skip(Attempt_->Offset);
  91. auto transfered = stream->ReadAll(*Attempt_->Request->GetStream());
  92. Attempt_->Offset += transfered;
  93. });
  94. }
  95. void THeavyRequestRetrier::Finish()
  96. {
  97. Retry([this] {
  98. Attempt_->Request->Finish()->GetResponse();
  99. Attempt_->Transaction->Commit();
  100. Attempt_.reset();
  101. });
  102. }
  103. void THeavyRequestRetrier::Retry(const std::function<void()> &function)
  104. {
  105. while (true) {
  106. try {
  107. if (!Attempt_) {
  108. TryStartAttempt();
  109. }
  110. function();
  111. return;
  112. } catch (const std::exception& ex) {
  113. YT_LOG_ERROR("RSP %v - attempt %v failed",
  114. Attempt_->RequestId,
  115. RequestRetryPolicy_->GetAttemptDescription());
  116. Attempt_.reset();
  117. TMaybe<TDuration> backoffDuration;
  118. if (const auto *errorResponse = dynamic_cast<const TErrorResponse *>(&ex)) {
  119. if (!IsRetriable(*errorResponse)) {
  120. throw;
  121. }
  122. backoffDuration = RequestRetryPolicy_->OnRetriableError(*errorResponse);
  123. } else {
  124. if (!IsRetriable(ex)) {
  125. throw;
  126. }
  127. backoffDuration = RequestRetryPolicy_->OnGenericError(ex);
  128. }
  129. if (!backoffDuration) {
  130. throw;
  131. }
  132. NDetail::TWaitProxy::Get()->Sleep(*backoffDuration);
  133. }
  134. }
  135. }
  136. void THeavyRequestRetrier::TryStartAttempt()
  137. {
  138. Attempt_ = std::make_unique<TAttempt>();
  139. Attempt_->Transaction = std::make_unique<TPingableTransaction>(
  140. Parameters_.ClientRetryPolicy, Parameters_.Context,
  141. Parameters_.TransactionId,
  142. Parameters_.TransactionPinger->GetChildTxPinger(),
  143. TStartTransactionOptions());
  144. auto header = Parameters_.Header;
  145. if (Parameters_.Context.ServiceTicketAuth) {
  146. header.SetServiceTicket(Parameters_.Context.ServiceTicketAuth->Ptr->IssueServiceTicket());
  147. } else {
  148. header.SetToken(Parameters_.Context.Token);
  149. }
  150. if (Parameters_.Context.ImpersonationUser) {
  151. header.SetImpersonationUser(*Parameters_.Context.ImpersonationUser);
  152. }
  153. auto hostName = GetProxyForHeavyRequest(Parameters_.Context);
  154. Attempt_->RequestId = CreateGuidAsString();
  155. UpdateHeaderForProxyIfNeed(hostName, Parameters_.Context, header);
  156. header.AddTransactionId(Attempt_->Transaction->GetId(), /* overwrite = */ true);
  157. header.SetRequestCompression(ToString(Parameters_.Context.Config->ContentEncoding));
  158. Attempt_->Request = Parameters_.Context.HttpClient->StartRequest(
  159. GetFullUrlForProxy(hostName, Parameters_.Context, header),
  160. Attempt_->RequestId, header);
  161. auto stream = StreamFactory_();
  162. stream->ReadAll(*Attempt_->Request->GetStream());
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. } // namespace NYT