-
Notifications
You must be signed in to change notification settings - Fork 296
/
tests.rs
5114 lines (4618 loc) · 177 KB
/
tests.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
use super::{
test_utilities::{ingress, instructions, SchedulerTest, SchedulerTestBuilder, TestInstallCode},
*,
};
#[cfg(test)]
use crate::scheduler::test_utilities::{on_response, other_side};
use candid::Encode;
use ic00::{
CanisterHttpRequestArgs, HttpMethod, SignWithECDSAArgs, TransformContext, TransformFunc,
};
use ic_base_types::PrincipalId;
use ic_config::{
execution_environment::STOP_CANISTER_TIMEOUT_DURATION,
subnet_config::{CyclesAccountManagerConfig, SchedulerConfig, SubnetConfig},
};
use ic_error_types::RejectCode;
use ic_ic00_types::{
self as ic00, BoundedHttpHeaders, CanisterHttpResponsePayload, CanisterIdRecord,
CanisterStatusType, DerivationPath, EcdsaCurve, EmptyBlob, Method, Payload as _,
};
use ic_interfaces::execution_environment::SubnetAvailableMemory;
use ic_logger::replica_logger::no_op_logger;
use ic_registry_routing_table::CanisterIdRange;
use ic_registry_subnet_type::SubnetType;
use ic_replicated_state::canister_state::system_state::PausedExecutionId;
use ic_replicated_state::testing::CanisterQueuesTesting;
use ic_replicated_state::testing::SystemStateTesting;
use ic_state_machine_tests::{PayloadBuilder, StateMachineBuilder};
use ic_test_utilities::types::ids::message_test_id;
use ic_test_utilities::{
state::{get_running_canister, get_stopped_canister, get_stopping_canister},
types::{
ids::{canister_test_id, subnet_test_id},
messages::RequestBuilder,
},
};
use ic_test_utilities_metrics::{
fetch_counter, fetch_gauge, fetch_gauge_vec, fetch_histogram_stats, fetch_int_gauge,
fetch_int_gauge_vec, metric_vec, HistogramStats,
};
use ic_test_utilities_time::mock_time;
use ic_types::methods::SystemMethod;
use ic_types::time::expiry_time_from_now;
use ic_types::{
messages::{
CallbackId, Payload, RejectContext, Response, StopCanisterCallId, MAX_RESPONSE_COUNT_BYTES,
},
Height,
};
use ic_types::{time::UNIX_EPOCH, ComputeAllocation, Cycles, NumBytes};
use ic_types_test_utils::ids::user_test_id;
use ic_universal_canister::{call_args, wasm, UNIVERSAL_CANISTER_WASM};
use proptest::prelude::*;
use std::collections::HashMap;
use std::{cmp::min, ops::Range};
use std::{convert::TryFrom, time::Duration};
const M: usize = 1_000_000;
const B: usize = 1_000 * M;
fn assert_floats_are_equal(val0: f64, val1: f64) {
if val0 > val1 {
assert!(val0 - val1 < 0.1);
} else {
assert!(val1 - val0 < 0.1);
}
}
#[test]
fn can_fully_execute_canisters_with_one_input_message_each() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::from(1 << 30),
max_instructions_per_message: NumInstructions::from(5),
max_instructions_per_message_without_dts: NumInstructions::from(5),
max_instructions_per_slice: NumInstructions::from(5),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let num_canisters = 3;
for _ in 0..num_canisters {
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(5));
}
test.execute_round(ExecutionRoundType::OrdinaryRound);
for canister in test.state().canisters_iter() {
assert_eq!(canister.system_state.queues().ingress_queue_size(), 0);
assert_eq!(
canister.scheduler_state.last_full_execution_round,
test.last_round()
);
let canister_metrics = &canister.system_state.canister_metrics;
assert_eq!(canister_metrics.skipped_round_due_to_no_messages, 0);
assert_eq!(canister_metrics.executed, 1);
assert_eq!(canister_metrics.interrupted_during_execution, 0);
}
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
3
);
assert_eq!(
test.state().metadata.subnet_metrics.num_canisters,
num_canisters
);
}
#[test]
fn stops_executing_messages_when_heap_delta_capacity_reached() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
subnet_heap_delta_capacity: NumBytes::from(10),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 0);
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 1);
assert_eq!(
test.scheduler()
.metrics
.round_skipped_due_to_current_heap_delta_above_limit
.get(),
1
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
2
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
#[test]
fn restarts_executing_messages_after_checkpoint_when_heap_delta_capacity_reached() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
subnet_heap_delta_capacity: NumBytes::from(10),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 0);
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.execute_round(ExecutionRoundType::CheckpointRound);
assert_eq!(NumBytes::from(0), test.state().metadata.heap_delta_estimate);
assert_eq!(test.ingress_queue_size(canister_id), 1);
assert_eq!(
test.scheduler()
.metrics
.round_skipped_due_to_current_heap_delta_above_limit
.get(),
1
);
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 0);
assert_eq!(
test.scheduler()
.metrics
.round_skipped_due_to_current_heap_delta_above_limit
.get(),
1
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
3
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
#[test]
fn canister_gets_heap_delta_rate_limited() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
subnet_heap_delta_capacity: NumBytes::from(10),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.with_rate_limiting_of_heap_delta()
.build();
let heap_delta_rate_limit = SchedulerConfig::application_subnet().heap_delta_rate_limit;
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(10).dirty_pages(1));
test.canister_state_mut(canister_id)
.scheduler_state
.heap_delta_debit = heap_delta_rate_limit * 2 - NumBytes::from(1);
// Current heap delta debit is over the limit, so the canister shouldn't run.
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 1);
// After getting a single round of credits we should be below the limit and able
// to run.
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 0);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
1
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
/// This test ensures that inner_loop() breaks out of the loop when the loop did
/// not consume any instructions.
#[test]
fn inner_loop_stops_when_no_instructions_consumed() {
// Create a canister with 1 input message that consumes half of
// max_instructions_per_round. This message is executed in the first
// iteration of the loop and in the second iteration of the loop, no
// instructions are consumed.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(100),
max_instructions_per_message: NumInstructions::new(50),
max_instructions_per_message_without_dts: NumInstructions::from(50),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(50));
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 0);
let metrics = &test.scheduler().metrics;
assert_eq!(metrics.execute_round_called.get(), 1);
assert_eq!(metrics.inner_round_loop_consumed_max_instructions.get(), 0);
assert_eq!(
metrics
.inner_loop_consumed_non_zero_instructions_count
.get(),
1
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
1
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
/// This test ensures that inner_loop() breaks out of the loop when the loop
/// consumes max_instructions_per_round.
#[test]
fn inner_loop_stops_when_max_instructions_per_round_consumed() {
// Create a canister with 3 input messages. 2 of them consume all of
// max_instructions_per_round. The 2 messages are executed in the first
// iteration of the loop and then the loop breaks.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(100),
max_instructions_per_message: NumInstructions::new(50),
max_instructions_per_message_without_dts: NumInstructions::from(50),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister_id = test.create_canister();
test.send_ingress(canister_id, ingress(50));
test.send_ingress(canister_id, ingress(50));
test.send_ingress(canister_id, ingress(50));
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(test.ingress_queue_size(canister_id), 1);
let metrics = &test.scheduler().metrics;
assert_eq!(metrics.execute_round_called.get(), 1);
assert_eq!(metrics.inner_round_loop_consumed_max_instructions.get(), 1);
assert_eq!(
metrics
.inner_loop_consumed_non_zero_instructions_count
.get(),
1
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
2
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
#[test]
fn basic_induct_messages_on_same_subnet_works() {
// Creates two canisters: caller and callee.
// Sends three ingress messages to the caller, where each message calls the
// callee and in the response callback makes another call to the callee.
// Everything should be executed within a single round thanks to the
// same-subnet message induction.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(1000),
max_instructions_per_message: NumInstructions::new(55),
max_instructions_per_message_without_dts: NumInstructions::from(55),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let caller = test.create_canister();
let callee = test.create_canister();
let message = ingress(50).call(
other_side(callee, 50),
on_response(50).call(other_side(callee, 50), on_response(50)),
);
test.send_ingress(caller, message.clone());
test.send_ingress(caller, message.clone());
test.send_ingress(caller, message);
test.execute_round(ExecutionRoundType::OrdinaryRound);
// All messages should be executed in a single round.
assert_eq!(test.ingress_queue_size(caller), 0);
let number_of_messages = test
.scheduler()
.metrics
.msg_execution_duration
.get_sample_count();
// Three ingress messages, six calls, six responses.
assert_eq!(number_of_messages, 3 + 6 + 6);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
3 + 6 + 6
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 2);
}
#[test]
fn induct_messages_on_same_subnet_handles_foreign_subnet() {
// Creates one canister. The canister performs a cross-net call. The
// cross-net message should remain in the output queue of the caller and
// should not be inducted.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(1000),
max_instructions_per_message: NumInstructions::new(50),
max_instructions_per_message_without_dts: NumInstructions::from(50),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let caller = test.create_canister();
let callee = test.xnet_canister_id();
let message = ingress(50).call(other_side(callee, 50), on_response(50));
test.send_ingress(caller, message);
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert!(test.canister_state(caller).has_output());
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
1
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
/// Creates state with one canister. The canister has a message for itself.
/// in its output queue. Ensures that `induct_messages_on_same_subnet()`
/// moves the message.
#[test]
fn induct_messages_to_self_works() {
// Sends three ingress messages to a canister, where each message calls the
// same canister and in the response callback makes another self-call.
// Everything should be executed within a single round thanks to the
// same-subnet message induction.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(1000),
max_instructions_per_message: NumInstructions::new(55),
max_instructions_per_message_without_dts: NumInstructions::from(55),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister_id = test.create_canister();
let message = ingress(50).call(
other_side(canister_id, 50),
on_response(50).call(other_side(canister_id, 50), on_response(50)),
);
test.send_ingress(canister_id, message.clone());
test.send_ingress(canister_id, message.clone());
test.send_ingress(canister_id, message);
test.execute_round(ExecutionRoundType::OrdinaryRound);
// All messages should be executed in a single round.
assert_eq!(test.ingress_queue_size(canister_id), 0);
let number_of_messages = test
.scheduler()
.metrics
.msg_execution_duration
.get_sample_count();
// Three ingress messages, six calls, six responses.
assert_eq!(number_of_messages, 3 + 6 + 6);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
3 + 6 + 6
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
/// Creates state with two canisters. Source canister has two requests for
/// itself and two requests for destination canister in its output queues.
/// Subnet only has enough message memory for two requests.
///
/// Ensures that `induct_messages_on_same_subnet()` respects memory limits
/// on application subnets and ignores them on system subnets.
#[test]
fn induct_messages_on_same_subnet_respects_memory_limits() {
// Runs a test with the given `available_memory` (expected to be limited to 2
// requests plus epsilon). Checks that the limit is enforced on application
// subnets and ignored on system subnets.
let run_test = |subnet_available_memory: SubnetAvailableMemory, subnet_type| {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(1),
max_instructions_per_message: NumInstructions::new(1),
max_instructions_per_message_without_dts: NumInstructions::from(1),
max_instructions_per_slice: NumInstructions::new(1),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.with_subnet_message_memory(subnet_available_memory.get_message_memory() as u64)
.with_subnet_type(subnet_type)
.build();
let source = test.create_canister();
let dest = test.create_canister();
let source_canister = test.canister_state_mut(source);
let self_request = RequestBuilder::default()
.sender(source)
.receiver(source)
.build();
source_canister
.push_output_request(self_request.clone().into(), mock_time())
.unwrap();
source_canister
.push_output_request(self_request.into(), mock_time())
.unwrap();
let other_request = RequestBuilder::default()
.sender(source)
.receiver(dest)
.build();
source_canister
.push_output_request(other_request.clone().into(), mock_time())
.unwrap();
source_canister
.push_output_request(other_request.into(), mock_time())
.unwrap();
test.induct_messages_on_same_subnet();
let source_canister = test.canister_state(source);
let dest_canister = test.canister_state(dest);
let source_canister_queues = source_canister.system_state.queues();
let dest_canister_queues = dest_canister.system_state.queues();
if subnet_type == SubnetType::Application {
// Only two messages should have been inducted. After two self-inductions on the
// source canister, the subnet message memory is exhausted.
assert_eq!(1, source_canister_queues.output_message_count());
assert_eq!(2, source_canister_queues.input_queues_message_count());
assert_eq!(1, dest_canister_queues.input_queues_message_count());
} else {
// On a system subnet, with no message memory limits, all messages should have
// been inducted.
assert_eq!(0, source_canister_queues.output_message_count());
assert_eq!(2, source_canister_queues.input_queues_message_count());
assert_eq!(2, dest_canister_queues.input_queues_message_count());
}
};
// Subnet has memory for 4 initial requests and 2 additional requests (plus
// epsilon, for small responses).
run_test(
SubnetAvailableMemory::new(0, MAX_RESPONSE_COUNT_BYTES as i64 * 75 / 10, 0),
SubnetType::Application,
);
// On system subnets limits will not be enforced for local messages, so running with 0 available
// memory should also lead to inducting messages on local subnet.
run_test(SubnetAvailableMemory::new(0, 0, 0), SubnetType::System);
}
/// Verifies that the [`SchedulerConfig::instruction_overhead_per_message`] puts
/// a limit on the number of update messages that will be executed in a single
/// round.
#[test]
fn test_message_limit_from_message_overhead() {
// Create two canisters on the same subnet. When each one receives a
// message, it sends a message to the other so that they ping-pong forever.
let scheduler_config = SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_message: NumInstructions::from(5_000_000_000),
max_instructions_per_message_without_dts: NumInstructions::from(5_000_000_000),
max_instructions_per_slice: NumInstructions::from(5_000_000_000),
max_instructions_per_round: NumInstructions::from(7_000_000_000),
instruction_overhead_per_message: NumInstructions::from(2_000_000),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
};
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(scheduler_config.clone())
.build();
let canister0 = test.create_canister();
let canister1 = test.create_canister();
// There are 7B instructions allowed per round, but we won't execute a
// message unless we know there are 5B instructions left since that is the
// maximum a message could use. So execution will stop when we've used 2B
// messages. There is an overhead of 2M instructions per message so this
// allows us to execute 1000 messages. We stop when we've gone over the
// limit, so one additional message will be handled.
let expected_number_of_messages = (scheduler_config.max_instructions_per_round
- scheduler_config.max_instructions_per_message)
/ scheduler_config.instruction_overhead_per_message
+ 1;
let mut callee = canister0;
let mut call = other_side(callee, 0);
for _ in 0..expected_number_of_messages * 3 {
callee = if callee == canister1 {
canister0
} else {
canister1
};
call = other_side(callee, 0).call(call, on_response(0));
}
let message = ingress(0).call(call, on_response(0));
test.send_ingress(canister0, message);
test.execute_round(ExecutionRoundType::OrdinaryRound);
let number_of_messages = test
.scheduler()
.metrics
.msg_execution_duration
.get_sample_count();
assert_eq!(number_of_messages, expected_number_of_messages);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
expected_number_of_messages
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 2);
}
/// A test to ensure that there are multiple iterations of the loop in
/// inner_round().
#[test]
fn test_multiple_iterations_of_inner_loop() {
// Create two canisters on the same subnet. In the first iteration, the
// first sends a message to the second. In the second iteration, the second
// executes the received message.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: NumInstructions::new(200),
max_instructions_per_message: NumInstructions::new(50),
max_instructions_per_message_without_dts: NumInstructions::new(50),
max_instructions_per_slice: NumInstructions::from(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister0 = test.create_canister();
let canister1 = test.create_canister();
let message = ingress(50).call(other_side(canister1, 50), on_response(50));
test.send_ingress(canister0, message);
test.execute_round(ExecutionRoundType::OrdinaryRound);
let metrics = &test.scheduler().metrics;
assert_eq!(metrics.execute_round_called.get(), 1);
assert!(metrics.round_inner_iteration_fin_induct.get_sample_count() >= 3);
assert_eq!(metrics.inner_round_loop_consumed_max_instructions.get(), 0);
assert_eq!(
metrics
.inner_loop_consumed_non_zero_instructions_count
.get(),
3
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
3
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 2);
}
/// A bug in the first implementation of heap delta rate limiting would prevent
/// a canister which generates heap delta from running after the second
/// iteration, even if it was below the limit. This test verifies that a
/// canister generating small heap deltas can run in many iterations.
#[test]
fn canister_can_run_for_multiple_iterations() {
// Create a canister which sends a message to itself on each iteration.
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
// The number of instructions will limit the canister to running at most 6 times.
max_instructions_per_round: NumInstructions::new(300),
max_instructions_per_message: NumInstructions::new(50),
max_instructions_per_message_without_dts: NumInstructions::from(50),
max_instructions_per_slice: NumInstructions::new(50),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister = test.create_canister();
let mut call = other_side(canister, 50).dirty_pages(1);
for _ in 0..10 {
call = other_side(canister, 50)
.dirty_pages(1)
.call(call, on_response(0));
}
test.send_ingress(canister, ingress(50).call(call, on_response(0)));
test.execute_round(ExecutionRoundType::OrdinaryRound);
// Verify that we actually ran 6 iterations.
assert_eq!(
test.scheduler()
.metrics
.inner_loop_consumed_non_zero_instructions_count
.get(),
6
);
assert_eq!(
test.state()
.metadata
.subnet_metrics
.update_transactions_total,
6
);
assert_eq!(test.state().metadata.subnet_metrics.num_canisters, 1);
}
#[test]
fn validate_consumed_instructions_metric() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_message: NumInstructions::from(50),
max_instructions_per_message_without_dts: NumInstructions::from(50),
max_instructions_per_slice: NumInstructions::new(50),
max_instructions_per_round: NumInstructions::from(400),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
let canister = test.create_canister();
test.send_ingress(canister, ingress(50).dirty_pages(1));
test.send_ingress(canister, ingress(50).dirty_pages(1));
test.execute_round(ExecutionRoundType::OrdinaryRound);
let metrics = &test.scheduler().metrics;
assert_eq!(
metrics.instructions_consumed_per_round.get_sample_count(),
2
);
assert_floats_are_equal(
metrics.instructions_consumed_per_round.get_sample_sum(),
100_f64,
);
assert_eq!(
metrics.instructions_consumed_per_message.get_sample_count(),
2
);
assert_floats_are_equal(
metrics.instructions_consumed_per_message.get_sample_sum(),
100_f64,
);
}
#[test]
fn only_charge_for_allocation_after_specified_duration() {
let mut test = SchedulerTestBuilder::new().build();
// Charging handles time=0 as a special case, so it should be set to some
// non-zero time.
let initial_time = Time::from_nanos_since_unix_epoch(1_000_000_000_000);
test.set_time(initial_time);
let time_between_batches = test
.scheduler()
.cycles_account_manager
.duration_between_allocation_charges()
/ 2;
// Just enough memory to cost us one cycle per second.
let bytes_per_cycle = (1_u128 << 30)
.checked_div(
CyclesAccountManagerConfig::application_subnet()
.gib_storage_per_second_fee
.get(),
)
.unwrap() as u64
+ 1;
let initial_cycles = 1_000_000;
let canister = test.create_canister_with(
Cycles::new(initial_cycles),
ComputeAllocation::zero(),
MemoryAllocation::Reserved(NumBytes::from(bytes_per_cycle)),
None,
Some(initial_time),
None,
);
// Don't charge because the time since the last charge is too small.
test.set_time(initial_time + time_between_batches);
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(
test.canister_state(canister).system_state.balance().get(),
initial_cycles
);
// The time of the current batch is now long enough that allocation charging
// should be triggered.
test.set_time(initial_time + 2 * time_between_batches);
test.execute_round(ExecutionRoundType::OrdinaryRound);
assert_eq!(
test.canister_state(canister).system_state.balance().get(),
initial_cycles - 10,
);
}
#[test]
fn charging_for_message_memory_works() {
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_message: NumInstructions::from(1),
max_instructions_per_message_without_dts: NumInstructions::from(1),
max_instructions_per_slice: NumInstructions::new(1),
max_instructions_per_round: NumInstructions::from(1),
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
instruction_overhead_per_canister_for_finalization: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
// Charging handles time=0 as a special case, so it should be set to some
// non-zero time.
let initial_time = Time::from_nanos_since_unix_epoch(1_000_000_000_000);
test.set_time(initial_time);
let initial_cycles = 1_000_000_000_000;
let canister = test.create_canister_with(
Cycles::new(initial_cycles),
ComputeAllocation::zero(),
MemoryAllocation::BestEffort,
None,
Some(initial_time),
None,
);
// Send an ingress that triggers an inter-canister call. Because of the scheduler
// configuration, we can only execute the ingress message but not the
// inter-canister message so this remain in the canister's input queue.
test.send_ingress(
canister,
ingress(1).call(other_side(canister, 1), on_response(1)),
);
test.execute_round(ExecutionRoundType::OrdinaryRound);
let balance_before = test.canister_state(canister).system_state.balance();
// Set time to at least one interval between charges to trigger a charge
// because of message memory consumption.
let charge_duration = test
.scheduler()
.cycles_account_manager
.duration_between_allocation_charges();
test.set_time(initial_time + charge_duration);
test.charge_for_resource_allocations();
// The balance of the canister should have been reduced by the cost of
// message memory during the charge period.
assert_eq!(
test.canister_state(canister).system_state.balance(),
balance_before
- test.memory_cost(
test.canister_state(canister).message_memory_usage(),
charge_duration,
),
);
}
#[test]
fn dont_execute_any_canisters_if_not_enough_instructions_in_round() {
let instructions_per_message = NumInstructions::from(5);
let mut test = SchedulerTestBuilder::new()
.with_scheduler_config(SchedulerConfig {
scheduler_cores: 2,
max_instructions_per_round: instructions_per_message - NumInstructions::from(1),
max_instructions_per_message: instructions_per_message,
max_instructions_per_message_without_dts: instructions_per_message,
max_instructions_per_slice: instructions_per_message,
instruction_overhead_per_message: NumInstructions::from(0),
instruction_overhead_per_canister: NumInstructions::from(0),
..SchedulerConfig::application_subnet()
})
.build();
for _ in 0..3 {
let canister = test.create_canister();
test.send_ingress(canister, ingress(instructions_per_message.get()));
}
test.execute_round(ExecutionRoundType::OrdinaryRound);
for canister_state in test.state().canisters_iter() {
let system_state = &canister_state.system_state;
assert_eq!(system_state.queues().ingress_queue_size(), 1);
assert_eq!(
canister_state.scheduler_state.last_full_execution_round,
ExecutionRound::from(0)
);
assert_eq!(
system_state
.canister_metrics
.skipped_round_due_to_no_messages,
0
);
assert_eq!(system_state.canister_metrics.executed, 0);
assert_eq!(
system_state.canister_metrics.interrupted_during_execution,
0
);
}
}
// Creates an initial state with some canisters that contain very few cycles.
// Ensures that after `execute_round` returns, the canisters have been
// uninstalled.
#[test]
fn canisters_with_insufficient_cycles_are_uninstalled() {
let initial_time = UNIX_EPOCH + Duration::from_secs(1);
let mut test = SchedulerTestBuilder::new().build();
for _ in 0..3 {
test.create_canister_with(
Cycles::new(100),
ComputeAllocation::zero(),
MemoryAllocation::Reserved(NumBytes::from(1 << 30)),
None,
Some(initial_time),
None,
);
}
test.set_time(
initial_time
+ test
.scheduler()
.cycles_account_manager
.duration_between_allocation_charges(),
);
test.execute_round(ExecutionRoundType::OrdinaryRound);
for (_, canister) in test.state().canister_states.iter() {
assert!(canister.execution_state.is_none());
assert_eq!(
canister.scheduler_state.compute_allocation,
ComputeAllocation::zero()
);
assert_eq!(
canister.system_state.memory_allocation,
MemoryAllocation::BestEffort
);
assert_eq!(canister.system_state.canister_version, 1);
}
assert_eq!(
test.scheduler()
.metrics
.num_canisters_uninstalled_out_of_cycles
.get(),
3
);
}
#[test]
fn dont_charge_allocations_for_long_running_canisters() {
let mut test = SchedulerTestBuilder::new().build();
let initial_time = UNIX_EPOCH + Duration::from_secs(1);
let initial_cycles = 10_000_000;
let canister = test.create_canister_with(
Cycles::new(initial_cycles),
ComputeAllocation::zero(),
MemoryAllocation::Reserved(NumBytes::from(1 << 30)),
None,
Some(initial_time),
None,
);