retry_policy.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_COMMON_RETRY_POLICY_H_
  19. #define LIB_COMMON_RETRY_POLICY_H_
  20. #include "common/util.h"
  21. #include <string>
  22. #include <stdint.h>
  23. namespace hdfs {
  24. class RetryAction {
  25. public:
  26. enum RetryDecision { FAIL, RETRY, FAILOVER_AND_RETRY };
  27. RetryDecision action;
  28. uint64_t delayMillis;
  29. std::string reason;
  30. RetryAction(RetryDecision in_action, uint64_t in_delayMillis,
  31. const std::string &in_reason)
  32. : action(in_action), delayMillis(in_delayMillis), reason(in_reason) {}
  33. static RetryAction fail(const std::string &reason) {
  34. return RetryAction(FAIL, 0, reason);
  35. }
  36. static RetryAction retry(uint64_t delay) {
  37. return RetryAction(RETRY, delay, "");
  38. }
  39. static RetryAction failover() {
  40. return RetryAction(FAILOVER_AND_RETRY, 0, "");
  41. }
  42. };
  43. class RetryPolicy {
  44. public:
  45. /*
  46. * If there was an error in communications, responds with the configured
  47. * action to take.
  48. */
  49. virtual RetryAction ShouldRetry(const Status &s, uint64_t retries,
  50. uint64_t failovers,
  51. bool isIdempotentOrAtMostOnce) const = 0;
  52. virtual ~RetryPolicy() {}
  53. };
  54. /*
  55. * Returns a fixed delay up to a certain number of retries
  56. */
  57. class FixedDelayRetryPolicy : public RetryPolicy {
  58. public:
  59. FixedDelayRetryPolicy(uint64_t delay, uint64_t max_retries)
  60. : delay_(delay), max_retries_(max_retries) {}
  61. RetryAction ShouldRetry(const Status &s, uint64_t retries,
  62. uint64_t failovers,
  63. bool isIdempotentOrAtMostOnce) const override;
  64. private:
  65. uint64_t delay_;
  66. uint64_t max_retries_;
  67. };
  68. /*
  69. * Never retries
  70. */
  71. class NoRetryPolicy : public RetryPolicy {
  72. public:
  73. RetryAction ShouldRetry(const Status &s, uint64_t retries,
  74. uint64_t failovers,
  75. bool isIdempotentOrAtMostOnce) const override;
  76. };
  77. }
  78. #endif