-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathretry.rs
579 lines (530 loc) · 17.7 KB
/
retry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use crate::{Result, RetryConfig, ServerGatewayApis, ServerGatewayOptions, WorkflowTaskCompletion};
use backoff::{backoff::Backoff, ExponentialBackoff};
use futures_retry::{ErrorHandler, FutureRetry, RetryPolicy};
use std::{fmt::Debug, future::Future, time::Duration};
use temporal_sdk_core_protos::{
coresdk::{common::Payload, workflow_commands::QueryResult},
temporal::api::{
common::v1::Payloads, enums::v1::WorkflowTaskFailedCause, failure::v1::Failure,
query::v1::WorkflowQuery, workflowservice::v1::*,
},
TaskToken,
};
use tonic::Code;
/// List of gRPC error codes that client will retry.
pub const RETRYABLE_ERROR_CODES: [Code; 7] = [
Code::DataLoss,
Code::Internal,
Code::Unknown,
Code::ResourceExhausted,
Code::Aborted,
Code::OutOfRange,
Code::Unavailable,
];
/// A wrapper for a [ServerGatewayApis] or [crate::WorkflowService] implementor which performs
/// auto-retries
#[derive(Debug, Clone)]
pub struct RetryGateway<SG> {
gateway: SG,
retry_config: RetryConfig,
}
impl<SG> RetryGateway<SG> {
/// Use the provided retry config with the provided gateway
pub const fn new(gateway: SG, retry_config: RetryConfig) -> Self {
Self {
gateway,
retry_config,
}
}
}
impl<SG> RetryGateway<SG> {
/// Return the inner client type
pub fn get_client(&self) -> &SG {
&self.gateway
}
/// Return the inner client type mutably
pub fn get_client_mut(&mut self) -> &mut SG {
&mut self.gateway
}
/// Disable retry and return the inner client type
pub fn into_inner(self) -> SG {
self.gateway
}
/// Wraps a call to the underlying client with retry capability.
///
/// This is the "old" path used by higher-level [ServerGatewayApis] implementors
pub(crate) async fn call_with_retry<R, F, Fut>(
&self,
factory: F,
call_name: &'static str,
) -> Result<R>
where
F: Fn() -> Fut + Unpin,
Fut: Future<Output = Result<R>>,
{
let rtc = self.get_retry_config(call_name);
let res = Self::make_future_retry(rtc, factory, call_name).await;
Ok(res.map_err(|(e, _attempt)| e)?.0)
}
pub(crate) fn get_retry_config(&self, call_name: &'static str) -> RetryConfig {
let call_type = Self::determine_call_type(call_name);
match call_type {
CallType::Normal => self.retry_config.clone(),
CallType::LongPoll => RetryConfig::poll_retry_policy(),
}
}
pub(crate) fn make_future_retry<R, F, Fut>(
rtc: RetryConfig,
factory: F,
call_name: &'static str,
) -> FutureRetry<F, TonicErrorHandler>
where
F: FnMut() -> Fut + Unpin,
Fut: Future<Output = Result<R>>,
{
let call_type = Self::determine_call_type(call_name);
FutureRetry::new(factory, TonicErrorHandler::new(rtc, call_type, call_name))
}
fn determine_call_type(call_name: &str) -> CallType {
match call_name {
"poll_workflow_task" | "poll_activity_task" => CallType::LongPoll,
_ => CallType::Normal,
}
}
}
#[derive(Debug)]
pub(crate) struct TonicErrorHandler {
backoff: ExponentialBackoff,
max_retries: usize,
call_type: CallType,
call_name: &'static str,
}
impl TonicErrorHandler {
fn new(cfg: RetryConfig, call_type: CallType, call_name: &'static str) -> Self {
Self {
max_retries: cfg.max_retries,
backoff: cfg.into(),
call_type,
call_name,
}
}
const fn should_log_retry_warning(&self, cur_attempt: usize) -> bool {
// Warn on more than 5 retries for unlimited retrying
if self.max_retries == 0 && cur_attempt > 5 {
return true;
}
// Warn if the attempts are more than 50% of max retries
if self.max_retries > 0 && cur_attempt * 2 >= self.max_retries {
return true;
}
false
}
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum CallType {
Normal,
LongPoll,
}
impl ErrorHandler<tonic::Status> for TonicErrorHandler {
type OutError = tonic::Status;
fn handle(&mut self, current_attempt: usize, e: tonic::Status) -> RetryPolicy<tonic::Status> {
// 0 max retries means unlimited retries
if self.max_retries > 0 && current_attempt >= self.max_retries {
return RetryPolicy::ForwardError(e);
}
// Long polls are OK with being cancelled or running into the timeout because there's
// nothing to do but retry anyway
let long_poll_allowed = self.call_type == CallType::LongPoll
&& [Code::Cancelled, Code::DeadlineExceeded].contains(&e.code());
if RETRYABLE_ERROR_CODES.contains(&e.code()) || long_poll_allowed {
if current_attempt == 1 {
debug!(error=?e, "gRPC call {} failed on first attempt", self.call_name);
} else if self.should_log_retry_warning(current_attempt) {
warn!(error=?e, "gRPC call {} retried {} times", self.call_name, current_attempt);
}
match self.backoff.next_backoff() {
None => RetryPolicy::ForwardError(e), // None is returned when we've ran out of time
Some(backoff) => {
if cfg!(test) {
// Allow unit tests to do lots of retries quickly. This does *not* apply
// during integration testing, importantly.
RetryPolicy::WaitRetry(Duration::from_millis(1))
} else {
RetryPolicy::WaitRetry(backoff)
}
}
}
} else {
RetryPolicy::ForwardError(e)
}
}
}
macro_rules! retry_call {
($myself:ident, $call_name:ident) => { retry_call!($myself, $call_name,) };
($myself:ident, $call_name:ident, $($args:expr),*) => {{
let call_name_str = stringify!($call_name);
let fact = || { $myself.get_client().$call_name($($args,)*)};
$myself.call_with_retry(fact, call_name_str).await
}}
}
// Ideally, this would be auto-implemented for anything that implements the raw client, but that
// breaks all our retry gateways which use a mock since it's based on this trait currently. Ideally
// we would create an automock for the WorkflowServiceClient copy-paste trait and use that, but
// that's a huge pain. Maybe one day tonic will provide traits.
#[async_trait::async_trait]
impl<SG> ServerGatewayApis for RetryGateway<SG>
where
SG: ServerGatewayApis + Send + Sync + 'static,
{
async fn start_workflow(
&self,
input: Vec<Payload>,
task_queue: String,
workflow_id: String,
workflow_type: String,
task_timeout: Option<Duration>,
) -> Result<StartWorkflowExecutionResponse> {
retry_call!(
self,
start_workflow,
input.clone(),
task_queue.clone(),
workflow_id.clone(),
workflow_type.clone(),
task_timeout
)
}
async fn poll_workflow_task(
&self,
task_queue: String,
is_sticky: bool,
) -> Result<PollWorkflowTaskQueueResponse> {
retry_call!(self, poll_workflow_task, task_queue.clone(), is_sticky)
}
async fn poll_activity_task(
&self,
task_queue: String,
) -> Result<PollActivityTaskQueueResponse> {
retry_call!(self, poll_activity_task, task_queue.clone())
}
async fn reset_sticky_task_queue(
&self,
workflow_id: String,
run_id: String,
) -> Result<ResetStickyTaskQueueResponse> {
retry_call!(
self,
reset_sticky_task_queue,
workflow_id.clone(),
run_id.clone()
)
}
async fn complete_workflow_task(
&self,
request: WorkflowTaskCompletion,
) -> Result<RespondWorkflowTaskCompletedResponse> {
retry_call!(self, complete_workflow_task, request.clone())
}
async fn complete_activity_task(
&self,
task_token: TaskToken,
result: Option<Payloads>,
) -> Result<RespondActivityTaskCompletedResponse> {
retry_call!(
self,
complete_activity_task,
task_token.clone(),
result.clone()
)
}
async fn record_activity_heartbeat(
&self,
task_token: TaskToken,
details: Option<Payloads>,
) -> Result<RecordActivityTaskHeartbeatResponse> {
retry_call!(
self,
record_activity_heartbeat,
task_token.clone(),
details.clone()
)
}
async fn cancel_activity_task(
&self,
task_token: TaskToken,
details: Option<Payloads>,
) -> Result<RespondActivityTaskCanceledResponse> {
retry_call!(
self,
cancel_activity_task,
task_token.clone(),
details.clone()
)
}
async fn fail_activity_task(
&self,
task_token: TaskToken,
failure: Option<Failure>,
) -> Result<RespondActivityTaskFailedResponse> {
retry_call!(
self,
fail_activity_task,
task_token.clone(),
failure.clone()
)
}
async fn fail_workflow_task(
&self,
task_token: TaskToken,
cause: WorkflowTaskFailedCause,
failure: Option<Failure>,
) -> Result<RespondWorkflowTaskFailedResponse> {
retry_call!(
self,
fail_workflow_task,
task_token.clone(),
cause,
failure.clone()
)
}
async fn signal_workflow_execution(
&self,
workflow_id: String,
run_id: String,
signal_name: String,
payloads: Option<Payloads>,
) -> Result<SignalWorkflowExecutionResponse> {
retry_call!(
self,
signal_workflow_execution,
workflow_id.clone(),
run_id.clone(),
signal_name.clone(),
payloads.clone()
)
}
async fn query_workflow_execution(
&self,
workflow_id: String,
run_id: String,
query: WorkflowQuery,
) -> Result<QueryWorkflowResponse> {
retry_call!(
self,
query_workflow_execution,
workflow_id.clone(),
run_id.clone(),
query.clone()
)
}
async fn describe_workflow_execution(
&self,
workflow_id: String,
run_id: Option<String>,
) -> Result<DescribeWorkflowExecutionResponse> {
retry_call!(
self,
describe_workflow_execution,
workflow_id.clone(),
run_id.clone()
)
}
async fn get_workflow_execution_history(
&self,
workflow_id: String,
run_id: Option<String>,
page_token: Vec<u8>,
) -> Result<GetWorkflowExecutionHistoryResponse> {
retry_call!(
self,
get_workflow_execution_history,
workflow_id.clone(),
run_id.clone(),
page_token.clone()
)
}
async fn respond_legacy_query(
&self,
task_token: TaskToken,
query_result: QueryResult,
) -> Result<RespondQueryTaskCompletedResponse> {
retry_call!(
self,
respond_legacy_query,
task_token.clone(),
query_result.clone()
)
}
async fn cancel_workflow_execution(
&self,
workflow_id: String,
run_id: Option<String>,
) -> Result<RequestCancelWorkflowExecutionResponse> {
retry_call!(
self,
cancel_workflow_execution,
workflow_id.clone(),
run_id.clone()
)
}
async fn terminate_workflow_execution(
&self,
workflow_id: String,
run_id: Option<String>,
) -> Result<TerminateWorkflowExecutionResponse> {
retry_call!(
self,
terminate_workflow_execution,
workflow_id.clone(),
run_id.clone()
)
}
async fn list_namespaces(&self) -> Result<ListNamespacesResponse> {
retry_call!(self, list_namespaces,)
}
fn get_options(&self) -> &ServerGatewayOptions {
self.gateway.get_options()
}
fn namespace(&self) -> &str {
self.gateway.namespace()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::MockServerGatewayApis;
use tonic::Status;
#[tokio::test]
async fn non_retryable_errors() {
for code in [
Code::InvalidArgument,
Code::NotFound,
Code::AlreadyExists,
Code::PermissionDenied,
Code::FailedPrecondition,
Code::Cancelled,
Code::DeadlineExceeded,
Code::Unauthenticated,
Code::Unimplemented,
] {
let mut mock_gateway = MockServerGatewayApis::new();
mock_gateway
.expect_cancel_activity_task()
.returning(move |_, _| Err(Status::new(code, "non-retryable failure")))
.times(1);
let retry_gateway = RetryGateway::new(mock_gateway, Default::default());
let result = retry_gateway
.cancel_activity_task(vec![1].into(), None)
.await;
// Expecting an error after a single attempt, since there was a non-retryable error.
assert!(result.is_err());
}
}
#[tokio::test]
async fn long_poll_non_retryable_errors() {
for code in [
Code::InvalidArgument,
Code::NotFound,
Code::AlreadyExists,
Code::PermissionDenied,
Code::FailedPrecondition,
Code::Unauthenticated,
Code::Unimplemented,
] {
let mut mock_gateway = MockServerGatewayApis::new();
mock_gateway
.expect_poll_workflow_task()
.returning(move |_, _| Err(Status::new(code, "non-retryable failure")))
.times(1);
mock_gateway
.expect_poll_activity_task()
.returning(move |_| Err(Status::new(code, "non-retryable failure")))
.times(1);
let retry_gateway = RetryGateway::new(mock_gateway, Default::default());
let result = retry_gateway
.poll_workflow_task("tq".to_string(), false)
.await;
assert!(result.is_err());
let result = retry_gateway.poll_activity_task("tq".to_string()).await;
assert!(result.is_err());
}
}
#[tokio::test]
async fn retryable_errors() {
for code in RETRYABLE_ERROR_CODES {
let mut mock_gateway = MockServerGatewayApis::new();
mock_gateway
.expect_cancel_activity_task()
.returning(move |_, _| Err(Status::new(code, "retryable failure")))
.times(3);
mock_gateway
.expect_cancel_activity_task()
.returning(|_, _| Ok(Default::default()))
.times(1);
let retry_gateway = RetryGateway::new(mock_gateway, Default::default());
let result = retry_gateway
.cancel_activity_task(vec![1].into(), None)
.await;
// Expecting successful response after retries
assert!(result.is_ok());
}
}
#[tokio::test]
async fn long_poll_retries_forever() {
let mut mock_gateway = MockServerGatewayApis::new();
mock_gateway
.expect_poll_workflow_task()
.returning(move |_, _| Err(Status::new(Code::Unknown, "retryable failure")))
.times(50);
mock_gateway
.expect_poll_workflow_task()
.returning(|_, _| Ok(Default::default()))
.times(1);
mock_gateway
.expect_poll_activity_task()
.returning(move |_| Err(Status::new(Code::Unknown, "retryable failure")))
.times(50);
mock_gateway
.expect_poll_activity_task()
.returning(|_| Ok(Default::default()))
.times(1);
let retry_gateway = RetryGateway::new(mock_gateway, Default::default());
let result = retry_gateway
.poll_workflow_task("tq".to_string(), false)
.await;
assert!(result.is_ok());
let result = retry_gateway.poll_activity_task("tq".to_string()).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn long_poll_retries_deadline_exceeded() {
// For some reason we will get cancelled in these situations occasionally (always?) too
for code in [Code::Cancelled, Code::DeadlineExceeded] {
let mut mock_gateway = MockServerGatewayApis::new();
mock_gateway
.expect_poll_workflow_task()
.returning(move |_, _| Err(Status::new(code, "retryable failure")))
.times(5);
mock_gateway
.expect_poll_workflow_task()
.returning(|_, _| Ok(Default::default()))
.times(1);
mock_gateway
.expect_poll_activity_task()
.returning(move |_| Err(Status::new(code, "retryable failure")))
.times(5);
mock_gateway
.expect_poll_activity_task()
.returning(|_| Ok(Default::default()))
.times(1);
let retry_gateway = RetryGateway::new(mock_gateway, Default::default());
let result = retry_gateway
.poll_workflow_task("tq".to_string(), false)
.await;
assert!(result.is_ok());
let result = retry_gateway.poll_activity_task("tq".to_string()).await;
assert!(result.is_ok());
}
}
}