Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(be): add priority on submisison queue #2305

Merged
merged 8 commits into from
Jan 22, 2025
Merged
53 changes: 48 additions & 5 deletions apps/backend/apps/client/src/submission/submission-pub.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Span, TraceService } from 'nestjs-otel'
import {
EXCHANGE,
JUDGE_MESSAGE_TYPE,
MESSAGE_PRIORITY_HIGH,
MESSAGE_PRIORITY_LOW,
MESSAGE_PRIORITY_MIDDLE,
RUN_MESSAGE_TYPE,
SUBMISSION_KEY,
USER_TESTCASE_MESSAGE_TYPE
Expand Down Expand Up @@ -78,12 +81,52 @@ export class SubmissionPublicationService {
await this.amqpConnection.publish(EXCHANGE, SUBMISSION_KEY, judgeRequest, {
messageId: String(submission.id),
persistent: true,
type: isTest
? RUN_MESSAGE_TYPE
: isUserTest
? USER_TESTCASE_MESSAGE_TYPE
: JUDGE_MESSAGE_TYPE
type: this.calculateMessageType(isTest, isUserTest),
priority: this.calculateMessagePriority(isTest, isUserTest)
})
span.end()
}

/**
* 채점 요청 메세지의 타입을 계산하여 반환
*
* - RUN_MESSAGE_TYPE: 오픈 테스트 케이스 실행
* - USER_TESTCASE_MESSAGE_TYPE: 사용자 정의 테스트 케이스 실행
* - JUDGE_MESSAGE_TYPE: 제출
*
* @param isTest - 테스트 제출 여부
* @param isUserTest - 사용자 정의 테스트 케이스 제출 여부
*/
private calculateMessageType(isTest: boolean, isUserTest: boolean) {
if (isTest) return RUN_MESSAGE_TYPE
if (isUserTest) return USER_TESTCASE_MESSAGE_TYPE
return JUDGE_MESSAGE_TYPE
}

/**
* 채점 요청 메세지의 우선순위를 계산하여 반환
*
* 우선순위 (0 ~ 3, 클 수록 우선순위 높음)
*
* - JUDGE_MESSAGE_TYPE: 3
* - RUN_MESSAGE_TYPE: 2
* - USER_TESTCASE_MESSAGE_TYPE: 2
* - DEFAULT: 1
*
* @param isTest - 테스트 제출 여부
* @param isUserTest - 사용자 정의 테스트 케이스 제출 여부
*/
private calculateMessagePriority(isTest: boolean, isUserTest: boolean) {
const msgType = this.calculateMessageType(isTest, isUserTest)

switch (msgType) {
case JUDGE_MESSAGE_TYPE:
return MESSAGE_PRIORITY_HIGH
case RUN_MESSAGE_TYPE:
case USER_TESTCASE_MESSAGE_TYPE:
return MESSAGE_PRIORITY_MIDDLE
default:
return MESSAGE_PRIORITY_LOW
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as sinon from 'sinon'
import {
EXCHANGE,
JUDGE_MESSAGE_TYPE,
MESSAGE_PRIORITY_HIGH,
MESSAGE_PRIORITY_MIDDLE,
RUN_MESSAGE_TYPE,
SUBMISSION_KEY
} from '@libs/constants'
Expand Down Expand Up @@ -103,7 +105,8 @@ describe('SubmissionPublicationService', () => {
amqpSpy.calledOnceWith(EXCHANGE, SUBMISSION_KEY, judgeRequest, {
messageId: String(submission.id),
persistent: true,
type: JUDGE_MESSAGE_TYPE
type: JUDGE_MESSAGE_TYPE,
priority: MESSAGE_PRIORITY_HIGH
})
).to.be.true
})
Expand Down Expand Up @@ -143,7 +146,8 @@ describe('SubmissionPublicationService', () => {
amqpSpy.calledOnceWith(EXCHANGE, SUBMISSION_KEY, judgeRequest, {
messageId: String(submission.id),
persistent: true,
type: RUN_MESSAGE_TYPE
type: RUN_MESSAGE_TYPE,
priority: MESSAGE_PRIORITY_MIDDLE
})
).to.be.true
})
Expand Down
7 changes: 7 additions & 0 deletions apps/backend/libs/constants/src/rabbitmq.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export const ORIGIN_HANDLER_NAME = 'codedang-handler'
export const JUDGE_MESSAGE_TYPE = 'judge'
export const RUN_MESSAGE_TYPE = 'run'
export const USER_TESTCASE_MESSAGE_TYPE = 'userTestCase'

/**
* 채점 요청 메세지 우선순위
*/
export const MESSAGE_PRIORITY_HIGH = 3
export const MESSAGE_PRIORITY_MIDDLE = 2
export const MESSAGE_PRIORITY_LOW = 1
3 changes: 2 additions & 1 deletion apps/infra/production/codedang/message_queue.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ resource "rabbitmq_queue" "submission_queue" {
vhost = rabbitmq_permissions.vh_perm.vhost

settings {
durable = true
durable = true
arguments_json = var.rabbitmq_arguments
}
}

Expand Down
8 changes: 8 additions & 0 deletions apps/infra/production/codedang/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ variable "rabbitmq_port" {
sensitive = true
}

variable "rabbitmq_arguments" {
default = <<EOF
{
"x-max-priority": 3
}
EOF
}

# TODO: description 넣고 공통부분은 object로 처리
variable "public_subnet1" { sensitive = true }
variable "public_subnet2" { sensitive = true }
Expand Down
3 changes: 2 additions & 1 deletion apps/infra/rc/codedang/message_queue.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ resource "rabbitmq_queue" "submission_queue" {
vhost = rabbitmq_permissions.vh_perm.vhost

settings {
durable = true
durable = true
arguments_json = var.rabbitmq_arguments
}
}

Expand Down
8 changes: 8 additions & 0 deletions apps/infra/rc/codedang/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ variable "rabbitmq_port" {
sensitive = true
}

variable "rabbitmq_arguments" {
default = <<EOF
{
"x-max-priority": 3
}
EOF
}

# TODO: description 넣고 공통부분은 object로 처리
variable "redis_port" { sensitive = true }
variable "jwt_secret" { sensitive = true }
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ rabbitmqadmin -H $RABBITMQ_HOST -u $RABBITMQ_DEFAULT_USER -p $RABBITMQ_DEFAULT_P
rabbitmqadmin -H $RABBITMQ_HOST -u $RABBITMQ_DEFAULT_USER -p $RABBITMQ_DEFAULT_PASS -V $RABBITMQ_DEFAULT_VHOST \
declare queue name="$JUDGE_RESULT_QUEUE_NAME" durable=true
rabbitmqadmin -H $RABBITMQ_HOST -u $RABBITMQ_DEFAULT_USER -p $RABBITMQ_DEFAULT_PASS -V $RABBITMQ_DEFAULT_VHOST \
declare queue name="$JUDGE_SUBMISSION_QUEUE_NAME" durable=true
declare queue name="$JUDGE_SUBMISSION_QUEUE_NAME" durable=true arguments='{"x-max-priority": 3}'

# Make bindings
rabbitmqadmin -H $RABBITMQ_HOST -u $RABBITMQ_DEFAULT_USER -p $RABBITMQ_DEFAULT_PASS -V $RABBITMQ_DEFAULT_VHOST \
Expand Down
Loading