Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mdcpp/mdoj
Browse files Browse the repository at this point in the history
  • Loading branch information
KAIYOHUGO committed Sep 23, 2024
2 parents f50b3c8 + afd084f commit 06e196d
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 57 deletions.
2 changes: 1 addition & 1 deletion backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ enum Submit {
Status,
Accept,
Score,
Public
Public,
}
#[derive(Iden)]
enum Testcase {
Expand Down
26 changes: 14 additions & 12 deletions backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ use std::{
use crossbeam_queue::SegQueue;
use dashmap::{DashMap, DashSet};
use tonic::{service::Interceptor, *};
use tracing::{debug_span, instrument, Instrument};
use tracing::{debug_span, instrument, Instrument, Span};
use uuid::Uuid;

use crate::config::{self, Judger as JudgerConfig};
use crate::config::{self, Judger as JudgeConfig};
use grpc::judger::{judger_client::*, *};

// TODO: add tracing

// about health score:
//
// health score is a number in range [-1,HEALTH_MAX_SCORE)
// Upstream with negitive health score is consider unhealthy, and should disconnect immediately
// Upstream with negative health score is consider unhealthy, and should disconnect immediately

/// Max score a health Upstream can reach
const HEALTH_MAX_SCORE: isize = 100;

/// Judger Client intercepted by BasicAuthInterceptor
/// Judge Client intercepted by BasicAuthInterceptor
type AuthJudgerClient = JudgerClient<
service::interceptor::InterceptedService<transport::Channel, BasicAuthInterceptor>,
>;
Expand All @@ -54,12 +54,12 @@ impl Interceptor for BasicAuthInterceptor {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
/// Info necessary to create connection, implement reuse logic
pub struct ConnectionDetail {
pub uri: String,
pub secret: Option<String>,
// TODO: reuse logic shouldn't be binded with connection creation logic
// TODO: reuse logic shouldn't be bound with connection creation logic
pub reuse: bool,
}

Expand Down Expand Up @@ -128,9 +128,10 @@ impl Drop for ConnGuard {
/// occupy future, should generally be spawn in a green thread
#[instrument(skip(router), level = "info")]
async fn discover<I: Routable + Send>(
config: JudgerConfig,
config: JudgeConfig,
router: Weak<Router>,
) -> Result<(), Error> {
let parent = Span::current();
let mut instance = I::new(config.clone())?;
loop {
match instance.discover().in_current_span().await {
Expand All @@ -141,7 +142,7 @@ async fn discover<I: Routable + Send>(
};
let uri = detail.uri.clone();
let (upstream, langs) = Upstream::new(detail).in_current_span().await?;
let _ = debug_span!("connected", uri = uri).entered();
let _ = debug_span!(parent: parent.clone(), "connected", uri = uri).entered();
for (uuid, lang) in langs.into_iter() {
router.langs.insert(lang);
loop {
Expand Down Expand Up @@ -175,8 +176,8 @@ pub struct Router {

impl Router {
// skip because config contain basic auth secret
#[instrument(name = "router_construct", level = "info", skip_all)]
pub fn new(config: Vec<JudgerConfig>) -> Result<Arc<Self>, Error> {
#[instrument(name = "router_construct", level = "debug", skip_all)]
pub fn new(config: Vec<JudgeConfig>) -> Result<Arc<Self>, Error> {
let self_ = Arc::new(Self {
routing_table: DashMap::default(),
langs: DashSet::default(),
Expand Down Expand Up @@ -235,6 +236,7 @@ pub struct Upstream {

impl Upstream {
/// create new Upstream
#[instrument(name = "connecting_upstream", err, level = "info")]
async fn new(detail: ConnectionDetail) -> Result<(Arc<Self>, Vec<(Uuid, LangInfo)>), Error> {
let mut client = detail.connect().await?;
let info = client.judger_info(()).await?;
Expand All @@ -245,7 +247,7 @@ impl Upstream {
let uuid = match Uuid::parse_str(&lang.lang_uid) {
Ok(x) => x,
Err(err) => {
log::warn!("invalid lang_uid from judger: {}", err);
log::warn!("invalid lang_uid from judge: {}", err);
continue;
}
};
Expand Down Expand Up @@ -304,7 +306,7 @@ where
// return new connection when available, will immediately retry true is returned
async fn route(&mut self) -> Result<RouteStatus, Error>;
/// create from config
fn new(config: JudgerConfig) -> Result<Self, Error>;
fn new(config: JudgeConfig) -> Result<Self, Error>;
}

/// wrapper for Routable(Error handling)
Expand Down
3 changes: 3 additions & 0 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<'a> From<WithAuth<'a, Model>> for AnnouncementFullInfo {
id: model.id,
title: model.title,
update_date: into_prost(model.update_at),
create_date: into_prost(model.create_at),
},
author_id: model.user_id,
content: model.content,
Expand All @@ -34,6 +35,7 @@ impl From<Model> for AnnouncementInfo {
id: value.id,
title: value.title,
update_date: into_prost(value.update_at),
create_date: into_prost(value.create_at),
}
}
}
Expand All @@ -44,6 +46,7 @@ impl From<PartialModel> for AnnouncementInfo {
id: value.id,
title: value.title,
update_date: into_prost(value.update_at),
create_date: into_prost(value.create_at),
}
}
}
Expand Down
11 changes: 2 additions & 9 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ impl<'a> From<WithAuth<'a, Model>> for ContestFullInfo {

impl WithAuthTrait for Model {}

impl From<user_contest::Model> for UserRank {
fn from(value: user_contest::Model) -> Self {
UserRank {
user_id: value.user_id,
score: value.score,
}
}
}

impl From<Model> for ContestInfo {
fn from(value: Model) -> Self {
ContestInfo {
Expand All @@ -41,6 +32,7 @@ impl From<Model> for ContestInfo {
begin: value.begin.map(into_prost),
end: value.end.map(into_prost),
need_password: value.password.is_some(),
public: value.public,
}
}
}
Expand All @@ -53,6 +45,7 @@ impl From<PartialModel> for ContestInfo {
begin: value.begin.map(into_prost),
end: value.end.map(into_prost),
need_password: value.password.is_some(),
public: value.public,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ impl<'a> From<WithAuth<'a, Model>> for ProblemFullInfo {
submit_count: model.submit_count,
ac_rate: model.ac_rate,
difficulty: model.difficulty,
create_at: into_prost(model.create_at),
update_at: into_prost(model.update_at),
public: model.public,
},
author: model.user_id,
writable,
Expand All @@ -38,6 +41,9 @@ impl From<PartialModel> for ProblemInfo {
submit_count: value.submit_count,
ac_rate: value.ac_rate,
difficulty: value.difficulty,
create_at: into_prost(value.create_at),
update_at: into_prost(value.update_at),
public: value.public,
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use tokio_stream::wrappers::ReceiverStream;

impl From<Model> for SubmitInfo {
fn from(value: Model) -> Self {
// TODO: solve devation and uncommitted submit!
let db_code: Code = value.status.unwrap().try_into().unwrap();
let db_code: Code = value
.status
.map(|x| x.try_into().ok())
.flatten()
.unwrap_or(Code::Unknown);
SubmitInfo {
id: value.id,
upload_time: into_prost(value.upload_at),
Expand Down
10 changes: 5 additions & 5 deletions backend/src/endpoint/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ impl<'a> From<WithAuth<'a, Model>> for UserFullInfo {
}
}

impl WithAuthTrait for Model {}

impl From<Model> for UserInfo {
fn from(value: Model) -> Self {
UserInfo {
Self {
username: value.username,
// FIXME: capture Error(database corruption?) instead!
score: value.score.try_into().unwrap_or_default(),
score: value.score as u64,
id: value.id,
create_at: into_prost(value.create_at),
}
}
}

impl WithAuthTrait for Model {}

#[async_trait]
impl User for ArcServer {
#[instrument(
Expand Down
2 changes: 0 additions & 2 deletions backend/src/entity/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
match data.0 {
Sort::UpdateDate => Column::UpdateAt,
Sort::CreateDate => Column::CreateAt,
Sort::Public => Column::Public,
}
}
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
Expand All @@ -221,7 +220,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
data.1 = match data.0 {
Sort::UpdateDate => model.update_at.to_string(),
Sort::CreateDate => model.create_at.to_string(),
Sort::Public => model.public.to_string(),
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions backend/src/entity/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,17 @@ impl Source for ColPagerTrait {
impl SortSource<PartialModel> for ColPagerTrait {
fn sort_col(data: &Self::Data) -> impl ColumnTrait {
match data.0 {
Sort::UpdateDate => Column::UpdateAt,
Sort::CreateDate => Column::CreateAt,
Sort::Begin => Column::Begin,
Sort::End => Column::End,
Sort::Public => Column::Public,
}
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
&data.1
}
fn save_val(data: &mut Self::Data, model: &PartialModel) {
data.1 = match data.0 {
Sort::UpdateDate => model.update_at.to_string(),
Sort::CreateDate => model.create_at.to_string(),
Sort::Begin => model.begin.unwrap().to_string(),
Sort::End => model.end.unwrap().to_string(),
Sort::Public => model.public.to_string(),
}
}
}
Expand All @@ -294,7 +288,7 @@ impl Paginator {
))
}
pub fn new(start_from_end: bool) -> Self {
Self::new_sort(Sort::CreateDate, start_from_end)
Self::new_sort(Sort::Begin, start_from_end)
}
}

Expand Down
2 changes: 0 additions & 2 deletions backend/src/entity/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
Sort::SubmitCount => Column::SubmitCount,
Sort::Difficulty => Column::Difficulty,
Sort::Order => Column::Order,
Sort::Public => Column::Public,
}
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
Expand All @@ -417,7 +416,6 @@ impl SortSource<PartialModel> for ColPagerTrait {
Sort::SubmitCount => model.submit_count.to_string(),
Sort::Difficulty => model.difficulty.to_string(),
Sort::Order => model.order.to_string(),
Sort::Public => model.public.to_string(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/util/code.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use grpc::backend::StateCode as BackendCode;
use grpc::judger::JudgerCode;

/// Stablized JudgeResponse Code, store in database
/// Stabilized JudgeResponse Code, store in database
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum Code {
Expand Down
24 changes: 8 additions & 16 deletions grpc/proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ message ProblemInfo {
// 3000 - inf geek
required uint32 difficulty = 5;
required float ac_rate = 4;
required google.protobuf.Timestamp update_at = 6;
required google.protobuf.Timestamp create_at = 7;
required bool public = 8;
}

message ProblemFullInfo {
Expand Down Expand Up @@ -177,7 +180,7 @@ message ListProblemRequest {
SORT_SUBMIT_COUNT = 3;
SORT_DIFFICULTY = 4;
SORT_ORDER = 5;
SORT_PUBLIC = 6;
// 6 is used
}
message Query {
optional int32 contest_id = 1;
Expand Down Expand Up @@ -299,6 +302,7 @@ message AnnouncementInfo {
required int32 id = 1;
required string title = 2;
required google.protobuf.Timestamp update_date = 3;
required google.protobuf.Timestamp create_date = 4;
}

message AnnouncementFullInfo {
Expand Down Expand Up @@ -358,7 +362,6 @@ message ListAnnouncementRequest {
enum Sort {
SORT_UPDATE_DATE = 0;
SORT_CREATE_DATE = 1;
SORT_PUBLIC = 2;
}
message Query {
optional Sort sort_by = 1;
Expand Down Expand Up @@ -591,6 +594,7 @@ message ContestInfo {
optional google.protobuf.Timestamp begin = 4;
optional google.protobuf.Timestamp end = 5;
required bool need_password = 6;
required bool public = 7;
}

message ContestFullInfo {
Expand Down Expand Up @@ -645,17 +649,6 @@ message UpdateContestRequest {
optional string request_id = 3;
}

message UserRank {
required int32 user_id = 1;
required uint32 score = 2;
}

message ListRankResponse {
repeated UserRank list = 1;
required string paginator = 2;
required uint64 remain = 3;
}

message JoinContestRequest {
required int32 id = 1;
optional string password = 2;
Expand All @@ -667,11 +660,9 @@ message JoinContestRequest {

message ListContestRequest {
enum Sort {
SORT_CREATE_DATE = 0;
SORT_UPDATE_DATE = 1;
SORT_BEGIN = 2;
SORT_END = 3;
SORT_PUBLIC = 4;
// leave 4, it's used
}
message Query {
optional Sort sort_by = 1;
Expand Down Expand Up @@ -720,6 +711,7 @@ message UserInfo {
required string username = 1;
required uint64 score = 4;
required int32 id = 3;
required google.protobuf.Timestamp create_at = 5;
}

message UserFullInfo {
Expand Down

0 comments on commit 06e196d

Please sign in to comment.