An online judge system(like codeforces, leetcode, etc) contains a problem set of algorithms to solve, while users can compile a piece of code and execute the generated binary with pre-constructed data to test if the code is correct. However details of algorithms won’t be discussed here, we mainly focus on how to build an online judge system from scratch.
For simplicity, we’ll name this ongoing online judge system
Justice. Fancy enough for me, :-P.
Justice is composed of three parts:
- justice-frontend: the website of
Justicewith an additional admin control panel, powered by yii2;
- justice-dispatcher: dispatches submissions to a local worker(AKA justice-sandbox) and fetches the results, powered by spring-boot;
- justice-sandbox: yet another sandbox written in Go, providing kernel-based namespace and cgroup isolation.
The featured image describes how they work together:
- user submits a piece of code, justice-frontend then updates MySQL and sends the judge submission to RabbitMQ;
- justice-dispatcher fetches the judge submission from RabbitMQ asynchronously: if the submitted code is written in Java, the dispatcher will judge the code itself, or it will delegate the code to justice-sandbox for judging;
- justice-sandbox runs the submitted code in a jailed environment, and returns the judge result in a json-encoded string;
- justice-dispatcher then updates MySQL / Redis with the result decoded from itself or justice-sandbox;
- As all of the three parts are stateless, they can scale out easily whenever necessary.
That’s it! And in the next article, we’ll discuss the front-end of
The whole project now is published at https://www.justice.plus.