retry_request.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "retry_lib.h"
  3. #include "wait_proxy.h"
  4. #include <yt/cpp/mapreduce/interface/errors.h>
  5. #include <yt/cpp/mapreduce/interface/fwd.h>
  6. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  7. #include <util/generic/guid.h>
  8. namespace NYT::NDetail {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <typename TResult>
  11. TResult RequestWithRetry(
  12. IRequestRetryPolicyPtr retryPolicy,
  13. std::function<TResult(TMutationId&)> func)
  14. {
  15. bool useSameMutationId = false;
  16. TMutationId mutationId;
  17. while (true) {
  18. try {
  19. retryPolicy->NotifyNewAttempt();
  20. if constexpr (std::is_same_v<TResult, void>) {
  21. func(mutationId);
  22. return;
  23. } else {
  24. return func(mutationId);
  25. }
  26. } catch (const TErrorResponse& e) {
  27. YT_LOG_ERROR("Retry failed %v - %v",
  28. e.GetError().GetMessage(),
  29. retryPolicy->GetAttemptDescription());
  30. useSameMutationId = e.IsTransportError();
  31. if (!IsRetriable(e)) {
  32. throw;
  33. }
  34. auto maybeRetryTimeout = retryPolicy->OnRetriableError(e);
  35. if (maybeRetryTimeout) {
  36. TWaitProxy::Get()->Sleep(*maybeRetryTimeout);
  37. } else {
  38. throw;
  39. }
  40. } catch (const std::exception& e) {
  41. YT_LOG_ERROR("Retry failed %v - %v",
  42. e.what(),
  43. retryPolicy->GetAttemptDescription());
  44. useSameMutationId = true;
  45. if (!IsRetriable(e)) {
  46. throw;
  47. }
  48. auto maybeRetryTimeout = retryPolicy->OnGenericError(e);
  49. if (maybeRetryTimeout) {
  50. TWaitProxy::Get()->Sleep(*maybeRetryTimeout);
  51. } else {
  52. throw;
  53. }
  54. }
  55. if (!useSameMutationId) {
  56. mutationId = {};
  57. }
  58. }
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. } // namespace NYT::NDetail