Hcode Online Judge (HOJ) Documentation

repository·master·Indexed 21 days ago

https://github.com/himitzh/hoj

A distributed, high-performance online judge system for competitive programming supporting ACM and OI modes. Built with a microservices architecture using Spring Boot 2.2.6.RELEASE, Spring Cloud Alibaba 2.2.1.RELEASE, and Vue 2.6.11. Features include cgroup-based security isolation, support for multiple programming languages, remote judging from platforms like Codeforces and AtCoder, and flexible deployment options via docker-compose in standalone or distributed modes.

Tokens
40.4K
Snippets
82
Records
139
Agent score
73%

What's inside Hcode Online Judge (HOJ)

  1. Overview of Hcode Online Judge (HOJ)

    master

    Hcode Online Judge (HOJ) is a distributed, microservices-based online judging system built using a decoupled frontend and backend architecture. It is designed to support both ACM and OI style programming contests and training.

    Core Features

    • Scalability: Distributed judging architecture.
    • Security: Uses cgroup to isolate user programs during judging, preventing resource exhaustion.
    • Versatility: Supports ACM and OI problem types, including special features like testlib evaluation, interactive judging, and subtask judging.
    • Content Sourcing: Supports local problem data and can crawl problems from popular OJs like HDU, Codeforces, GYM, POJ, AtCoder, SPOJ, and Libre.
    • Social & Training: Includes discussion forums (problem, contest, and public), an internal messaging system, private/public training sets, and various team types (private, public, protected).
    • UI/UX: Responsive design supporting mobile devices and full internationalization (Chinese/English).
  2. Overview of HOJ features

    master

    Hcode Online Judge (HOJ) is an open-source online judge system built on SpringBoot, SpringCloud Alibaba, and Vue.js. It supports a wide range of programming languages and evaluation modes.

    Key Capabilities

    • Multi-language Support: C, C++, C#, Python, PyPy, Go, Java, JavaScript, PHP, Ruby, and Rust.
    • Remote Judge Support: Can integrate with HDU, POJ, Codeforces, GYM, AtCoder, SPOJ, and LIBRE.
    • Evaluation Modes: Supports General, Special, Interactive, Self-test, Subtask, and File IO modes.
    • Contest Features: Includes Star team, Attention team, External Rank, and Scroll Board functionality.
    • Community Features: Supports group management and discussion areas.
  3. Overview of HOJ SQL and Configuration Files

    master

    The sqlAndsetting directory contains the essential files required for initializing and configuring the Hcode Online Judge (HOJ) environment. This includes:

    • Database Creation Files: SQL scripts used to initialize the HOJ database schema.
    • Database Data Files: Pre-populated data files for the database.
    • Service Configuration Files: Configuration files used to set up and manage HOJ services.
  4. System Architecture and Technology Stack

    master

    Hcode Online Judge (HOJ) is a distributed, decoupled system designed for online programming contests and training. It uses a microservices architecture where the frontend and backend are separated, and services are orchestrated using Docker and Docker-Compose on Ubuntu systems.

    Core Components

    Frontend

    • Framework: Vue2 with Element-UI.
    • Features: Responsive layout for mobile support.
    • Editors:
      • CodeMirror for online code editing.
      • Mavon-Editor for rich text editing.
      • Vxe-Table for data tables.

    Backend (hoj-backend / Data Service)

    • Framework: Spring Boot.
    • Service Discovery & Config: Nacos (supports dynamic configuration refreshing).
    • Database & Persistence: MySQL with Mybatis-Plus.
    • Security: Shiro (manages user roles, permissions, and token refreshing).
    • Caching & Queuing: Redis (used for data caching and as a list for the evaluation queue).

    Evaluation Service (hoj-judgeserver)

    • Framework: Spring Boot.
    • Integration: Registers with Nacos for scheduling by hoj-backend and retrieves configurations.
    • Evaluation Modes:
      • Local Evaluation: Uses the Go-Judge (SandBox) for high-performance, secure execution. Supports normal, special, interactive evaluation, and online debugging.
      • Remote Evaluation: Uses crawler technology to submit code to external platforms (e.g., HDU, Codeforces, POJ), retrieves submission IDs, and polls for results to write back to the database.

    Request Flow

    1. User requests are sent to Nginx.
    2. Nginx proxies requests to either the Vue static files or the Backend services.
    3. The Backend processes business logic using MySQL and Redis, returning JSON to the Vue frontend.
    4. For evaluation requests: hoj-backend queries Nacos for healthy hoj-judgeserver instances, sends the task to a judge, which then executes the code via the Go-Judge sandbox and updates the database.
  5. What is the Judger-SandBox?

    master
    The Judger-SandBox is a high-performance, reusable security sandbox written in Go. It uses cgroups for permission control to ensure that user-submitted code is executed safely. It typically listens on port 5050. In a production environment, this port is generally not exposed to the public internet.
  6. Understand the HOJ judging and dispatching workflow

    master

    The HOJ judging process follows a specific lifecycle from submission to result storage:

    1. Submission: A user submits code from a problem detail page.
    2. Persistence: The backend validates the submission and writes it to the MySQL database.
    3. Queueing: The task is placed into a Redis queue for pending evaluations.
    4. Dispatching: A dispatcher retrieves the task from Redis, queries Nacos for healthy judge service instances, and uses pessimistic locking to manage concurrency. The task is then sent to an available judge instance.
    5. Evaluation: The judge service calls the secure sandbox (Go-Judge) via HTTP to compile and run the code. It compares results against test cases and writes the final status back to the database.
    6. Polling: The frontend initiates a timer that queries the result every 2 seconds until the status is no longer 'evaluating'.
  7. How Teams work in HOJ

    master

    A Team functions as an independent sub-instance of HOJ. It contains its own isolated data for problems, training, contests, evaluations, discussions, announcements, and rankings. Data within a team is completely isolated from the main HOJ site data.

    Teams are categorized into three types:

    • Public Teams (公开团队): Users can join directly without an application.
    • Protected Teams (保护团队): Users must submit an application and a reason for joining, which requires administrator approval.
    • Private Teams (私有团队): Users must provide a valid invitation code and a reason for joining, which requires administrator approval.
  8. Manage Contest Access Permissions

    master

    Contests in HOJ can be configured with three levels of access control:

    • Public Contest (公开赛): All users can view contest details, problems, submissions, leaderboards, and discussions. Anyone can submit during the contest period.
    • Protected Contest (保护赛): All users can view details, problems, submissions, leaderboards, and discussions, but a password is required to submit during the contest phase.
    • Private Contest (私有赛): Only users with the contest password can enter the contest to view details, problems, submissions, leaderboards, and discussions, including the ability to submit.
  9. Understand HOJ Contest Modes (ACM vs OI)

    master

    HOJ supports two primary contest formats with different scoring and ranking behaviors:

    ACM Mode

    Follows strict ACM-ICPC rules. The Seal Time Rank setting determines if the leaderboard is frozen (sealed).

    • Sealing Options: 30 minutes before the end, 1 hour before the end, or for the entire duration.
    • Behavior during Sealing:
      • Super Admins & Contest Creators: Can view problem statistics and submit data normally. Submissions are visible, but they will not be included in the leaderboard automatically. The leaderboard must be manually refreshed to see updates.
      • Regular Users & Other Admins: Can see their own submission results, but cannot see anyone else's submissions made after the seal time. They cannot see problem statistics. The leaderboard remains frozen at the state it was in before the seal.

    OI Mode

    Scoring is based on individual problem points. Multiple submissions are judged by either the last submission or the highest score submission (depending on configuration). Ranking is determined by the total score across all problems.

    • Behavior during Sealing:
      • Super Admins & Contest Creators: Same as ACM mode (can see stats/results, but results won't update the leaderboard without manual refresh).
      • Regular Users & Other Admins: Cannot see their own or anyone else's submission results made after the seal time. They cannot see problem statistics. The leaderboard remains frozen at the state it was in before the seal.

    Note: By default, all data becomes visible again once the contest ends, but the backend can be configured to continue the seal after the contest concludes.

  10. Understand ACM-style judge modes

    master

    HOJ supports two evaluation modes for ACM-style problems. These modes determine how the judge handles multiple test cases and when it stops the evaluation process.

    1. Default (All Test Cases): The judge runs all test cases and aggregates the results. If all test cases pass, the status is Accepted. If any fail, the final status is set to the status of the first non-Accepted test case encountered.
    2. Stop on Error (Sequential Evaluation): The judge runs test cases in order. If a test case result is not Accepted, the evaluation stops immediately, and that specific test case's result is returned as the final status.
  11. How the four judging modes work

    master

    HOJ supports four distinct judging modes, each with a different execution logic:

    1. Ordinary Judging (普通评测)

    • Process: Compiles user code $\rightarrow$ Runs program using standard input files $\rightarrow$ Checks time/memory limits $\rightarrow$ Compares user output against standard output.
    • Failure points: Compilation failure or exceeding time/memory limits.

    2. Special Judging (特殊评测)

    • Process: Compiles user code $\rightarrow$ Compiles a special program (SPJ) if not already compiled $\rightarrow$ Runs user program $\rightarrow$ Uses the SPJ to compare standard input, standard output, and user output.
    • Use case: When standard output comparison is insufficient and custom logic is needed to validate results.

    3. Interactive Judging (交互评测)

    • Process: Compiles user code $\rightarrow$ Compiles an interactive program $\rightarrow$ Runs both programs simultaneously, allowing them to interact via standard input/output streams.
    • Use case: For problems where the judge must respond to user program actions in real-time.

    4. Remote Judging (远程评测)

    • Process: Uses crawler/simulation technology to submit code to external platforms (e.g., HDU, POJ, Codeforces, GYM, AtCoder, SPOJ).
    • Implementation:
      • Requires platform account cookies configured in the submission API parameters.
      • Includes a retry mechanism for submission failures.
      • If a submission ID is obtained, a background thread polls the remote platform every 3 seconds.
      • If no result is found within 3 minutes, the submission is marked as failed. Otherwise, the remote result is mapped to the HOJ format.
  12. Understand the difference between Public and Private Training

    master

    HOJ provides two types of training modes, both of which function as problem sets (题单) but differ in how they track progress:

    • Public Training (公开训练): A collection of problems accessible to users. Submission data is synchronized with the public problem library. It is primarily used to aggregate specific problem types.
    • Private Training (私有训练): A password-protected collection of problems. Unlike public training, it maintains a Record Leaderboard (记录榜单) that tracks user progress specifically within that training session.

    Important Note on Permissions: Training sets can only display problems that have public permissions. The submission data shown in training is identical to the data in the public problem library.