change divisor for tier calculation

This commit is contained in:
Bryan Stitt 2023-06-12 20:44:52 -07:00
parent c771479d94
commit bba7ccf7eb
2 changed files with 15 additions and 18 deletions

View File

@ -49,15 +49,15 @@ impl ConsensusRpcData {
#[derive(Constructor, Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub struct RpcRanking {
tier: u8,
tier: u32,
backup: bool,
head_num: Option<U64>,
}
impl RpcRanking {
pub fn add_offset(&self, offset: u8) -> Self {
pub fn add_offset(&self, offset: u32) -> Self {
Self {
tier: self.tier + offset,
tier: self.tier.saturating_add(offset),
backup: self.backup,
head_num: self.head_num,
}
@ -70,7 +70,7 @@ impl RpcRanking {
}
}
fn sort_key(&self) -> (bool, u8, Reverse<Option<U64>>) {
fn sort_key(&self) -> (bool, u32, Reverse<Option<U64>>) {
// TODO: add soft_limit here? add peak_ewma here?
// TODO: should backup or tier be checked first? now that tiers are automated, backups
// TODO: should we include a random number in here?
@ -104,7 +104,7 @@ pub enum ShouldWaitForBlock {
/// TODO: one data structure of head_rpcs and other_rpcs that is sorted best first
#[derive(Clone, Serialize)]
pub struct ConsensusWeb3Rpcs {
pub(crate) tier: u8,
pub(crate) tier: u32,
pub(crate) backups_needed: bool,
// TODO: this is already inside best_rpcs. Don't skip, instead make a shorter serialize
@ -494,16 +494,13 @@ impl ConsensusFinder {
trace!("weighted_latencies: {}", encoded);
}
// TODO: get someone who is better at math to do something smarter
// this is not a very good use of stddev, but it works for now
let stddev = hist.stdev();
// TODO: get someone who is better at math to do something smarter. maybe involving stddev?
let divisor = 30f64.max(min_latency as f64 / 2.0);
for (rpc, weighted_latency_ms) in weighted_latencies.into_iter() {
let tier = (weighted_latency_ms - min_latency) as f64 / stddev;
let tier = (weighted_latency_ms - min_latency) as f64 / divisor;
let tier = tier.floor() as u64;
let tier = tier.clamp(u8::MIN.into(), u8::MAX.into()) as u8;
let tier = tier.floor() as u32;
// TODO: this should be trace
trace!(
@ -745,7 +742,7 @@ impl ConsensusFinder {
None
}
pub fn worst_tier(&self) -> Option<u8> {
pub fn worst_tier(&self) -> Option<u32> {
self.rpc_heads
.iter()
.map(|(x, _)| x.tier.load(atomic::Ordering::Relaxed))

View File

@ -27,7 +27,7 @@ use serde_json::json;
use std::cmp::Reverse;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{self, AtomicU64, AtomicU8, AtomicUsize};
use std::sync::atomic::{self, AtomicU32, AtomicU64, AtomicUsize};
use std::{cmp::Ordering, sync::Arc};
use tokio::select;
use tokio::sync::watch;
@ -68,7 +68,7 @@ pub struct Web3Rpc {
/// peak_latency is only inside an Option so that the "Default" derive works. it will always be set.
pub(super) peak_latency: Option<PeakEwmaLatency>,
/// Automatically set priority
pub(super) tier: AtomicU8,
pub(super) tier: AtomicU32,
/// Track total requests served
/// TODO: maybe move this to graphana
pub(super) total_requests: AtomicUsize,
@ -230,7 +230,7 @@ impl Web3Rpc {
/// TODO: tests on this!
/// TODO: should tier or block number take priority?
/// TODO: should this return a struct that implements sorting traits?
fn sort_on(&self, max_block: Option<U64>) -> (bool, u8, Reverse<U64>) {
fn sort_on(&self, max_block: Option<U64>) -> (bool, u32, Reverse<U64>) {
let mut head_block = self
.head_block
.as_ref()
@ -251,7 +251,7 @@ impl Web3Rpc {
pub fn sort_for_load_balancing_on(
&self,
max_block: Option<U64>,
) -> ((bool, u8, Reverse<U64>), OrderedFloat<f64>) {
) -> ((bool, u32, Reverse<U64>), OrderedFloat<f64>) {
let sort_on = self.sort_on(max_block);
let weighted_peak_ewma_seconds = self.weighted_peak_ewma_seconds();
@ -267,7 +267,7 @@ impl Web3Rpc {
pub fn shuffle_for_load_balancing_on(
&self,
max_block: Option<U64>,
) -> ((bool, u8, Reverse<U64>), u32) {
) -> ((bool, u32, Reverse<U64>), u32) {
let sort_on = self.sort_on(max_block);
let mut rng = nanorand::tls_rng();