Skip to content

Commit

Permalink
feat: 🧪 improve logging, interceptor fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 7, 2023
1 parent b2ae9f9 commit 47acf86
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 8 deletions.
5 changes: 5 additions & 0 deletions backend/src/controller/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl From<HashValue> for Vec<u8> {
}

impl CryptoController {
#[tracing::instrument(level = "info")]
pub fn new(config: &GlobalConfig) -> Self {
let rng = rand::SystemRandom::new();
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
Expand Down Expand Up @@ -73,16 +74,19 @@ impl CryptoController {
.to_vec();
HashValue(hashed)
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn sign(&self, src: &str) -> Vec<u8> {
self.signer.sign(src.as_bytes()).as_ref().to_vec()
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn verify(&self, src: &[u8], signature: &[u8]) -> bool {
let peer_public_key = signature::UnparsedPublicKey::new(
&signature::ED25519,
self.signer.public_key().as_ref(),
);
peer_public_key.verify(src, signature).is_ok()
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn encode<M: Serialize>(&self, obj: M) -> Result<Vec<u8>> {
let mut raw = bincode::serialize(&obj)?;
let signature = self.signer.sign(&raw);
Expand All @@ -91,6 +95,7 @@ impl CryptoController {

Ok(raw)
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn decode<M: DeserializeOwned>(&self, raw: &[u8]) -> Result<M> {
let (raw, signature) = raw.split_at(raw.len() - 64);
if !self.verify(raw, signature) {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/controller/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ impl DupController {
#[cfg(feature = "single-instance")]
self.dups.insert((user_id, uuid), result);
}
#[tracing::instrument(level = "debug", skip(self))]
pub fn check(&self, user_id: i32, uuid: &Uuid) -> Option<i32> {
#[cfg(feature = "single-instance")]
if let Some(x) = self.dups.get(&(user_id, *uuid)) {
log::debug!("duplicated request_id: {}, result: {}", uuid, x);
return Some(x);
}
None
Expand Down
7 changes: 6 additions & 1 deletion backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ pub struct ConnGuard {
impl ConnGuard {
pub fn report_success(&mut self) {
self.upstream.healthy.fetch_add(3, Ordering::Acquire);
self.upstream.healthy.fetch_min(HEALTHY_THRESHOLD, Ordering::Acquire);
self.upstream
.healthy
.fetch_min(HEALTHY_THRESHOLD, Ordering::Acquire);
}
}

Expand Down Expand Up @@ -108,6 +110,7 @@ async fn discover<I: Routable + Send>(
loop {
match instance.discover().await {
RouteStatus::NewConnection(detail) => {
log::info!("new upstream found: {}", detail.uri);
let router = match router.upgrade() {
Some(x) => x,
None => break,
Expand Down Expand Up @@ -141,6 +144,8 @@ pub struct Router {
}

impl Router {
// skip because config contain basic auth secret
#[tracing::instrument(level = "debug",skip_all)]
pub async fn new(config: Vec<JudgerConfig>) -> Result<Arc<Self>, Error> {
let self_ = Arc::new(Self {
routing_table: Map::new(),
Expand Down
1 change: 1 addition & 0 deletions backend/src/controller/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ impl TokenController {

Ok(Some(()))
}
#[instrument(skip_all, name="token_removal", fields(uid = user_id))]
pub async fn remove_by_user_id(
&self,
user_id: i32,
Expand Down
9 changes: 9 additions & 0 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl From<Model> for ContestInfo {

#[async_trait]
impl ContestSet for Arc<Server> {
#[instrument(skip_all, level = "debug")]
async fn list(
&self,
req: Request<ListRequest>,
Expand Down Expand Up @@ -120,6 +121,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(ListContestResponse { list, next_session }))
}
#[instrument(skip_all, level = "debug")]
async fn search_by_text(
&self,
req: Request<TextSearchRequest>,
Expand All @@ -146,6 +148,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(ListContestResponse { list, next_session }))
}
#[instrument(skip_all, level = "debug")]
async fn full_info(
&self,
req: Request<ContestId>,
Expand All @@ -162,6 +165,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(model.into()))
}
#[instrument(skip_all, level = "debug")]
async fn create(
&self,
req: Request<CreateContestRequest>,
Expand Down Expand Up @@ -202,6 +206,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(model.id.unwrap().into()))
}
#[instrument(skip_all, level = "debug")]
async fn update(&self, req: Request<UpdateContestRequest>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down Expand Up @@ -253,6 +258,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<ContestId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -264,6 +270,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn join(&self, req: Request<JoinContestRequest>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down Expand Up @@ -298,6 +305,7 @@ impl ContestSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn exit(&self, req: Request<ContestId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -314,6 +322,7 @@ impl ContestSet for Arc<Server> {
}

#[doc = " return up to 10 user"]
#[instrument(skip_all, level = "debug")]
async fn rank(&self, req: Request<ContestId>) -> Result<Response<Users>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down
7 changes: 7 additions & 0 deletions backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl From<Model> for EducationInfo {

#[async_trait]
impl EducationSet for Arc<Server> {
#[instrument(skip_all, level = "debug")]
async fn create(
&self,
req: Request<CreateEducationRequest>,
Expand Down Expand Up @@ -118,6 +119,7 @@ impl EducationSet for Arc<Server> {

Ok(Response::new(model.id.unwrap().into()))
}
#[instrument(skip_all, level = "debug")]
async fn update(&self, req: Request<UpdateEducationRequest>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down Expand Up @@ -148,6 +150,7 @@ impl EducationSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<EducationId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -159,6 +162,7 @@ impl EducationSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn link(&self, req: Request<EducationLink>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -183,6 +187,7 @@ impl EducationSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn unlink(&self, req: Request<EducationLink>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -208,6 +213,7 @@ impl EducationSet for Arc<Server> {
Ok(Response::new(()))
}

#[instrument(skip_all, level = "debug")]
async fn list_by_problem(
&self,
req: Request<ListByRequest>,
Expand All @@ -234,6 +240,7 @@ impl EducationSet for Arc<Server> {

Ok(Response::new(ListEducationResponse { list, next_session }))
}
#[instrument(skip_all, level = "debug")]
async fn full_info_by_problem(
&self,
req: Request<EducationLink>,
Expand Down
1 change: 1 addition & 0 deletions backend/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod tools {
pub use super::util::error::Error;
pub use crate::grpc::TonicStream;
pub use crate::init::db::DB;
pub use tracing::instrument;
}
pub mod endpoints {
pub use super::util::{
Expand Down
1 change: 1 addition & 0 deletions backend/src/endpoint/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ impl PlaygroundSet for Arc<Server> {
#[doc = " Server streaming response type for the Run method."]
type RunStream = TonicStream<PlaygroundResult>;

#[instrument(skip_all, level = "debug")]
async fn run(
&self,
req: Request<PlaygroundRequest>,
Expand Down
12 changes: 12 additions & 0 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl From<Model> for ProblemFullInfo {

#[async_trait]
impl ProblemSet for Arc<Server> {
#[instrument(skip_all, level = "debug")]
async fn list(
&self,
req: Request<ListRequest>,
Expand Down Expand Up @@ -126,6 +127,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(ListProblemResponse { list, next_session }))
}
#[instrument(skip_all, level = "debug")]
async fn search_by_text(
&self,
req: Request<TextSearchRequest>,
Expand All @@ -152,6 +154,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(ListProblemResponse { list, next_session }))
}
#[instrument(skip_all, level = "debug")]
async fn full_info(
&self,
req: Request<ProblemId>,
Expand All @@ -168,6 +171,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(model.into()))
}
#[instrument(skip_all, level = "debug")]
async fn create(
&self,
req: Request<CreateProblemRequest>,
Expand Down Expand Up @@ -198,6 +202,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(model.id.unwrap().into()))
}
#[instrument(skip_all, level = "debug")]
async fn update(&self, req: Request<UpdateProblemRequest>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down Expand Up @@ -240,6 +245,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<ProblemId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -251,6 +257,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn link(&self, req: Request<ProblemLink>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -275,6 +282,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn unlink(&self, req: Request<ProblemLink>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -299,6 +307,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn publish(&self, req: Request<ProblemId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -320,6 +329,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn unpublish(&self, req: Request<ProblemId>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -341,6 +351,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(()))
}
#[instrument(skip_all, level = "debug")]
async fn full_info_by_contest(
&self,
req: Request<ProblemLink>,
Expand Down Expand Up @@ -368,6 +379,7 @@ impl ProblemSet for Arc<Server> {

Ok(Response::new(model.into()))
}
#[instrument(skip_all, level = "debug")]
async fn list_by_contest(
&self,
req: Request<ListByRequest>,
Expand Down
8 changes: 8 additions & 0 deletions backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl From<Model> for SubmitInfo {

#[async_trait]
impl SubmitSet for Arc<Server> {
#[instrument(skip_all, level = "debug")]
async fn list(
&self,
req: Request<ListRequest>,
Expand Down Expand Up @@ -88,6 +89,7 @@ impl SubmitSet for Arc<Server> {
Ok(Response::new(ListSubmitResponse { list, next_session }))
}

#[instrument(skip_all, level = "debug")]
async fn list_by_problem(
&self,
req: Request<ListByRequest>,
Expand Down Expand Up @@ -115,6 +117,7 @@ impl SubmitSet for Arc<Server> {
Ok(Response::new(ListSubmitResponse { list, next_session }))
}

#[instrument(skip_all, level = "debug")]
async fn info(&self, req: Request<SubmitId>) -> Result<Response<SubmitInfo>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -128,6 +131,7 @@ impl SubmitSet for Arc<Server> {
Ok(Response::new(model.into()))
}

#[instrument(skip_all, level = "debug")]
async fn create(
&self,
req: Request<CreateSubmitRequest>,
Expand Down Expand Up @@ -173,6 +177,7 @@ impl SubmitSet for Arc<Server> {
Ok(Response::new(self.submit.submit(submit).await?.into()))
}

#[instrument(skip_all, level = "debug")]
async fn remove(&self, req: Request<SubmitId>) -> std::result::Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand All @@ -193,6 +198,7 @@ impl SubmitSet for Arc<Server> {
type FollowStream = TonicStream<SubmitStatus>;

#[doc = " are not guarantee to yield status"]
#[instrument(skip_all, level = "debug")]
async fn follow(&self, req: Request<SubmitId>) -> Result<Response<Self::FollowStream>, Status> {
let (_, req) = self.parse_request(req).await?;

Expand All @@ -204,6 +210,7 @@ impl SubmitSet for Arc<Server> {
))
}

#[instrument(skip_all, level = "debug")]
async fn rejudge(&self, req: Request<RejudgeRequest>) -> Result<Response<()>, Status> {
let db = DB.get().unwrap();
let (auth, req) = self.parse_request(req).await?;
Expand Down Expand Up @@ -253,6 +260,7 @@ impl SubmitSet for Arc<Server> {
#[doc = " Server streaming response type for the ListLangs method."]
type ListLangsStream = TonicStream<Language>;

#[instrument(skip_all, level = "debug")]
async fn list_langs(&self, _: Request<()>) -> Result<Response<Self::ListLangsStream>, Status> {
let langs = self.submit.list_lang().into_iter().map(|x| Ok(x.into()));

Expand Down
Loading

0 comments on commit 47acf86

Please sign in to comment.