retry_lib.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include "fwd.h"
  3. #include <yt/cpp/mapreduce/interface/fwd.h>
  4. #include <util/datetime/base.h>
  5. #include <util/generic/maybe.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/generic/string.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. // IRequestRetryPolicy class controls retries of single request.
  11. class IRequestRetryPolicy
  12. : public virtual TThrRefBase
  13. {
  14. public:
  15. // Helper function that returns text description of current attempt, e.g.
  16. // "attempt 3 / 10"
  17. // used in logs.
  18. virtual TString GetAttemptDescription() const = 0;
  19. // Library code calls this function before any request attempt.
  20. virtual void NotifyNewAttempt() = 0;
  21. // OnRetriableError is called whenever client gets YT error that can be retried (e.g. operation limit exceeded).
  22. // OnGenericError is called whenever request failed due to generic error like network error.
  23. //
  24. // Both methods must return nothing if policy doesn't want to retry this error.
  25. // Otherwise method should return backoff time.
  26. virtual TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) = 0;
  27. virtual TMaybe<TDuration> OnGenericError(const std::exception& e) = 0;
  28. // OnIgnoredError is called whenever client gets an error but is going to ignore it.
  29. virtual void OnIgnoredError(const TErrorResponse& /*e*/) = 0;
  30. };
  31. using IRequestRetryPolicyPtr = ::TIntrusivePtr<IRequestRetryPolicy>;
  32. ////////////////////////////////////////////////////////////////////////////////
  33. // IClientRetryPolicy controls creation of policies for individual requests.
  34. class IClientRetryPolicy
  35. : public virtual TThrRefBase
  36. {
  37. public:
  38. virtual IRequestRetryPolicyPtr CreatePolicyForGenericRequest() = 0;
  39. virtual IRequestRetryPolicyPtr CreatePolicyForStartOperationRequest() = 0;
  40. };
  41. ////////////////////////////////////////////////////////////////////////////////
  42. class TAttemptLimitedRetryPolicy
  43. : public IRequestRetryPolicy
  44. {
  45. public:
  46. explicit TAttemptLimitedRetryPolicy(ui32 attemptLimit, const TConfigPtr& config);
  47. void NotifyNewAttempt() override;
  48. TMaybe<TDuration> OnGenericError(const std::exception& e) override;
  49. TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) override;
  50. void OnIgnoredError(const TErrorResponse& e) override;
  51. TString GetAttemptDescription() const override;
  52. bool IsAttemptLimitExceeded() const;
  53. protected:
  54. const TConfigPtr Config_;
  55. private:
  56. const ui32 AttemptLimit_;
  57. ui32 Attempt_ = 0;
  58. };
  59. ////////////////////////////////////////////////////////////////////////////////
  60. IRequestRetryPolicyPtr CreateDefaultRequestRetryPolicy(const TConfigPtr& config);
  61. IClientRetryPolicyPtr CreateDefaultClientRetryPolicy(IRetryConfigProviderPtr retryConfigProvider, const TConfigPtr& config);
  62. IRetryConfigProviderPtr CreateDefaultRetryConfigProvider();
  63. ////////////////////////////////////////////////////////////////////////////////
  64. // Check if error returned by YT can be retried
  65. bool IsRetriable(const TErrorResponse& errorResponse);
  66. bool IsRetriable(const std::exception& ex);
  67. // Get backoff duration for errors returned by YT.
  68. TDuration GetBackoffDuration(const TErrorResponse& errorResponse, const TConfigPtr& config);
  69. // Get backoff duration for errors that are not TErrorResponse.
  70. TDuration GetBackoffDuration(const std::exception& error, const TConfigPtr& config);
  71. TDuration GetBackoffDuration(const TConfigPtr& config);
  72. ////////////////////////////////////////////////////////////////////////////////
  73. } // namespace NYT