自建 Online Judge 系统

前言

先稍微介绍一下什么是 Online Judge(底下简称 OJ)系统,简单来说就是像 leetcode 那样啦,可以送出程式码解题,然后让系统去批改,并且得到最后的结果。

在 leetcode 流行以前,最知名的 OJ 大概就是 UVa Online Judge,俗称 ACM

如果刚好有需求,想要自己架一个 OJ 的话,该怎么办呢?

开源 OJ 系统

在网路上搜寻一下,可以找到几个开源的 OJ 系统,其中星星数比较多看起来也比较稳定的是底下三个:

  1. DMOJ
  2. NOJ
  3. QDUOJ

DMOJ

这一套功能看起来最丰富最完整,而且支援的语言最多,可以到 60 几种!而且还支援 Google, Facebook, Github 这些第三方登入。后端是 Python 写的,而且一直持续有在维护,文件也满完整的。

唯一的缺点大概就是介面比较阳春一点,没那么讨喜。

NOJ

中国南京邮电大学开源出来的系统,是用 Laravel 写成的。介面使用 Material UI,看起来比较现代,但是文件比较不完整。

QDUOJ

中国青岛大学开源出来的,后端是 Python + Django,前端是 Vue,采用 docker 部署简单快速,支援的程式语言有:C, C++, Java 跟 Python。介面的部分则是使用 Ant Design。

想要架哪一套就是根据自己需求而定,如果 GitHub 上提供的文件完整的话,照着做就行了。若是不完整也可以透过 Issue 提问,英文不好的话也不需要太过担心,这三个 repo 用中文应该也都可以通。

而我最后选择的是最后一套,青岛大学开源出来的 OJ。会选这一套是因为介面我满喜欢的,然后是这三套里面部署最容易的一套。

部署流程在这边:https://github.com/QingdaoU/OnlineJudgeDeploy/tree/2.0,因为是采用 docker 部署,所以真的容易,基本上就是把 docker-compose.yml 拉下来然后跑个指令就搞定了。

进一步研究 QDUOJ

首先我们来看一下这个系统的架构,在 GitHub 的文件上有写底下一共分成几个模组:

  1. Backend(Django): https://github.com/QingdaoU/OnlineJudge
  2. Frontend(Vue): https://github.com/QingdaoU/OnlineJudgeFE
  3. Judger Sandbox(Seccomp): https://github.com/QingdaoU/Judger
  4. JudgeServer(A wrapper for Judger): https://github.com/QingdaoU/JudgeServer

而我们最关心的问题(如何新增语言),已经有人在 Issue 里问过了:How to add more language support, such as Ruby

里面提到只要修改这个档案即可:https://github.com/QingdaoU/OnlineJudge/blob/master/judge/languages.py

这档案就是一些设定,而这边你也可以大概猜出 Judge Server 会做什么事情。在这里,每个语言的设定都会有 compile_command 跟 command,前者是来拿编译的指令,后者是拿来跑程式的指令。由于这个 OJ 的输出入都是透过 stdin/stdout,所以当你想要新增一种新的程式语言的时候,只要跟系统说该怎么去执行就好。

相反地,有些 OJ 是采用 function 的方式来填空,例如说开头提到的 leetcode,这时候若是要新增一个语言就会比较麻烦一些,因为你要额外再提供 function 的模板。

照理来说我们只要再如法泡制,加上这样的设定就行了:

js_lang_config = { "run" : { "exe_name" : "solution.js" , "command" : "/usr/bin/nodejs {exe_path}" , "seccomp_rule" : "general" ,    } }

但若是这样去跑,会发现有问题,搜了一下发现已经有人反映过:problem with addding js to language configs,解法是把 seccomp_rule 设成 None。

什么是 seccomp 呢?这就跟 OJ 的原理有关了!大家可以先仔细想想 OJ 中最重要的一个问题:

要如何安全地执行使用者提交的程式码?

若是不知道这问题是在问什么,可以想像以下情形:

  1. 有人写了一行重开机的程式码怎么办?
  2. 有人写了一个无穷回圈怎么办?
  3. 有人写了一个会把主机帐号密码传送到外部的程式怎么办?

由此可以看出,执行程式码可没有那么简单,而这块也是 OJ 最核心的一部分。

QDUOJ 的 Judger 原始码在这边:https://github.com/QingdaoU/Judger/tree/newnew/src

是用 C 写的,会 fork 一个新的 process,设定一些规则之后用 execve 来执行指令。在程式码里面也可以看出是使用 seccomp 这个东西来防止我们上面所提到的内容。

总之呢,QDUOJ 分层做得很不错,执行流程大概是这样的:

  1. 进入 Vue 做的前端页面
  2. 送出程式码,call 后端 API(Python)
  3. 后端 API 再呼叫 Judge Server API(Go)
  4. Judge Server API 呼叫 Judger 执行指令(C,execve + seccomp 执行)

所以每一个专案负责自己的事项,各司其职。

再讲回来前面提到要加上 JavaScript 这一块,尽管把 seccomp_rule 设成 None 以后,执行 JavaScript 依然会出现错误。我研究了一两天,发现问题是出在题目的记忆体限制太小,我猜测是 Node.js 要执行时本来就会吃比较多记忆体,只要把记忆体改大(例如说 1024MB)就搞定了。

不过还没结束,还有最后一个问题,那就是 ubuntu 16.04 上的 Node.js 版本满旧的,要换成新的才能使用 ES6 那些语法,解法是去改 JudgeServer 的 Dockerfile,新增一个安装 Node.js 新版的指令就好。

都改完以后,就可以来部署自己的版本了!只要先把 docker image build 好,然后更改我们最前面操作的 docker-compose 档案就可以了。

虽然说上面讲的云淡风轻,但那时候我在找 Node.js 到底为什么会一直 Runtime error 的时候找到快崩溃,因为错误讯息满不明确的,我一直以为是指令有错,是后来我才灵机一动想说:「咦,该不会是其他问题吧」,才发现是记忆体问题。

总而言之呢,如果你没有想要改东西,只是单纯想要部署的话,在这边诚心推荐 QDUOJ,部署真的简单方便,介面也好看。

自己写一个 OJ

我后来在研究资料的时候找到一些不错的开源解法,以后有人想要自己写的话可以参考考。

第一个是 IOI 开源出来的 sandbox:isolate,可以安全地执行指令。

第二个更神奇,直接给你 Judge 的 API,而且是免费的:Judge0 API。只要按照他的格式把输入传进去,就会跟你说判题结果,所以连 Judge Server 都可以不用自己做。

标签: Online Judge , QDUOJ , acm , noip , oj , 教程 , 算法 ,

共有87条评论

  1. I have read some good stuff here. Definitely woeth bookmarking for revisiting.I surprise howw so much effort you put tto create this kind of wonerful informative website.

  2. гипермаркет велосипедов в москве

  3. Thanks for sharing your thoughts about . Regards

  4. I know this website offers quality based articles and additional material, is there any other web site which presents these information in quality?

  5. My spouse and I stumbled over here by a different page and thought I should check things out. I like what I see so now i'm following you. Look forward to checking out your web page again.

  6. Whats up this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

  7. Hi there, You have done an excellent job. I'll certainly digg it and personally suggest to my friends. I am sure they'll be benefited from this web site.

  8. Hey! Thiss post could nnot be writtyen any better! Readng this poost reminds me of myy previous room mate!He always kept cbatting about this. I wiill forward tthis write-up to him. Fairrly certaqin he will have a good read. Thank you for sharing!

  9. I am in fact happy to read this blog posts which consists of lots of valuable information, thanks for providing these kinds of data.

  10. О людях в целом можно сказать, что они неблагодарны и непостоянны, склонны к лицемерию и обману, что их влечет нажива и пугает опасность; пока ты делаешь для них добро, они твои всей душой, обещают ничего для тебя не щадить: ни крови, ни жизни, ни детей, ни имущества, но когда ты будешь в них нуждаться, они сейчас же от тебя отвернутся. Государю, который, полагаясь на их обещания, не примет никаких мер на случай опасности, придется худо. Ибо дружбу, которая дается за деньги, а не приобретается величием и благородством души, можно купить, но нельзя удержать для того, чтобы воспользоваться в трудное время. Интерес – лучшее побуждение к занятию

  11. + for the post

  12. Thanks for the post

  13. No matter if some one searches for his required thing, thus he/she wishes to be available that in detail, so that thing is maintained over here.

  14. imbl

  15. Its like you learn my thoughts! You seem to know a lot about this, such as you wrote the ebook in it or something. I believe that you could do with some percent to force the message house a bit, however instead of that, that is magnificent blog. A fantastic read. I will certainly be back.

  16. Пассажирские перевозки в Москве

  17. Hi there, every time i used to check website posts here in the early hours in the break of day, for the reason that i enjoy to learn more and more.

  18. Heya i am for the primary time here. I came across this board and I in finding It truly useful & it helped me out a lot. I hope to present one thing back and aid others like you aided me.

  19. I was very pleased to discover this page. I want to to thank you for your time due to this wonderful read!! I definitely savored every little bit of it and I have you saved to fav to see new information in your web site.

  20. Аренда минивэна в Москве

  21. Аренда автобуса в Москве

  22. Hello, I enjoy reading all of your article. I wanted to write a little comment to support you.

  23. Pretty great post. I just stumbled upon your weblog and wished to say that I have really loved surfing around your weblog posts. After all I will be subscribing in your feed and I am hoping you write once more soon!

  24. Hello there! I know this is kinda off topic nevertheless I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa? My blog addresses a lot of the same topics as yours and I think we could greatly benefit from each other. If you might be interested feel free to shoot me an email. I look forward to hearing from you! Wonderful blog by the way!

  25. These are actually wonderful ideas in on the topic of blogging. You have touched some pleasant things here. Any way keep up wrinting.

  26. Hi there! I juszt wanted to ask if you ever have anyy problems witth hackers? My last bloog (wordpress) wwas hacked and I nded uup losinjg many months of hard wotk ddue tto no backup. Do yoou have any methods to stop hackers?

  27. Whenn someone wites ann article he/she mantains the imjage oof a user in his/her mind that hhow a user can bbe aware oof it. Thhus that's why thjis parfagraph is perfect. Thanks!

  28. Magnificent goods from you, man. I have understand your stuff previous to and you are just too fantastic. I really like what you have acquired here, really like what you're stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I cant wait to read much more from you. This is really a great site.

  29. Everyone loves what you guys are usually up too. This type of clever work and exposure! Keep up the superb works guys I've incorporated you guys to our blogroll.

  30. nothing special

  31. Pretty nice post. I just stumbled upon yoyr blog annd wanted too say that I have really enjoyed surfing aroound ykur blog posts. In aany cqse I will bbe subxcribing to your feed andd I hope yoou write again verty soon!

  32. Hi, i read your blog frrom time tto tike and i own a similar one and i was justt wojdering if you gett a loot of spam comments? If so how do yyou prottect against it, anny plugin orr anything youu caan suggest? I geet sso muh lately it's driving me crazy so any help is very much appreciated.

  33. Временная регистрация в Санкт-Петербурге

  34. прописка в санкт-петербурге

  35. временная регистрация в москве

  36. This is the right webpage for everyone who really wants to find out about this topic. You know so much its almost tough to argue with you (not that I actually would want to…HaHa). You definitely put a brand new spin on a topic which has been discussed for many years. Excellent stuff, just excellent!

  37. What's up, yeah this piece off writting iss really fstidious annd I ave learned lot off things frm it abut blogging. thanks.

  38. Howdy excellent blog! Doees running a blog succh as this take a lott of work? I have virtually nno knowledge off coding butt I had been hopinjg to start mmy own blog soon. Anyway,should you hsve anyy suggestions or tips forr nnew boog oowners please share. I undeerstand this is offf topikc buut I just wanted to ask. Thanks a lot!

  39. windows 10 activator

  40. internet download manager crack

  41. office activator

  42. promokod-dlya-1xbet.xyz

  43. Magnificent oods from you, man. I've undderstand youur stuff previous too aand you are juszt extremely excellent. I really like what you've acxquired here, certainly like whazt yoou are saying and the way inn wwhich you saay it. You make iit entertaining aand yyou stiill tawke care of to keep iit sensible. I cant wait to redad mmuch more from you. Thhis iis acfually a wondrful site.

  44. At thiks mment I am readfy tto doo my breakfast, afterrward having my breakfast coing aggain to read additional news.

  45. buy cryptocurrency with credit card

  46. Unquestionably believe that which you said. Your favorite reason appeared to be on the internet the simplest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will likely be back to get more. Thanks

  47. Have you ever considered about including a little bit more than just your articles? I mean, what you say is fundamental and everything. However just imagine if you added some great images or videos to give your posts more, "pop"! Your content is excellent but with images and clips, this blog could undeniably be one of the greatest in its niche. Superb blog!

  48. I’m not that much of a online reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your website to come back down the road. Many thanks

  49. Мне нужен хороший психолог

  50. Hi, I do believe this is a great website. I stumbledupon it ;) I may come back once again since i have book-marked it. Money and freedom is the best way to change, may you be rich and continue to guide others.

  51. I was very pleased to uncover this great site. I want to to thank you for your time for this particularly wonderful read!! I definitely savored every part of it and I have you saved as a favorite to look at new things on your website.

  52. WOW just what I was searching for. Came here by searching for

  53. Ремонт квартир в Москве

  54. Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your posts. Can you recommend any other blogs/websites/forums that go over the same topics? Many thanks!

  55. I'm gone to inform my little brother, that he should also go to see this web site on regular basis to obtain updated from most up-to-date news.

  56. If you are going for best contents like me, only visit this web page daily as it offers feature contents, thanks

  57. I am sure this piece of writing has touched all the internet people, its really really fastidious post on building up new web site.

  58. I don't know if it's just me or if perhaps everybody else experiencing problems with your site. It seems like some of the text on your content are running off the screen. Can someone else please comment and let me know if this is happening to them too? This could be a issue with my browser because I've had this happen before. Kudos

  59. Right here is the perfect webpage for anyone who hopes to understand this topic. You understand a whole lot its almost tough to argue with you (not that I actually will need to…HaHa). You definitely put a brand new spin on a topic that's been discussed for years. Excellent stuff, just excellent!

  60. Hi there would you mind stating which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I'm looking for something unique. P.S Apologies for getting off-topic but I had to ask!

  61. After looking into a number of the blog articles on your website, I really like your way of blogging. I saved as a favorite it to my bookmark website list and will be checking back soon. Please visit my web site as well and tell me how you feel.

  62. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  63. What's up mates, its enormous article on the topic of cultureand completely defined, keep it up all the time.

  64. Please let me know if you're looking for a author for your weblog. You have some really good articles and I feel I would be a good asset. If you ever want to take some of the load off, I'd absolutely love to write some content for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Kudos!

  65. Pretty! This was an incredibly wonderful article. Thank you for providing these details.

  66. hello there and thank you for your info – I have certainly picked up something new from right here. I did however expertise several technical points using this web site, since I experienced to reload the website lots of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I'm complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for a lot more of your respective fascinating content. Ensure that you update this again soon.

  67. Everything is very open with a clear description of the challenges. It was truly informative. Your website is very useful. Thank you for sharing!

  68. Fine way of explaining, and nice paragraph to take facts about my presentation subject matter, which i am going to deliver in school.

  69. My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a number of websites for about a year and am anxious about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can import all my wordpress content into it? Any kind of help would be really appreciated!

  70. Prrtty nice post. I simply stumbled upon your eblog and wantted to mention that I havve truly enjoyed surfing around your weblog posts. After all I will bbe subscribinng in your rsss feeed and I'm hopping yyou wrife oce more soon!

  71. Fantastic web site. Lots of useful info here. I am sending it to a few pals ans also sharing in delicious. And certainly, thanks to your sweat!

  72. Definitely consider that that you said. Your favourite justification seemed to be on the net the simplest factor to keep in mind of. I say to you, I certainly get irked whilst other folks consider issues that they just don't understand about. You controlled to hit the nail upon the top as smartly as outlined out the whole thing without having side effect , other folks could take a signal. Will probably be back to get more. Thanks

  73. Hi there to every , as I am truly keen of reading this website's post to be updated daily. It includes nice data.

  74. My partner and I stumbled over here from a different website and thought I might check things out. I like what I see so now i'm following you. Look forward to looking at your web page again.

  75. Thanks , I have recently been searching for information approximately this subject for a long time and yours is the best I have came upon till now. However, what concerning the bottom line? Are you positive concerning the supply?

  76. Good day I am so grateful I found your blog page, I really found you by mistake, while I was browsing on Google for something else, Anyhow I am here now and would just like to say cheers for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don't have time to read it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic work.

  77. Its such as you learn my mind! You appear to grasp so much about this, such as you wrote the e-book in it or something. I think that you can do with some percent to power the message home a bit, but other than that, this is great blog. An excellent read. I'll certainly be back.

  78. My family always say that I am wasting my time here at net, but I know I am getting know-how all the time by reading thes good posts.

  79. Hello mates, good post and nice arguments commented at this place, I am really enjoying by these.

  80. Fantastic goods from you, man. I have keep in mind your stuff prior to and you're simply too fantastic. I actually like what you've bought right here, certainly like what you're stating and the best way in which you say it. You're making it enjoyable and you still care for to keep it sensible. I can't wait to learn far more from you. This is actually a great web site.

  81. Awesome issues here. I'm very satisfied to peer your article. Thank you a lot and I am looking forward to touch you. Will you please drop me a mail?

  82. Appreciate the recommendation. Will try it out.

  83. We stumbled over here different web address and thought I should check things out. I like what I see so i am just following you. Look forward to looking over your web page yet again.

  84. Piece of writing writing is also a excitement, if you know then you can write or else it is difficult to write.

  85. First off I would like to say great blog! I had a quick question in which I'd like to ask if you do not mind. I was interested to know how you center yourself and clear your head before writing. I've had difficulty clearing my mind in getting my thoughts out there. I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes are wasted just trying to figure out how to begin. Any recommendations or tips? Thanks!

  86. It's awesome to pay a visit this site and reading the views of all colleagues concerning this paragraph, while I am also keen of getting knowledge.

  87. Howdy! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely glad I found it and I'll be book-marking and checking back often!

添加新评论