raw_batch_request_ut.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <yt/cpp/mapreduce/raw_client/raw_batch_request.h>
  2. #include <yt/cpp/mapreduce/http/context.h>
  3. #include <yt/cpp/mapreduce/interface/client_method_options.h>
  4. #include <yt/cpp/mapreduce/interface/errors.h>
  5. #include <yt/cpp/mapreduce/common/retry_lib.h>
  6. #include <library/cpp/testing/gtest/gtest.h>
  7. using namespace NYT;
  8. using namespace NYT::NDetail;
  9. using namespace NYT::NDetail::NRawClient;
  10. class TTestRetryPolicy
  11. : public IRequestRetryPolicy
  12. {
  13. private:
  14. static constexpr int RetriableCode = 904;
  15. public:
  16. void NotifyNewAttempt() override
  17. { }
  18. TMaybe<TDuration> OnGenericError(const std::exception& /*e*/) override
  19. {
  20. return TDuration::Seconds(42);
  21. }
  22. void OnIgnoredError(const TErrorResponse& /*e*/) override
  23. { }
  24. TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) override
  25. {
  26. if (e.GetError().GetCode() == RetriableCode) {
  27. return TDuration::Seconds(e.GetError().GetAttributes().at("retry_interval").AsUint64());
  28. } else {
  29. return Nothing();
  30. }
  31. }
  32. TString GetAttemptDescription() const override
  33. {
  34. return "attempt";
  35. }
  36. static TNode GenerateRetriableError(TDuration retryDuration)
  37. {
  38. Y_ABORT_UNLESS(retryDuration - TDuration::Seconds(retryDuration.Seconds()) == TDuration::Zero());
  39. return TNode()
  40. ("code", RetriableCode)
  41. ("attributes",
  42. TNode()
  43. ("retry_interval", retryDuration.Seconds()));
  44. }
  45. };
  46. TString GetPathFromRequest(const TNode& params)
  47. {
  48. return params.AsMap().at("parameters").AsMap().at("path").AsString();
  49. }
  50. TVector<TString> GetAllPathsFromRequestList(const TNode& requestList)
  51. {
  52. TVector<TString> result;
  53. for (const auto& request : requestList.AsList()) {
  54. result.push_back(GetPathFromRequest(request)); }
  55. return result;
  56. }
  57. TEST(TBatchRequestImplTest, ParseResponse) {
  58. TClientContext context;
  59. TRawBatchRequest batchRequest(context.Config);
  60. EXPECT_EQ(batchRequest.BatchSize(), 0u);
  61. auto get1 = batchRequest.Get(
  62. TTransactionId(),
  63. "//getOk",
  64. TGetOptions());
  65. auto get2 = batchRequest.Get(
  66. TTransactionId(),
  67. "//getError-3",
  68. TGetOptions());
  69. auto get3 = batchRequest.Get(
  70. TTransactionId(),
  71. "//getError-5",
  72. TGetOptions());
  73. EXPECT_EQ(batchRequest.BatchSize(), 3u);
  74. auto testRetryPolicy = MakeIntrusive<TTestRetryPolicy>();
  75. const TInstant now = TInstant::Seconds(100500);
  76. TRawBatchRequest retryBatch(context.Config);
  77. batchRequest.ParseResponse(
  78. TNode()
  79. .Add(TNode()("output", 5))
  80. .Add(TNode()("error",
  81. TTestRetryPolicy::GenerateRetriableError(TDuration::Seconds(3))))
  82. .Add(TNode()("error",
  83. TTestRetryPolicy::GenerateRetriableError(TDuration::Seconds(5)))),
  84. "<no-request-id>",
  85. testRetryPolicy,
  86. &retryBatch,
  87. now);
  88. EXPECT_EQ(batchRequest.BatchSize(), 0u);
  89. EXPECT_EQ(retryBatch.BatchSize(), 2u);
  90. TNode retryParameterList;
  91. TInstant nextTry;
  92. retryBatch.FillParameterList(3, &retryParameterList, &nextTry);
  93. EXPECT_EQ(
  94. GetAllPathsFromRequestList(retryParameterList),
  95. TVector<TString>({"//getError-3", "//getError-5"}));
  96. EXPECT_EQ(nextTry, now + TDuration::Seconds(5));
  97. }