-
Notifications
You must be signed in to change notification settings - Fork 296
/
response.rs
1215 lines (1152 loc) · 46.8 KB
/
response.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This module defines how response callbacks are executed.
// See https://internetcomputer.org/docs/interface-spec/index.html#callback-invocation
use std::sync::Arc;
use ic_base_types::CanisterId;
use ic_constants::LOG_CANISTER_OPERATION_CYCLES_THRESHOLD;
use ic_replicated_state::canister_state::system_state::CyclesUseCase;
use ic_embedders::wasm_executor::{
wasm_execution_error, CanisterStateChanges, PausedWasmExecution, WasmExecutionResult,
};
use ic_interfaces::execution_environment::{
CanisterOutOfCyclesError, HypervisorError, WasmExecutionOutput,
};
use ic_logger::{error, info, ReplicaLogger};
use ic_replicated_state::{CallContext, CallOrigin, CanisterState};
use ic_sys::PAGE_SIZE;
use ic_system_api::{ApiType, ExecutionParameters};
use ic_types::ingress::WasmResult;
use ic_types::messages::{
CallContextId, CallbackId, CanisterMessage, CanisterMessageOrTask, Payload, RequestMetadata,
Response,
};
use ic_types::methods::{Callback, FuncRef, WasmClosure};
use ic_types::Cycles;
use ic_types::{NumBytes, NumInstructions, Time};
use ic_wasm_types::WasmEngineError::FailedToApplySystemChanges;
use crate::execution::common::{
self, action_to_response, apply_canister_state_changes, update_round_limits,
};
use crate::execution_environment::{
log_dirty_pages, ExecuteMessageResult, ExecutionResponse, PausedExecution, RoundContext,
RoundLimits,
};
use crate::metrics::CallTreeMetrics;
use ic_config::flag_status::FlagStatus;
#[cfg(test)]
mod tests;
/// A percentage of the total message limit reserved for executing the cleanup
/// callback.
const RESERVED_CLEANUP_INSTRUCTIONS_IN_PERCENT: u64 = 5;
/// The algorithm for executing the response callback works with two canisters:
/// - `clean_canister`: the canister state from the current replicated state
/// without any changes by the ongoing execution.
/// - `helper.canister()`: the canister state that contains changes done by
/// the ongoing execution. This state is re-created in each entry point of
/// the algorithm by applying the state changes to `clean_canister`.
///
/// Summary of the algorithm:
/// 1. The main entry point is `execute_response()` that takes `clean_canister`
/// as input. The function looks up the callback/call context and computes
/// the refund cycles. It remembers the refund cycles as the initial state
/// changes because they need to be applied to `clean_canister` in each entry
/// point: `execute_response()`, `PausedResponseExecution::resume()`
/// and `PausedCleanupExecution::resume()`.
///
/// 2. The next step is to execute the response callback, which may become
/// paused if it exceeds the slice limit. In that case the function
/// returns the unchanged `clean_canister` along with the paused execution.
///
/// 3. If the response callback execution fails, then the algorithm runs the
/// cleanup callback if it exists. That execution may also become paused
/// if it exceeds the slice limit. In that case the function returns the
/// unchanged `clean_canister` along with the paused execution.
///
/// 4. The final step is to unregister the callback and the call context if
/// needed and to refund the remaining execution cycles.
///
/// ```text
/// [begin]
/// │
/// ▼
/// [look up callback/call context and prepare initial state changes]
/// │
/// │
/// │ exceeded slice
/// ▼ instruction limit
/// [execute response] ───────────────────────► PausedResponseExecution
/// │ │ │ ▲
/// │ │ └─────────┘
/// │ finished execution │ exceeded slice
/// │◄─────────────────────────────────────────┘ instruction limit
/// │
/// │
/// │ exceeded slice
/// ▼ on error instruction limit
/// [error?]───────────►[execute cleanup()]───────────────────► PausedCleanupExecution
/// │ │ │ ▲
/// │ │ └────────┘
/// │ finished execution │ exceeded slice
/// │◄─────────────────────────────────────────────────────────┘ instruction limit
/// │
/// [unregister callback, refund execution cycles]
/// │
/// ▼
/// [end]
///```
/// Contains fields of `ResponseHelper` that are necessary for resuming the
/// response execution.
#[derive(Debug)]
struct PausedResponseHelper {
refund_for_sent_cycles: Cycles,
refund_for_response_transmission: Cycles,
initial_cycles_balance: Cycles,
response_sender: CanisterId,
}
/// A helper that implements and keeps track of response execution steps.
/// It is used to safely pause and resume a response execution.
struct ResponseHelper {
canister: CanisterState,
refund_for_sent_cycles: Cycles,
refund_for_response_transmission: Cycles,
initial_cycles_balance: Cycles,
response_sender: CanisterId,
applied_subnet_memory_reservation: NumBytes,
}
impl ResponseHelper {
/// Construct a new helper by cloning the clean canister state and
/// precomputing the cycles to refund.
fn new(
clean_canister: &CanisterState,
response: &Response,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
) -> Self {
// Canister A sends a request to canister B with some cycles.
// Canister B can accept some of the cycles in the request.
// The unaccepted cycles are returned to A in the response.
//
// Therefore, the cycles in the response must not exceed the cycles in
// the request. Otherwise, there might be potentially malicious faults.
debug_assert!(response.refund <= original.callback.cycles_sent);
let refund_for_sent_cycles = if response.refund > original.callback.cycles_sent {
round.counters.response_cycles_refund_error.inc();
error!(
round.log,
"[EXC-BUG] Canister got a response with too many cycles. \
Originator {} respondent {} max cycles expected {} got {}.",
response.originator,
response.respondent,
original.callback.cycles_sent,
response.refund,
);
original.callback.cycles_sent
} else {
response.refund
};
// The canister that sends a request must also pay the fee for
// the transmission of the response. As we do not know how big
// the response might be, we reserve cycles for the largest
// possible response when the request is being sent. Now that we
// have received the response, we can refund the cycles based on
// the actual size of the response.
let refund_for_response_transmission = round
.cycles_account_manager
.refund_for_response_transmission(
round.log,
round.counters.response_cycles_refund_error,
response,
original.callback.prepayment_for_response_transmission,
original.subnet_size,
);
let canister = clean_canister.clone();
let initial_cycles_balance = canister.system_state.balance();
let response_sender = response.respondent;
let mut helper = Self {
canister,
refund_for_sent_cycles,
refund_for_response_transmission,
initial_cycles_balance,
response_sender,
applied_subnet_memory_reservation: NumBytes::new(0),
};
helper.apply_subnet_memory_reservation(original, round_limits);
helper
}
/// Refunds the canister for the cycles that were not accepted by the callee
/// and for the cycles reserved for response transmission.
///
/// These are the only state changes to the initial canister state before
/// executing Wasm code.
fn apply_initial_refunds(&mut self) {
self.canister
.system_state
.add_cycles(self.refund_for_sent_cycles, CyclesUseCase::NonConsumed);
self.canister.system_state.add_cycles(
self.refund_for_response_transmission,
CyclesUseCase::RequestAndResponseTransmission,
);
}
/// Checks that the canister has not been uninstalled:
/// - the call context is not deleted
/// - the execute state exists.
///
/// DTS relies on the invariant that once this validation succeeds, it will
/// also continue to succeed later on for the same canister while DTS
/// execution is in progress.
//
// The concern about large `Err` variants is about propagation of large
// errors through the `?` operator, see https://rust-lang.github.io/rust-clippy/master/index.html#/result_large_err.
// In this case, the result is local to this module, so we allow the warning
// as keeping the `Result` is more readable than using a custom enum.
#[allow(clippy::result_large_err)]
fn validate(
self,
call_context: &CallContext,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
) -> Result<ResponseHelper, ExecuteMessageResult> {
// If the call context was deleted (e.g. in uninstall), then do not execute anything.
if call_context.is_deleted() {
debug_assert!(call_context.has_responded());
if !call_context.has_responded() {
// This case is unreachable because `is_deleted() => has_responded()`
// is a critical invariant and should hold.
round.counters.invalid_canister_state_error.inc();
error!(
round.log,
"[EXC-BUG] Canister {} has a deleted context that has not responded",
self.canister.system_state.canister_id,
);
// Since this branch doesn't call `early_finish()`, it needs to manually
// revert the subnet memory reservation.
self.revert_subnet_memory_reservation(original, round_limits);
return Err(ExecuteMessageResult::Finished {
canister: self.canister,
heap_delta: NumBytes::from(0),
instructions_used: NumInstructions::from(0),
response: ExecutionResponse::Empty,
call_duration: Some(round.time.saturating_duration_since(call_context.time())),
});
}
// Since the call context has responded, passing `Ok(None)` will produce
// an empty response and take care of all other bookkeeping.
let result: Result<Option<WasmResult>, HypervisorError> = Ok(None);
return Err(self.early_finish(result, original, round, round_limits));
}
// Validate that the canister has an `ExecutionState`.
debug_assert!(self.canister.execution_state.is_some());
if self.canister.execution_state.is_none() {
round.counters.invalid_canister_state_error.inc();
error!(
round.log,
"[EXC-BUG] Canister {} is attempting to execute a response, but the execution state does not exist.",
self.canister.system_state.canister_id,
);
let result = Err(HypervisorError::WasmModuleNotFound);
return Err(self.early_finish(result, original, round, round_limits));
}
Ok(self)
}
/// Returns a struct with all the necessary information to replay the
/// initial steps in subsequent rounds.
fn pause(
&self,
original: &OriginalContext,
round_limits: &mut RoundLimits,
) -> PausedResponseHelper {
self.revert_subnet_memory_reservation(original, round_limits);
PausedResponseHelper {
refund_for_sent_cycles: self.refund_for_sent_cycles,
refund_for_response_transmission: self.refund_for_response_transmission,
initial_cycles_balance: self.initial_cycles_balance,
response_sender: self.response_sender,
}
}
/// Replays validation and the initial steps on the given clean canister and
/// returns the helper to continue the DTS execution.
///
/// It panics if the clean canister doesn't have the expected callback,
/// call context, and execution state because it is not possible to invoke
/// the cleanup callback in such cases.
///
/// It returns an error if the cycles balance of the clean canister differs
/// from the cycles balances at the start of the DTS execution.
fn resume(
paused: PausedResponseHelper,
clean_canister: &CanisterState,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
) -> Result<ResponseHelper, (ResponseHelper, HypervisorError)> {
// We expect the function call to succeed because the call context and
// the callback have been checked in `execute_response()`.
// Note that we cannot return an error here because the cleanup callback
// cannot be invoked without a valid call context and a callback.
let (_, _, call_context, _) = common::get_call_context_and_callback(
clean_canister,
&original.message,
round.log,
round.counters.unexpected_response_error,
)
.expect("Failed to resume DTS response: get call context and callback");
let mut helper = Self {
canister: clean_canister.clone(),
refund_for_sent_cycles: paused.refund_for_sent_cycles,
refund_for_response_transmission: paused.refund_for_response_transmission,
initial_cycles_balance: clean_canister.system_state.balance(),
response_sender: paused.response_sender,
applied_subnet_memory_reservation: NumBytes::new(0),
};
helper.apply_subnet_memory_reservation(original, round_limits);
helper.apply_initial_refunds();
// This validation succeeded in `execute_response()` and we expect it to
// succeed here too.
// Note that we cannot return an error here because the cleanup callback
// cannot be invoked without a valid call context and a callback.
helper = helper
.validate(&call_context, original, round, round_limits)
.expect("Failed to resume DTS response: validation");
// The cycles balance of the clean canister must not change during the
// DTS execution.
if helper.initial_cycles_balance != paused.initial_cycles_balance {
let msg = "Mismatch in cycles balance when resuming a response call".to_string();
let err = HypervisorError::WasmEngineError(FailedToApplySystemChanges(msg));
return Err((helper, err));
}
Ok(helper)
}
/// Processes the output and the state changes of Wasm execution of the
/// response callback.
fn handle_wasm_execution_of_response_callback(
mut self,
mut output: WasmExecutionOutput,
canister_state_changes: Option<CanisterStateChanges>,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
reserved_cleanup_instructions: NumInstructions,
call_tree_metrics: &dyn CallTreeMetrics,
) -> Result<ExecuteMessageResult, (Self, HypervisorError, NumInstructions)> {
self.canister
.system_state
.apply_ingress_induction_cycles_debit(
self.canister.canister_id(),
round.log,
round.counters.charging_from_balance_error,
);
// Check that the cycles balance does not go below zero after applying
// the Wasm execution state changes.
if let Some(state_changes) = &canister_state_changes {
let old_balance = self.canister.system_state.balance();
let requested = state_changes.system_state_changes.removed_cycles();
// Note that we ignore the freezing threshold as required by the spec.
if old_balance < requested {
let reveal_top_up = self
.canister
.controllers()
.contains(&original.call_origin.get_principal());
let err = CanisterOutOfCyclesError {
canister_id: self.canister.canister_id(),
available: old_balance,
requested,
threshold: original.freezing_threshold,
reveal_top_up,
};
info!(
round.log,
"[DTS] Failed response callback execution of canister {} due to concurrent cycle change: {:?}.",
self.canister.canister_id(),
err,
);
let err = HypervisorError::InsufficientCyclesBalance(err);
// Return total instructions: wasm executor leftovers + cleanup reservation.
return Err((
self,
err,
output.num_instructions_left + reserved_cleanup_instructions,
));
}
}
apply_canister_state_changes(
canister_state_changes,
self.canister.execution_state.as_mut().unwrap(),
&mut self.canister.system_state,
&mut output,
round_limits,
round.time,
round.network_topology,
round.hypervisor.subnet_id(),
round.log,
round.counters.state_changes_error,
call_tree_metrics,
original.call_context_creation_time,
);
// Return total instructions: wasm executor leftovers + cleanup reservation.
let instructions_available = output.num_instructions_left + reserved_cleanup_instructions;
match output.wasm_result {
Ok(_) => Ok(self.finish(
output.wasm_result,
instructions_available,
NumBytes::from((output.instance_stats.dirty_pages * PAGE_SIZE) as u64),
original,
round,
round_limits,
)),
Err(err) => Err((self, err, instructions_available)),
}
}
/// Processes the output and the state changes of Wasm execution of the
/// cleanup callback.
fn handle_wasm_execution_of_cleanup_callback(
mut self,
mut output: WasmExecutionOutput,
canister_state_changes: Option<CanisterStateChanges>,
callback_err: HypervisorError,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
call_tree_metrics: &dyn CallTreeMetrics,
) -> ExecuteMessageResult {
self.canister
.system_state
.apply_ingress_induction_cycles_debit(
self.canister.canister_id(),
round.log,
round.counters.charging_from_balance_error,
);
if let Some(state_changes) = &canister_state_changes {
let requested = state_changes.system_state_changes.removed_cycles();
// A cleanup callback cannot accept and send cycles.
assert_eq!(requested.get(), 0);
}
apply_canister_state_changes(
canister_state_changes,
self.canister.execution_state.as_mut().unwrap(),
&mut self.canister.system_state,
&mut output,
round_limits,
round.time,
round.network_topology,
round.hypervisor.subnet_id(),
round.log,
round.counters.state_changes_error,
call_tree_metrics,
original.call_context_creation_time,
);
match output.wasm_result {
Ok(_) => {
// Note that, even though the callback has succeeded,
// the original callback error is returned.
self.finish(
Err(callback_err),
output.num_instructions_left,
NumBytes::from((output.instance_stats.dirty_pages * PAGE_SIZE) as u64),
original,
round,
round_limits,
)
}
Err(cleanup_err) => {
let result = Err(HypervisorError::Cleanup {
callback_err: Box::new(callback_err),
cleanup_err: Box::new(cleanup_err),
});
self.finish(
result,
output.num_instructions_left,
NumBytes::from(0),
original,
round,
round_limits,
)
}
}
}
/// Completes execution of the response and cleanup callbacks:
/// - Unregisters the callback.
/// - Unregisters the call context if there are no outstanding calls.
/// - Refunds the remaining execution cycles.
fn finish(
mut self,
result: Result<Option<WasmResult>, HypervisorError>,
instructions_left: NumInstructions,
heap_delta: NumBytes,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
) -> ExecuteMessageResult {
self.revert_subnet_memory_reservation(original, round_limits);
let instructions_used = NumInstructions::from(
original
.message_instruction_limit
.get()
.saturating_sub(instructions_left.get()),
);
let (action, call_context) = self
.canister
.system_state
.call_context_manager_mut()
.unwrap()
.on_canister_result(
original.call_context_id,
Some(original.callback_id),
result,
instructions_used,
);
let response = action_to_response(
&self.canister,
action,
original.call_origin.clone(),
round.time,
round.log,
round.counters.ingress_with_cycles_error,
);
round.cycles_account_manager.refund_unused_execution_cycles(
&mut self.canister.system_state,
instructions_left,
original.message_instruction_limit,
original.callback.prepayment_for_response_execution,
round.counters.execution_refund_error,
original.subnet_size,
round.log,
);
if self.refund_for_sent_cycles.get() > LOG_CANISTER_OPERATION_CYCLES_THRESHOLD {
info!(
round.log,
"Canister {} received unaccepted {} cycles as refund from canister {}.",
self.canister.system_state.canister_id,
self.refund_for_sent_cycles,
self.response_sender,
);
}
if original.log_dirty_pages == FlagStatus::Enabled {
log_dirty_pages(
round.log,
&original.canister_id,
format!("reponse_to_{}", original.message.originator).as_str(),
heap_delta.get() as usize / PAGE_SIZE,
instructions_used,
);
}
ExecuteMessageResult::Finished {
canister: self.canister,
response,
instructions_used,
heap_delta,
call_duration: call_context
.map(|call_context| round.time.saturating_duration_since(call_context.time())),
}
}
/// Completes execution of the response and cleanup callbacks without
/// consuming any instructions and without producing any heap delta.
fn early_finish(
self,
result: Result<Option<WasmResult>, HypervisorError>,
original: &OriginalContext,
round: &RoundContext,
round_limits: &mut RoundLimits,
) -> ExecuteMessageResult {
self.finish(
result,
original.message_instruction_limit,
NumBytes::from(0),
original,
round,
round_limits,
)
}
fn canister(&self) -> &CanisterState {
&self.canister
}
fn refund_for_sent_cycles(&self) -> Cycles {
self.refund_for_sent_cycles
}
fn apply_subnet_memory_reservation(
&mut self,
original: &OriginalContext,
round_limits: &mut RoundLimits,
) {
let reservation = original.subnet_memory_reservation;
round_limits.subnet_available_memory.apply_reservation(
reservation,
NumBytes::new(0),
NumBytes::new(0),
);
debug_assert_eq!(self.applied_subnet_memory_reservation, NumBytes::new(0));
self.applied_subnet_memory_reservation = reservation;
}
fn revert_subnet_memory_reservation(
&self,
original: &OriginalContext,
round_limits: &mut RoundLimits,
) {
debug_assert_eq!(
self.applied_subnet_memory_reservation,
original.subnet_memory_reservation
);
round_limits.subnet_available_memory.revert_reservation(
self.applied_subnet_memory_reservation,
NumBytes::new(0),
NumBytes::new(0),
);
}
}
/// Context variables that remain the same throughput the entire deterministic
/// time slicing execution of a response.
#[derive(Clone, Debug)]
struct OriginalContext {
callback: Callback,
call_context_id: CallContextId,
callback_id: CallbackId,
call_origin: CallOrigin,
time: Time,
call_context_creation_time: Time,
request_metadata: RequestMetadata,
message_instruction_limit: NumInstructions,
message: Arc<Response>,
subnet_size: usize,
freezing_threshold: Cycles,
canister_id: CanisterId,
subnet_memory_reservation: NumBytes,
instructions_executed: NumInstructions,
log_dirty_pages: FlagStatus,
}
/// Struct used to hold necessary information for the
/// deterministic time slicing execution of a response.
#[derive(Debug)]
struct PausedResponseExecution {
paused_wasm_execution: Box<dyn PausedWasmExecution>,
helper: PausedResponseHelper,
execution_parameters: ExecutionParameters,
reserved_cleanup_instructions: NumInstructions,
original: OriginalContext,
}
impl PausedExecution for PausedResponseExecution {
/// Resumes the response callback execution taking the current clean
/// canister state as input. There are two outcomes:
/// - execution is paused again: in this case the function returns the
/// unchanged clean canister state along with the paused execution.
/// - execution finishes: in this case the function applies the initial
/// state changes and the state changes from the Wasm execution to the
/// clean canister state and proceeds to executing the cleanup callback
/// if needed. Note that if execution of the cleanup callback gets paused
/// then it will return the clean canister state.
fn resume(
self: Box<Self>,
clean_canister: CanisterState,
round: RoundContext,
round_limits: &mut RoundLimits,
_subnet_size: usize,
call_tree_metrics: &dyn CallTreeMetrics,
) -> ExecuteMessageResult {
info!(
round.log,
"[DTS] Resuming paused response callback {:?} of canister {}.",
self.original.callback_id,
clean_canister.canister_id(),
);
// The height of the `clean_canister` state increases with every call of
// `resume()`. We re-create the helper based on `clean_canister` so that
// the Wasm state changes are applied to the up-to-date state.
let (helper, result) = match ResponseHelper::resume(
self.helper,
&clean_canister,
&self.original,
&round,
round_limits,
) {
Ok(helper) => {
let execution_state = helper.canister().execution_state.as_ref().unwrap();
let result = self.paused_wasm_execution.resume(execution_state);
(helper, result)
}
Err((helper, err)) => {
info!(
round.log,
"[DTS] Failed to resume paused response callback {:?} of canister {}: {:?}.",
self.original.callback_id,
clean_canister.canister_id(),
err,
);
self.paused_wasm_execution.abort();
let result = wasm_execution_error(
err,
self.execution_parameters.instruction_limits.message(),
);
(helper, result)
}
};
process_response_result(
result,
clean_canister,
helper,
self.execution_parameters,
self.reserved_cleanup_instructions,
self.original,
round,
round_limits,
call_tree_metrics,
)
}
fn abort(self: Box<Self>, log: &ReplicaLogger) -> (CanisterMessageOrTask, Cycles) {
info!(
log,
"[DTS] Aborting paused response callback {:?} of canister {}.",
self.original.callback_id,
self.original.canister_id,
);
self.paused_wasm_execution.abort();
let message = CanisterMessage::Response(self.original.message);
// No cycles were prepaid for execution during this DTS execution.
(CanisterMessageOrTask::Message(message), Cycles::zero())
}
}
/// Struct used to hold necessary information for the
/// deterministic time slicing execution of a cleanup callback.
#[derive(Debug)]
struct PausedCleanupExecution {
paused_wasm_execution: Box<dyn PausedWasmExecution>,
helper: PausedResponseHelper,
execution_parameters: ExecutionParameters,
callback_err: HypervisorError,
original: OriginalContext,
}
impl PausedExecution for PausedCleanupExecution {
/// Resumes the cleanup callback execution taking the current clean
/// canister state as input. There are two outcomes:
/// - execution is paused again: in this case the function returns the
/// unchanged clean canister state along with the paused execution.
/// - execution finishes: in this case the function applies the initial
/// state changes and the state changes from the Wasm execution to the
/// clean canister state.
fn resume(
self: Box<Self>,
clean_canister: CanisterState,
round: RoundContext,
round_limits: &mut RoundLimits,
_subnet_size: usize,
call_tree_metrics: &dyn CallTreeMetrics,
) -> ExecuteMessageResult {
info!(
round.log,
"[DTS] Resuming paused cleanup callback {:?} of canister {}.",
self.original.callback_id,
clean_canister.canister_id(),
);
// The height of the `clean_canister` state increases with every call of
// `resume()`. We re-create the helper based on `clean_state` so that
// the Wasm state changes are applied to the up-to-date state.
//
// Note that we don't apply changes from the response callback execution
// because the cleanup callback runs only if the response callback fails.
let (helper, result) = match ResponseHelper::resume(
self.helper,
&clean_canister,
&self.original,
&round,
round_limits,
) {
Ok(helper) => {
let execution_state = helper.canister().execution_state.as_ref().unwrap();
let result = self.paused_wasm_execution.resume(execution_state);
(helper, result)
}
Err((helper, err)) => {
info!(
round.log,
"[DTS] Failed to resume paused cleanup callback {:?} of canister {}: {:?}.",
self.original.callback_id,
clean_canister.canister_id(),
err,
);
self.paused_wasm_execution.abort();
let result = wasm_execution_error(
err,
self.execution_parameters.instruction_limits.message(),
);
(helper, result)
}
};
process_cleanup_result(
result,
clean_canister,
helper,
self.execution_parameters,
self.callback_err,
self.original,
round,
round_limits,
call_tree_metrics,
)
}
fn abort(self: Box<Self>, log: &ReplicaLogger) -> (CanisterMessageOrTask, Cycles) {
info!(
log,
"[DTS] Aborting paused cleanup callback {:?} of canister {}.",
self.original.callback_id,
self.original.canister_id,
);
self.paused_wasm_execution.abort();
let message = CanisterMessage::Response(self.original.message);
// No cycles were prepaid for execution during this DTS execution.
(CanisterMessageOrTask::Message(message), Cycles::zero())
}
}
/// Executes the response callback.
///
/// If execution finishes, then the function returns the new canister state.
/// If execution is paused, then the function returns the clean canister state
/// without any changes.
#[allow(clippy::too_many_arguments)]
pub fn execute_response(
clean_canister: CanisterState,
response: Arc<Response>,
time: Time,
execution_parameters: ExecutionParameters,
round: RoundContext,
round_limits: &mut RoundLimits,
subnet_size: usize,
subnet_memory_reservation: NumBytes,
call_tree_metrics: &dyn CallTreeMetrics,
log_dirty_pages: FlagStatus,
) -> ExecuteMessageResult {
let (callback, callback_id, call_context, call_context_id) =
match common::get_call_context_and_callback(
&clean_canister,
&response,
round.log,
round.counters.unexpected_response_error,
) {
Some(r) => r,
None => {
// This case is unreachable because the call context and
// callback should always exist.
return ExecuteMessageResult::Finished {
canister: clean_canister,
instructions_used: NumInstructions::from(0),
heap_delta: NumBytes::from(0),
response: ExecutionResponse::Empty,
call_duration: None,
};
}
};
let freezing_threshold = round.cycles_account_manager.freeze_threshold_cycles(
clean_canister.system_state.freeze_threshold,
clean_canister.system_state.memory_allocation,
clean_canister.memory_usage(),
clean_canister.message_memory_usage(),
clean_canister.compute_allocation(),
subnet_size,
clean_canister.system_state.reserved_balance(),
);
let original = OriginalContext {
callback,
call_context_id,
callback_id,
call_origin: call_context.call_origin().clone(),
time,
call_context_creation_time: call_context.time(),
request_metadata: call_context.metadata().clone(),
message_instruction_limit: execution_parameters.instruction_limits.message(),
message: Arc::clone(&response),
subnet_size,
freezing_threshold,
canister_id: clean_canister.canister_id(),
subnet_memory_reservation,
instructions_executed: call_context.instructions_executed(),
log_dirty_pages,
};
let mut helper =
ResponseHelper::new(&clean_canister, &response, &original, &round, round_limits);
helper.apply_initial_refunds();
let helper = match helper.validate(&call_context, &original, &round, round_limits) {
Ok(helper) => helper,
Err(result) => {
return result;
}
};
let closure = match response.response_payload {
Payload::Data(_) => original.callback.on_reply.clone(),
Payload::Reject(_) => original.callback.on_reject.clone(),
};
let func_ref = match original.call_origin {
CallOrigin::Ingress(_, _) | CallOrigin::CanisterUpdate(_, _) | CallOrigin::SystemTask => {
FuncRef::UpdateClosure(closure)
}
CallOrigin::CanisterQuery(_, _) | CallOrigin::Query(_) => FuncRef::QueryClosure(closure),
};
let api_type = match &response.response_payload {
Payload::Data(payload) => ApiType::reply_callback(
time,
original.call_origin.get_principal(),
payload.to_vec(),
helper.refund_for_sent_cycles(),
call_context_id,
call_context.has_responded(),
execution_parameters.execution_mode.clone(),
call_context.instructions_executed(),
),
Payload::Reject(context) => ApiType::reject_callback(
time,
original.call_origin.get_principal(),
context.clone(),
helper.refund_for_sent_cycles(),
call_context_id,
call_context.has_responded(),
execution_parameters.execution_mode.clone(),
call_context.instructions_executed(),
),
};
let (execution_parameters, reserved_cleanup_instructions) =
reserve_cleanup_instructions(execution_parameters);
let result = round.hypervisor.execute_dts(
api_type,
helper.canister().execution_state.as_ref().unwrap(),
&helper.canister().system_state,
helper.canister().memory_usage(),
helper.canister().message_memory_usage(),
execution_parameters.clone(),
func_ref,
original.request_metadata.clone(),
round_limits,
round.network_topology,
);
process_response_result(
result,
clean_canister,
helper,
execution_parameters,
reserved_cleanup_instructions,
original,
round,
round_limits,
call_tree_metrics,
)
}
// Reserves a percentage of message instructions limit for a cleanup callback execution.
fn reserve_cleanup_instructions(
mut execution_parameters: ExecutionParameters,
) -> (ExecutionParameters, NumInstructions) {
let instruction_limits = &mut execution_parameters.instruction_limits;
let initial_message_limit = instruction_limits.message();
let reserved_cleanup_instructions =
(initial_message_limit * RESERVED_CLEANUP_INSTRUCTIONS_IN_PERCENT) / 100;
instruction_limits.reduce_by(reserved_cleanup_instructions);
(execution_parameters, reserved_cleanup_instructions)
}
// Helper function to execute response cleanup.