retry_heavy_write_request.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace NYT {
  14. using ::ToString;
  15. ////////////////////////////////////////////////////////////////////////////////
  16. void RetryHeavyWriteRequest(
  17. const IClientRetryPolicyPtr& clientRetryPolicy,
  18. const ITransactionPingerPtr& transactionPinger,
  19. const TClientContext& context,
  20. const TTransactionId& parentId,
  21. THttpHeader& header,
  22. std::function<THolder<IInputStream>()> streamMaker)
  23. {
  24. int retryCount = context.Config->RetryCount;
  25. if (context.ServiceTicketAuth) {
  26. header.SetServiceTicket(context.ServiceTicketAuth->Ptr->IssueServiceTicket());
  27. } else {
  28. header.SetToken(context.Token);
  29. }
  30. if (context.ImpersonationUser) {
  31. header.SetImpersonationUser(*context.ImpersonationUser);
  32. }
  33. for (int attempt = 0; attempt < retryCount; ++attempt) {
  34. TPingableTransaction attemptTx(clientRetryPolicy, context, parentId, transactionPinger->GetChildTxPinger(), TStartTransactionOptions());
  35. auto input = streamMaker();
  36. TString requestId;
  37. try {
  38. auto hostName = GetProxyForHeavyRequest(context);
  39. requestId = CreateGuidAsString();
  40. UpdateHeaderForProxyIfNeed(hostName, context, header);
  41. header.AddTransactionId(attemptTx.GetId(), /* overwrite = */ true);
  42. header.SetRequestCompression(ToString(context.Config->ContentEncoding));
  43. auto request = context.HttpClient->StartRequest(GetFullUrl(hostName, context, header), requestId, header);
  44. TransferData(input.Get(), request->GetStream());
  45. request->Finish()->GetResponse();
  46. } catch (TErrorResponse& e) {
  47. YT_LOG_ERROR("RSP %v - attempt %v failed",
  48. requestId,
  49. attempt);
  50. if (!IsRetriable(e) || attempt + 1 == retryCount) {
  51. throw;
  52. }
  53. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  54. continue;
  55. } catch (std::exception& e) {
  56. YT_LOG_ERROR("RSP %v - %v - attempt %v failed",
  57. requestId,
  58. e.what(),
  59. attempt);
  60. if (attempt + 1 == retryCount) {
  61. throw;
  62. }
  63. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  64. continue;
  65. }
  66. attemptTx.Commit();
  67. return;
  68. }
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////
  71. } // namespace NYT