blob: f9668be0faa8646c9a4ce06a5b3cd789781359f3 [file] [log] [blame]
Mark D. Roth14c072c2016-08-26 08:31:34 -07001//
2// Copyright 2016, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30//
31
32#include "src/core/lib/channel/deadline_filter.h"
33
34#include <stdbool.h>
35#include <string.h>
36
Mark D. Rothf28763c2016-09-14 15:18:40 -070037#include <grpc/support/alloc.h>
Mark D. Roth14c072c2016-08-26 08:31:34 -070038#include <grpc/support/log.h>
Mark D. Roth1bbe6cb2016-08-31 08:33:37 -070039#include <grpc/support/sync.h>
Mark D. Roth14c072c2016-08-26 08:31:34 -070040#include <grpc/support/time.h>
41
Mark D. Rothf28763c2016-09-14 15:18:40 -070042#include "src/core/lib/iomgr/exec_ctx.h"
Mark D. Roth14c072c2016-08-26 08:31:34 -070043#include "src/core/lib/iomgr/timer.h"
Craig Tillera59c16c2016-10-31 07:25:01 -070044#include "src/core/lib/slice/slice_internal.h"
Mark D. Roth14c072c2016-08-26 08:31:34 -070045
Mark D. Roth72f6da82016-09-02 13:42:38 -070046//
47// grpc_deadline_state
48//
49
50// Timer callback.
Mark D. Roth932b10c2016-09-09 08:44:30 -070051static void timer_callback(grpc_exec_ctx* exec_ctx, void* arg,
52 grpc_error* error) {
Mark D. Roth72f6da82016-09-02 13:42:38 -070053 grpc_call_element* elem = arg;
54 grpc_deadline_state* deadline_state = elem->call_data;
Mark D. Roth72f6da82016-09-02 13:42:38 -070055 if (error != GRPC_ERROR_CANCELLED) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080056 grpc_call_element_signal_error(
57 exec_ctx, elem,
58 grpc_error_set_int(GRPC_ERROR_CREATE("Deadline Exceeded"),
59 GRPC_ERROR_INT_GRPC_STATUS,
60 GRPC_STATUS_DEADLINE_EXCEEDED));
Mark D. Roth72f6da82016-09-02 13:42:38 -070061 }
62 GRPC_CALL_STACK_UNREF(exec_ctx, deadline_state->call_stack, "deadline_timer");
63}
64
65// Starts the deadline timer.
Mark D. Roth932b10c2016-09-09 08:44:30 -070066static void start_timer_if_needed(grpc_exec_ctx* exec_ctx,
Mark D. Roth72f6da82016-09-02 13:42:38 -070067 grpc_call_element* elem,
68 gpr_timespec deadline) {
Craig Tiller4447c2c2017-02-16 12:35:13 -080069 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
Craig Tiller0a77de82017-02-16 12:39:33 -080070 if (gpr_time_cmp(deadline, gpr_inf_future(GPR_CLOCK_MONOTONIC)) == 0) {
Craig Tiller4447c2c2017-02-16 12:35:13 -080071 return;
72 }
Mark D. Roth72f6da82016-09-02 13:42:38 -070073 grpc_deadline_state* deadline_state = elem->call_data;
Craig Tillerc84886b2017-02-16 13:10:38 -080074 grpc_deadline_timer_state cur_state;
75 grpc_closure* closure = NULL;
76retry:
77 cur_state =
78 (grpc_deadline_timer_state)gpr_atm_acq_load(&deadline_state->timer_state);
79 switch (cur_state) {
80 case GRPC_DEADLINE_STATE_PENDING:
Craig Tillerac942f42017-02-22 09:13:14 -080081 // Note: We do not start the timer if there is already a timer
Craig Tillerc84886b2017-02-16 13:10:38 -080082 return;
83 case GRPC_DEADLINE_STATE_FINISHED:
84 if (gpr_atm_rel_cas(&deadline_state->timer_state,
85 GRPC_DEADLINE_STATE_FINISHED,
86 GRPC_DEADLINE_STATE_PENDING)) {
Craig Tillerac942f42017-02-22 09:13:14 -080087 // If we've already created and destroyed a timer, we always create a
88 // new closure: we have no other guarantee that the inlined closure is
89 // not in use (it may hold a pending call to timer_callback)
Craig Tillerc84886b2017-02-16 13:10:38 -080090 closure = grpc_closure_create(timer_callback, elem,
91 grpc_schedule_on_exec_ctx);
92 } else {
93 goto retry;
Craig Tiller4447c2c2017-02-16 12:35:13 -080094 }
Craig Tillerc84886b2017-02-16 13:10:38 -080095 break;
96 case GRPC_DEADLINE_STATE_INITIAL:
97 if (gpr_atm_rel_cas(&deadline_state->timer_state,
98 GRPC_DEADLINE_STATE_INITIAL,
99 GRPC_DEADLINE_STATE_PENDING)) {
100 closure =
101 grpc_closure_init(&deadline_state->timer_callback, timer_callback,
102 elem, grpc_schedule_on_exec_ctx);
103 } else {
104 goto retry;
105 }
106 break;
Craig Tiller4447c2c2017-02-16 12:35:13 -0800107 }
Craig Tillerc84886b2017-02-16 13:10:38 -0800108 GPR_ASSERT(closure);
Craig Tillerac942f42017-02-22 09:13:14 -0800109 GRPC_CALL_STACK_REF(deadline_state->call_stack, "deadline_timer");
Craig Tillerc84886b2017-02-16 13:10:38 -0800110 grpc_timer_init(exec_ctx, &deadline_state->timer, deadline, closure,
111 gpr_now(GPR_CLOCK_MONOTONIC));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700112}
113
114// Cancels the deadline timer.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700115static void cancel_timer_if_needed(grpc_exec_ctx* exec_ctx,
116 grpc_deadline_state* deadline_state) {
Craig Tillerac942f42017-02-22 09:13:14 -0800117 if (gpr_atm_rel_cas(&deadline_state->timer_state, GRPC_DEADLINE_STATE_PENDING,
118 GRPC_DEADLINE_STATE_FINISHED)) {
Craig Tillerc84886b2017-02-16 13:10:38 -0800119 grpc_timer_cancel(exec_ctx, &deadline_state->timer);
Craig Tillerac942f42017-02-22 09:13:14 -0800120 } else {
121 // timer was either in STATE_INITAL (nothing to cancel)
122 // OR in STATE_FINISHED (again nothing to cancel)
Craig Tiller4447c2c2017-02-16 12:35:13 -0800123 }
Mark D. Roth72f6da82016-09-02 13:42:38 -0700124}
125
126// Callback run when the call is complete.
Mark D. Roth932b10c2016-09-09 08:44:30 -0700127static void on_complete(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700128 grpc_deadline_state* deadline_state = arg;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700129 cancel_timer_if_needed(exec_ctx, deadline_state);
130 // Invoke the next callback.
Craig Tillerc84886b2017-02-16 13:10:38 -0800131 grpc_closure_run(exec_ctx, deadline_state->next_on_complete,
132 GRPC_ERROR_REF(error));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700133}
134
135// Inject our own on_complete callback into op.
136static void inject_on_complete_cb(grpc_deadline_state* deadline_state,
137 grpc_transport_stream_op* op) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700138 deadline_state->next_on_complete = op->on_complete;
Craig Tiller91031da2016-12-28 15:44:25 -0800139 grpc_closure_init(&deadline_state->on_complete, on_complete, deadline_state,
140 grpc_schedule_on_exec_ctx);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700141 op->on_complete = &deadline_state->on_complete;
142}
143
Mark D. Rothe40dd292016-10-05 14:58:37 -0700144void grpc_deadline_state_init(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
145 grpc_call_stack* call_stack) {
146 grpc_deadline_state* deadline_state = elem->call_data;
147 memset(deadline_state, 0, sizeof(*deadline_state));
148 deadline_state->call_stack = call_stack;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700149}
150
151void grpc_deadline_state_destroy(grpc_exec_ctx* exec_ctx,
152 grpc_call_element* elem) {
153 grpc_deadline_state* deadline_state = elem->call_data;
154 cancel_timer_if_needed(exec_ctx, deadline_state);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700155}
156
Mark D. Rothf28763c2016-09-14 15:18:40 -0700157// Callback and associated state for starting the timer after call stack
158// initialization has been completed.
159struct start_timer_after_init_state {
160 grpc_call_element* elem;
161 gpr_timespec deadline;
162 grpc_closure closure;
163};
164static void start_timer_after_init(grpc_exec_ctx* exec_ctx, void* arg,
165 grpc_error* error) {
166 struct start_timer_after_init_state* state = arg;
167 start_timer_if_needed(exec_ctx, state->elem, state->deadline);
168 gpr_free(state);
169}
170
Mark D. Rothe40dd292016-10-05 14:58:37 -0700171void grpc_deadline_state_start(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
172 gpr_timespec deadline) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700173 // Deadline will always be infinite on servers, so the timer will only be
174 // set on clients with a finite deadline.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700175 deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700176 if (gpr_time_cmp(deadline, gpr_inf_future(GPR_CLOCK_MONOTONIC)) != 0) {
177 // When the deadline passes, we indicate the failure by sending down
178 // an op with cancel_error set. However, we can't send down any ops
179 // until after the call stack is fully initialized. If we start the
180 // timer here, we have no guarantee that the timer won't pop before
181 // call stack initialization is finished. To avoid that problem, we
182 // create a closure to start the timer, and we schedule that closure
183 // to be run after call stack initialization is done.
184 struct start_timer_after_init_state* state = gpr_malloc(sizeof(*state));
185 state->elem = elem;
186 state->deadline = deadline;
Craig Tiller91031da2016-12-28 15:44:25 -0800187 grpc_closure_init(&state->closure, start_timer_after_init, state,
188 grpc_schedule_on_exec_ctx);
189 grpc_closure_sched(exec_ctx, &state->closure, GRPC_ERROR_NONE);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700190 }
Mark D. Roth72f6da82016-09-02 13:42:38 -0700191}
192
Mark D. Rothe40dd292016-10-05 14:58:37 -0700193void grpc_deadline_state_reset(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
194 gpr_timespec new_deadline) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700195 grpc_deadline_state* deadline_state = elem->call_data;
Craig Tiller4447c2c2017-02-16 12:35:13 -0800196 cancel_timer_if_needed(exec_ctx, deadline_state);
197 start_timer_if_needed(exec_ctx, elem, new_deadline);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700198}
199
200void grpc_deadline_state_client_start_transport_stream_op(
201 grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
202 grpc_transport_stream_op* op) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700203 grpc_deadline_state* deadline_state = elem->call_data;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800204 if (op->cancel_error != GRPC_ERROR_NONE) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700205 cancel_timer_if_needed(exec_ctx, deadline_state);
206 } else {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700207 // Make sure we know when the call is complete, so that we can cancel
208 // the timer.
209 if (op->recv_trailing_metadata != NULL) {
210 inject_on_complete_cb(deadline_state, op);
211 }
212 }
213}
214
215//
216// filter code
217//
218
Mark D. Roth72f6da82016-09-02 13:42:38 -0700219// Constructor for channel_data. Used for both client and server filters.
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800220static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx,
221 grpc_channel_element* elem,
222 grpc_channel_element_args* args) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700223 GPR_ASSERT(!args->is_last);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800224 return GRPC_ERROR_NONE;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700225}
226
227// Destructor for channel_data. Used for both client and server filters.
228static void destroy_channel_elem(grpc_exec_ctx* exec_ctx,
Mark D. Roth932b10c2016-09-09 08:44:30 -0700229 grpc_channel_element* elem) {}
Mark D. Roth72f6da82016-09-02 13:42:38 -0700230
Mark D. Roth14c072c2016-08-26 08:31:34 -0700231// Call data used for both client and server filter.
232typedef struct base_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700233 grpc_deadline_state deadline_state;
Mark D. Roth14c072c2016-08-26 08:31:34 -0700234} base_call_data;
235
236// Additional call data used only for the server filter.
237typedef struct server_call_data {
238 base_call_data base; // Must be first.
239 // The closure for receiving initial metadata.
240 grpc_closure recv_initial_metadata_ready;
241 // Received initial metadata batch.
242 grpc_metadata_batch* recv_initial_metadata;
243 // The original recv_initial_metadata_ready closure, which we chain to
244 // after our own closure is invoked.
245 grpc_closure* next_recv_initial_metadata_ready;
246} server_call_data;
247
Mark D. Roth14c072c2016-08-26 08:31:34 -0700248// Constructor for call_data. Used for both client and server filters.
Mark D. Roth932b10c2016-09-09 08:44:30 -0700249static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx,
Mark D. Roth14c072c2016-08-26 08:31:34 -0700250 grpc_call_element* elem,
Craig Tillerc52ba3a2017-02-15 22:57:43 -0800251 const grpc_call_element_args* args) {
Mark D. Roth14c072c2016-08-26 08:31:34 -0700252 // Note: size of call data is different between client and server.
Mark D. Rothf28763c2016-09-14 15:18:40 -0700253 memset(elem->call_data, 0, elem->filter->sizeof_call_data);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700254 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
255 grpc_deadline_state_start(exec_ctx, elem, args->deadline);
Mark D. Roth14c072c2016-08-26 08:31:34 -0700256 return GRPC_ERROR_NONE;
257}
258
259// Destructor for call_data. Used for both client and server filters.
260static void destroy_call_elem(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
261 const grpc_call_final_info* final_info,
262 void* and_free_memory) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700263 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothd2b45332016-08-26 11:18:00 -0700264}
265
Mark D. Roth14c072c2016-08-26 08:31:34 -0700266// Method for starting a call op for client filter.
267static void client_start_transport_stream_op(grpc_exec_ctx* exec_ctx,
268 grpc_call_element* elem,
269 grpc_transport_stream_op* op) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700270 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth14c072c2016-08-26 08:31:34 -0700271 // Chain to next filter.
272 grpc_call_next_op(exec_ctx, elem, op);
273}
274
275// Callback for receiving initial metadata on the server.
Mark D. Roth932b10c2016-09-09 08:44:30 -0700276static void recv_initial_metadata_ready(grpc_exec_ctx* exec_ctx, void* arg,
277 grpc_error* error) {
Mark D. Roth14c072c2016-08-26 08:31:34 -0700278 grpc_call_element* elem = arg;
279 server_call_data* calld = elem->call_data;
280 // Get deadline from metadata and start the timer if needed.
Mark D. Roth932b10c2016-09-09 08:44:30 -0700281 start_timer_if_needed(exec_ctx, elem, calld->recv_initial_metadata->deadline);
Mark D. Roth14c072c2016-08-26 08:31:34 -0700282 // Invoke the next callback.
283 calld->next_recv_initial_metadata_ready->cb(
284 exec_ctx, calld->next_recv_initial_metadata_ready->cb_arg, error);
285}
286
287// Method for starting a call op for server filter.
288static void server_start_transport_stream_op(grpc_exec_ctx* exec_ctx,
289 grpc_call_element* elem,
290 grpc_transport_stream_op* op) {
291 server_call_data* calld = elem->call_data;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800292 if (op->cancel_error != GRPC_ERROR_NONE) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700293 cancel_timer_if_needed(exec_ctx, &calld->base.deadline_state);
Mark D. Roth1bbe6cb2016-08-31 08:33:37 -0700294 } else {
295 // If we're receiving initial metadata, we need to get the deadline
296 // from the recv_initial_metadata_ready callback. So we inject our
297 // own callback into that hook.
298 if (op->recv_initial_metadata_ready != NULL) {
299 calld->next_recv_initial_metadata_ready = op->recv_initial_metadata_ready;
300 calld->recv_initial_metadata = op->recv_initial_metadata;
301 grpc_closure_init(&calld->recv_initial_metadata_ready,
Craig Tiller91031da2016-12-28 15:44:25 -0800302 recv_initial_metadata_ready, elem,
303 grpc_schedule_on_exec_ctx);
Mark D. Roth1bbe6cb2016-08-31 08:33:37 -0700304 op->recv_initial_metadata_ready = &calld->recv_initial_metadata_ready;
305 }
306 // Make sure we know when the call is complete, so that we can cancel
307 // the timer.
308 // Note that we trigger this on recv_trailing_metadata, even though
309 // the client never sends trailing metadata, because this is the
310 // hook that tells us when the call is complete on the server side.
311 if (op->recv_trailing_metadata != NULL) {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700312 inject_on_complete_cb(&calld->base.deadline_state, op);
Mark D. Roth1bbe6cb2016-08-31 08:33:37 -0700313 }
Mark D. Rothd2b45332016-08-26 11:18:00 -0700314 }
Mark D. Roth14c072c2016-08-26 08:31:34 -0700315 // Chain to next filter.
316 grpc_call_next_op(exec_ctx, elem, op);
317}
318
319const grpc_channel_filter grpc_client_deadline_filter = {
320 client_start_transport_stream_op,
321 grpc_channel_next_op,
322 sizeof(base_call_data),
323 init_call_elem,
324 grpc_call_stack_ignore_set_pollset_or_pollset_set,
325 destroy_call_elem,
Mark D. Rothb3405f0a2016-09-09 08:46:28 -0700326 0, // sizeof(channel_data)
Mark D. Roth14c072c2016-08-26 08:31:34 -0700327 init_channel_elem,
328 destroy_channel_elem,
329 grpc_call_next_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -0700330 grpc_channel_next_get_info,
Mark D. Roth14c072c2016-08-26 08:31:34 -0700331 "deadline",
332};
333
334const grpc_channel_filter grpc_server_deadline_filter = {
335 server_start_transport_stream_op,
336 grpc_channel_next_op,
337 sizeof(server_call_data),
338 init_call_elem,
339 grpc_call_stack_ignore_set_pollset_or_pollset_set,
340 destroy_call_elem,
Mark D. Rothb3405f0a2016-09-09 08:46:28 -0700341 0, // sizeof(channel_data)
Mark D. Roth14c072c2016-08-26 08:31:34 -0700342 init_channel_elem,
343 destroy_channel_elem,
344 grpc_call_next_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -0700345 grpc_channel_next_get_info,
Mark D. Roth14c072c2016-08-26 08:31:34 -0700346 "deadline",
347};