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

Add logging options for queue #105

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fef17be
Fix embed access
minhnhatnoe Jun 4, 2023
a050bc2
Run go mod tidy
minhnhatnoe Jun 4, 2023
d4a196a
Script with database wipe
minhnhatnoe Jun 4, 2023
f7ac3b5
Add instruction for production_test.ps1
minhnhatnoe Jun 4, 2023
a73e79e
Remove ineffective break statement
minhnhatnoe Jun 4, 2023
e4735d0
Move modules
minhnhatnoe Jun 4, 2023
72753dc
function to get sandbox by name
minhnhatnoe Jun 4, 2023
5dbd48e
WIP Code
minhnhatnoe Jun 4, 2023
429bdb0
WIP
minhnhatnoe Jun 5, 2023
5ed8d18
WIP, still
minhnhatnoe Jun 5, 2023
46bb770
Finalize input and spawn problem
minhnhatnoe Jun 6, 2023
eb5e369
Add perf testset to DB
minhnhatnoe Jun 6, 2023
2ca6e97
Structure
minhnhatnoe Jun 6, 2023
bf89a77
User and solution
minhnhatnoe Jun 6, 2023
8f86573
WIP
minhnhatnoe Jun 6, 2023
121310e
WIP
minhnhatnoe Jun 7, 2023
4f83bc7
Merge pull request #3 from minhnhatnoe/windows-embed
minhnhatnoe Jun 7, 2023
c07c289
Change names
minhnhatnoe Jun 8, 2023
18e4b0e
Put sandbox warning inside NewSandbox
minhnhatnoe Jun 9, 2023
8ced478
Optimize scoring
minhnhatnoe Jun 9, 2023
4f9297f
gofmt
minhnhatnoe Jun 9, 2023
8fc5324
WIP
minhnhatnoe Jun 9, 2023
1a18ef9
New option-choosing for sandbox
minhnhatnoe Jun 9, 2023
2951041
Merge pull request #4 from minhnhatnoe/stress
minhnhatnoe Jun 9, 2023
8151a73
Ignore warnings
minhnhatnoe Jun 9, 2023
5fa698e
Can disable sandbox logs
minhnhatnoe Jun 10, 2023
7e7822f
Sandbox options
minhnhatnoe Jun 10, 2023
fb5b020
Add options for queue
minhnhatnoe Jun 10, 2023
bfcb85f
Everything
minhnhatnoe Jun 10, 2023
eb3cbf0
Merge
minhnhatnoe Jun 10, 2023
ae7e1ce
Merge pull request #5 from minhnhatnoe/queue-options
minhnhatnoe Jun 10, 2023
40f88b8
Remove worker logs
minhnhatnoe Jun 10, 2023
8c500b6
Expand logs
minhnhatnoe Jun 10, 2023
311b719
Merge branch 'stress-testing' into queue-options
minhnhatnoe Jun 10, 2023
5afdabe
Delete test/performance directory
minhnhatnoe Jun 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions cmd/kjudge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
_ "github.com/natsukagami/kjudge/models"
"github.com/natsukagami/kjudge/server"
"github.com/natsukagami/kjudge/worker"
"github.com/natsukagami/kjudge/worker/isolate"
"github.com/natsukagami/kjudge/worker/raw"
"github.com/natsukagami/kjudge/worker/queue"
)

var (
Expand All @@ -34,15 +33,9 @@ func main() {
}
defer db.Close()

var sandbox worker.Sandbox
switch *sandboxImpl {
case "raw":
log.Println("'raw' sandbox selected. WE ARE NOT RESPONSIBLE FOR ANY BREAKAGE CAUSED BY FOREIGN CODE.")
sandbox = &raw.Sandbox{}
case "isolate":
sandbox = isolate.New()
default:
log.Fatalf("Sandbox %s doesn't exists or not yet implemented.", *sandboxImpl)
sandbox, err := worker.NewSandbox(*sandboxImpl)
if err != nil {
log.Fatalf("%v", err)
}

opts := []server.Opt{}
Expand All @@ -51,7 +44,7 @@ func main() {
}

// Start the queue
queue := worker.Queue{Sandbox: sandbox, DB: db}
queue := queue.NewQueue(db, sandbox)

// Build the server
server, err := server.New(db, opts...)
Expand Down
4 changes: 2 additions & 2 deletions db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"
"io/fs"
"log"
"path/filepath"
"path"
"regexp"
"sort"

Expand Down Expand Up @@ -44,7 +44,7 @@ func (db *DB) migrate() error {

// Do migrations one by one
for _, name := range versions {
sqlFile := filepath.Join(assetsSql, name+".sql")
sqlFile := path.Join(assetsSql, name+".sql")
file, err := fs.ReadFile(embed.Content, sqlFile)
if err != nil {
return errors.Wrapf(err, "File %s", sqlFile)
Expand Down
17 changes: 12 additions & 5 deletions embed/embed_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ import (
"log"
"os"
"path/filepath"
"runtime"
)

// Content serves content in the /embed directory.
var Content fs.FS

func getEmbedDir() string {
_, path, _, _ := runtime.Caller(0)
return filepath.Dir(path)
}

func init() {
wd, err := os.Getwd()
if err != nil {
log.Panicf("cannot get current directory: %v", err)
}
// wd, err := os.Getwd()
// if err != nil {
// log.Panicf("cannot get current directory: %v", err)
// }

embedDir := filepath.Join(wd, "embed")
// embedDir := filepath.Join(wd, "embed")
embedDir := getEmbedDir()
stat, err := os.Stat(embedDir)
if err != nil {
log.Panicf("cannot stat embed directory: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.0
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
golang.org/x/net v0.7.0
golang.org/x/net v0.7.0 // indirect
golang.org/x/text v0.7.0
golang.org/x/tools v0.1.12
google.golang.org/appengine v1.6.5 // indirect
Expand Down
8 changes: 8 additions & 0 deletions scripts/windows/production_test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$ErrorActionPreference = "Stop"
Remove-Item kjudge.db*

& "scripts\windows\production_build.ps1"

Invoke-Expression ".\kjudge $args"

# pwsh -c scripts/windows/production_test.ps1 --sandbox=raw to run this test script
19 changes: 14 additions & 5 deletions worker/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ import (

// CompileContext is the information needed to perform compilation.
type CompileContext struct {
DB *sqlx.Tx
Sub *models.Submission
Problem *models.Problem
DB *sqlx.Tx
Sub *models.Submission
Problem *models.Problem
AllowLogs bool
}

func (c *CompileContext) Log(format string, v ...interface{}) {
if !c.AllowLogs {
return
}
log.Printf(format, v...)
}

// Compile performs compilation.
Expand Down Expand Up @@ -65,7 +73,7 @@ func Compile(c *CompileContext) (bool, error) {
return false, c.Sub.Write(c.DB)
}

log.Printf("[WORKER] Compiling submission %v\n", c.Sub.ID)
c.Log("[WORKER] Compiling submission %v\n", c.Sub.ID)

// Now, create a temporary directory.
dir, err := os.MkdirTemp("", "*")
Expand Down Expand Up @@ -96,7 +104,8 @@ func Compile(c *CompileContext) (bool, error) {
c.Sub.CompiledSource = nil
c.Sub.Verdict = models.VerdictCompileError
}
log.Printf("[WORKER] Compiling submission %v succeeded (result = %v).", c.Sub.ID, result)

c.Log("[WORKER] Compiling submission %v succeeded (result = %v).", c.Sub.ID, result)

return result, c.Sub.Write(c.DB)
}
Expand Down
47 changes: 39 additions & 8 deletions worker/queue.go → worker/queue/queue.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package worker
package queue

import (
"log"
Expand All @@ -7,20 +7,30 @@ import (
"github.com/mattn/go-sqlite3"
"github.com/natsukagami/kjudge/db"
"github.com/natsukagami/kjudge/models"
"github.com/natsukagami/kjudge/worker"
"github.com/natsukagami/kjudge/worker/sandbox"
"github.com/pkg/errors"
)

// Queue implements a queue that runs each job one by one.
type Queue struct {
DB *db.DB
Sandbox Sandbox
DB *db.DB
Sandbox sandbox.Runner
Settings Settings
}

func NewQueue(db *db.DB, sandbox sandbox.Runner, options ...Option) Queue {
setting := DefaultSettings
for _, option := range options {
setting = option(setting)
}
return Queue{DB: db, Sandbox: sandbox, Settings: setting}
}

// Start starts the queue. It is blocking, so might wanna "go run" it.
func (q *Queue) Start() {
// Register the update callback
toUpdate := q.startHook()

for {
// Get the newest job
job, err := models.FirstJob(q.DB)
Expand All @@ -41,6 +51,25 @@ func (q *Queue) Start() {
}
}

// Run starts the queue, solves all pending jobs, then returns
func (q *Queue) Run() {
for {
job, err := models.FirstJob(q.DB)
if err != nil {
log.Printf("[WORKER] Fetching job failed: %+v\n", err)
continue
}
if job == nil {
return
}

if err := q.HandleJob(job); err != nil {
log.Printf("[WORKER] Handling job failed: %+v\n", err)
}
_ = job.Delete(q.DB)
}
}

// HandleJob dispatches a job.
func (q *Queue) HandleJob(job *models.Job) error {
// Start a job with a context and submission
Expand All @@ -60,7 +89,8 @@ func (q *Queue) HandleJob(job *models.Job) error {
}
switch job.Type {
case models.JobTypeCompile:
if _, err := Compile(&CompileContext{DB: tx, Sub: sub, Problem: problem}); err != nil {
if _, err := worker.Compile(&worker.CompileContext{
DB: tx, Sub: sub, Problem: problem, AllowLogs: q.Settings.LogCompile}); err != nil {
return err
}
case models.JobTypeRun:
Expand All @@ -72,16 +102,17 @@ func (q *Queue) HandleJob(job *models.Job) error {
if err != nil {
return err
}
if err := Run(q.Sandbox, &RunContext{
DB: tx, Sub: sub, Problem: problem, TestGroup: tg, Test: test}); err != nil {
if err := worker.Run(q.Sandbox, &worker.RunContext{
DB: tx, Sub: sub, Problem: problem, TestGroup: tg, Test: test, AllowLogs: q.Settings.LogRun}); err != nil {
return err
}
case models.JobTypeScore:
contest, err := models.GetContest(tx, problem.ContestID)
if err != nil {
return err
}
if err := Score(&ScoreContext{DB: tx, Sub: sub, Problem: problem, Contest: contest}); err != nil {
if err := worker.Score(&worker.ScoreContext{
DB: tx, Sub: sub, Problem: problem, Contest: contest, AllowLogs: q.Settings.LogScore}); err != nil {
return err
}
}
Expand Down
32 changes: 32 additions & 0 deletions worker/queue/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package queue

type Settings struct {
LogCompile bool
LogRun bool
LogScore bool
}

var DefaultSettings = Settings{LogCompile: true, LogRun: true, LogScore: true}

type Option func(Settings) Settings

func CompileLogs(enable bool) Option {
return func(o Settings) Settings {
o.LogCompile = enable
return o
}
}

func RunLogs(enable bool) Option {
return func(o Settings) Settings {
o.LogRun = enable
return o
}
}

func ScoreLogs(enable bool) Option {
return func(o Settings) Settings {
o.LogScore = enable
return o
}
}
Loading