Megatest

Check-in [b7bca59fa9]
Login
Overview
Comment:added threaded queue example
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | v1.65
Files: files | file ages | folders
SHA1: b7bca59fa9703a5a024fc550340524fc591132fc
User & Date: bjbarcla on 2018-02-12 15:58:58
Other Links: branch diff | manifest | tags
Context
2018-02-13
16:57
added filter for -generate-html-structure check-in: 1e9a20f13a user: pjhatwal tags: v1.65
2018-02-12
15:58
added threaded queue example check-in: b7bca59fa9 user: bjbarcla tags: v1.65
01:18
wip Leaf check-in: 18868f44fd user: bb tags: v1.64-synclaunch-threaded-q
2018-02-09
15:29
Updated megatest version check-in: f598a7e51e user: jmoon18 tags: v1.65, v1.6509
Changes

Added example/q/threaded-queue.scm version [cba8217f2c].


































































































































































































































































































































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
#!/opt/chicken/bin/csi -s

(use mailbox-threads typed-records matchable mailbox posix)

;;; create a threaded job queue
;;; submit job
;;;   - command line
;;;   - working dir / default pwd
;;;   - env hash    / default current env
;;;   - callback on exit 0 / default noop
;;;   - callback on nonzero exit / default noop
;; tjq == threaded job queue; a job is a unix command
(define getenv get-environment-variable)
(defstruct tjq:job
    id  ;; assigned at construction time when added to waiting q
  state ;; assigned at construction time when added to waiting q
  (pid #f) ;; assigned when moved from ready q to running q
  (threadobj #f)
  (normal-exit #f)
  (exit-code #f) ;; assigned when moved from running to done
  (time-entered-waiting #f)
  (time-entered-ready   #f)
  (time-entered-running #f)
  (time-entered-done    #f)
  ;; following are key options to submit method
  (work-dir     (getenv "PWD")) ;; where to execute job
  (setenvs     '())            ;; alist of envvars to set when running job
  (cmdline     "/bin/true")    ;; job command line; if string, run in subshell, if list, exec.
  (success-cb  (lambda () #t)) ;; fires when exitcode is 0
  (fail-cb     (lambda () #t)));; fires when exitcode is not 0

 
             

(define (tjq:exception e)
  (print "Exception: "e)
  ;;(print-call-chain)
  (exit 1))

(define (tjq:job-thread job-id job dispatch-thread timeout-seconds)
  ;;(print "job-thread setup for jobid "job-id)
  (letrec
      ((this-thread
        (make-thread
         (lambda ()
           (tjq:job-threadobj-set! job this-thread)
           ;;(print "job-thread started for jobid "job-id)
           (let loop ((pid #f))
             ;;(print "job-thr("job-id")> loop top.")
             (match (thread-receive timeout-seconds 'timeout)
               ('timeout
                ;;(print "job-thr("job-id")> timeout; pid="pid" before cond1")
                (cond
                 ((number? pid) ;; check if still running
                  ;;(print "job-thr("job-id")> timeout; pid="pid" cond1 pid-is-number branch")
                  (let-values (((pid-or-zero normal-exit exitcode-or-signal)
                                (process-wait pid #t)))


                    ;; can get for large number of parallel threads (~ >= 42)
                    ;;                     Warning (#<thread: anonymous>): in thread: (process-wait) waiting for child process failed - No child processes: 11322
                    
                    ;; 	Call history:
                    
                    ;; 	threaded-queue.scm:56: ##sys#call-with-values	  
                    ;; 	threaded-queue.scm:57: process-wait	  	<--
                    
                    ;; Warning (#<thread: anonymous>): in thread: (process-wait) waiting for child process failed - No child processes: 11323did job 467
                    ;; did job 464
                    
                    ;; Warning (#<thread: anonymous>): in thread: (process-wait) waiting for child process failed - No child processes: 11318
                    
                    ;; 	Call history:
                    
                    ;; 	threaded-queue.scm:56: ##sys#call-with-values	  
                    ;; 	threaded-queue.scm:57: process-wait	  	<--
                    

                    ;;(print "job-thr("job-id")> pid-or-zero="pid-or-zero)
                    (cond
                     ((zero? pid-or-zero)
                      ;;(print "job-thr("job-id")> timeout; pid="pid" cond2 pid-or-zero is zero branch")
                      ;;(print "job-thr("job-id")> zero; loop.")
                      (loop pid))
                     (else
                      ;;(print "job-thr("job-id")> timeout; pid="pid" cond2 else branch")
                      (tjq:job-normal-exit-set! job
                                                (if normal-exit 'normal 'signal))
                      (tjq:job-exit-code-set! job exitcode-or-signal)
                      
                      (thread-send dispatch-thread (list 'job-now-done job-id))))
                    ;;(print "job-thr("job-id")> after cond2")
                    ))
                 (else
                  ;;(print "job-thr("job-id")> timeout; pid="pid" cond1 else branch")
                  ;;(print "job-thr("job-id")> no action; loop")
                  (thread-sleep! timeout-seconds)
                  (loop pid))))
               ('run
                ;;(print "job-thr("job-id")> run called")
                (let* ((cmdline (tjq:job-cmdline job))
                       (newpid (if (list? cmdline)
                                (process-run (car cmdline) (cdr cmdline))
                                (process-run cmdline))))
                  (tjq:job-pid-set! job newpid)
                  (thread-send dispatch-thread
                               (list 'job-now-running job-id))
                  (loop newpid)))
               (e
                (print "tjq:job-thread("job-id") illegal message received:")
                (pp e)
               (exit 1))))
           ;;(print "job-thread finished for jobid "job-id)
           #f))))
    this-thread))
       
       
(define (tjq:dispatcher-thread qname job-hash sync-job-cap obj #!key
                               (job-thread-timeout-seconds 0.1)
                               (timeout-seconds 0.1)
                               )
  (letrec
   (;; options to configure behavior of dispatcher
    (stop #f)
    ;;(timeout-seconds 0.5)
    ;;(job-thread-timeout-seconds 0.5)
    ;; define long-running thread which receives requests and coordinates
    ;; job execution
    (this-thread
     (make-thread
      (lambda ()
        (let loop ((next-job-id 0)
                   ;; four job queues holding job mbox-type threads
                   ;; they advance from one to the next
                   ;; once in done, the thread has completed.
                   (waiting '()) ;; stay here until count(running) < sync-job-cap
                   (ready   '()) ;; launch jobs in here
                   (running '()) ;; wait for pid to complete, then move do done
                   (done    '())) ;; 
          ;;(print "loop top")
          (match (thread-receive timeout-seconds 'timeout)
              ;; (let ((res (thread-receive timeout-seconds 'timeout)
              ;;        (if (not stop)
                         
              ;;            'done)))
              ;;   res)
            ('timeout
             ;;(print "to: "stop" ; next-job-id="next-job-id)
             (if (and
                  (not next-job-id) ;; we're draining jobs
                  (null? waiting)
                  (null? ready)
                  (null? running)
                  )
                 (begin
                   (print "Drained.  Done.")
                   (set! stop #t)
                   #f)
                 ;;*** when timeout happens, examine job queues
                 ;;    and move jobs thru their lifecycle
                 ;;** count waitings
                 ;; move min(sync-job-cap - running total, waiting total) to ready
                 ;; foreach ready, run it
                 ;;(print "disp: ready="ready)
                 (begin
                   (for-each (lambda (job-id)
                               (let* ((job (hash-table-ref job-hash job-id))
                                      (job-thread (tjq:job-threadobj job)))
                                 (tjq:job-state-set! job 'ready)
                                 (thread-send job-thread 'run)))
                             ready)
                   (let* ((new-running (flatten ready running))
                          (avail-slots (- sync-job-cap (length new-running)))
                          (queueable-count (min avail-slots (length waiting))))
                     (let-values (((new-ready new-waiting)
                                   (split-at waiting queueable-count)))
                       (loop next-job-id new-waiting new-ready new-running done))))))

            (('job-now-running job-id)
             (let ((job (hash-table-ref job-hash job-id)))
               (tjq:job-state-set! job 'running)
               (loop next-job-id waiting ready running done)))
            (('job-now-done job-id)
             (let* ((job (hash-table-ref job-hash job-id))
                   (successful
                    (and
                     (eq? 'normal (tjq:job-normal-exit job))
                     (zero? (tjq:job-exit-code job))))
                   (new-running (filter
                                 (lambda (x) (not (eq? x job-id)))
                                 running))
                   (new-done    (cons job-id done)))
               (tjq:job-state-set! job 'done)
               (loop next-job-id waiting ready new-running new-done)))
            (('method 'ping '() return-mbox)
             (print "got ping")
             (mailbox-send! return-mbox 'pong)
             (loop next-job-id waiting ready running done))
            
            (('method 'submit args return-mbox)
             (if (not next-job-id)
                 (begin
                   (print "refuse to submit new job -- draining jobs now.")
                   (mailbox-send! return-mbox #f)
                   (loop #f waiting ready running done))
                 (let* ((job-id next-job-id)
                        (job (apply
                              make-tjq:job
                              id: job-id
                              time-entered-waiting: (current-seconds)
                              state: 'waiting
                              args))
                        (job-thread (tjq:job-thread job-id job this-thread job-thread-timeout-seconds)))
                   (hash-table-set! job-hash job-id job)
                   (thread-start! job-thread)
                   (mailbox-send! return-mbox job-id)
                   (loop
                    (add1 next-job-id)
                    (cons job-id waiting)
                    ready running done))))

            (('method 'kill)
             ;; (for-each (job-id)
             ;;  (lambda (job-id)
             ;;    (let* ((job (hash-table-ref job-hash job-id))
             ;;           (job-thread (tjq:job-threadobj job)))
             ;;      (thread-send job-thread 'abort))
             )
            
            (('method 'drain args return-mbox)
             (mailbox-send! return-mbox 'drain)
             (loop #f waiting ready running done))
                
                       
            ;; (for-each
            ;;   (lambda (job-id)
            ;;     (let* ((job (hash-table-ref job-hash job-id))
            ;;            (job-thread (tjq:job-threadobj job)))
            ;;       (thread-join! job-thread)))
            ;;   '() ;; FIXME
            ;;  )
            
            (e
             (print "tjq:dispatcher-thread> no matching pattern.  dispatcher received: ")
             (pp e)
             (exit 1))
            (('method x args return-mbox)
             (mailbox-send! return-mbox (list 'missing-method))
             (loop next-job-id waiting ready running done))
            ) ;; end match
          ;;(print "Done dispatch thread")
          #f
          )))))
   this-thread))
  

       
(define (tjq:new #!key (qname (gensym)) (sync-job-cap 100))
  (let* ((job-hash (make-hash-table))
         (obj-mbox (make-mailbox)))
    (letrec
        ((dispatch-thread
          (tjq:dispatcher-thread
           job-thread-timeout-seconds: 0.01
           timeout-seconds: 0.01

           qname job-hash sync-job-cap obj))
         (obj
          (lambda (op . args)
            (cond
             ((eq? op 'event-loop)
              (print "got event-loop")
              (thread-join! dispatch-thread)
              (print "after thread-join dispatch-thread")
              )
             ((eq? op 'drain)
              (thread-send dispatch-thread (list 'method op args obj-mbox))
              (thread-join! dispatch-thread)
              (print "Done with queue "qname)
              #t)
             (else
              ;;(print "send method op="op)
              (thread-send dispatch-thread (list 'method op args obj-mbox))
              (let* ((res (mailbox-receive! obj-mbox)))
                (if (eq? res 'missing-method)
                    (begin
                      (print "missing method "op" called.")
                      (tjq:exception 'missing-method))
                    res)))))

          ) ;; end obj binding
         ); end letrec bindings
      (thread-start! dispatch-thread)
      obj)))
         
                            
(define (test-tjq-simple)
  (let* ((q (tjq:new qname: 'test-q sync-job-cap: 3)))
    ;(q 'submit "ls -l")
    ;(q 'drain)

    (pp (q 'ping))
    (pp (q 'ping))
    (thread-sleep! 0.1)
    ;(q 'event-loop)
    )
  )

(define (test-submit)
  (let* ((q (tjq:new qname: 'test-q sync-job-cap: 3)))
    (q 'submit cmdline: "sleep 2; echo job well done")
    (thread-sleep! 4)))


(define (test-drain-simple)
  (let* ((q (tjq:new qname: 'test-q sync-job-cap: 3)))
    ;(q 'submit cmdline: "sleep 2; echo job well done")
    (thread-sleep! 1)
    (q 'drain)))
  

(define (test-submit-bunch)
  (let* ((q (tjq:new qname: 'test-q sync-job-cap: 3)))
    (for-each (lambda (x)
                (let* ((cmd (conc "echo did job "x)))
                  (print "submit it--"x)
                  (q 'submit cmdline: cmd))
               )
             (iota 6))
    ;;(thread-sleep! 10)
    (q 'drain)
    ;;(q 'event-loop)
    ))

(define (test-submit-bunch2)
  (let* ((q (tjq:new qname: 'test-q sync-job-cap: 20 )))
    (for-each (lambda (x)
                ;;(let* ((cmd (conc "echo did job "x)))
                (let* ((cmd "/bin/true"))
                  ;;(print "submit it--"x)
                  (q 'submit cmdline: cmd))
               )
             (iota 6000))
    ;;(thread-sleep! 10)
    (q 'drain)
    ;;(q 'event-loop)
    ))

;(test-tjq-simple)
;;(test-submit)
;;(test-drain-simple)
(print "top")
(test-submit-bunch2)