| 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 <stdint.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include <atomic> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
|
| 24 |
#include <grpc/byte_buffer.h> |
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/status.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
|
| 31 |
#include "src/core/lib/channel/channel_args.h" |
| 32 |
#include "src/core/lib/config/core_configuration.h" |
| 33 |
#include "src/core/lib/gpr/useful.h" |
| 34 |
#include "test/core/end2end/cq_verifier.h" |
| 35 |
#include "test/core/end2end/end2end_tests.h" |
| 36 |
#include "test/core/util/test_config.h" |
| 37 |
#include "test/core/util/test_lb_policies.h" |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
std::atomic<int> g_num_lb_picks; |
| 42 |
|
| 43 |
} // namespace |
| 44 |
|
| 45 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 46 |
|
| 47 |
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 48 |
const char* test_name, |
| 49 |
grpc_channel_args* client_args, |
| 50 |
grpc_channel_args* server_args) { |
| 51 |
grpc_end2end_test_fixture f; |
| 52 |
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
| 53 |
f = config.create_fixture(client_args, server_args); |
| 54 |
config.init_server(&f, server_args); |
| 55 |
config.init_client(&f, client_args); |
| 56 |
return f; |
| 57 |
} |
| 58 |
|
| 59 |
static gpr_timespec n_seconds_from_now(int n) { |
| 60 |
return grpc_timeout_seconds_to_deadline(n); |
| 61 |
} |
| 62 |
|
| 63 |
static gpr_timespec five_seconds_from_now(void) { |
| 64 |
return n_seconds_from_now(5); |
| 65 |
} |
| 66 |
|
| 67 |
static void drain_cq(grpc_completion_queue* cq) { |
| 68 |
grpc_event ev; |
| 69 |
do { |
| 70 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 71 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 72 |
} |
| 73 |
|
| 74 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 75 |
if (!f->server) return; |
| 76 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 77 |
grpc_event ev; |
| 78 |
do { |
| 79 |
ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5), |
| 80 |
nullptr); |
| 81 |
} while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000)); |
| 82 |
grpc_server_destroy(f->server); |
| 83 |
f->server = nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 87 |
if (!f->client) return; |
| 88 |
grpc_channel_destroy(f->client); |
| 89 |
f->client = nullptr; |
| 90 |
} |
| 91 |
|
| 92 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 93 |
shutdown_server(f); |
| 94 |
shutdown_client(f); |
| 95 |
|
| 96 |
grpc_completion_queue_shutdown(f->cq); |
| 97 |
drain_cq(f->cq); |
| 98 |
grpc_completion_queue_destroy(f->cq); |
| 99 |
} |
| 100 |
|
| 101 |
// Tests that we retry properly when the LB policy fails the call before |
| 102 |
// it ever gets to the transport, even if recv_trailing_metadata isn't |
| 103 |
// started by the application until after the LB pick fails. |
| 104 |
// - 1 retry allowed for ABORTED status |
| 105 |
// - on first attempt, LB policy fails with ABORTED before application |
| 106 |
// starts recv_trailing_metadata op |
| 107 |
static void test_retry_lb_fail(grpc_end2end_test_config config) { |
| 108 |
grpc_call* c; |
| 109 |
grpc_op ops[6]; |
| 110 |
grpc_op* op; |
| 111 |
grpc_metadata_array initial_metadata_recv; |
| 112 |
grpc_metadata_array trailing_metadata_recv; |
| 113 |
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
| 114 |
grpc_byte_buffer* request_payload = |
| 115 |
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 116 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 117 |
grpc_status_code status; |
| 118 |
grpc_call_error error; |
| 119 |
grpc_slice details; |
| 120 |
|
| 121 |
g_num_lb_picks.store(0, std::memory_order_relaxed); |
| 122 |
|
| 123 |
grpc_arg args[] = { |
| 124 |
grpc_channel_arg_integer_create( |
| 125 |
const_cast<char*>(GRPC_ARG_ENABLE_RETRIES), 1), |
| 126 |
grpc_channel_arg_string_create( |
| 127 |
const_cast<char*>(GRPC_ARG_SERVICE_CONFIG), |
| 128 |
const_cast<char*>( |
| 129 |
"{\n" |
| 130 |
" \"loadBalancingConfig\": [ {\n" |
| 131 |
" \"fail_lb\": {}\n" |
| 132 |
" } ],\n" |
| 133 |
" \"methodConfig\": [ {\n" |
| 134 |
" \"name\": [\n" |
| 135 |
" { \"service\": \"service\", \"method\": \"method\" }\n" |
| 136 |
" ],\n" |
| 137 |
" \"retryPolicy\": {\n" |
| 138 |
" \"maxAttempts\": 2,\n" |
| 139 |
" \"initialBackoff\": \"1s\",\n" |
| 140 |
" \"maxBackoff\": \"120s\",\n" |
| 141 |
" \"backoffMultiplier\": 1.6,\n" |
| 142 |
" \"retryableStatusCodes\": [ \"UNAVAILABLE\" ]\n" |
| 143 |
" }\n" |
| 144 |
" } ]\n" |
| 145 |
"}")), |
| 146 |
}; |
| 147 |
grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args}; |
| 148 |
grpc_end2end_test_fixture f = |
| 149 |
begin_test(config, "retry_lb_fail", &client_args, nullptr); |
| 150 |
|
| 151 |
grpc_core::CqVerifier cqv(f.cq); |
| 152 |
|
| 153 |
gpr_timespec deadline = five_seconds_from_now(); |
| 154 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 155 |
grpc_slice_from_static_string("/service/method"), |
| 156 |
nullptr, deadline, nullptr); |
| 157 |
GPR_ASSERT(c); |
| 158 |
|
| 159 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 160 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 161 |
|
| 162 |
memset(ops, 0, sizeof(ops)); |
| 163 |
op = ops; |
| 164 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 165 |
op->data.send_initial_metadata.count = 0; |
| 166 |
op++; |
| 167 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 168 |
nullptr); |
| 169 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 170 |
|
| 171 |
cqv.Expect(tag(1), false); |
| 172 |
cqv.Verify(); |
| 173 |
|
| 174 |
memset(ops, 0, sizeof(ops)); |
| 175 |
op = ops; |
| 176 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 177 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 178 |
op->data.recv_status_on_client.status = &status; |
| 179 |
op->data.recv_status_on_client.status_details = &details; |
| 180 |
op++; |
| 181 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2), |
| 182 |
nullptr); |
| 183 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 184 |
|
| 185 |
cqv.Expect(tag(2), true); |
| 186 |
cqv.Verify(); |
| 187 |
|
| 188 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 189 |
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "LB pick failed")); |
| 190 |
|
| 191 |
grpc_slice_unref(details); |
| 192 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 193 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 194 |
grpc_byte_buffer_destroy(request_payload); |
| 195 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 196 |
|
| 197 |
grpc_call_unref(c); |
| 198 |
|
| 199 |
int num_picks = g_num_lb_picks.load(std::memory_order_relaxed); |
| 200 |
gpr_log(GPR_INFO, "NUM LB PICKS: %d", num_picks); |
| 201 |
GPR_ASSERT(num_picks == 2); |
| 202 |
|
| 203 |
end_test(&f); |
| 204 |
config.tear_down_data(&f); |
| 205 |
} |
| 206 |
|
| 207 |
void retry_lb_fail(grpc_end2end_test_config config) { |
| 208 |
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
| 209 |
test_retry_lb_fail(config); |
| 210 |
} |
| 211 |
|
| 212 |
void retry_lb_fail_pre_init(void) { |
| 213 |
grpc_core::CoreConfiguration::RegisterBuilder( |
| 214 |
[](grpc_core::CoreConfiguration::Builder* builder) { |
| 215 |
grpc_core::RegisterFailLoadBalancingPolicy( |
| 216 |
builder, absl::UnavailableError("LB pick failed"), &g_num_lb_picks); |
| 217 |
}); |
| 218 |
} |