dfinity / agent-rs

A collection of libraries and tools for building software around the Internet Computer, in Rust.
https://sdk.dfinity.org/
Apache License 2.0
124 stars 77 forks source link

feat: add penalty for nodes unavailability in latency-based routing #587

Closed nikolay-komarevskiy closed 2 months ago

nikolay-komarevskiy commented 2 months ago

Description

In the current implementation of the latency-based routing, the probability of choosing a node as a routing URL is proportional to the node's score. The score is calculated as an inverse value of the moving average of latency measurements over some sliding window. Hence, nodes with smaller latencies produce higher score values and are more likely to be selected. However, in the current approach when a node was unavailable (within the sliding window), it was completely removed from routing. This was a "harsh" punishment, which is not always optimal, as latency can experience sporadic spikes.

In this PR we introduce a more sophisticated and customizable way of calculating node's score, where the combination of node's latency and availability are used to produce the final score. Node's score is now penalized, if it was unavailable within the sliding window measurements and not set to zero as before. A node is only removed from routing if it was unavailable in the last measurement.

Approach:

Example:

let latencies_secs = [None,Some(0.190),Some(0.195),Some(0.180),Some(0.195),Some(0.180),Some(0.185),Some(0.2),Some(0.192),Some(0.215),Some(0.175),None,Some(0.190),None,Some(0.187),None,Some(0.165),Some(0.189),None,Some(0.192),Some(0.200),Some(0.187),Some(0.178),Some(0.185)]];

image

How Has This Been Tested?

Additional tests in latency_based_routing.rs have been introduced.

Checklist: