Skip to content

Commit

Permalink
Propagate router estimation errors (#867)
Browse files Browse the repository at this point in the history
* propagate estimation errors

* fix build

* format
  • Loading branch information
sanity authored Oct 11, 2023
1 parent b9c76f1 commit d96d5e6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
27 changes: 23 additions & 4 deletions crates/core/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod util;
use crate::ring::{Location, PeerKeyLocation};
use isotonic_estimator::{EstimatorType, IsotonicEstimator, IsotonicEvent};
use serde::Serialize;
use std::time::Duration;
use std::{fmt, time::Duration};
use util::{Mean, TransferSpeed};

#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -196,15 +196,24 @@ impl Router {
let time_to_response_start_estimate = self
.response_start_time_estimator
.estimate_retrieval_time(peer, contract_location)
.unwrap();
.map_err(|e| {
RoutingError::EstimationError(format!(
"Response Start Time Estimation failed: {}",
e
))
})?;
let failure_estimate = self
.failure_estimator
.estimate_retrieval_time(peer, contract_location)
.unwrap();
.map_err(|e| {
RoutingError::EstimationError(format!("Failure Estimation failed: {}", e))
})?;
let transfer_rate_estimate = self
.transfer_rate_estimator
.estimate_retrieval_time(peer, contract_location)
.unwrap();
.map_err(|e| {
RoutingError::EstimationError(format!("Transfer Rate Estimation failed: {}", e))
})?;

// This is a fairly naive approach, assuming that the cost of a failure is a multiple
// of the cost of success.
Expand Down Expand Up @@ -233,6 +242,16 @@ impl Router {
#[derive(Debug)]
pub(crate) enum RoutingError {
InsufficientDataError,
EstimationError(String),
}

impl fmt::Display for RoutingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RoutingError::InsufficientDataError => write!(f, "Insufficient data provided"),
RoutingError::EstimationError(err_msg) => write!(f, "Estimation error: {}", err_msg),
}
}
}

#[derive(Debug, Clone, Copy, Serialize)]
Expand Down
10 changes: 9 additions & 1 deletion crates/core/src/router/isotonic_estimator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ring::{Distance, Location, PeerKeyLocation};
use pav_regression::pav::{IsotonicRegression, Point};
use serde::Serialize;
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

const MIN_POINTS_FOR_REGRESSION: usize = 5;

Expand Down Expand Up @@ -160,6 +160,14 @@ pub(crate) enum EstimationError {
InsufficientData, // Error indicating that there is not enough data for estimation
}

impl fmt::Display for EstimationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EstimationError::InsufficientData => write!(f, "Insufficient data for estimation"),
}
}
}

/// A routing event is a single request to a peer for a contract, and some value indicating
/// the result of the request, such as the time it took to retrieve the contract.
#[derive(Debug, Clone)]
Expand Down

0 comments on commit d96d5e6

Please sign in to comment.