Skip to content

Commit 10dbd46

Browse files
authored
Merge pull request #1363 from herbie-fp/codex/implement-fifo-job-queue-for-workers
Dispatch jobs to workers in FIFO order
2 parents 652a7dc + 2a3f7e4 commit 10dbd46

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/api/server.rkt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(require openssl/sha1)
44
(require (only-in xml write-xexpr))
55
(require json)
6+
(require data/queue)
67

78
(require "../syntax/read.rkt"
89
"../syntax/sugar.rkt"
@@ -231,6 +232,7 @@
231232
(define busy-workers (make-hash))
232233
(define waiting-workers (make-hash))
233234
(define current-jobs (make-hash))
235+
(define queued-job-ids (make-queue))
234236
(when (eq? worker-count #t)
235237
(set! worker-count (processor-count)))
236238
(for ([i (in-range worker-count)])
@@ -243,20 +245,19 @@
243245

244246
;; Private API
245247
[(list 'assign self)
246-
(define reassigned (make-hash))
248+
(define reassigned '())
247249
(for ([(wid worker) (in-hash waiting-workers)]
248-
[(jid command) (in-hash queued-jobs)])
250+
#:when (not (queue-empty? queued-job-ids)))
251+
(define jid (dequeue! queued-job-ids))
252+
(define command (hash-ref queued-jobs jid))
249253
(log "Starting worker [~a] on [~a].\n" jid (test-name (herbie-command-test command)))
250-
; Check if the job is already in progress.
251-
(unless (hash-has-key? current-jobs jid)
252-
(place-channel-put worker (list 'apply self command jid))
253-
(hash-set! current-jobs jid wid)
254-
(hash-set! reassigned wid jid)
255-
(hash-set! busy-workers wid worker)))
256-
; remove X many jobs from the Q and update waiting-workers
257-
(for ([(wid jid) (in-hash reassigned)])
258-
(hash-remove! waiting-workers wid)
259-
(hash-remove! queued-jobs jid))]
254+
(place-channel-put worker (list 'apply self command jid))
255+
(hash-set! current-jobs jid wid)
256+
(hash-set! busy-workers wid worker)
257+
(set! reassigned (cons wid reassigned))
258+
(hash-remove! queued-jobs jid))
259+
(for ([wid reassigned])
260+
(hash-remove! waiting-workers wid))]
260261
; Job is finished save work and free worker. Move work to 'send state.
261262
[(list 'finished self wid job-id result)
262263
(log "Job ~a finished, saving result.\n" job-id)
@@ -285,6 +286,7 @@
285286
(place-channel-put self (list 'send job-id (hash-ref completed-jobs job-id)))]
286287
[else
287288
(hash-set! queued-jobs job-id job)
289+
(enqueue! queued-job-ids job-id)
288290
(place-channel-put self (list 'assign self))])]
289291
[(list 'wait handler self job-id)
290292
(log "Waiting for job: ~a\n" job-id)

0 commit comments

Comments
 (0)