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
17 #include <algorithm>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25 #include "gtest/gtest.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/impl/channel_arg_names.h>
29 #include <grpc/status.h>
30 #include <grpc/support/log.h>
31
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/gprpp/orphanable.h"
35 #include "src/core/lib/gprpp/ref_counted_ptr.h"
36 #include "src/core/lib/gprpp/time.h"
37 #include "src/core/lib/json/json.h"
38 #include "src/core/load_balancing/lb_policy.h"
39 #include "src/core/load_balancing/lb_policy_factory.h"
40 #include "test/core/end2end/end2end_tests.h"
41 #include "test/core/util/test_lb_policies.h"
42
43 namespace grpc_core {
44 namespace {
45
46 constexpr absl::string_view kDropPolicyName = "drop_lb";
47
48 class DropPolicy : public LoadBalancingPolicy {
49 public:
DropPolicy(Args args)50 explicit DropPolicy(Args args) : LoadBalancingPolicy(std::move(args)) {}
51
name() const52 absl::string_view name() const override { return kDropPolicyName; }
53
UpdateLocked(UpdateArgs)54 absl::Status UpdateLocked(UpdateArgs) override {
55 channel_control_helper()->UpdateState(GRPC_CHANNEL_READY, absl::Status(),
56 MakeRefCounted<DropPicker>());
57 return absl::OkStatus();
58 }
59
ResetBackoffLocked()60 void ResetBackoffLocked() override {}
ShutdownLocked()61 void ShutdownLocked() override {}
62
63 private:
64 class DropPicker : public SubchannelPicker {
65 public:
Pick(PickArgs)66 PickResult Pick(PickArgs /*args*/) override {
67 return PickResult::Drop(
68 absl::UnavailableError("Call dropped by drop LB policy"));
69 }
70 };
71 };
72
73 class DropLbConfig : public LoadBalancingPolicy::Config {
74 public:
name() const75 absl::string_view name() const override { return kDropPolicyName; }
76 };
77
78 class DropPolicyFactory : public LoadBalancingPolicyFactory {
79 public:
CreateLoadBalancingPolicy(LoadBalancingPolicy::Args args) const80 OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
81 LoadBalancingPolicy::Args args) const override {
82 return MakeOrphanable<DropPolicy>(std::move(args));
83 }
84
name() const85 absl::string_view name() const override { return kDropPolicyName; }
86
87 absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>>
ParseLoadBalancingConfig(const Json &) const88 ParseLoadBalancingConfig(const Json& /*json*/) const override {
89 return MakeRefCounted<DropLbConfig>();
90 }
91 };
92
93 std::vector<PickArgsSeen>* g_pick_args_vector = nullptr;
94
RegisterDropPolicy(CoreConfiguration::Builder * builder)95 void RegisterDropPolicy(CoreConfiguration::Builder* builder) {
96 builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory(
97 std::make_unique<DropPolicyFactory>());
98 }
99
100 // Tests that we don't retry when the LB policy drops a call,
101 // even when there is retry configuration in the service config.
102 // - 1 retry allowed for UNAVAILABLE status
103 // - first attempt returns UNAVAILABLE due to LB drop but does not retry
CORE_END2END_TEST(RetryTest,RetryLbDrop)104 CORE_END2END_TEST(RetryTest, RetryLbDrop) {
105 CoreConfiguration::RegisterBuilder([](CoreConfiguration::Builder* builder) {
106 RegisterTestPickArgsLoadBalancingPolicy(
107 builder,
108 [](const PickArgsSeen& pick_args) {
109 GPR_ASSERT(g_pick_args_vector != nullptr);
110 g_pick_args_vector->push_back(pick_args);
111 },
112 kDropPolicyName);
113 });
114 CoreConfiguration::RegisterBuilder(RegisterDropPolicy);
115 std::vector<PickArgsSeen> pick_args_seen;
116 g_pick_args_vector = &pick_args_seen;
117 InitServer(ChannelArgs());
118 InitClient(ChannelArgs().Set(
119 GRPC_ARG_SERVICE_CONFIG,
120 "{\n"
121 " \"loadBalancingConfig\": [ {\n"
122 " \"test_pick_args_lb\": {}\n"
123 " } ],\n"
124 " \"methodConfig\": [ {\n"
125 " \"name\": [\n"
126 " { \"service\": \"service\", \"method\": \"method\" }\n"
127 " ],\n"
128 " \"retryPolicy\": {\n"
129 " \"maxAttempts\": 2,\n"
130 " \"initialBackoff\": \"1s\",\n"
131 " \"maxBackoff\": \"120s\",\n"
132 " \"backoffMultiplier\": 1.6,\n"
133 " \"retryableStatusCodes\": [ \"UNAVAILABLE\" ]\n"
134 " }\n"
135 " } ]\n"
136 "}"));
137 auto c =
138 NewClientCall("/service/method").Timeout(Duration::Seconds(5)).Create();
139 IncomingMessage server_message;
140 IncomingMetadata server_initial_metadata;
141 IncomingStatusOnClient server_status;
142 c.NewBatch(1)
143 .SendInitialMetadata({})
144 .SendMessage("foo")
145 .RecvMessage(server_message)
146 .SendCloseFromClient()
147 .RecvInitialMetadata(server_initial_metadata)
148 .RecvStatusOnClient(server_status);
149 Expect(1, true);
150 Step();
151 EXPECT_EQ(server_status.status(), GRPC_STATUS_UNAVAILABLE);
152 EXPECT_EQ(server_status.message(), "Call dropped by drop LB policy");
153 EXPECT_EQ(pick_args_seen.size(), 1);
154 g_pick_args_vector = nullptr;
155 }
156
157 } // namespace
158 } // namespace grpc_core
159