1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/core/end2end/end2end_tests.h"
20
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <grpc/byte_buffer.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30
31 #include "src/core/lib/channel/channel_stack_builder.h"
32 #include "src/core/lib/surface/channel_init.h"
33 #include "test/core/end2end/cq_verifier.h"
34
35 enum { TIMEOUT = 200000 };
36
37 static bool g_enable_filter = false;
38 static gpr_mu g_mu;
39 static gpr_timespec g_client_latency;
40 static gpr_timespec g_server_latency;
41
tag(intptr_t t)42 static void* tag(intptr_t t) { return (void*)t; }
43
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)44 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
45 const char* test_name,
46 grpc_channel_args* client_args,
47 grpc_channel_args* server_args) {
48 grpc_end2end_test_fixture f;
49 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
50 f = config.create_fixture(client_args, server_args);
51 config.init_server(&f, server_args);
52 config.init_client(&f, client_args);
53 return f;
54 }
55
n_seconds_from_now(int n)56 static gpr_timespec n_seconds_from_now(int n) {
57 return grpc_timeout_seconds_to_deadline(n);
58 }
59
five_seconds_from_now(void)60 static gpr_timespec five_seconds_from_now(void) {
61 return n_seconds_from_now(5);
62 }
63
drain_cq(grpc_completion_queue * cq)64 static void drain_cq(grpc_completion_queue* cq) {
65 grpc_event ev;
66 do {
67 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
68 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
69 }
70
shutdown_server(grpc_end2end_test_fixture * f)71 static void shutdown_server(grpc_end2end_test_fixture* f) {
72 if (!f->server) return;
73 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
74 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
75 grpc_timeout_seconds_to_deadline(5),
76 nullptr)
77 .type == GRPC_OP_COMPLETE);
78 grpc_server_destroy(f->server);
79 f->server = nullptr;
80 }
81
shutdown_client(grpc_end2end_test_fixture * f)82 static void shutdown_client(grpc_end2end_test_fixture* f) {
83 if (!f->client) return;
84 grpc_channel_destroy(f->client);
85 f->client = nullptr;
86 }
87
end_test(grpc_end2end_test_fixture * f)88 static void end_test(grpc_end2end_test_fixture* f) {
89 shutdown_server(f);
90 shutdown_client(f);
91
92 grpc_completion_queue_shutdown(f->cq);
93 drain_cq(f->cq);
94 grpc_completion_queue_destroy(f->cq);
95 grpc_completion_queue_destroy(f->shutdown_cq);
96 }
97
98 // Simple request via a server filter that saves the reported latency value.
test_request(grpc_end2end_test_config config)99 static void test_request(grpc_end2end_test_config config) {
100 grpc_call* c;
101 grpc_call* s;
102 grpc_slice request_payload_slice =
103 grpc_slice_from_copied_string("hello world");
104 grpc_byte_buffer* request_payload =
105 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
106 grpc_end2end_test_fixture f =
107 begin_test(config, "filter_latency", nullptr, nullptr);
108 cq_verifier* cqv = cq_verifier_create(f.cq);
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_metadata_array request_metadata_recv;
114 grpc_byte_buffer* request_payload_recv = nullptr;
115 grpc_call_details call_details;
116 grpc_status_code status;
117 grpc_call_error error;
118 grpc_slice details;
119 int was_cancelled = 2;
120
121 gpr_mu_lock(&g_mu);
122 g_client_latency = gpr_time_0(GPR_TIMESPAN);
123 g_server_latency = gpr_time_0(GPR_TIMESPAN);
124 gpr_mu_unlock(&g_mu);
125 const gpr_timespec start_time = gpr_now(GPR_CLOCK_MONOTONIC);
126
127 gpr_timespec deadline = five_seconds_from_now();
128 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
129 grpc_slice_from_static_string("/foo"), nullptr,
130 deadline, nullptr);
131 GPR_ASSERT(c);
132
133 grpc_metadata_array_init(&initial_metadata_recv);
134 grpc_metadata_array_init(&trailing_metadata_recv);
135 grpc_metadata_array_init(&request_metadata_recv);
136 grpc_call_details_init(&call_details);
137
138 memset(ops, 0, sizeof(ops));
139 op = ops;
140 op->op = GRPC_OP_SEND_INITIAL_METADATA;
141 op->data.send_initial_metadata.count = 0;
142 op->data.send_initial_metadata.metadata = nullptr;
143 op->flags = 0;
144 op->reserved = nullptr;
145 op++;
146 op->op = GRPC_OP_SEND_MESSAGE;
147 op->data.send_message.send_message = request_payload;
148 op->flags = 0;
149 op->reserved = nullptr;
150 op++;
151 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
152 op->flags = 0;
153 op->reserved = nullptr;
154 op++;
155 op->op = GRPC_OP_RECV_INITIAL_METADATA;
156 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
157 op->flags = 0;
158 op->reserved = nullptr;
159 op++;
160 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
161 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
162 op->data.recv_status_on_client.status = &status;
163 op->data.recv_status_on_client.status_details = &details;
164 op->flags = 0;
165 op->reserved = nullptr;
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 error =
172 grpc_server_request_call(f.server, &s, &call_details,
173 &request_metadata_recv, f.cq, f.cq, tag(101));
174 GPR_ASSERT(GRPC_CALL_OK == error);
175
176 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
177 cq_verify(cqv);
178
179 memset(ops, 0, sizeof(ops));
180 op = ops;
181 op->op = GRPC_OP_SEND_INITIAL_METADATA;
182 op->data.send_initial_metadata.count = 0;
183 op->flags = 0;
184 op->reserved = nullptr;
185 op++;
186 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
187 op->data.send_status_from_server.trailing_metadata_count = 0;
188 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
189 grpc_slice status_string = grpc_slice_from_static_string("xyz");
190 op->data.send_status_from_server.status_details = &status_string;
191 op->flags = 0;
192 op->reserved = nullptr;
193 op++;
194 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
195 op->data.recv_close_on_server.cancelled = &was_cancelled;
196 op->flags = 0;
197 op->reserved = nullptr;
198 op++;
199 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
200 nullptr);
201 GPR_ASSERT(GRPC_CALL_OK == error);
202
203 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
204 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
205 cq_verify(cqv);
206
207 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
208 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
209
210 grpc_slice_unref(details);
211 grpc_metadata_array_destroy(&initial_metadata_recv);
212 grpc_metadata_array_destroy(&trailing_metadata_recv);
213 grpc_metadata_array_destroy(&request_metadata_recv);
214 grpc_call_details_destroy(&call_details);
215
216 grpc_call_unref(s);
217 grpc_call_unref(c);
218
219 cq_verifier_destroy(cqv);
220
221 grpc_byte_buffer_destroy(request_payload);
222 grpc_byte_buffer_destroy(request_payload_recv);
223
224 end_test(&f);
225 config.tear_down_data(&f);
226
227 const gpr_timespec end_time = gpr_now(GPR_CLOCK_MONOTONIC);
228 const gpr_timespec max_latency = gpr_time_sub(end_time, start_time);
229
230 // Perform checks after test tear-down
231 // Guards against the case that there's outstanding channel-related work on a
232 // call prior to verification
233 gpr_mu_lock(&g_mu);
234 GPR_ASSERT(gpr_time_cmp(max_latency, g_client_latency) >= 0);
235 GPR_ASSERT(gpr_time_cmp(gpr_time_0(GPR_TIMESPAN), g_client_latency) <= 0);
236 GPR_ASSERT(gpr_time_cmp(max_latency, g_server_latency) >= 0);
237 GPR_ASSERT(gpr_time_cmp(gpr_time_0(GPR_TIMESPAN), g_server_latency) <= 0);
238 // Server latency should always be smaller than client latency, however since
239 // we only calculate latency at destruction time, and that might mean that we
240 // need to wait for outstanding channel-related work, this isn't verifiable
241 // right now (the server MAY hold on to the call for longer than the client).
242 // GPR_ASSERT(gpr_time_cmp(g_server_latency, g_client_latency) < 0);
243 gpr_mu_unlock(&g_mu);
244 }
245
246 /*******************************************************************************
247 * Test latency filter
248 */
249
init_call_elem(grpc_call_element * elem,const grpc_call_element_args * args)250 static grpc_error* init_call_elem(grpc_call_element* elem,
251 const grpc_call_element_args* args) {
252 return GRPC_ERROR_NONE;
253 }
254
client_destroy_call_elem(grpc_call_element * elem,const grpc_call_final_info * final_info,grpc_closure * ignored)255 static void client_destroy_call_elem(grpc_call_element* elem,
256 const grpc_call_final_info* final_info,
257 grpc_closure* ignored) {
258 gpr_mu_lock(&g_mu);
259 g_client_latency = final_info->stats.latency;
260 gpr_mu_unlock(&g_mu);
261 }
262
server_destroy_call_elem(grpc_call_element * elem,const grpc_call_final_info * final_info,grpc_closure * ignored)263 static void server_destroy_call_elem(grpc_call_element* elem,
264 const grpc_call_final_info* final_info,
265 grpc_closure* ignored) {
266 gpr_mu_lock(&g_mu);
267 g_server_latency = final_info->stats.latency;
268 gpr_mu_unlock(&g_mu);
269 }
270
init_channel_elem(grpc_channel_element * elem,grpc_channel_element_args * args)271 static grpc_error* init_channel_elem(grpc_channel_element* elem,
272 grpc_channel_element_args* args) {
273 return GRPC_ERROR_NONE;
274 }
275
destroy_channel_elem(grpc_channel_element * elem)276 static void destroy_channel_elem(grpc_channel_element* elem) {}
277
278 static const grpc_channel_filter test_client_filter = {
279 grpc_call_next_op,
280 grpc_channel_next_op,
281 0,
282 init_call_elem,
283 grpc_call_stack_ignore_set_pollset_or_pollset_set,
284 client_destroy_call_elem,
285 0,
286 init_channel_elem,
287 destroy_channel_elem,
288 grpc_channel_next_get_info,
289 "client_filter_latency"};
290
291 static const grpc_channel_filter test_server_filter = {
292 grpc_call_next_op,
293 grpc_channel_next_op,
294 0,
295 init_call_elem,
296 grpc_call_stack_ignore_set_pollset_or_pollset_set,
297 server_destroy_call_elem,
298 0,
299 init_channel_elem,
300 destroy_channel_elem,
301 grpc_channel_next_get_info,
302 "server_filter_latency"};
303
304 /*******************************************************************************
305 * Registration
306 */
307
maybe_add_filter(grpc_channel_stack_builder * builder,void * arg)308 static bool maybe_add_filter(grpc_channel_stack_builder* builder, void* arg) {
309 grpc_channel_filter* filter = static_cast<grpc_channel_filter*>(arg);
310 if (g_enable_filter) {
311 // Want to add the filter as close to the end as possible, to make
312 // sure that all of the filters work well together. However, we
313 // can't add it at the very end, because the connected channel filter
314 // must be the last one. So we add it right before the last one.
315 grpc_channel_stack_builder_iterator* it =
316 grpc_channel_stack_builder_create_iterator_at_last(builder);
317 GPR_ASSERT(grpc_channel_stack_builder_move_prev(it));
318 const bool retval = grpc_channel_stack_builder_add_filter_before(
319 it, filter, nullptr, nullptr);
320 grpc_channel_stack_builder_iterator_destroy(it);
321 return retval;
322 } else {
323 return true;
324 }
325 }
326
init_plugin(void)327 static void init_plugin(void) {
328 gpr_mu_init(&g_mu);
329 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
330 maybe_add_filter,
331 (void*)&test_client_filter);
332 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
333 maybe_add_filter,
334 (void*)&test_client_filter);
335 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
336 maybe_add_filter,
337 (void*)&test_server_filter);
338 }
339
destroy_plugin(void)340 static void destroy_plugin(void) { gpr_mu_destroy(&g_mu); }
341
filter_latency(grpc_end2end_test_config config)342 void filter_latency(grpc_end2end_test_config config) {
343 g_enable_filter = true;
344 test_request(config);
345 g_enable_filter = false;
346 }
347
filter_latency_pre_init(void)348 void filter_latency_pre_init(void) {
349 grpc_register_plugin(init_plugin, destroy_plugin);
350 }
351