web3-proxy/web3_proxy/src/app_stats.rs

428 lines
17 KiB
Rust
Raw Normal View History

2022-10-10 07:15:07 +03:00
use crate::frontend::authorization::{AuthorizedKey, RequestMetadata};
use crate::jsonrpc::JsonRpcForwardedResponse;
use anyhow::Context;
use chrono::{TimeZone, Utc};
use derive_more::From;
2022-10-10 07:15:07 +03:00
use entities::rpc_accounting;
2022-10-11 08:13:00 +03:00
use hdrhistogram::Histogram;
2022-10-10 07:15:07 +03:00
use moka::future::{Cache, CacheBuilder};
use sea_orm::{ActiveModelTrait, DatabaseConnection};
2022-10-11 08:13:00 +03:00
use std::sync::atomic::{AtomicU64, Ordering};
2022-10-10 07:15:07 +03:00
use std::sync::Arc;
2022-10-25 06:41:59 +03:00
use std::time::Duration;
2022-10-21 01:51:56 +03:00
use tokio::sync::{broadcast, Mutex as AsyncMutex};
2022-10-03 21:08:01 +03:00
use tokio::task::JoinHandle;
2022-10-10 07:15:07 +03:00
use tracing::{error, info, trace};
2022-10-10 07:15:07 +03:00
/// TODO: where should this be defined?
/// TODO: can we use something inside sea_orm instead?
#[derive(Debug)]
2022-10-10 07:15:07 +03:00
pub struct ProxyResponseStat {
2022-10-27 03:12:42 +03:00
rpc_key_id: u64,
2022-10-10 07:15:07 +03:00
method: String,
period_seconds: u64,
period_timestamp: u64,
request_bytes: u64,
/// if this is 0, there was a cache_hit
2022-10-25 06:41:59 +03:00
backend_requests: u64,
error_response: bool,
response_bytes: u64,
response_millis: u64,
}
2022-10-11 08:13:00 +03:00
pub type TimeBucketTimestamp = u64;
2022-10-11 20:34:25 +03:00
pub struct ProxyResponseHistograms {
request_bytes: Histogram<u64>,
response_bytes: Histogram<u64>,
response_millis: Histogram<u64>,
}
impl Default for ProxyResponseHistograms {
fn default() -> Self {
// TODO: how many significant figures?
let request_bytes = Histogram::new(5).expect("creating request_bytes histogram");
let response_bytes = Histogram::new(5).expect("creating response_bytes histogram");
let response_millis = Histogram::new(5).expect("creating response_millis histogram");
Self {
request_bytes,
response_bytes,
response_millis,
}
}
}
2022-10-10 07:15:07 +03:00
// TODO: impl From for our database model
pub struct ProxyResponseAggregate {
2022-10-11 08:13:00 +03:00
// these are the key
2022-10-27 03:12:42 +03:00
// rpc_key_id: u64,
2022-10-10 07:15:07 +03:00
// method: String,
// error_response: bool,
2022-10-11 08:13:00 +03:00
// TODO: this is the grandparent key. get it from there somehow
period_timestamp: u64,
2022-10-25 06:41:59 +03:00
frontend_requests: AtomicU64,
backend_requests: AtomicU64,
backend_retries: AtomicU64,
no_servers: AtomicU64,
cache_misses: AtomicU64,
cache_hits: AtomicU64,
2022-10-11 08:13:00 +03:00
sum_request_bytes: AtomicU64,
sum_response_bytes: AtomicU64,
sum_response_millis: AtomicU64,
2022-10-11 20:34:25 +03:00
histograms: AsyncMutex<ProxyResponseHistograms>,
2022-10-11 08:13:00 +03:00
}
#[derive(Clone, Debug, From, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserProxyResponseKey {
2022-10-27 03:12:42 +03:00
rpc_key_id: u64,
2022-10-11 08:13:00 +03:00
method: String,
error_response: bool,
}
2022-10-10 07:15:07 +03:00
pub type UserProxyResponseCache = Cache<
2022-10-11 08:13:00 +03:00
UserProxyResponseKey,
2022-10-10 07:15:07 +03:00
Arc<ProxyResponseAggregate>,
hashbrown::hash_map::DefaultHashBuilder,
>;
2022-10-11 08:13:00 +03:00
/// key is the "time bucket's timestamp" (timestamp / period * period)
2022-10-10 07:15:07 +03:00
pub type TimeProxyResponseCache =
2022-10-11 08:13:00 +03:00
Cache<TimeBucketTimestamp, UserProxyResponseCache, hashbrown::hash_map::DefaultHashBuilder>;
2022-10-10 07:15:07 +03:00
pub struct StatEmitter {
chain_id: u64,
db_conn: DatabaseConnection,
period_seconds: u64,
/// the outer cache has a TTL and a handler for expiration
aggregated_proxy_responses: TimeProxyResponseCache,
save_rx: flume::Receiver<UserProxyResponseCache>,
}
2022-10-10 07:15:07 +03:00
/// A stat that we aggregate and then store in a database.
#[derive(Debug, From)]
2022-10-03 21:08:01 +03:00
pub enum Web3ProxyStat {
ProxyResponse(ProxyResponseStat),
2022-10-03 21:08:01 +03:00
}
2022-10-10 07:15:07 +03:00
impl ProxyResponseStat {
// TODO: should RequestMetadata be in an arc? or can we handle refs here?
pub fn new(
method: String,
authorized_key: AuthorizedKey,
metadata: Arc<RequestMetadata>,
response: &JsonRpcForwardedResponse,
) -> Self {
// TODO: do this without serializing to a string. this is going to slow us down!
let response_bytes = serde_json::to_string(response)
.expect("serializing here should always work")
.len() as u64;
let backend_requests = metadata.backend_requests.load(Ordering::Acquire);
let period_seconds = metadata.period_seconds;
let period_timestamp =
2022-10-21 02:50:06 +03:00
(metadata.start_datetime.timestamp() as u64) / period_seconds * period_seconds;
let request_bytes = metadata.request_bytes;
let error_response = metadata.error_response.load(Ordering::Acquire);
2022-10-11 20:34:25 +03:00
2022-10-21 02:50:06 +03:00
// TODO: timestamps could get confused by leap seconds. need tokio time instead
let response_millis = metadata.start_instant.elapsed().as_millis() as u64;
2022-10-12 00:31:34 +03:00
2022-10-10 07:15:07 +03:00
Self {
2022-10-27 03:12:42 +03:00
rpc_key_id: authorized_key.rpc_key_id,
2022-10-10 07:15:07 +03:00
method,
backend_requests,
period_seconds,
period_timestamp,
request_bytes,
error_response,
response_bytes,
response_millis,
2022-10-03 21:08:01 +03:00
}
}
}
impl StatEmitter {
2022-10-10 07:15:07 +03:00
pub fn new(chain_id: u64, db_conn: DatabaseConnection, period_seconds: u64) -> Arc<Self> {
let (save_tx, save_rx) = flume::unbounded();
2022-10-11 08:13:00 +03:00
// this needs to be long enough that there are definitely no outstanding queries
// TODO: what should the "safe" multiplier be? what if something is late?
let ttl_seconds = period_seconds * 3;
2022-10-10 07:15:07 +03:00
let aggregated_proxy_responses = CacheBuilder::default()
2022-10-11 08:13:00 +03:00
.time_to_live(Duration::from_secs(ttl_seconds))
.eviction_listener_with_queued_delivery_mode(move |_, v, _| {
// this function must not panic!
if let Err(err) = save_tx.send(v) {
error!(?err, "unable to save. sender closed!");
}
})
2022-10-10 07:15:07 +03:00
.build_with_hasher(hashbrown::hash_map::DefaultHashBuilder::new());
let s = Self {
chain_id,
db_conn,
period_seconds,
aggregated_proxy_responses,
save_rx,
2022-10-10 07:15:07 +03:00
};
Arc::new(s)
}
pub async fn spawn(
2022-10-10 07:15:07 +03:00
self: Arc<Self>,
2022-10-21 01:51:56 +03:00
shutdown_receiver: broadcast::Receiver<()>,
) -> anyhow::Result<(
flume::Sender<Web3ProxyStat>,
JoinHandle<anyhow::Result<()>>,
JoinHandle<anyhow::Result<()>>,
)> {
let (aggregate_tx, aggregate_rx) = flume::unbounded::<Web3ProxyStat>();
2022-10-03 21:08:01 +03:00
2022-10-11 08:13:00 +03:00
// TODO: join and flatten these handles
2022-10-21 01:51:56 +03:00
let aggregate_handle = tokio::spawn(
self.clone()
.aggregate_stats_loop(aggregate_rx, shutdown_receiver),
);
let save_handle = tokio::spawn(self.save_stats_loop());
2022-10-11 08:13:00 +03:00
Ok((aggregate_tx, aggregate_handle, save_handle))
}
2022-10-11 08:13:00 +03:00
/// simple future that reads the channel and aggregates stats in a local cache.
async fn aggregate_stats_loop(
self: Arc<Self>,
aggregate_rx: flume::Receiver<Web3ProxyStat>,
2022-10-21 01:51:56 +03:00
mut shutdown_receiver: broadcast::Receiver<()>,
2022-10-11 08:13:00 +03:00
) -> anyhow::Result<()> {
2022-10-21 02:50:06 +03:00
loop {
tokio::select! {
x = aggregate_rx.recv_async() => {
match x {
Ok(x) => {
trace!(?x, "aggregating stat");
// TODO: increment global stats (in redis? in local cache for prometheus?)
// TODO: batch stats?
// TODO: where can we wait on this handle?
let clone = self.clone();
tokio::spawn(async move { clone.aggregate_stat(x).await });
},
Err(err) => {
error!(?err, "aggregate_rx");
}
2022-10-21 01:51:56 +03:00
}
}
2022-10-21 02:50:06 +03:00
x = shutdown_receiver.recv() => {
match x {
Ok(_) => info!("aggregate stats loop shutting down"),
Err(err) => error!(?err, "shutdown receiver"),
}
break;
2022-10-21 01:51:56 +03:00
}
}
2022-10-11 08:13:00 +03:00
}
2022-10-21 01:51:56 +03:00
// shutting down. force a save of any pending stats
2022-10-11 08:13:00 +03:00
// we do not use invalidate_all because that is done on a background thread
for (key, _) in self.aggregated_proxy_responses.into_iter() {
self.aggregated_proxy_responses.invalidate(&key).await;
}
2022-10-03 21:08:01 +03:00
2022-10-21 01:51:56 +03:00
info!("aggregate stats loop finished");
2022-10-10 07:15:07 +03:00
2022-10-11 08:13:00 +03:00
Ok(())
}
2022-10-03 21:08:01 +03:00
2022-10-11 08:13:00 +03:00
async fn save_stats_loop(self: Arc<Self>) -> anyhow::Result<()> {
while let Ok(x) = self.save_rx.recv_async().await {
// TODO: batch these
for (k, v) in x.into_iter() {
// TODO: this is a lot of variables
2022-10-11 08:13:00 +03:00
let period_datetime = Utc.timestamp(v.period_timestamp as i64, 0);
let frontend_requests = v.frontend_requests.load(Ordering::Acquire);
let backend_requests = v.backend_requests.load(Ordering::Acquire);
let backend_retries = v.backend_retries.load(Ordering::Acquire);
2022-10-12 00:31:34 +03:00
let no_servers = v.no_servers.load(Ordering::Acquire);
2022-10-11 08:13:00 +03:00
let cache_misses = v.cache_misses.load(Ordering::Acquire);
let cache_hits = v.cache_hits.load(Ordering::Acquire);
2022-10-11 20:34:25 +03:00
let sum_request_bytes = v.sum_request_bytes.load(Ordering::Acquire);
let sum_response_millis = v.sum_response_millis.load(Ordering::Acquire);
let sum_response_bytes = v.sum_response_bytes.load(Ordering::Acquire);
2022-10-11 08:13:00 +03:00
2022-10-11 20:34:25 +03:00
let histograms = v.histograms.lock().await;
let request_bytes = &histograms.request_bytes;
2022-10-11 08:13:00 +03:00
let min_request_bytes = request_bytes.min();
let mean_request_bytes = request_bytes.mean();
let p50_request_bytes = request_bytes.value_at_quantile(0.50);
let p90_request_bytes = request_bytes.value_at_quantile(0.90);
let p99_request_bytes = request_bytes.value_at_quantile(0.99);
let max_request_bytes = request_bytes.max();
2022-10-11 20:34:25 +03:00
let response_millis = &histograms.response_millis;
2022-10-11 08:13:00 +03:00
let min_response_millis = response_millis.min();
let mean_response_millis = response_millis.mean();
let p50_response_millis = response_millis.value_at_quantile(0.50);
let p90_response_millis = response_millis.value_at_quantile(0.90);
let p99_response_millis = response_millis.value_at_quantile(0.99);
let max_response_millis = response_millis.max();
2022-10-11 20:34:25 +03:00
let response_bytes = &histograms.response_bytes;
2022-10-11 08:13:00 +03:00
let min_response_bytes = response_bytes.min();
let mean_response_bytes = response_bytes.mean();
let p50_response_bytes = response_bytes.value_at_quantile(0.50);
let p90_response_bytes = response_bytes.value_at_quantile(0.90);
let p99_response_bytes = response_bytes.value_at_quantile(0.99);
let max_response_bytes = response_bytes.max();
2022-10-11 20:34:25 +03:00
drop(histograms);
2022-10-11 08:13:00 +03:00
let stat = rpc_accounting::ActiveModel {
2022-10-11 20:34:25 +03:00
id: sea_orm::NotSet,
2022-10-27 03:12:42 +03:00
rpc_key_id: sea_orm::Set(k.rpc_key_id),
2022-10-11 08:13:00 +03:00
chain_id: sea_orm::Set(self.chain_id),
method: sea_orm::Set(k.method.clone()),
error_response: sea_orm::Set(k.error_response),
period_datetime: sea_orm::Set(period_datetime),
frontend_requests: sea_orm::Set(frontend_requests),
backend_requests: sea_orm::Set(backend_requests),
backend_retries: sea_orm::Set(backend_retries),
2022-10-12 00:31:34 +03:00
no_servers: sea_orm::Set(no_servers),
2022-10-11 08:13:00 +03:00
cache_misses: sea_orm::Set(cache_misses),
cache_hits: sea_orm::Set(cache_hits),
sum_request_bytes: sea_orm::Set(sum_request_bytes),
min_request_bytes: sea_orm::Set(min_request_bytes),
mean_request_bytes: sea_orm::Set(mean_request_bytes),
p50_request_bytes: sea_orm::Set(p50_request_bytes),
p90_request_bytes: sea_orm::Set(p90_request_bytes),
p99_request_bytes: sea_orm::Set(p99_request_bytes),
max_request_bytes: sea_orm::Set(max_request_bytes),
sum_response_millis: sea_orm::Set(sum_response_millis),
min_response_millis: sea_orm::Set(min_response_millis),
mean_response_millis: sea_orm::Set(mean_response_millis),
p50_response_millis: sea_orm::Set(p50_response_millis),
p90_response_millis: sea_orm::Set(p90_response_millis),
p99_response_millis: sea_orm::Set(p99_response_millis),
max_response_millis: sea_orm::Set(max_response_millis),
sum_response_bytes: sea_orm::Set(sum_response_bytes),
min_response_bytes: sea_orm::Set(min_response_bytes),
mean_response_bytes: sea_orm::Set(mean_response_bytes),
p50_response_bytes: sea_orm::Set(p50_response_bytes),
p90_response_bytes: sea_orm::Set(p90_response_bytes),
p99_response_bytes: sea_orm::Set(p99_response_bytes),
max_response_bytes: sea_orm::Set(max_response_bytes),
};
// TODO: if this fails, rever adding the user, too
if let Err(err) = stat
.save(&self.db_conn)
.await
.context("Saving rpc_accounting stat")
{
error!(?err, "unable to save aggregated stats");
}
}
2022-10-11 08:13:00 +03:00
}
2022-10-03 21:08:01 +03:00
2022-10-11 08:13:00 +03:00
info!("stat saver exited");
2022-10-03 21:08:01 +03:00
2022-10-11 08:13:00 +03:00
Ok(())
2022-10-03 21:08:01 +03:00
}
2022-10-10 07:15:07 +03:00
pub async fn aggregate_stat(&self, stat: Web3ProxyStat) -> anyhow::Result<()> {
trace!(?stat, "aggregating");
2022-10-10 07:15:07 +03:00
match stat {
Web3ProxyStat::ProxyResponse(stat) => {
2022-10-11 08:13:00 +03:00
// TODO: move this whole closure to another function?
2022-10-11 20:34:25 +03:00
debug_assert_eq!(stat.period_seconds, self.period_seconds);
2022-10-10 07:15:07 +03:00
2022-10-11 08:13:00 +03:00
// get the user cache for the current period
2022-10-10 07:15:07 +03:00
let user_cache = self
.aggregated_proxy_responses
.get_with(stat.period_timestamp, async move {
2022-10-10 07:15:07 +03:00
CacheBuilder::default()
.build_with_hasher(hashbrown::hash_map::DefaultHashBuilder::new())
})
.await;
2022-10-27 03:12:42 +03:00
let key = (stat.rpc_key_id, stat.method, stat.error_response).into();
2022-10-10 07:15:07 +03:00
let user_aggregate = user_cache
.get_with(key, async move {
2022-10-11 20:34:25 +03:00
let histograms = ProxyResponseHistograms::default();
2022-10-10 07:15:07 +03:00
let aggregate = ProxyResponseAggregate {
period_timestamp: stat.period_timestamp,
2022-10-11 08:13:00 +03:00
// start most things at 0 because we add outside this getter
frontend_requests: 0.into(),
backend_requests: 0.into(),
backend_retries: 0.into(),
2022-10-12 00:31:34 +03:00
no_servers: 0.into(),
2022-10-11 08:13:00 +03:00
cache_misses: 0.into(),
cache_hits: 0.into(),
sum_request_bytes: 0.into(),
sum_response_bytes: 0.into(),
sum_response_millis: 0.into(),
2022-10-11 20:34:25 +03:00
histograms: AsyncMutex::new(histograms),
2022-10-10 07:15:07 +03:00
};
Arc::new(aggregate)
})
.await;
2022-10-11 20:34:25 +03:00
// a stat always come from just 1 frontend request
user_aggregate
.frontend_requests
.fetch_add(1, Ordering::Acquire);
2022-10-12 00:31:34 +03:00
if stat.backend_requests == 0 {
// no backend request. cache hit!
user_aggregate.cache_hits.fetch_add(1, Ordering::Acquire);
} else {
// backend requests! cache miss!
user_aggregate.cache_misses.fetch_add(1, Ordering::Acquire);
// a stat might have multiple backend requests
user_aggregate
.backend_requests
.fetch_add(stat.backend_requests, Ordering::Acquire);
}
2022-10-11 08:13:00 +03:00
user_aggregate
.sum_request_bytes
.fetch_add(stat.request_bytes, Ordering::Release);
2022-10-11 08:13:00 +03:00
user_aggregate
.sum_response_bytes
.fetch_add(stat.response_bytes, Ordering::Release);
2022-10-11 20:34:25 +03:00
user_aggregate
.sum_response_millis
.fetch_add(stat.response_millis, Ordering::Release);
2022-10-11 08:13:00 +03:00
2022-10-11 20:34:25 +03:00
{
let mut histograms = user_aggregate.histograms.lock().await;
2022-10-11 08:13:00 +03:00
// TODO: use `record_correct`?
histograms.request_bytes.record(stat.request_bytes)?;
histograms.response_millis.record(stat.response_millis)?;
2022-10-12 00:31:34 +03:00
histograms.response_bytes.record(stat.response_bytes)?;
2022-10-11 20:34:25 +03:00
}
2022-10-10 07:15:07 +03:00
}
}
Ok(())
}
2022-10-03 21:08:01 +03:00
}