retry_heavy_write_request.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. header.AddTransactionId(attemptTx.GetId(), /* overwrite = */ true);
  41. header.SetRequestCompression(ToString(context.Config->ContentEncoding));
  42. auto request = context.HttpClient->StartRequest(GetFullUrl(hostName, context, header), requestId, header);
  43. TransferData(input.Get(), request->GetStream());
  44. request->Finish()->GetResponse();
  45. } catch (TErrorResponse& e) {
  46. YT_LOG_ERROR("RSP %v - attempt %v failed",
  47. requestId,
  48. attempt);
  49. if (!IsRetriable(e) || attempt + 1 == retryCount) {
  50. throw;
  51. }
  52. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  53. continue;
  54. } catch (std::exception& e) {
  55. YT_LOG_ERROR("RSP %v - %v - attempt %v failed",
  56. requestId,
  57. e.what(),
  58. attempt);
  59. if (attempt + 1 == retryCount) {
  60. throw;
  61. }
  62. NDetail::TWaitProxy::Get()->Sleep(GetBackoffDuration(e, context.Config));
  63. continue;
  64. }
  65. attemptTx.Commit();
  66. return;
  67. }
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. } // namespace NYT