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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use std::{
collections::BTreeMap,
future::Future,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
};
use custom_debug_derive::Debug;
use futures::stream::{FuturesUnordered, StreamExt};
use linera_base::{
crypto::ValidatorPublicKey,
data_types::{Blob, BlobContent, BlockHeight},
identifiers::{BlobId, ChainId},
time::{Duration, Instant},
};
use linera_chain::types::ConfirmedBlockCertificate;
use rand::distributions::{Distribution, WeightedIndex};
use tracing::instrument;
use super::{
cache::{RequestsCache, SubsumingKey},
in_flight_tracker::{InFlightMatch, InFlightTracker},
node_info::NodeInfo,
request::{RequestKey, RequestResult},
scoring::ScoringWeights,
};
use crate::{
client::{
communicate_concurrently,
requests_scheduler::{in_flight_tracker::Subscribed, request::Cacheable},
RequestsSchedulerConfig,
},
environment::Environment,
node::{NodeError, ValidatorNode},
remote_node::RemoteNode,
};
#[cfg(with_metrics)]
pub(super) mod metrics {
use std::sync::LazyLock;
use linera_base::prometheus_util::{
exponential_bucket_latencies, register_histogram_vec, register_int_counter,
register_int_counter_vec,
};
use prometheus::{HistogramVec, IntCounter, IntCounterVec};
/// Histogram of response times per validator (in milliseconds)
pub(super) static VALIDATOR_RESPONSE_TIME: LazyLock<HistogramVec> = LazyLock::new(|| {
register_histogram_vec(
"requests_scheduler_response_time_ms",
"Response time for requests to validators in milliseconds",
&["validator"],
exponential_bucket_latencies(10000.0), // up to 10 seconds
)
});
/// Counter of total requests made to each validator
pub(super) static VALIDATOR_REQUEST_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec(
"requests_scheduler_request_total",
"Total number of requests made to each validator",
&["validator"],
)
});
/// Counter of successful requests per validator
pub(super) static VALIDATOR_REQUEST_SUCCESS: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec(
"requests_scheduler_request_success",
"Number of successful requests to each validator",
&["validator"],
)
});
/// Counter for requests that were resolved from the response cache.
pub(super) static REQUEST_CACHE_DEDUPLICATION: LazyLock<IntCounter> = LazyLock::new(|| {
register_int_counter(
"requests_scheduler_request_deduplication_total",
"Number of requests that were deduplicated by finding the result in the cache.",
)
});
/// Counter for requests that were served from cache
pub static REQUEST_CACHE_HIT: LazyLock<IntCounter> = LazyLock::new(|| {
register_int_counter(
"requests_scheduler_request_cache_hit_total",
"Number of requests that were served from cache",
)
});
}
/// Manages a pool of validator nodes with intelligent load balancing and performance tracking.
///
/// The `RequestsScheduler` maintains performance metrics for each validator node using
/// Exponential Moving Averages (EMA) and uses these metrics to make intelligent routing
/// decisions. It prevents node overload through request capacity limits and automatically
/// retries failed requests on alternative nodes.
///
/// # Examples
///
/// ```ignore
/// // Create with default configuration (balanced scoring)
/// let manager = RequestsScheduler::new(validator_nodes);
///
/// // Create with custom configuration prioritizing low latency
/// let latency_weights = ScoringWeights {
/// latency: 0.6,
/// success: 0.3,
/// load: 0.1,
/// };
/// let manager = RequestsScheduler::with_config(
/// validator_nodes,
/// latency_weights, // custom scoring weights
/// 0.2, // higher alpha for faster adaptation
/// 3000.0, // max expected latency (3 seconds)
/// Duration::from_secs(60), // 60 second cache TTL
/// 200, // cache up to 200 entries
/// Duration::from_millis(200), // max request TTL
/// Duration::from_millis(150), // retry delay
/// );
/// ```
#[derive(Debug, Clone)]
pub struct RequestsScheduler<Env: Environment> {
/// Thread-safe map of validator nodes indexed by their public keys.
/// Each node is wrapped with EMA-based performance tracking information.
nodes: Arc<tokio::sync::RwLock<BTreeMap<ValidatorPublicKey, NodeInfo<Env>>>>,
/// Default scoring weights applied to new nodes.
weights: ScoringWeights,
/// Default EMA smoothing factor for new nodes.
alpha: f64,
/// Default maximum expected latency in milliseconds for score normalization.
max_expected_latency: f64,
/// Delay between starting requests to alternative peers.
retry_delay: Duration,
/// Tracks in-flight requests to deduplicate concurrent requests for the same data.
in_flight_tracker: InFlightTracker<RemoteNode<Env::ValidatorNode>>,
/// Cache of recently completed requests with their results and timestamps.
cache: RequestsCache<RequestKey, RequestResult>,
}
impl<Env: Environment> RequestsScheduler<Env> {
/// Creates a new `RequestsScheduler` with the provided configuration.
pub fn new(
nodes: impl IntoIterator<Item = RemoteNode<Env::ValidatorNode>>,
config: RequestsSchedulerConfig,
) -> Self {
Self::with_config(
nodes,
ScoringWeights::default(),
config.alpha,
config.max_accepted_latency_ms,
Duration::from_millis(config.cache_ttl_ms),
config.cache_max_size,
Duration::from_millis(config.max_request_ttl_ms),
Duration::from_millis(config.retry_delay_ms),
)
}
/// Creates a new `RequestsScheduler` with custom configuration.
///
/// # Arguments
/// - `nodes`: Initial set of validator nodes
/// - `max_requests_per_node`: Maximum concurrent requests per node
/// - `weights`: Scoring weights for performance metrics
/// - `alpha`: EMA smoothing factor (0 < alpha < 1)
/// - `max_expected_latency_ms`: Maximum expected latency for score normalization
/// - `cache_ttl`: Time-to-live for cached responses
/// - `max_cache_size`: Maximum number of entries in the cache
/// - `max_request_ttl`: Maximum latency for an in-flight request before we stop deduplicating it
/// - `retry_delay_ms`: Delay in milliseconds between starting requests to different peers.
#[allow(clippy::too_many_arguments)]
pub fn with_config(
nodes: impl IntoIterator<Item = RemoteNode<Env::ValidatorNode>>,
weights: ScoringWeights,
alpha: f64,
max_expected_latency_ms: f64,
cache_ttl: Duration,
max_cache_size: usize,
max_request_ttl: Duration,
retry_delay: Duration,
) -> Self {
assert!(alpha > 0.0 && alpha < 1.0, "Alpha must be in (0, 1) range");
Self {
nodes: Arc::new(tokio::sync::RwLock::new(
nodes
.into_iter()
.map(|node| {
(
node.public_key,
NodeInfo::with_config(node, weights, alpha, max_expected_latency_ms),
)
})
.collect(),
)),
weights,
alpha,
max_expected_latency: max_expected_latency_ms,
retry_delay,
in_flight_tracker: InFlightTracker::new(max_request_ttl),
cache: RequestsCache::new(cache_ttl, max_cache_size),
}
}
/// Executes an operation with an automatically selected peer, handling deduplication,
/// tracking, and peer selection.
///
/// This method provides a high-level API for executing operations against remote nodes
/// while leveraging the [`RequestsScheduler`]'s intelligent peer selection, performance tracking,
/// and request deduplication capabilities.
///
/// # Type Parameters
/// - `R`: The inner result type (what the operation returns on success)
/// - `F`: The async closure type that takes a `RemoteNode` and returns a future
/// - `Fut`: The future type returned by the closure
///
/// # Arguments
/// - `key`: Unique identifier for request deduplication
/// - `operation`: Async closure that takes a selected peer and performs the operation
///
/// # Returns
/// The result from the operation, potentially from cache or a deduplicated in-flight request
///
/// # Example
/// ```ignore
/// let result: Result<Vec<ConfirmedBlockCertificate>, NodeError> = requests_scheduler
/// .with_best(
/// RequestKey::Certificates { chain_id, start, limit },
/// |peer| async move {
/// peer.download_certificates_from(chain_id, start, limit).await
/// }
/// )
/// .await;
/// ```
#[allow(unused)]
async fn with_best<R, F, Fut>(&self, key: RequestKey, operation: F) -> Result<R, NodeError>
where
R: Cacheable + Clone + Send + 'static,
F: Fn(RemoteNode<Env::ValidatorNode>) -> Fut,
Fut: Future<Output = Result<R, NodeError>> + 'static,
{
// Select the best available peer
let peer = self
.select_best_peer()
.await
.ok_or_else(|| NodeError::WorkerError {
error: "No validators available".to_string(),
})?;
self.with_peer(key, peer, operation).await
}
/// Executes an operation with a specific peer.
///
/// Similar to [`with_best`](Self::with_best), but uses the provided peer directly
/// instead of selecting the best available peer. This is useful when you need to
/// query a specific validator node.
///
/// # Type Parameters
/// - `R`: The inner result type (what the operation returns on success)
/// - `F`: The async closure type that takes a `RemoteNode` and returns a future
/// - `Fut`: The future type returned by the closure
///
/// # Arguments
/// - `key`: Unique identifier for request deduplication
/// - `peer`: The specific peer to use for the operation
/// - `operation`: Async closure that takes the peer and performs the operation
///
/// # Returns
/// The result from the operation, potentially from cache or a deduplicated in-flight request
async fn with_peer<R, F, Fut>(
&self,
key: RequestKey,
peer: RemoteNode<Env::ValidatorNode>,
operation: F,
) -> Result<R, NodeError>
where
R: Cacheable + Clone + Send + 'static,
F: Fn(RemoteNode<Env::ValidatorNode>) -> Fut,
Fut: Future<Output = Result<R, NodeError>> + 'static,
{
self.add_peer(peer.clone()).await;
self.in_flight_tracker
.add_alternative_peer(&key, peer.clone())
.await;
// Clone the nodes Arc so we can move it into the closure
let nodes = self.nodes.clone();
self.deduplicated_request(key, peer, move |peer| {
let fut = operation(peer.clone());
let nodes = nodes.clone();
async move { Self::track_request(nodes, peer, fut).await }
})
.await
}
#[instrument(level = "trace", skip_all)]
async fn download_blob(
&self,
peers: &[RemoteNode<Env::ValidatorNode>],
blob_id: BlobId,
timeout: Duration,
) -> Result<Option<Blob>, NodeError> {
let key = RequestKey::Blob(blob_id);
communicate_concurrently(
peers,
async move |peer| {
self.with_peer(key, peer, move |peer| async move {
peer.download_blob(blob_id).await
})
.await
},
|errors| errors.last().cloned().unwrap(),
timeout,
)
.await
.map_err(|(_validator, error)| error)
}
/// Downloads the blobs with the given IDs. This is done in one concurrent task per blob.
/// Uses intelligent peer selection based on scores and load balancing.
/// Returns `None` if it couldn't find all blobs.
#[instrument(level = "trace", skip_all)]
pub async fn download_blobs(
&self,
peers: &[RemoteNode<Env::ValidatorNode>],
blob_ids: &[BlobId],
timeout: Duration,
) -> Result<Option<Vec<Blob>>, NodeError> {
let mut stream = blob_ids
.iter()
.map(|blob_id| self.download_blob(peers, *blob_id, timeout))
.collect::<FuturesUnordered<_>>();
let mut blobs = Vec::new();
while let Some(maybe_blob) = stream.next().await {
blobs.push(maybe_blob?);
}
Ok(blobs.into_iter().collect::<Option<Vec<_>>>())
}
pub async fn download_certificates(
&self,
peer: &RemoteNode<Env::ValidatorNode>,
chain_id: ChainId,
start: BlockHeight,
limit: u64,
) -> Result<Vec<ConfirmedBlockCertificate>, NodeError> {
let heights = (start.0..start.0 + limit)
.map(BlockHeight)
.collect::<Vec<_>>();
self.with_peer(
RequestKey::Certificates {
chain_id,
heights: heights.clone(),
},
peer.clone(),
move |peer| {
let heights = heights.clone();
async move {
Box::pin(peer.download_certificates_by_heights(chain_id, heights)).await
}
},
)
.await
}
/// Downloads certificates from any of the given validators, using staggered
/// concurrent requests so that slow validators are quickly bypassed.
pub async fn download_certificates_from_validators(
&self,
peers: &[RemoteNode<Env::ValidatorNode>],
chain_id: ChainId,
start: BlockHeight,
limit: u64,
timeout: Duration,
) -> Result<Vec<ConfirmedBlockCertificate>, NodeError> {
let heights = (start.0..start.0 + limit)
.map(BlockHeight)
.collect::<Vec<_>>();
let key = RequestKey::Certificates {
chain_id,
heights: heights.clone(),
};
communicate_concurrently(
peers,
async move |peer| {
self.with_peer(key, peer, move |peer| {
let heights = heights.clone();
async move {
Box::pin(peer.download_certificates_by_heights(chain_id, heights)).await
}
})
.await
},
|errors| errors.last().cloned().unwrap(),
timeout,
)
.await
.map_err(|(_validator, error)| error)
}
pub async fn download_certificates_by_heights(
&self,
peer: &RemoteNode<Env::ValidatorNode>,
chain_id: ChainId,
heights: Vec<BlockHeight>,
) -> Result<Vec<ConfirmedBlockCertificate>, NodeError> {
self.with_peer(
RequestKey::Certificates {
chain_id,
heights: heights.clone(),
},
peer.clone(),
move |peer| {
let heights = heights.clone();
async move {
peer.download_certificates_by_heights(chain_id, heights)
.await
}
},
)
.await
}
pub async fn download_certificate_for_blob(
&self,
peer: &RemoteNode<Env::ValidatorNode>,
blob_id: BlobId,
) -> Result<ConfirmedBlockCertificate, NodeError> {
self.with_peer(
RequestKey::CertificateForBlob(blob_id),
peer.clone(),
move |peer| async move { peer.download_certificate_for_blob(blob_id).await },
)
.await
}
pub async fn download_pending_blob(
&self,
peer: &RemoteNode<Env::ValidatorNode>,
chain_id: ChainId,
blob_id: BlobId,
) -> Result<BlobContent, NodeError> {
self.with_peer(
RequestKey::PendingBlob { chain_id, blob_id },
peer.clone(),
move |peer| async move { peer.node.download_pending_blob(chain_id, blob_id).await },
)
.await
}
/// Returns the alternative peers registered for an in-flight request, if any.
///
/// This can be used to retry a failed request with alternative data sources
/// that were registered during request deduplication.
pub async fn get_alternative_peers(
&self,
key: &RequestKey,
) -> Option<Vec<RemoteNode<Env::ValidatorNode>>> {
self.in_flight_tracker.get_alternative_peers(key).await
}
/// Returns current performance metrics for all managed nodes.
///
/// Each entry contains:
/// - Performance score (f64, normalized 0.0-1.0)
/// - EMA success rate (f64, 0.0-1.0)
/// - Total requests processed (u64)
///
/// Useful for monitoring and debugging node performance.
pub async fn get_node_scores(&self) -> BTreeMap<ValidatorPublicKey, (f64, f64, u64)> {
let nodes = self.nodes.read().await;
let mut result = BTreeMap::new();
for (key, info) in nodes.iter() {
let score = info.calculate_score();
result.insert(
*key,
(score, info.ema_success_rate(), info.total_requests()),
);
}
result
}
/// Wraps a request operation with performance tracking and capacity management.
///
/// This method:
/// 1. Measures response time
/// 2. Updates node metrics based on success/failure
///
/// # Arguments
/// - `nodes`: Arc to the nodes map for updating metrics
/// - `peer`: The remote node to track metrics for
/// - `operation`: Future that performs the actual request
///
/// # Behavior
/// Executes the provided future and tracks metrics for the given peer.
async fn track_request<T, Fut>(
nodes: Arc<tokio::sync::RwLock<BTreeMap<ValidatorPublicKey, NodeInfo<Env>>>>,
peer: RemoteNode<Env::ValidatorNode>,
operation: Fut,
) -> Result<T, NodeError>
where
Fut: Future<Output = Result<T, NodeError>> + 'static,
{
let start_time = Instant::now();
let public_key = peer.public_key;
// Execute the operation
let result = operation.await;
// Update metrics and release slot
let response_time_ms = start_time.elapsed().as_millis() as u64;
let is_success = result.is_ok();
{
let mut nodes_guard = nodes.write().await;
if let Some(info) = nodes_guard.get_mut(&public_key) {
info.update_metrics(is_success, response_time_ms);
let score = info.calculate_score();
tracing::trace!(
node = %public_key,
address = %info.node.node.address(),
success = %is_success,
response_time_ms = %response_time_ms,
score = %score,
total_requests = %info.total_requests(),
"Request completed"
);
}
}
// Record Prometheus metrics
#[cfg(with_metrics)]
{
let validator_name = public_key.to_string();
metrics::VALIDATOR_RESPONSE_TIME
.with_label_values(&[&validator_name])
.observe(response_time_ms as f64);
metrics::VALIDATOR_REQUEST_TOTAL
.with_label_values(&[&validator_name])
.inc();
if is_success {
metrics::VALIDATOR_REQUEST_SUCCESS
.with_label_values(&[&validator_name])
.inc();
}
}
result
}
/// Deduplicates concurrent requests for the same data.
///
/// If a request for the same key is already in flight, this method waits for
/// the existing request to complete and returns its result. Otherwise, it
/// executes the operation and broadcasts the result to all waiting callers.
///
/// This method also performs **subsumption-based deduplication**: if a larger
/// request that contains all the data needed by this request is already cached
/// or in flight, we can extract the subset result instead of making a new request.
///
/// # Arguments
/// - `key`: Unique identifier for the request
/// - `operation`: Async closure that performs the actual request
///
/// # Returns
/// The result from either the in-flight request or the newly executed operation
async fn deduplicated_request<T, F, Fut>(
&self,
key: RequestKey,
peer: RemoteNode<Env::ValidatorNode>,
operation: F,
) -> Result<T, NodeError>
where
T: Cacheable + Clone + Send + 'static,
F: Fn(RemoteNode<Env::ValidatorNode>) -> Fut,
Fut: Future<Output = Result<T, NodeError>> + 'static,
{
// Check cache for exact or subsuming match
if let Some(result) = self.cache.get(&key).await {
return Ok(result);
}
// Check if there's an in-flight request (exact or subsuming)
if let Some(in_flight_match) = self.in_flight_tracker.try_subscribe(&key).await {
match in_flight_match {
InFlightMatch::Exact(Subscribed(mut receiver)) => {
tracing::trace!(
?key,
"deduplicating request (exact match) - joining existing in-flight request"
);
#[cfg(with_metrics)]
metrics::REQUEST_CACHE_DEDUPLICATION.inc();
// Wait for result from existing request
match receiver.recv().await {
Ok(result) => match result.as_ref().clone() {
Ok(res) => match T::try_from(res) {
Ok(converted) => {
tracing::trace!(
?key,
"received result from deduplicated in-flight request"
);
return Ok(converted);
}
Err(_) => {
tracing::warn!(
?key,
"failed to convert result from deduplicated in-flight request, will execute independently"
);
}
},
Err(error) => {
tracing::trace!(
?key,
%error,
"in-flight request failed",
);
// Fall through to execute a new request
}
},
Err(_) => {
tracing::trace!(?key, "in-flight request sender dropped");
// Fall through to execute a new request
}
}
}
InFlightMatch::Subsuming {
key: subsuming_key,
outcome: Subscribed(mut receiver),
} => {
tracing::trace!(
?key,
subsumed_by = ?subsuming_key,
"deduplicating request (subsumption) - joining larger in-flight request"
);
#[cfg(with_metrics)]
metrics::REQUEST_CACHE_DEDUPLICATION.inc();
// Wait for result from the subsuming request
match receiver.recv().await {
Ok(result) => {
match result.as_ref() {
Ok(res) => {
if let Some(extracted) =
key.try_extract_result(&subsuming_key, res)
{
tracing::trace!(
?key,
"extracted subset result from larger in-flight request"
);
match T::try_from(extracted) {
Ok(converted) => return Ok(converted),
Err(_) => {
tracing::trace!(
?key,
"failed to convert extracted result, will execute independently"
);
}
}
} else {
// Extraction failed, fall through to execute our own request
tracing::trace!(
?key,
"failed to extract from subsuming request, will execute independently"
);
}
}
Err(error) => {
tracing::trace!(
?key,
?error,
"subsuming in-flight request failed",
);
// Fall through to execute our own request
}
}
}
Err(_) => {
tracing::trace!(?key, "subsuming in-flight request sender dropped");
}
}
}
}
};
// Create new in-flight entry for this request
self.in_flight_tracker.insert_new(key.clone()).await;
// Remove the peer we're about to use from alternatives (it shouldn't retry with itself)
self.in_flight_tracker
.remove_alternative_peer(&key, &peer)
.await;
// Execute request with staggered parallel - first peer starts immediately,
// alternatives are tried after stagger delays (even if first peer is slow but not failing)
tracing::trace!(?key, ?peer, "executing staggered parallel request");
let result = self
.try_staggered_parallel(&key, peer, &operation, self.retry_delay)
.await;
let result_for_broadcast: Result<RequestResult, NodeError> = result.clone().map(Into::into);
let shared_result = Arc::new(result_for_broadcast);
// Broadcast result and clean up
self.in_flight_tracker
.complete_and_broadcast(&key, shared_result.clone())
.await;
if let Ok(success) = shared_result.as_ref() {
self.cache
.store(key.clone(), Arc::new(success.clone()))
.await;
}
result
}
/// Tries alternative peers in staggered parallel fashion.
///
/// Launches requests starting with the first peer, then dynamically pops alternative peers
/// with a stagger delay between each. Returns the first successful result. This provides
/// a balance between sequential (slow) and fully parallel (wasteful) approaches.
///
/// # Arguments
/// - `key`: The request key (for logging and popping alternatives)
/// - `first_peer`: The initial peer to try first (at time 0)
/// - `operation`: The operation to execute on each peer
/// - `staggered_delay_ms`: Delay in milliseconds between starting each subsequent peer
///
/// # Returns
/// The first successful result, or the last error if all fail
async fn try_staggered_parallel<T, F, Fut>(
&self,
key: &RequestKey,
first_peer: RemoteNode<Env::ValidatorNode>,
operation: &F,
staggered_delay: Duration,
) -> Result<T, NodeError>
where
T: 'static,
F: Fn(RemoteNode<Env::ValidatorNode>) -> Fut,
Fut: Future<Output = Result<T, NodeError>> + 'static,
{
use futures::{
future::{select, Either},
stream::{FuturesUnordered, StreamExt},
};
use linera_base::time::timer::sleep;
let mut futures: FuturesUnordered<Fut> = FuturesUnordered::new();
let peer_index = AtomicU32::new(0);
let push_future = |futures: &mut FuturesUnordered<Fut>, fut: Fut| {
futures.push(fut);
peer_index.fetch_add(1, Ordering::SeqCst)
};
// Start the first peer immediately (no delay)
push_future(&mut futures, operation(first_peer));
let mut last_error = NodeError::UnexpectedMessage;
let mut next_delay = Box::pin(sleep(staggered_delay * peer_index.load(Ordering::SeqCst)));
// Phase 1: Race between futures completion and delays (while alternatives might exist)
loop {
// Exit condition: no futures running and can't start any more
if futures.is_empty() {
if let Some(peer) = self.in_flight_tracker.pop_alternative_peer(key).await {
push_future(&mut futures, operation(peer));
next_delay =
Box::pin(sleep(staggered_delay * peer_index.load(Ordering::SeqCst)));
} else {
// No futures and no alternatives - we're done
break;
}
}
let next_result = Box::pin(futures.next());
match select(next_result, next_delay).await {
// A request completed
Either::Left((Some(result), delay_fut)) => {
// Keep the delay future for next iteration
next_delay = delay_fut;
match result {
Ok(value) => {
tracing::trace!(?key, "staggered parallel request succeeded");
return Ok(value);
}
Err(error) => {
tracing::debug!(
?key,
%error,
"staggered parallel request attempt failed"
);
last_error = error;
// Immediately try next alternative
if let Some(peer) =
self.in_flight_tracker.pop_alternative_peer(key).await
{
push_future(&mut futures, operation(peer));
next_delay = Box::pin(sleep(
staggered_delay * peer_index.load(Ordering::SeqCst),
));
}
}
}
}
// All running futures completed
Either::Left((None, delay_fut)) => {
// Restore the delay future
next_delay = delay_fut;
// Will check at top of loop if we should try more alternatives
continue;
}
// Delay elapsed - try to start next peer
Either::Right((_, _)) => {
if let Some(peer) = self.in_flight_tracker.pop_alternative_peer(key).await {
push_future(&mut futures, operation(peer));
next_delay =
Box::pin(sleep(staggered_delay * peer_index.load(Ordering::SeqCst)));
} else {
// No more alternatives - break out to phase 2
break;
}
}
}
}
// Phase 2: No more alternatives, just wait for remaining futures to complete
while let Some(result) = futures.next().await {
match result {
Ok(value) => {
tracing::trace!(?key, "staggered parallel request succeeded");
return Ok(value);
}
Err(error) => {
tracing::debug!(
?key,
%error,
"staggered parallel request attempt failed"
);
last_error = error;
}
}
}
// All attempts failed
tracing::debug!(?key, "all staggered parallel retry attempts failed");
Err(last_error)
}
/// Returns all peers ordered by their score (highest first).
///
/// Only includes peers that can currently accept requests. Each peer is paired
/// with its calculated score based on latency, success rate, and availability.
///
/// # Returns
/// A vector of `(score, peer)` tuples sorted by score in descending order.
/// Returns an empty vector if no peers can accept requests.
async fn peers_by_score(&self) -> Vec<(f64, RemoteNode<Env::ValidatorNode>)> {
let nodes = self.nodes.read().await;
// Filter nodes that can accept requests and calculate their scores
let mut scored_nodes = Vec::new();
for info in nodes.values() {
let score = info.calculate_score();
scored_nodes.push((score, info.node.clone()));
}
// Sort by score (highest first)
scored_nodes.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
scored_nodes
}
/// Selects the best available peer using weighted random selection from top performers.
///
/// This method:
/// 1. Sorts nodes by performance score
/// 2. Performs weighted random selection from the top 3 performers
///
/// This approach balances between choosing high-performing nodes and distributing
/// load across multiple validators to avoid creating hotspots.
///
/// Returns `None` if no nodes are available.
async fn select_best_peer(&self) -> Option<RemoteNode<Env::ValidatorNode>> {
let scored_nodes = self.peers_by_score().await;
if scored_nodes.is_empty() {
return None;
}
// Use weighted random selection from top performers (top 3 or all if less)
let top_count = scored_nodes.len().min(3);
let top_nodes = &scored_nodes[..top_count];
// Create weights based on normalized scores
// Add small epsilon to prevent zero weights
let weights: Vec<f64> = top_nodes.iter().map(|(score, _)| score.max(0.01)).collect();
if let Ok(dist) = WeightedIndex::new(&weights) {
let mut rng = rand::thread_rng();
let index = dist.sample(&mut rng);
Some(top_nodes[index].1.clone())
} else {
// Fallback to the best node if weights are invalid
tracing::warn!("failed to create weighted distribution, defaulting to best node");
Some(scored_nodes[0].1.clone())
}
}
/// Adds a new peer to the manager if it doesn't already exist.
async fn add_peer(&self, node: RemoteNode<Env::ValidatorNode>) {
let mut nodes = self.nodes.write().await;
let public_key = node.public_key;
nodes.entry(public_key).or_insert_with(|| {
NodeInfo::with_config(node, self.weights, self.alpha, self.max_expected_latency)
});
}
}
#[cfg(test)]
mod tests {
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use linera_base::{
crypto::{CryptoHash, InMemorySigner},
data_types::BlockHeight,
identifiers::ChainId,
time::Duration,
};
use linera_chain::types::ConfirmedBlockCertificate;
use tokio::sync::oneshot;
use super::{super::request::RequestKey, *};
use crate::{
client::requests_scheduler::{MAX_REQUEST_TTL_MS, STAGGERED_DELAY_MS},
node::NodeError,
};
type TestEnvironment = crate::environment::Test;
/// Helper function to create a test RequestsScheduler with custom configuration
fn create_test_manager(
in_flight_timeout: Duration,
cache_ttl: Duration,
) -> Arc<RequestsScheduler<TestEnvironment>> {
let mut manager = RequestsScheduler::with_config(
vec![], // No actual nodes needed for these tests
ScoringWeights::default(),
0.1,
1000.0,
cache_ttl,
100,
in_flight_timeout,
Duration::from_millis(STAGGERED_DELAY_MS),
);
// Replace the tracker with one using the custom timeout
manager.in_flight_tracker = InFlightTracker::new(in_flight_timeout);
Arc::new(manager)
}
/// Helper function to create a test result
fn test_result_ok() -> Result<Vec<ConfirmedBlockCertificate>, NodeError> {
Ok(vec![])
}
/// Helper function to create a test request key
fn test_key() -> RequestKey {
RequestKey::Certificates {
chain_id: ChainId(CryptoHash::test_hash("test")),
heights: vec![BlockHeight(0), BlockHeight(1)],
}
}
/// Helper function to create a dummy peer for testing
fn dummy_peer() -> RemoteNode<<TestEnvironment as Environment>::ValidatorNode> {
use crate::test_utils::{MemoryStorageBuilder, TestBuilder};
// Create a minimal test builder to get a validator node
let mut builder = futures::executor::block_on(async {
TestBuilder::new(
MemoryStorageBuilder::default(),
1,
0,
linera_base::crypto::InMemorySigner::new(None),
)
.await
.unwrap()
});
let node = builder.node(0);
let public_key = node.name();
RemoteNode { public_key, node }
}
#[tokio::test]
async fn test_cache_hit_returns_cached_result() {
// Create a manager with standard cache TTL
let manager = create_test_manager(Duration::from_secs(60), Duration::from_secs(60));
let key = test_key();
let peer = dummy_peer();
// Track how many times the operation is executed
let execution_count = Arc::new(AtomicUsize::new(0));
let execution_count_clone = execution_count.clone();
// First call - should execute the operation and cache the result
let result1: Result<Vec<ConfirmedBlockCertificate>, NodeError> = manager
.deduplicated_request(key.clone(), peer.clone(), |_| {
let count = execution_count_clone.clone();
async move {
count.fetch_add(1, Ordering::SeqCst);
test_result_ok()
}
})
.await;
assert!(result1.is_ok());
assert_eq!(execution_count.load(Ordering::SeqCst), 1);
// Second call - should return cached result without executing the operation
let execution_count_clone2 = execution_count.clone();
let result2: Result<Vec<ConfirmedBlockCertificate>, NodeError> = manager
.deduplicated_request(key.clone(), peer.clone(), |_| {
let count = execution_count_clone2.clone();
async move {
count.fetch_add(1, Ordering::SeqCst);
test_result_ok()
}
})
.await;
assert_eq!(result1, result2);
// Operation should still only have been executed once (cache hit)
assert_eq!(execution_count.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_in_flight_request_deduplication() {
let manager = create_test_manager(Duration::from_secs(60), Duration::from_secs(60));
let key = test_key();
let peer = dummy_peer();
// Track how many times the operation is executed
let execution_count = Arc::new(AtomicUsize::new(0));
// Create a channel to control when the first operation completes
let (tx, rx) = oneshot::channel();
let rx = Arc::new(tokio::sync::Mutex::new(Some(rx)));
// Start first request (will be slow - waits for signal)
let manager_clone = Arc::clone(&manager);
let key_clone = key.clone();
let execution_count_clone = execution_count.clone();
let rx_clone = Arc::clone(&rx);
let peer_clone = peer.clone();
let first_request = tokio::spawn(async move {
manager_clone
.deduplicated_request(key_clone, peer_clone, |_| {
let count = execution_count_clone.clone();
let rx = Arc::clone(&rx_clone);
async move {
count.fetch_add(1, Ordering::SeqCst);
// Wait for signal before completing
if let Some(receiver) = rx.lock().await.take() {
receiver.await.unwrap();
}
test_result_ok()
}
})
.await
});
// Start second request - should deduplicate and wait for the first
let execution_count_clone2 = execution_count.clone();
let second_request = tokio::spawn(async move {
manager
.deduplicated_request(key, peer, |_| {
let count = execution_count_clone2.clone();
async move {
count.fetch_add(1, Ordering::SeqCst);
test_result_ok()
}
})
.await
});
// Signal the first request to complete
tx.send(()).unwrap();
// Both requests should complete successfully
let result1: Result<Vec<ConfirmedBlockCertificate>, NodeError> =
first_request.await.unwrap();
let result2: Result<Vec<ConfirmedBlockCertificate>, NodeError> =
second_request.await.unwrap();
assert!(result1.is_ok());
assert_eq!(result1, result2);
// Operation should only have been executed once (deduplication worked)
assert_eq!(execution_count.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_multiple_subscribers_all_notified() {
let manager = create_test_manager(Duration::from_secs(60), Duration::from_secs(60));
let key = test_key();
let peer = dummy_peer();
// Track how many times the operation is executed
let execution_count = Arc::new(AtomicUsize::new(0));
// Create a channel to control when the operation completes
let (tx, rx) = oneshot::channel();
let rx = Arc::new(tokio::sync::Mutex::new(Some(rx)));
// Start first request (will be slow - waits for signal)
let manager_clone1 = Arc::clone(&manager);
let key_clone1 = key.clone();
let execution_count_clone = execution_count.clone();
let rx_clone = Arc::clone(&rx);
let peer_clone = peer.clone();
let first_request = tokio::spawn(async move {
manager_clone1
.deduplicated_request(key_clone1, peer_clone, |_| {
let count = execution_count_clone.clone();
let rx = Arc::clone(&rx_clone);
async move {
count.fetch_add(1, Ordering::SeqCst);
if let Some(receiver) = rx.lock().await.take() {
receiver.await.unwrap();
}
test_result_ok()
}
})
.await
});
// Start multiple additional requests - all should deduplicate
let mut handles = vec![];
for _ in 0..5 {
let manager_clone = Arc::clone(&manager);
let key_clone = key.clone();
let execution_count_clone = execution_count.clone();
let peer_clone = peer.clone();
let handle = tokio::spawn(async move {
manager_clone
.deduplicated_request(key_clone, peer_clone, |_| {
let count = execution_count_clone.clone();
async move {
count.fetch_add(1, Ordering::SeqCst);
test_result_ok()
}
})
.await
});
handles.push(handle);
}
// Signal the first request to complete
tx.send(()).unwrap();
// First request should complete successfully
let result: Result<Vec<ConfirmedBlockCertificate>, NodeError> =
first_request.await.unwrap();
assert!(result.is_ok());
// All subscriber requests should also complete successfully
for handle in handles {
assert_eq!(handle.await.unwrap(), result);
}
// Operation should only have been executed once (all requests were deduplicated)
assert_eq!(execution_count.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_timeout_triggers_new_request() {
// Create a manager with a very short in-flight timeout
let manager = create_test_manager(Duration::from_millis(50), Duration::from_secs(60));
let key = test_key();
let peer = dummy_peer();
// Track how many times the operation is executed
let execution_count = Arc::new(AtomicUsize::new(0));
// Create a channel to control when the first operation completes
let (tx, rx) = oneshot::channel();
let rx = Arc::new(tokio::sync::Mutex::new(Some(rx)));
// Start first request (will be slow - waits for signal)
let manager_clone = Arc::clone(&manager);
let key_clone = key.clone();
let execution_count_clone = execution_count.clone();
let rx_clone = Arc::clone(&rx);
let peer_clone = peer.clone();
let first_request = tokio::spawn(async move {
manager_clone
.deduplicated_request(key_clone, peer_clone, |_| {
let count = execution_count_clone.clone();
let rx = Arc::clone(&rx_clone);
async move {
count.fetch_add(1, Ordering::SeqCst);
if let Some(receiver) = rx.lock().await.take() {
receiver.await.unwrap();
}
test_result_ok()
}
})
.await
});
// Wait for the timeout to elapse
tokio::time::sleep(Duration::from_millis(MAX_REQUEST_TTL_MS + 1)).await;
// Start second request - should NOT deduplicate because first request exceeded timeout
let execution_count_clone2 = execution_count.clone();
let second_request = tokio::spawn(async move {
manager
.deduplicated_request(key, peer, |_| {
let count = execution_count_clone2.clone();
async move {
count.fetch_add(1, Ordering::SeqCst);
test_result_ok()
}
})
.await
});
// Wait for second request to complete
let result2: Result<Vec<ConfirmedBlockCertificate>, NodeError> =
second_request.await.unwrap();
assert!(result2.is_ok());
// Complete the first request
tx.send(()).unwrap();
let result1: Result<Vec<ConfirmedBlockCertificate>, NodeError> =
first_request.await.unwrap();
assert!(result1.is_ok());
// Operation should have been executed twice (timeout triggered new request)
assert_eq!(execution_count.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn test_alternative_peers_registered_on_deduplication() {
use linera_base::identifiers::BlobType;
use crate::test_utils::{MemoryStorageBuilder, TestBuilder};
// Create a test environment with three validators
let mut builder = TestBuilder::new(
MemoryStorageBuilder::default(),
3,
0,
InMemorySigner::new(None),
)
.await
.unwrap();
// Get validator nodes
let nodes: Vec<_> = (0..3)
.map(|i| {
let node = builder.node(i);
let public_key = node.name();
RemoteNode { public_key, node }
})
.collect();
// Create a RequestsScheduler
let manager: Arc<RequestsScheduler<TestEnvironment>> =
Arc::new(RequestsScheduler::with_config(
nodes.clone(),
ScoringWeights::default(),
0.1,
1000.0,
Duration::from_secs(60),
100,
Duration::from_millis(MAX_REQUEST_TTL_MS),
Duration::from_millis(STAGGERED_DELAY_MS),
));
let key = RequestKey::Blob(BlobId::new(
CryptoHash::test_hash("test_blob"),
BlobType::Data,
));
// Create a channel to control when first request completes
let (tx, rx) = oneshot::channel();
let rx = Arc::new(tokio::sync::Mutex::new(Some(rx)));
// Start first request with node 0 (will block until signaled)
let manager_clone = Arc::clone(&manager);
let node_clone = nodes[0].clone();
let key_clone = key.clone();
let rx_clone = Arc::clone(&rx);
let first_request = tokio::spawn(async move {
manager_clone
.with_peer(key_clone, node_clone, move |_peer| {
let rx = Arc::clone(&rx_clone);
async move {
// Wait for signal
if let Some(receiver) = rx.lock().await.take() {
receiver.await.unwrap();
}
Ok(None) // Return Option<Blob>
}
})
.await
});
// Give first request time to start and become in-flight
tokio::time::sleep(Duration::from_millis(100)).await;
// Start second and third requests with different nodes
// These should register as alternatives and wait for the first request
let handles: Vec<_> = vec![nodes[1].clone(), nodes[2].clone()]
.into_iter()
.map(|node| {
let manager_clone = Arc::clone(&manager);
let key_clone = key.clone();
tokio::spawn(async move {
manager_clone
.with_peer(key_clone, node, |_peer| async move {
Ok(None) // Return Option<Blob>
})
.await
})
})
.collect();
// Give time for alternative peers to register
tokio::time::sleep(Duration::from_millis(100)).await;
// Alternatives are being popped as staggered parallel runs.
// The first request is blocked waiting for the signal, so staggered parallel has started
// and may have already popped one or both alternatives. We just verify that at least
// one alternative was registered (before being popped).
// This test primarily validates that alternatives can be registered during deduplication.
// Signal first request to complete
tx.send(()).unwrap();
// Wait for all requests to complete
let _result1 = first_request.await.unwrap();
for handle in handles {
handle.await.unwrap().ok();
}
// After completion, the in-flight entry should be removed
tokio::time::sleep(Duration::from_millis(50)).await;
let alt_peers = manager.get_alternative_peers(&key).await;
assert!(
alt_peers.is_none(),
"Expected in-flight entry to be removed after completion"
);
}
#[tokio::test]
async fn test_staggered_parallel_retry_on_failure() {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::test_utils::{MemoryStorageBuilder, TestBuilder};
// Create a test environment with four validators
let mut builder = TestBuilder::new(
MemoryStorageBuilder::default(),
4,
0,
InMemorySigner::new(None),
)
.await
.unwrap();
// Get validator nodes
let nodes: Vec<_> = (0..4)
.map(|i| {
let node = builder.node(i);
let public_key = node.name();
RemoteNode { public_key, node }
})
.collect();
let staggered_delay = Duration::from_millis(100);
// Store public keys for comparison
let node0_key = nodes[0].public_key;
let node2_key = nodes[2].public_key;
// Create a RequestsScheduler
let manager: Arc<RequestsScheduler<TestEnvironment>> =
Arc::new(RequestsScheduler::with_config(
nodes.clone(),
ScoringWeights::default(),
0.1,
1000.0,
Duration::from_secs(60),
100,
Duration::from_millis(MAX_REQUEST_TTL_MS),
staggered_delay,
));
let key = test_key();
// Track when each peer is called
let call_times = Arc::new(tokio::sync::Mutex::new(Vec::new()));
let start_time = Instant::now();
// Track call count per peer
let call_count = Arc::new(AtomicU64::new(0));
let call_times_clone = Arc::clone(&call_times);
let call_count_clone = Arc::clone(&call_count);
// Test the staggered parallel retry logic directly
let operation = |peer: RemoteNode<<TestEnvironment as Environment>::ValidatorNode>| {
let times = Arc::clone(&call_times_clone);
let count = Arc::clone(&call_count_clone);
let start = start_time;
async move {
let elapsed = Instant::now().duration_since(start);
times.lock().await.push((peer.public_key, elapsed));
count.fetch_add(1, Ordering::SeqCst);
if peer.public_key == node0_key {
// Node 0 fails quickly
Err(NodeError::UnexpectedMessage)
} else if peer.public_key == node2_key {
// Node 2 succeeds after a delay
tokio::time::sleep(staggered_delay / 2).await;
Ok(vec![])
} else {
// Other nodes take longer or fail
tokio::time::sleep(staggered_delay * 2).await;
Err(NodeError::UnexpectedMessage)
}
}
};
// Setup: Insert in-flight entry and register alternative peers
manager.in_flight_tracker.insert_new(key.clone()).await;
// Register nodes 3, 2, 1 as alternatives (will be popped in reverse: 1, 2, 3)
for node in nodes.iter().skip(1).rev() {
manager
.in_flight_tracker
.add_alternative_peer(&key, node.clone())
.await;
}
// Use node 0 as first peer, alternatives will be popped: node 1, then 2, then 3
let result: Result<Vec<ConfirmedBlockCertificate>, NodeError> = manager
.try_staggered_parallel(&key, nodes[0].clone(), &operation, staggered_delay)
.await;
// Should succeed with result from node 2
assert!(
result.is_ok(),
"Expected request to succeed with alternative peer"
);
// Verify timing: calls should be staggered, not sequential
let times = call_times.lock().await;
// Can't test exactly 2 b/c we sleep _inside_ the operation and increase right at the start of it.
assert!(
times.len() >= 2,
"Should have tried at least 2 peers, got {}",
times.len()
);
// First call should be at ~0ms
assert!(
times[0].1.as_millis() < 50,
"First peer should be called immediately, was called at {}ms",
times[0].1.as_millis()
);
// Second call should start immediately after first fails (aggressive retry)
// When node 0 fails immediately, we immediately start node 1
if times.len() > 1 {
let delay = times[1].1.as_millis();
assert!(
delay < 50,
"Second peer should be called immediately on first failure, got {}ms",
delay
);
}
// Total time should be significantly less than sequential (which would be
// ~650ms: 200ms + 200ms + 50ms + 200ms). With parallel staggered retry:
// node0 fails immediately, node1 starts immediately, the next delay is
// 200ms (peer_index=2), node2 starts at ~200ms and succeeds at ~250ms.
let total_time = Instant::now().duration_since(start_time).as_millis();
assert!(
total_time < 500,
"Total time should be less than 500ms (sequential would be ~650ms), got {}ms",
total_time
);
}
}