retry_lib.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. virtual IRequestRetryPolicyPtr CreatePolicyForReaderRequest() = 0;
  41. };
  42. ////////////////////////////////////////////////////////////////////////////////
  43. class TAttemptLimitedRetryPolicy
  44. : public IRequestRetryPolicy
  45. {
  46. public:
  47. explicit TAttemptLimitedRetryPolicy(ui32 attemptLimit, const TConfigPtr& config);
  48. void NotifyNewAttempt() override;
  49. TMaybe<TDuration> OnGenericError(const std::exception& e) override;
  50. TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) override;
  51. void OnIgnoredError(const TErrorResponse& e) override;
  52. TString GetAttemptDescription() const override;
  53. bool IsAttemptLimitExceeded() const;
  54. protected:
  55. const TConfigPtr Config_;
  56. private:
  57. const ui32 AttemptLimit_;
  58. ui32 Attempt_ = 0;
  59. };
  60. ////////////////////////////////////////////////////////////////////////////////
  61. IRequestRetryPolicyPtr CreateDefaultRequestRetryPolicy(const TConfigPtr& config);
  62. IClientRetryPolicyPtr CreateDefaultClientRetryPolicy(IRetryConfigProviderPtr retryConfigProvider, const TConfigPtr& config);
  63. IRetryConfigProviderPtr CreateDefaultRetryConfigProvider();
  64. ////////////////////////////////////////////////////////////////////////////////
  65. // Check if error returned by YT can be retried
  66. bool IsRetriable(const TErrorResponse& errorResponse);
  67. bool IsRetriable(const std::exception& ex);
  68. // Get backoff duration for errors returned by YT.
  69. TDuration GetBackoffDuration(const TErrorResponse& errorResponse, const TConfigPtr& config);
  70. // Get backoff duration for errors that are not TErrorResponse.
  71. TDuration GetBackoffDuration(const std::exception& error, const TConfigPtr& config);
  72. TDuration GetBackoffDuration(const TConfigPtr& config);
  73. ////////////////////////////////////////////////////////////////////////////////
  74. } // namespace NYT