Skip to content

Commit

Permalink
feat: ✨ improve hit rate metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 9, 2023
1 parent ed966e1 commit 9d41d66
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
19 changes: 8 additions & 11 deletions backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,16 @@ impl JudgerController {

MeterGuard(self)
}
#[instrument(skip(self,ps_guard, stream, model, scores))]
#[instrument(skip(self, ps_guard, stream, model, scores))]
async fn stream(
self:Arc<Self>,
self: Arc<Self>,
ps_guard: PubGuard<Result<SubmitStatus, Status>, i32>,
mut stream: tonic::Streaming<JudgeResponse>,
mut model: submit::ActiveModel,
mut scores: Vec<u32>,
submit_id: i32,
) {
let _=self.record();
let _ = self.record();
let mut result = 0;
let mut running_case = 0;
let mut time = 0;
Expand Down Expand Up @@ -233,7 +233,7 @@ impl JudgerController {
log::warn!("failed to commit the judge result: {}", err);
}
}
pub async fn submit(self:&Arc<Self>, submit: Submit) -> Result<i32, Error> {
pub async fn submit(self: &Arc<Self>, submit: Submit) -> Result<i32, Error> {
check_rate_limit!(self);
let db = DB.get().unwrap();

Expand Down Expand Up @@ -285,13 +285,10 @@ impl JudgerController {

conn.report_success();

tokio::spawn(self.clone().stream(
tx,
res.into_inner(),
submit_model,
scores,
submit_id,
));
tokio::spawn(
self.clone()
.stream(tx, res.into_inner(), submit_model, scores, submit_id),
);

Ok(submit_id)
}
Expand Down
44 changes: 43 additions & 1 deletion backend/src/controller/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use opentelemetry::{global, metrics::UpDownCounter};
use std::sync::atomic::{AtomicUsize, Ordering};

use crossbeam_queue::SegQueue;
use opentelemetry::{
global,
metrics::{ObservableGauge, UpDownCounter},
};

use crate::init::logger::PACKAGE_NAME;

Expand Down Expand Up @@ -27,3 +33,39 @@ impl MetricsController {
}
}
}

pub struct RateMetrics<const S: usize> {
meter: ObservableGauge<f64>,
record: SegQueue<bool>,
sets: AtomicUsize,
}

impl<const S: usize> RateMetrics<S> {
pub fn new(name: &'static str) -> Self {
let record = SegQueue::new();
for _ in 0..S {
record.push(true);
}
Self {
meter: global::meter(PACKAGE_NAME)
.f64_observable_gauge(name)
.init(),
record,
sets: AtomicUsize::new(S),
}
}
pub fn set(&self) {
self.record.push(true);
if !self.record.pop().unwrap() {
let sets = self.sets.fetch_sub(1, Ordering::Acquire);
self.meter.observe((sets as f64) / (S as f64), &[])
}
}
pub fn unset(&self) {
self.record.push(false);
if self.record.pop().unwrap() {
let sets = self.sets.fetch_add(1, Ordering::Acquire);
self.meter.observe((sets as f64) / (S as f64), &[])
}
}
}
21 changes: 8 additions & 13 deletions backend/src/controller/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use chrono::{Duration, Local, NaiveDateTime};
use entity::token;
use opentelemetry::{global, metrics::ObservableGauge};
use quick_cache::sync::Cache;
use ring::rand::*;
use sea_orm::{
Expand All @@ -10,10 +9,9 @@ use std::sync::Arc;
use tokio::time;
use tracing::{instrument, Instrument, Span};

use crate::{
init::{db::DB, logger::PACKAGE_NAME},
report_internal,
};
use crate::{init::db::DB, report_internal};

use super::metrics::RateMetrics;

const CLEAN_DUR: time::Duration = time::Duration::from_secs(60 * 30);
type Rand = [u8; 20];
Expand Down Expand Up @@ -65,7 +63,7 @@ pub struct TokenController {
#[cfg(feature = "single-instance")]
cache: Cache<Rand, CachedToken>,
rand: SystemRandom,
cache_meter: ObservableGauge<u64>,
cache_meter: RateMetrics<30>,
}

impl TokenController {
Expand All @@ -78,9 +76,7 @@ impl TokenController {
#[cfg(feature = "single-instance")]
cache,
rand: SystemRandom::new(),
cache_meter: global::meter(PACKAGE_NAME)
.u64_observable_gauge("cached_token")
.init(),
cache_meter: RateMetrics::new("hitrate_token"),
});
tokio::spawn(async move {
let db = DB.get().unwrap();
Expand Down Expand Up @@ -159,6 +155,7 @@ impl TokenController {
let token = match cache_result {
Some(token) => {
tracing::trace!(user_id = token.user_id, "cache_hit");
self.cache_meter.set();
token
}
None => {
Expand All @@ -171,12 +168,10 @@ impl TokenController {
.into();

tracing::trace!(user_id = token.user_id, "cache_missed");
self.cache_meter.unset();

#[cfg(feature = "single-instance")]
{
self.cache.insert(rand, token.clone());
self.cache_meter.observe(self.cache.weight(), &[]);
}
self.cache.insert(rand, token.clone());

token
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/endpoint/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ impl UserSet for Arc<Server> {
let model = model.save(db).await.map_err(Into::<Error>::into)?;

self.metrics.user.add(1, &[]);
let id=model.id.unwrap();

let id = model.id.unwrap();
self.dup.store(user_id, uuid, id);

Ok(Response::new(id.into()))
Expand Down

0 comments on commit 9d41d66

Please sign in to comment.