| 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 <inttypes.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <memory> |
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "absl/status/status.h" |
| 26 |
#include "absl/status/statusor.h" |
| 27 |
#include "absl/strings/string_view.h" |
| 28 |
|
| 29 |
#include <grpc/byte_buffer.h> |
| 30 |
#include <grpc/grpc.h> |
| 31 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 32 |
#include <grpc/slice.h> |
| 33 |
#include <grpc/status.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
|
| 36 |
#include "src/core/lib/channel/channel_args.h" |
| 37 |
#include "src/core/lib/config/core_configuration.h" |
| 38 |
#include "src/core/lib/gpr/useful.h" |
| 39 |
#include "src/core/lib/gprpp/orphanable.h" |
| 40 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 41 |
#include "src/core/lib/json/json.h" |
| 42 |
#include "src/core/lib/load_balancing/lb_policy.h" |
| 43 |
#include "src/core/lib/load_balancing/lb_policy_factory.h" |
| 44 |
#include "test/core/end2end/cq_verifier.h" |
| 45 |
#include "test/core/end2end/end2end_tests.h" |
| 46 |
#include "test/core/util/test_config.h" |
| 47 |
#include "test/core/util/test_lb_policies.h" |
| 48 |
|
| 49 |
namespace grpc_core { |
| 50 |
namespace { |
| 51 |
|
| 52 |
constexpr absl::string_view kDropPolicyName = "drop_lb"; |
| 53 |
|
| 54 |
class DropPolicy : public LoadBalancingPolicy { |
| 55 |
public: |
| 56 |
explicit DropPolicy(Args args) : LoadBalancingPolicy(std::move(args)) {} |
| 57 |
|
| 58 |
absl::string_view name() const override { return kDropPolicyName; } |
| 59 |
|
| 60 |
absl::Status UpdateLocked(UpdateArgs) override { |
| 61 |
channel_control_helper()->UpdateState(GRPC_CHANNEL_READY, absl::Status(), |
| 62 |
std::make_unique<DropPicker>()); |
| 63 |
return absl::OkStatus(); |
| 64 |
} |
| 65 |
|
| 66 |
void ResetBackoffLocked() override {} |
| 67 |
void ShutdownLocked() override {} |
| 68 |
|
| 69 |
private: |
| 70 |
class DropPicker : public SubchannelPicker { |
| 71 |
public: |
| 72 |
PickResult Pick(PickArgs /*args*/) override { |
| 73 |
return PickResult::Drop( |
| 74 |
absl::UnavailableError("Call dropped by drop LB policy")); |
| 75 |
} |
| 76 |
}; |
| 77 |
}; |
| 78 |
|
| 79 |
class DropLbConfig : public LoadBalancingPolicy::Config { |
| 80 |
public: |
| 81 |
absl::string_view name() const override { return kDropPolicyName; } |
| 82 |
}; |
| 83 |
|
| 84 |
class DropPolicyFactory : public LoadBalancingPolicyFactory { |
| 85 |
public: |
| 86 |
OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( |
| 87 |
LoadBalancingPolicy::Args args) const override { |
| 88 |
return MakeOrphanable<DropPolicy>(std::move(args)); |
| 89 |
} |
| 90 |
|
| 91 |
absl::string_view name() const override { return kDropPolicyName; } |
| 92 |
|
| 93 |
absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> |
| 94 |
ParseLoadBalancingConfig(const Json& /*json*/) const override { |
| 95 |
return MakeRefCounted<DropLbConfig>(); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
std::vector<PickArgsSeen>* g_pick_args_vector = nullptr; |
| 100 |
|
| 101 |
void RegisterDropPolicy(CoreConfiguration::Builder* builder) { |
| 102 |
builder->lb_policy_registry()->RegisterLoadBalancingPolicyFactory( |
| 103 |
std::make_unique<DropPolicyFactory>()); |
| 104 |
} |
| 105 |
|
| 106 |
} // namespace |
| 107 |
} // namespace grpc_core |
| 108 |
|
| 109 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 110 |
|
| 111 |
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 112 |
const char* test_name, |
| 113 |
grpc_channel_args* client_args, |
| 114 |
grpc_channel_args* server_args) { |
| 115 |
grpc_end2end_test_fixture f; |
| 116 |
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
| 117 |
f = config.create_fixture(client_args, server_args); |
| 118 |
config.init_server(&f, server_args); |
| 119 |
config.init_client(&f, client_args); |
| 120 |
return f; |
| 121 |
} |
| 122 |
|
| 123 |
static gpr_timespec n_seconds_from_now(int n) { |
| 124 |
return grpc_timeout_seconds_to_deadline(n); |
| 125 |
} |
| 126 |
|
| 127 |
static gpr_timespec five_seconds_from_now(void) { |
| 128 |
return n_seconds_from_now(5); |
| 129 |
} |
| 130 |
|
| 131 |
static void drain_cq(grpc_completion_queue* cq) { |
| 132 |
grpc_event ev; |
| 133 |
do { |
| 134 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 135 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 136 |
} |
| 137 |
|
| 138 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 139 |
if (!f->server) return; |
| 140 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 141 |
grpc_event ev; |
| 142 |
do { |
| 143 |
ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5), |
| 144 |
nullptr); |
| 145 |
} while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000)); |
| 146 |
grpc_server_destroy(f->server); |
| 147 |
f->server = nullptr; |
| 148 |
} |
| 149 |
|
| 150 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 151 |
if (!f->client) return; |
| 152 |
grpc_channel_destroy(f->client); |
| 153 |
f->client = nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 157 |
shutdown_server(f); |
| 158 |
shutdown_client(f); |
| 159 |
|
| 160 |
grpc_completion_queue_shutdown(f->cq); |
| 161 |
drain_cq(f->cq); |
| 162 |
grpc_completion_queue_destroy(f->cq); |
| 163 |
} |
| 164 |
|
| 165 |
// Tests that we don't retry when the LB policy drops a call, |
| 166 |
// even when there is retry configuration in the service config. |
| 167 |
// - 1 retry allowed for UNAVAILABLE status |
| 168 |
// - first attempt returns UNAVAILABLE due to LB drop but does not retry |
| 169 |
static void test_retry_lb_drop(grpc_end2end_test_config config) { |
| 170 |
grpc_call* c; |
| 171 |
grpc_op ops[6]; |
| 172 |
grpc_op* op; |
| 173 |
grpc_metadata_array initial_metadata_recv; |
| 174 |
grpc_metadata_array trailing_metadata_recv; |
| 175 |
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
| 176 |
grpc_byte_buffer* request_payload = |
| 177 |
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 178 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 179 |
grpc_status_code status; |
| 180 |
grpc_call_error error; |
| 181 |
grpc_slice details; |
| 182 |
|
| 183 |
std::vector<grpc_core::PickArgsSeen> pick_args_seen; |
| 184 |
grpc_core::g_pick_args_vector = &pick_args_seen; |
| 185 |
|
| 186 |
grpc_arg args[] = { |
| 187 |
grpc_channel_arg_string_create( |
| 188 |
const_cast<char*>(GRPC_ARG_SERVICE_CONFIG), |
| 189 |
const_cast<char*>( |
| 190 |
"{\n" |
| 191 |
" \"loadBalancingConfig\": [ {\n" |
| 192 |
" \"test_pick_args_lb\": {}\n" |
| 193 |
" } ],\n" |
| 194 |
" \"methodConfig\": [ {\n" |
| 195 |
" \"name\": [\n" |
| 196 |
" { \"service\": \"service\", \"method\": \"method\" }\n" |
| 197 |
" ],\n" |
| 198 |
" \"retryPolicy\": {\n" |
| 199 |
" \"maxAttempts\": 2,\n" |
| 200 |
" \"initialBackoff\": \"1s\",\n" |
| 201 |
" \"maxBackoff\": \"120s\",\n" |
| 202 |
" \"backoffMultiplier\": 1.6,\n" |
| 203 |
" \"retryableStatusCodes\": [ \"UNAVAILABLE\" ]\n" |
| 204 |
" }\n" |
| 205 |
" } ]\n" |
| 206 |
"}")), |
| 207 |
}; |
| 208 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args}; |
| 209 |
grpc_end2end_test_fixture f = |
| 210 |
begin_test(config, "retry_lb_drop", &client_args, nullptr); |
| 211 |
|
| 212 |
grpc_core::CqVerifier cqv(f.cq); |
| 213 |
|
| 214 |
gpr_timespec deadline = five_seconds_from_now(); |
| 215 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 216 |
grpc_slice_from_static_string("/service/method"), |
| 217 |
nullptr, deadline, nullptr); |
| 218 |
GPR_ASSERT(c); |
| 219 |
|
| 220 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 221 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 222 |
|
| 223 |
memset(ops, 0, sizeof(ops)); |
| 224 |
op = ops; |
| 225 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 226 |
op->data.send_initial_metadata.count = 0; |
| 227 |
op++; |
| 228 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 229 |
op->data.send_message.send_message = request_payload; |
| 230 |
op++; |
| 231 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 232 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 233 |
op++; |
| 234 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 235 |
op++; |
| 236 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 237 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 238 |
op++; |
| 239 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 240 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 241 |
op->data.recv_status_on_client.status = &status; |
| 242 |
op->data.recv_status_on_client.status_details = &details; |
| 243 |
op++; |
| 244 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 245 |
nullptr); |
| 246 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 247 |
|
| 248 |
cqv.Expect(tag(1), true); |
| 249 |
cqv.Verify(); |
| 250 |
|
| 251 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 252 |
GPR_ASSERT(0 == |
| 253 |
grpc_slice_str_cmp(details, "Call dropped by drop LB policy")); |
| 254 |
|
| 255 |
grpc_slice_unref(details); |
| 256 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 257 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 258 |
grpc_byte_buffer_destroy(request_payload); |
| 259 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 260 |
|
| 261 |
grpc_call_unref(c); |
| 262 |
|
| 263 |
gpr_log(GPR_INFO, "NUMBER OF LB PICKS: %" PRIuPTR, pick_args_seen.size()); |
| 264 |
GPR_ASSERT(pick_args_seen.size() == 1); |
| 265 |
|
| 266 |
grpc_core::g_pick_args_vector = nullptr; |
| 267 |
|
| 268 |
end_test(&f); |
| 269 |
config.tear_down_data(&f); |
| 270 |
} |
| 271 |
|
| 272 |
void retry_lb_drop(grpc_end2end_test_config config) { |
| 273 |
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
| 274 |
test_retry_lb_drop(config); |
| 275 |
} |
| 276 |
|
| 277 |
void retry_lb_drop_pre_init(void) { |
| 278 |
grpc_core::CoreConfiguration::RegisterBuilder( |
| 279 |
[](grpc_core::CoreConfiguration::Builder* builder) { |
| 280 |
grpc_core::RegisterTestPickArgsLoadBalancingPolicy( |
| 281 |
builder, |
| 282 |
[](const grpc_core::PickArgsSeen& pick_args) { |
| 283 |
GPR_ASSERT(grpc_core::g_pick_args_vector != nullptr); |
| 284 |
grpc_core::g_pick_args_vector->push_back(pick_args); |
| 285 |
}, |
| 286 |
grpc_core::kDropPolicyName); |
| 287 |
}); |
| 288 |
grpc_core::CoreConfiguration::RegisterBuilder(grpc_core::RegisterDropPolicy); |
| 289 |
} |