retry_lb_drop.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Copyright 2017 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <grpc/byte_buffer.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include <grpc/support/string_util.h>
  23. #include <grpc/support/time.h>
  24. #include "src/core/ext/filters/client_channel/lb_policy_registry.h"
  25. #include "src/core/lib/channel/channel_args.h"
  26. #include "src/core/lib/gpr/string.h"
  27. #include "src/core/lib/gpr/useful.h"
  28. #include "src/core/lib/iomgr/exec_ctx.h"
  29. #include "test/core/end2end/cq_verifier.h"
  30. #include "test/core/end2end/end2end_tests.h"
  31. #include "test/core/end2end/tests/cancel_test_helpers.h"
  32. #include "test/core/util/test_lb_policies.h"
  33. namespace grpc_core {
  34. namespace {
  35. const char* kDropPolicyName = "drop_lb";
  36. class DropPolicy : public LoadBalancingPolicy {
  37. public:
  38. explicit DropPolicy(Args args) : LoadBalancingPolicy(std::move(args)) {}
  39. const char* name() const override { return kDropPolicyName; }
  40. void UpdateLocked(UpdateArgs) override {
  41. channel_control_helper()->UpdateState(GRPC_CHANNEL_READY, absl::Status(),
  42. absl::make_unique<DropPicker>());
  43. }
  44. void ResetBackoffLocked() override {}
  45. void ShutdownLocked() override {}
  46. private:
  47. class DropPicker : public SubchannelPicker {
  48. public:
  49. PickResult Pick(PickArgs /*args*/) override {
  50. return PickResult::Drop(
  51. absl::UnavailableError("Call dropped by drop LB policy"));
  52. }
  53. };
  54. };
  55. class DropLbConfig : public LoadBalancingPolicy::Config {
  56. public:
  57. const char* name() const override { return kDropPolicyName; }
  58. };
  59. class DropPolicyFactory : public LoadBalancingPolicyFactory {
  60. public:
  61. OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
  62. LoadBalancingPolicy::Args args) const override {
  63. return MakeOrphanable<DropPolicy>(std::move(args));
  64. }
  65. const char* name() const override { return kDropPolicyName; }
  66. RefCountedPtr<LoadBalancingPolicy::Config> ParseLoadBalancingConfig(
  67. const Json& /*json*/, grpc_error_handle* /*error*/) const override {
  68. return MakeRefCounted<DropLbConfig>();
  69. }
  70. };
  71. std::vector<PickArgsSeen>* g_pick_args_vector = nullptr;
  72. void RegisterDropPolicy() {
  73. LoadBalancingPolicyRegistry::Builder::RegisterLoadBalancingPolicyFactory(
  74. absl::make_unique<DropPolicyFactory>());
  75. RegisterTestPickArgsLoadBalancingPolicy(
  76. [](const PickArgsSeen& pick_args) {
  77. GPR_ASSERT(g_pick_args_vector != nullptr);
  78. g_pick_args_vector->push_back(pick_args);
  79. },
  80. kDropPolicyName);
  81. }
  82. } // namespace
  83. } // namespace grpc_core
  84. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  85. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  86. const char* test_name,
  87. grpc_channel_args* client_args,
  88. grpc_channel_args* server_args) {
  89. grpc_end2end_test_fixture f;
  90. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  91. f = config.create_fixture(client_args, server_args);
  92. config.init_server(&f, server_args);
  93. config.init_client(&f, client_args);
  94. return f;
  95. }
  96. static gpr_timespec n_seconds_from_now(int n) {
  97. return grpc_timeout_seconds_to_deadline(n);
  98. }
  99. static gpr_timespec five_seconds_from_now(void) {
  100. return n_seconds_from_now(5);
  101. }
  102. static void drain_cq(grpc_completion_queue* cq) {
  103. grpc_event ev;
  104. do {
  105. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  106. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  107. }
  108. static void shutdown_server(grpc_end2end_test_fixture* f) {
  109. if (!f->server) return;
  110. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  111. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  112. grpc_timeout_seconds_to_deadline(5),
  113. nullptr)
  114. .type == GRPC_OP_COMPLETE);
  115. grpc_server_destroy(f->server);
  116. f->server = nullptr;
  117. }
  118. static void shutdown_client(grpc_end2end_test_fixture* f) {
  119. if (!f->client) return;
  120. grpc_channel_destroy(f->client);
  121. f->client = nullptr;
  122. }
  123. static void end_test(grpc_end2end_test_fixture* f) {
  124. shutdown_server(f);
  125. shutdown_client(f);
  126. grpc_completion_queue_shutdown(f->cq);
  127. drain_cq(f->cq);
  128. grpc_completion_queue_destroy(f->cq);
  129. grpc_completion_queue_destroy(f->shutdown_cq);
  130. }
  131. // Tests that we don't retry when the LB policy drops a call,
  132. // even when there is retry configuration in the service config.
  133. // - 1 retry allowed for UNAVAILABLE status
  134. // - first attempt returns UNAVAILABLE due to LB drop but does not retry
  135. static void test_retry_lb_drop(grpc_end2end_test_config config) {
  136. grpc_call* c;
  137. grpc_op ops[6];
  138. grpc_op* op;
  139. grpc_metadata_array initial_metadata_recv;
  140. grpc_metadata_array trailing_metadata_recv;
  141. grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
  142. grpc_byte_buffer* request_payload =
  143. grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  144. grpc_byte_buffer* response_payload_recv = nullptr;
  145. grpc_status_code status;
  146. grpc_call_error error;
  147. grpc_slice details;
  148. std::vector<grpc_core::PickArgsSeen> pick_args_seen;
  149. grpc_core::g_pick_args_vector = &pick_args_seen;
  150. grpc_arg args[] = {
  151. grpc_channel_arg_string_create(
  152. const_cast<char*>(GRPC_ARG_SERVICE_CONFIG),
  153. const_cast<char*>(
  154. "{\n"
  155. " \"loadBalancingConfig\": [ {\n"
  156. " \"test_pick_args_lb\": {}\n"
  157. " } ],\n"
  158. " \"methodConfig\": [ {\n"
  159. " \"name\": [\n"
  160. " { \"service\": \"service\", \"method\": \"method\" }\n"
  161. " ],\n"
  162. " \"retryPolicy\": {\n"
  163. " \"maxAttempts\": 2,\n"
  164. " \"initialBackoff\": \"1s\",\n"
  165. " \"maxBackoff\": \"120s\",\n"
  166. " \"backoffMultiplier\": 1.6,\n"
  167. " \"retryableStatusCodes\": [ \"UNAVAILABLE\" ]\n"
  168. " }\n"
  169. " } ]\n"
  170. "}")),
  171. };
  172. grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
  173. grpc_end2end_test_fixture f =
  174. begin_test(config, "retry_lb_drop", &client_args, nullptr);
  175. cq_verifier* cqv = cq_verifier_create(f.cq);
  176. gpr_timespec deadline = five_seconds_from_now();
  177. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  178. grpc_slice_from_static_string("/service/method"),
  179. nullptr, deadline, nullptr);
  180. GPR_ASSERT(c);
  181. grpc_metadata_array_init(&initial_metadata_recv);
  182. grpc_metadata_array_init(&trailing_metadata_recv);
  183. memset(ops, 0, sizeof(ops));
  184. op = ops;
  185. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  186. op->data.send_initial_metadata.count = 0;
  187. op++;
  188. op->op = GRPC_OP_SEND_MESSAGE;
  189. op->data.send_message.send_message = request_payload;
  190. op++;
  191. op->op = GRPC_OP_RECV_MESSAGE;
  192. op->data.recv_message.recv_message = &response_payload_recv;
  193. op++;
  194. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  195. op++;
  196. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  197. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  198. op++;
  199. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  200. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  201. op->data.recv_status_on_client.status = &status;
  202. op->data.recv_status_on_client.status_details = &details;
  203. op++;
  204. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  205. nullptr);
  206. GPR_ASSERT(GRPC_CALL_OK == error);
  207. CQ_EXPECT_COMPLETION(cqv, tag(1), true);
  208. cq_verify(cqv);
  209. GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
  210. GPR_ASSERT(0 ==
  211. grpc_slice_str_cmp(details, "Call dropped by drop LB policy"));
  212. grpc_slice_unref(details);
  213. grpc_metadata_array_destroy(&initial_metadata_recv);
  214. grpc_metadata_array_destroy(&trailing_metadata_recv);
  215. grpc_byte_buffer_destroy(request_payload);
  216. grpc_byte_buffer_destroy(response_payload_recv);
  217. grpc_call_unref(c);
  218. cq_verifier_destroy(cqv);
  219. gpr_log(GPR_INFO, "NUMBER OF LB PICKS: %" PRIuPTR, pick_args_seen.size());
  220. GPR_ASSERT(pick_args_seen.size() == 1);
  221. grpc_core::g_pick_args_vector = nullptr;
  222. end_test(&f);
  223. config.tear_down_data(&f);
  224. }
  225. void retry_lb_drop(grpc_end2end_test_config config) {
  226. GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL);
  227. test_retry_lb_drop(config);
  228. }
  229. void retry_lb_drop_pre_init(void) { grpc_core::RegisterDropPolicy(); }