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
|
(tt:client-connect-to-server ttdat dbfname run-id testsuite server-start-proc)))))))
;; returns ( result . ping_time )
(define (tt:timed-ping host port server-id)
(let* ((start-time (current-milliseconds))
(result (tt:ping host port server-id)))
(cons result (- (current-milliseconds) start-time))))
(define *server-load* (make-hash-table))
(define (tt:save-server-meta host port meta)
(hash-table-set! *server-load* (conc host":"port) meta))
(define (tt:get-server-threads dat)
(let* ((host (car dat))
(port (cadr dat))
(meta (tt:get-server-meta host port #t)))
(if (list? meta)
(alist-ref 'sload meta)
#f)))
;; lazy get, does not auto-refresh meta, this might be a problem
;;
(define (tt:get-server-meta host port #!optional (do-ping #f))
(let* ((meta (hash-table-ref/default *server-load* (conc host":"port) #f)))
(if (and (not meta)
do-ping)
(begin
(tt:timed-ping host port #f)
(hash-table-ref/default *server-load* (conc host":"port) #f))
meta)))
(define (tt:ping host port server-id #!optional (tries-left 5))
(let* ((res (tt:send-receive-direct host port `(ping #f #f #f) ping-mode: #t)) ;; please send me your server-id
(try-again (lambda ()
(if (> tries-left 0)
(begin
(thread-sleep! 1)
(tt:ping host port server-id (- tries-left 1)))
|
|
>
|
|
>
|
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
(tt:client-connect-to-server ttdat dbfname run-id testsuite server-start-proc)))))))
;; returns ( result . ping_time )
(define (tt:timed-ping host port server-id)
(let* ((start-time (current-milliseconds))
(result (tt:ping host port server-id)))
(cons result (- (current-milliseconds) start-time))))
;; host:port => ( meta . when-updated)
(define *server-load* (make-hash-table))
(define (tt:save-server-meta host port meta)
(hash-table-set! *server-load* (conc host":"port) (cons meta (current-seconds))))
(define (tt:get-server-threads dat)
(let* ((host (car dat))
(port (cadr dat))
(dat (tt:get-server-meta host port #t))
(meta (car dat)))
(if (list? meta)
(alist-ref 'sload meta)
#f)))
;; lazy get, does not auto-refresh meta, this might be a problem
;;
(define (tt:get-server-meta host port #!optional (do-ping #f))
(let* ((get-meta (lambda ()
(let* ((dat (hash-table-ref/default *server-load* (conc host":"port) #f)))
(if dat (car dat) #f))))
(meta (get-meta)))
(if (and (not meta)
do-ping)
(begin
(tt:timed-ping host port #f)
(get-meta))
meta)))
(define (tt:wait-on-server-load run-id ttdat)
(let* ((dbfname (dbmod:run-id->dbfname run-id))
(get-lowest-thread-load
(lambda ()
(let* ((sdats (tt:get-server-info-sorted ttdat dbfname)))
(car (map tt:get-server-threads sdats))))))
(if ttdat
(let loop ()
(if (> (get-lowest-thread-load) 5) ;; load is pretty high
(begin
(debug:print 0 *default-log-port* "Servers appear overloaded, waiting...")
(thread-sleep! 1)
(loop))))
(debug:print 0 *default-log-port* "Can't wait on server load, *ttdat* not set"))))
(define (tt:ping host port server-id #!optional (tries-left 5))
(let* ((res (tt:send-receive-direct host port `(ping #f #f #f) ping-mode: #t)) ;; please send me your server-id
(try-again (lambda ()
(if (> tries-left 0)
(begin
(thread-sleep! 1)
(tt:ping host port server-id (- tries-left 1)))
|