retry_cancel_during_delay.cc
Go to the documentation of this file.
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 <stdio.h>
18 #include <string.h>
19 
20 #include <string>
21 
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_format.h"
24 
25 #include <grpc/byte_buffer.h>
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30 
39 
40 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
41 
43  const char* test_name,
44  grpc_channel_args* client_args,
45  grpc_channel_args* server_args) {
47  gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
48  f = config.create_fixture(client_args, server_args);
49  config.init_server(&f, server_args);
50  config.init_client(&f, client_args);
51  return f;
52 }
53 
56 }
57 
59  return n_seconds_from_now(5);
60 }
61 
63  grpc_event ev;
64  do {
66  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68 
70  if (!f->server) return;
71  grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
72  grpc_event ev;
73  do {
75  nullptr);
76  } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
77  grpc_server_destroy(f->server);
78  f->server = nullptr;
79 }
80 
82  if (!f->client) return;
83  grpc_channel_destroy(f->client);
84  f->client = nullptr;
85 }
86 
90 
92  drain_cq(f->cq);
94 }
95 
96 // Tests retry cancellation during backoff.
99  grpc_call* c;
100  grpc_call* s;
101  grpc_op ops[6];
102  grpc_op* op;
107  grpc_slice request_payload_slice = grpc_slice_from_static_string("foo");
108  grpc_slice response_payload_slice = grpc_slice_from_static_string("bar");
109  grpc_byte_buffer* request_payload =
110  grpc_raw_byte_buffer_create(&request_payload_slice, 1);
111  grpc_byte_buffer* response_payload =
112  grpc_raw_byte_buffer_create(&response_payload_slice, 1);
113  grpc_byte_buffer* request_payload_recv = nullptr;
118  int was_cancelled = 2;
119  char* peer;
120 
121  std::string service_config = absl::StrFormat(
122  "{\n"
123  " \"methodConfig\": [ {\n"
124  " \"name\": [\n"
125  " { \"service\": \"service\", \"method\": \"method\" }\n"
126  " ],\n"
127  " \"retryPolicy\": {\n"
128  " \"maxAttempts\": 3,\n"
129  " \"initialBackoff\": \"%ds\",\n"
130  " \"maxBackoff\": \"120s\",\n"
131  " \"backoffMultiplier\": 1.6,\n"
132  " \"retryableStatusCodes\": [ \"ABORTED\" ]\n"
133  " }\n"
134  " } ]\n"
135  "}",
137 
138  grpc_arg args[] = {
140  const_cast<char*>(service_config.c_str())),
141  };
142  grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args};
143  std::string name = absl::StrCat("retry_cancel_during_delay/", mode.name);
145  begin_test(config, name.c_str(), &client_args, nullptr);
146 
147  cq_verifier* cqv = cq_verifier_create(f.cq);
148 
149  gpr_timespec expect_finish_before = n_seconds_from_now(10);
150  gpr_timespec deadline = five_seconds_from_now();
151  c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
152  grpc_slice_from_static_string("/service/method"),
153  nullptr, deadline, nullptr);
154  GPR_ASSERT(c);
155 
156  peer = grpc_call_get_peer(c);
157  GPR_ASSERT(peer != nullptr);
158  gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
159  gpr_free(peer);
160 
165  grpc_slice status_details = grpc_slice_from_static_string("xyz");
166 
167  // Client starts a batch with all 6 ops.
168  memset(ops, 0, sizeof(ops));
169  op = ops;
172  op++;
174  op->data.send_message.send_message = request_payload;
175  op++;
178  op++;
180  op++;
183  op++;
188  op++;
189  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
190  nullptr);
192 
193  // Server gets a call and fails with retryable status.
194  error =
196  &request_metadata_recv, f.cq, f.cq, tag(101));
198  CQ_EXPECT_COMPLETION(cqv, tag(101), true);
199  cq_verify(cqv);
200 
201  peer = grpc_call_get_peer(s);
202  GPR_ASSERT(peer != nullptr);
203  gpr_log(GPR_DEBUG, "server_peer=%s", peer);
204  gpr_free(peer);
205  peer = grpc_call_get_peer(c);
206  GPR_ASSERT(peer != nullptr);
207  gpr_log(GPR_DEBUG, "client_peer=%s", peer);
208  gpr_free(peer);
209 
210  memset(ops, 0, sizeof(ops));
211  op = ops;
214  op++;
218  op->data.send_status_from_server.status_details = &status_details;
219  op++;
222  op++;
223  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
224  nullptr);
226 
227  CQ_EXPECT_COMPLETION(cqv, tag(102), true);
228  cq_verify(cqv);
229 
230  grpc_call_unref(s);
235 
236  // Server should never get a second call, because the initial retry
237  // delay is longer than the call's deadline.
238  error =
240  &request_metadata_recv, f.cq, f.cq, tag(201));
242 
243  // Initiate cancellation.
244  GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
245 
246  CQ_EXPECT_COMPLETION(cqv, tag(1), true);
247  cq_verify(cqv);
248 
250 
251  gpr_log(GPR_INFO, "status=%d expected=%d", status, mode.expect_status);
252  gpr_log(GPR_INFO, "message=\"%s\"",
254  GPR_ASSERT(status == mode.expect_status);
256 
257  // Make sure we didn't wait the full deadline before failing.
258  gpr_log(
259  GPR_INFO, "Expect completion before: %s",
260  absl::FormatTime(grpc_core::ToAbslTime(expect_finish_before)).c_str());
261  GPR_ASSERT(gpr_time_cmp(finish_time, expect_finish_before) < 0);
262 
268  grpc_byte_buffer_destroy(request_payload);
269  grpc_byte_buffer_destroy(response_payload);
270  grpc_byte_buffer_destroy(request_payload_recv);
272 
274 
275  cq_verifier_destroy(cqv);
276 
277  end_test(&f);
278  config.tear_down_data(&f);
279 }
280 
283  for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) {
285  }
286 }
287 
end_test
static void end_test(grpc_end2end_test_fixture *f)
Definition: retry_cancel_during_delay.cc:87
grpc_arg
Definition: grpc_types.h:103
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_op::grpc_op_data::grpc_op_send_message::send_message
struct grpc_byte_buffer * send_message
Definition: grpc_types.h:668
grpc_call_error
grpc_call_error
Definition: grpc_types.h:464
retry_cancel_during_delay_pre_init
void retry_cancel_during_delay_pre_init(void)
Definition: retry_cancel_during_delay.cc:288
grpc_call_details_destroy
GRPCAPI void grpc_call_details_destroy(grpc_call_details *details)
Definition: call_details.cc:36
grpc_call_details_init
GRPCAPI void grpc_call_details_init(grpc_call_details *details)
Definition: call_details.cc:30
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::trailing_metadata
grpc_metadata_array * trailing_metadata
Definition: grpc_types.h:701
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status
grpc_status_code * status
Definition: grpc_types.h:702
grpc_raw_byte_buffer_create
GRPCAPI grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slices, size_t nslices)
Definition: byte_buffer.cc:34
n_seconds_from_now
static gpr_timespec n_seconds_from_now(int n)
Definition: retry_cancel_during_delay.cc:54
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core::ToAbslTime
absl::Time ToAbslTime(gpr_timespec ts)
Definition: src/core/lib/gprpp/time_util.cc:68
memset
return memset(p, 0, total)
shutdown_client
static void shutdown_client(grpc_end2end_test_fixture *f)
Definition: retry_cancel_during_delay.cc:81
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_op::grpc_op_data::send_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_send_initial_metadata send_initial_metadata
grpc_call_get_peer
GRPCAPI char * grpc_call_get_peer(grpc_call *call)
Definition: call.cc:1774
grpc_metadata_array
Definition: grpc_types.h:579
grpc_call_details
Definition: grpc_types.h:585
string.h
grpc_core::StringViewFromSlice
absl::string_view StringViewFromSlice(const grpc_slice &slice)
Definition: slice_internal.h:93
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
shutdown_server
static void shutdown_server(grpc_end2end_test_fixture *f)
Definition: retry_cancel_during_delay.cc:69
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Definition: grpc_types.h:554
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
begin_test
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, const char *test_name, grpc_channel_args *client_args, grpc_channel_args *server_args)
Definition: retry_cancel_during_delay.cc:42
time_util.h
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
five_seconds_from_now
static gpr_timespec five_seconds_from_now(void)
Definition: retry_cancel_during_delay.cc:58
status
absl::Status status
Definition: rls.cc:251
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
setup.name
name
Definition: setup.py:542
time.h
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
grpc_end2end_test_config
Definition: end2end_tests.h:53
retry_cancel_during_delay
void retry_cancel_during_delay(grpc_end2end_test_config config)
Definition: retry_cancel_during_delay.cc:281
grpc_channel_args
Definition: grpc_types.h:132
grpc_op::grpc_op_data::recv_message
struct grpc_op::grpc_op_data::grpc_op_recv_message recv_message
absl::FormatTime
std::string FormatTime(absl::string_view format, absl::Time t, absl::TimeZone tz)
Definition: abseil-cpp/absl/time/format.cc:74
grpc_op::data
union grpc_op::grpc_op_data data
grpc_end2end_test_fixture
Definition: end2end_tests.h:46
tag
static void * tag(intptr_t t)
Definition: retry_cancel_during_delay.cc:40
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
Definition: metadata_array.cc:35
grpc_op::grpc_op_data::grpc_op_recv_message::recv_message
struct grpc_byte_buffer ** recv_message
Definition: grpc_types.h:693
grpc_server_request_call
GRPCAPI grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, grpc_call_details *details, grpc_metadata_array *request_metadata, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Definition: src/core/lib/surface/server.cc:1526
trailing_metadata_recv
static grpc_metadata_array trailing_metadata_recv
Definition: test/core/fling/client.cc:43
test_retry_cancel_during_delay
static void test_retry_cancel_during_delay(grpc_end2end_test_config config, cancellation_mode mode)
Definition: retry_cancel_during_delay.cc:97
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GRPC_OP_RECV_INITIAL_METADATA
@ GRPC_OP_RECV_INITIAL_METADATA
Definition: grpc_types.h:617
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
GRPC_OP_SEND_STATUS_FROM_SERVER
@ GRPC_OP_SEND_STATUS_FROM_SERVER
Definition: grpc_types.h:612
cancel_test_helpers.h
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Definition: call.cc:1770
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status
grpc_status_code status
Definition: grpc_types.h:673
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
drain_cq
static void drain_cq(grpc_completion_queue *cq)
Definition: retry_cancel_during_delay.cc:62
cq_verifier_destroy
void cq_verifier_destroy(cq_verifier *v)
Definition: cq_verifier.cc:92
grpc.h
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
response_payload_recv
static grpc_byte_buffer * response_payload_recv
Definition: test/core/fling/client.cc:44
grpc_byte_buffer
Definition: grpc_types.h:43
grpc_test_slowdown_factor
int64_t grpc_test_slowdown_factor()
Definition: test/core/util/test_config.cc:76
grpc_op
Definition: grpc_types.h:640
GRPC_OP_SEND_MESSAGE
@ GRPC_OP_SEND_MESSAGE
Definition: grpc_types.h:602
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
cq_verifier_create
cq_verifier * cq_verifier_create(grpc_completion_queue *cq)
Definition: cq_verifier.cc:86
was_cancelled
static int was_cancelled
Definition: test/core/fling/server.cc:58
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
cq_verifier
Definition: cq_verifier.cc:76
request_metadata_recv
static grpc_metadata_array request_metadata_recv
Definition: test/core/fling/server.cc:48
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
CQ_EXPECT_COMPLETION
#define CQ_EXPECT_COMPLETION(v, tag, success)
Definition: cq_verifier.h:58
end2end_tests.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc_op::op
grpc_op_type op
Definition: grpc_types.h:642
grpc_op::grpc_op_data::grpc_op_send_initial_metadata::count
size_t count
Definition: grpc_types.h:653
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status_details
grpc_slice * status_details
Definition: grpc_types.h:703
details
static grpc_slice details
Definition: test/core/fling/client.cc:46
grpc_channel_create_call
GRPCAPI grpc_call * grpc_channel_create_call(grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask, grpc_completion_queue *completion_queue, grpc_slice method, const grpc_slice *host, gpr_timespec deadline, void *reserved)
Definition: channel.cc:311
grpc_op::grpc_op_data::recv_close_on_server
struct grpc_op::grpc_op_data::grpc_op_recv_close_on_server recv_close_on_server
GRPC_OP_RECV_MESSAGE
@ GRPC_OP_RECV_MESSAGE
Definition: grpc_types.h:621
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
GRPC_PROPAGATE_DEFAULTS
#define GRPC_PROPAGATE_DEFAULTS
Definition: propagation_bits.h:45
grpc_op::grpc_op_data::send_status_from_server
struct grpc_op::grpc_op_data::grpc_op_send_status_from_server send_status_from_server
cancellation_modes
static const cancellation_mode cancellation_modes[]
Definition: cancel_test_helpers.h:36
cq_verifier.h
GRPC_ARG_SERVICE_CONFIG
#define GRPC_ARG_SERVICE_CONFIG
Definition: grpc_types.h:304
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
GRPC_OP_SEND_INITIAL_METADATA
@ GRPC_OP_SEND_INITIAL_METADATA
Definition: grpc_types.h:598
grpc_op::grpc_op_data::send_message
struct grpc_op::grpc_op_data::grpc_op_send_message send_message
GRPC_STATUS_ABORTED
@ GRPC_STATUS_ABORTED
Definition: include/grpc/impl/codegen/status.h:104
alloc.h
grpc_op::grpc_op_data::recv_status_on_client
struct grpc_op::grpc_op_data::grpc_op_recv_status_on_client recv_status_on_client
grpc_op::grpc_op_data::grpc_op_send_status_from_server::trailing_metadata_count
size_t trailing_metadata_count
Definition: grpc_types.h:671
grpc_server_shutdown_and_notify
GRPCAPI void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Definition: src/core/lib/surface/server.cc:1503
grpc_byte_buffer_destroy
GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)
Definition: byte_buffer.cc:81
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
cq_verify
void cq_verify(cq_verifier *v, int timeout_sec)
Definition: cq_verifier.cc:268
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
exec_ctx.h
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
GRPC_OP_RECV_CLOSE_ON_SERVER
@ GRPC_OP_RECV_CLOSE_ON_SERVER
Definition: grpc_types.h:633
channel_args.h
cancellation_mode
Definition: cancel_test_helpers.h:24
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_op::grpc_op_data::recv_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_recv_initial_metadata recv_initial_metadata
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status_details
grpc_slice * status_details
Definition: grpc_types.h:677
gpr_timespec
Definition: gpr_types.h:50
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
GRPC_OP_RECV_STATUS_ON_CLIENT
@ GRPC_OP_RECV_STATUS_ON_CLIENT
Definition: grpc_types.h:627
grpc_op::grpc_op_data::grpc_op_recv_initial_metadata::recv_initial_metadata
grpc_metadata_array * recv_initial_metadata
Definition: grpc_types.h:685
op
static grpc_op * op
Definition: test/core/fling/client.cc:47
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
initial_metadata_recv
static grpc_metadata_array initial_metadata_recv
Definition: test/core/fling/client.cc:42
grpc_call_start_batch
GRPCAPI grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag, void *reserved)
Definition: call.cc:1831
grpc_event::tag
void * tag
Definition: grpc_types.h:576
grpc_op::grpc_op_data::grpc_op_recv_close_on_server::cancelled
int * cancelled
Definition: grpc_types.h:714
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
GRPC_OP_SEND_CLOSE_FROM_CLIENT
@ GRPC_OP_SEND_CLOSE_FROM_CLIENT
Definition: grpc_types.h:607
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
call_details
static grpc_call_details call_details
Definition: test/core/fling/server.cc:47
FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL
#define FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL
Definition: end2end_tests.h:37
grpc_metadata_array_init
GRPCAPI void grpc_metadata_array_init(grpc_metadata_array *array)
Definition: metadata_array.cc:30


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:05