retry_request.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include "fwd.h"
  3. #include <yt/cpp/mapreduce/common/fwd.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/http_client.h>
  7. #include <yt/cpp/mapreduce/interface/errors.h>
  8. #include <yt/cpp/mapreduce/interface/fwd.h>
  9. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  10. #include <util/datetime/base.h>
  11. #include <util/generic/maybe.h>
  12. #include <util/generic/string.h>
  13. namespace NYT::NDetail {
  14. ////////////////////////////////////////////////////////////////////////////////
  15. struct TResponseInfo
  16. {
  17. TString RequestId;
  18. TString Response;
  19. int HttpCode = 0;
  20. };
  21. ////////////////////////////////////////////////////////////////////////////////
  22. struct TRequestConfig
  23. {
  24. NHttpClient::THttpConfig HttpConfig;
  25. bool IsHeavy = false;
  26. };
  27. ////////////////////////////////////////////////////////////////////////////////
  28. template <typename TResult>
  29. TResult RequestWithRetry(
  30. IRequestRetryPolicyPtr retryPolicy,
  31. std::function<TResult(TMutationId&)> func)
  32. {
  33. bool useSameMutationId = false;
  34. TMutationId mutationId;
  35. while (true) {
  36. try {
  37. retryPolicy->NotifyNewAttempt();
  38. if constexpr (std::is_same_v<TResult, void>) {
  39. func(mutationId);
  40. return;
  41. } else {
  42. return func(mutationId);
  43. }
  44. } catch (const TErrorResponse& e) {
  45. YT_LOG_ERROR("Retry failed %v - %v",
  46. e.GetError().GetMessage(),
  47. retryPolicy->GetAttemptDescription());
  48. useSameMutationId = e.IsTransportError();
  49. if (!IsRetriable(e)) {
  50. throw;
  51. }
  52. auto maybeRetryTimeout = retryPolicy->OnRetriableError(e);
  53. if (maybeRetryTimeout) {
  54. TWaitProxy::Get()->Sleep(*maybeRetryTimeout);
  55. } else {
  56. throw;
  57. }
  58. } catch (const std::exception& e) {
  59. YT_LOG_ERROR("Retry failed %v - %v",
  60. e.what(),
  61. retryPolicy->GetAttemptDescription());
  62. useSameMutationId = true;
  63. if (!IsRetriable(e)) {
  64. throw;
  65. }
  66. auto maybeRetryTimeout = retryPolicy->OnGenericError(e);
  67. if (maybeRetryTimeout) {
  68. TWaitProxy::Get()->Sleep(*maybeRetryTimeout);
  69. } else {
  70. throw;
  71. }
  72. }
  73. if (!useSameMutationId) {
  74. mutationId = {};
  75. }
  76. }
  77. }
  78. NHttpClient::IHttpResponsePtr RequestWithoutRetry(
  79. const TClientContext& context,
  80. TMutationId& mutationId,
  81. THttpHeader& header,
  82. TMaybe<TStringBuf> body = {},
  83. const TRequestConfig& config = {});
  84. ////////////////////////////////////////////////////////////////////////////////
  85. } // namespace NYT::NDetail