@@ -30,10 +30,10 @@ pub struct ContextInternalInner {
30
30
pub ( crate ) read : InputReceiver ,
31
31
pub ( crate ) write : OutputSender ,
32
32
pub ( super ) handler_state : HandlerStateNotifier ,
33
- // Flag to indicate whether span replay attribute should be set
34
- // When replaying this is set on the sys call
35
- // When not replaying this is reset on the sys call that transitioned the state
36
- pub ( super ) tracing_replaying_flag : bool ,
33
+
34
+ /// We remember here the state of the span replaying field state, because setting it might be expensive (it's guarded behind locks and other stuff).
35
+ /// For details, see [ContextInternalInner::maybe_flip_span_replaying_field]
36
+ pub ( super ) span_replaying_field_state : bool ,
37
37
}
38
38
39
39
impl ContextInternalInner {
@@ -48,11 +48,12 @@ impl ContextInternalInner {
48
48
read,
49
49
write,
50
50
handler_state,
51
- tracing_replaying_flag : true ,
51
+ span_replaying_field_state : false ,
52
52
}
53
53
}
54
54
55
55
pub ( super ) fn fail ( & mut self , e : Error ) {
56
+ self . maybe_flip_span_replaying_field ( ) ;
56
57
self . vm . notify_error (
57
58
CoreError :: new ( 500u16 , e. 0 . to_string ( ) )
58
59
. with_stacktrace ( Cow :: Owned ( format ! ( "{:#}" , e. 0 ) ) ) ,
@@ -61,19 +62,13 @@ impl ContextInternalInner {
61
62
self . handler_state . mark_error ( e) ;
62
63
}
63
64
64
- pub ( super ) fn set_tracing_replaying_flag ( & mut self ) {
65
- if !self . vm . is_processing ( ) {
66
- // Replay record is not yet set in the span
67
- if self . tracing_replaying_flag {
68
- tracing:: Span :: current ( ) . record ( "replaying" , true ) ;
69
- self . tracing_replaying_flag = false ;
70
- }
71
- } else {
72
- // Replay record is not yet reset in the span
73
- if !self . tracing_replaying_flag {
74
- tracing:: Span :: current ( ) . record ( "replaying" , false ) ;
75
- self . tracing_replaying_flag = true ;
76
- }
65
+ pub ( super ) fn maybe_flip_span_replaying_field ( & mut self ) {
66
+ if !self . span_replaying_field_state && self . vm . is_replaying ( ) {
67
+ tracing:: Span :: current ( ) . record ( "restate.sdk.is_replaying" , true ) ;
68
+ self . span_replaying_field_state = true ;
69
+ } else if self . span_replaying_field_state && !self . vm . is_replaying ( ) {
70
+ tracing:: Span :: current ( ) . record ( "restate.sdk.is_replaying" , false ) ;
71
+ self . span_replaying_field_state = false ;
77
72
}
78
73
}
79
74
}
@@ -211,6 +206,7 @@ impl ContextInternal {
211
206
} ,
212
207
) )
213
208
} ) ;
209
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
214
210
215
211
match input_result {
216
212
Ok ( Ok ( i) ) => {
@@ -244,6 +240,7 @@ impl ContextInternal {
244
240
) -> impl Future < Output = Result < Option < T > , TerminalError > > + Send {
245
241
let mut inner_lock = must_lock ! ( self . inner) ;
246
242
let handle = unwrap_or_trap ! ( inner_lock, inner_lock. vm. sys_state_get( key. to_owned( ) ) ) ;
243
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
247
244
248
245
let poll_future = get_async_result ( Arc :: clone ( & self . inner ) , handle) . map ( |res| match res {
249
246
Ok ( Value :: Void ) => Ok ( Ok ( None ) ) ,
@@ -267,6 +264,7 @@ impl ContextInternal {
267
264
pub fn get_keys ( & self ) -> impl Future < Output = Result < Vec < String > , TerminalError > > + Send {
268
265
let mut inner_lock = must_lock ! ( self . inner) ;
269
266
let handle = unwrap_or_trap ! ( inner_lock, inner_lock. vm. sys_state_get_keys( ) ) ;
267
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
270
268
271
269
let poll_future = get_async_result ( Arc :: clone ( & self . inner ) , handle) . map ( |res| match res {
272
270
Ok ( Value :: Failure ( f) ) => Ok ( Err ( f. into ( ) ) ) ,
@@ -287,6 +285,7 @@ impl ContextInternal {
287
285
match t. serialize ( ) {
288
286
Ok ( b) => {
289
287
let _ = inner_lock. vm . sys_state_set ( key. to_owned ( ) , b) ;
288
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
290
289
}
291
290
Err ( e) => {
292
291
inner_lock. fail ( Error :: serialization ( "set_state" , e) ) ;
@@ -295,11 +294,15 @@ impl ContextInternal {
295
294
}
296
295
297
296
pub fn clear ( & self , key : & str ) {
298
- let _ = must_lock ! ( self . inner) . vm . sys_state_clear ( key. to_string ( ) ) ;
297
+ let mut inner_lock = must_lock ! ( self . inner) ;
298
+ let _ = inner_lock. vm . sys_state_clear ( key. to_string ( ) ) ;
299
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
299
300
}
300
301
301
302
pub fn clear_all ( & self ) {
302
- let _ = must_lock ! ( self . inner) . vm . sys_state_clear_all ( ) ;
303
+ let mut inner_lock = must_lock ! ( self . inner) ;
304
+ let _ = inner_lock. vm . sys_state_clear_all ( ) ;
305
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
303
306
}
304
307
305
308
pub fn sleep (
@@ -314,6 +317,7 @@ impl ContextInternal {
314
317
inner_lock,
315
318
inner_lock. vm. sys_sleep( now + sleep_duration, Some ( now) )
316
319
) ;
320
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
317
321
318
322
let poll_future = get_async_result ( Arc :: clone ( & self . inner ) , handle) . map ( |res| match res {
319
323
Ok ( Value :: Void ) => Ok ( Ok ( ( ) ) ) ,
@@ -349,6 +353,7 @@ impl ContextInternal {
349
353
) ;
350
354
351
355
let call_handle = unwrap_or_trap ! ( inner_lock, inner_lock. vm. sys_call( target, input) ) ;
356
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
352
357
drop ( inner_lock) ;
353
358
354
359
// Let's prepare the two futures here
@@ -432,6 +437,7 @@ impl ContextInternal {
432
437
return Either :: Right ( TrapFuture :: < ( ) > :: default ( ) ) ;
433
438
}
434
439
} ;
440
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
435
441
drop ( inner_lock) ;
436
442
437
443
let invocation_id_fut = InterceptErrorFuture :: new (
@@ -473,6 +479,7 @@ impl ContextInternal {
473
479
) {
474
480
let mut inner_lock = must_lock ! ( self . inner) ;
475
481
let maybe_awakeable_id_and_handle = inner_lock. vm . sys_awakeable ( ) ;
482
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
476
483
477
484
let ( awakeable_id, handle) = match maybe_awakeable_id_and_handle {
478
485
Ok ( ( s, handle) ) => ( s, handle) ,
@@ -533,6 +540,7 @@ impl ContextInternal {
533
540
) -> impl Future < Output = Result < T , TerminalError > > + Send {
534
541
let mut inner_lock = must_lock ! ( self . inner) ;
535
542
let handle = unwrap_or_trap ! ( inner_lock, inner_lock. vm. sys_get_promise( name. to_owned( ) ) ) ;
543
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
536
544
drop ( inner_lock) ;
537
545
538
546
let poll_future = get_async_result ( Arc :: clone ( & self . inner ) , handle) . map ( |res| match res {
@@ -558,6 +566,7 @@ impl ContextInternal {
558
566
) -> impl Future < Output = Result < Option < T > , TerminalError > > + Send {
559
567
let mut inner_lock = must_lock ! ( self . inner) ;
560
568
let handle = unwrap_or_trap ! ( inner_lock, inner_lock. vm. sys_peek_promise( name. to_owned( ) ) ) ;
569
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
561
570
drop ( inner_lock) ;
562
571
563
572
let poll_future = get_async_result ( Arc :: clone ( & self . inner ) , handle) . map ( |res| match res {
@@ -646,6 +655,7 @@ impl ContextInternal {
646
655
} ;
647
656
648
657
let _ = inner_lock. vm . sys_write_output ( res_to_write) ;
658
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
649
659
}
650
660
651
661
pub fn end ( & self ) {
@@ -880,6 +890,7 @@ impl<InvIdFut: Future<Output = Result<String, TerminalError>> + Send> Invocation
880
890
let inv_id = cloned_invocation_id_fut. await ?;
881
891
let mut inner_lock = must_lock ! ( cloned_ctx) ;
882
892
let _ = inner_lock. vm . sys_cancel_invocation ( inv_id) ;
893
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
883
894
drop ( inner_lock) ;
884
895
Ok ( ( ) )
885
896
}
@@ -924,6 +935,7 @@ where
924
935
let inv_id = cloned_invocation_id_fut. await ?;
925
936
let mut inner_lock = must_lock ! ( cloned_ctx) ;
926
937
let _ = inner_lock. vm . sys_cancel_invocation ( inv_id) ;
938
+ inner_lock. maybe_flip_span_replaying_field ( ) ;
927
939
drop ( inner_lock) ;
928
940
Ok ( ( ) )
929
941
}
0 commit comments