-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtermite_core.scm
712 lines (583 loc) · 20.1 KB
/
termite_core.scm
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
;; Copyright (C) 2005-2008 by Guillaume Germain, All Rights Reserved.
;; File: "termite.scm"
;; this is the main file for the Termite system
(import recv
utils
uuid
exception
match)
(declare
(standard-bindings)
(extended-bindings)
(block))
;; ----------------------------------------------------------------------------
;; System configuration & global data
(define current-node (lambda () (error "uninitialized node")))
(define *global-mutex* (make-mutex "global termite mutex"))
;; translation tables for "published" PIDs
(define *foreign->local* (make-table weak-values: #t))
(define *local->foreign* (make-table weak-keys: #t))
;; translation table for "published" tags
(define *uuid->tag* (make-table weak-values: #t))
;; Get the current time in seconds.
(define (now)
(time->seconds
(current-time)))
;; TODO Improve this
(define (formatted-current-time)
(let* ((port (open-process "date"))
(time (read-line port)))
(close-port port)
time))
;; ----------------------------------------------------------------------------
;; Datatypes
(define (process? obj) (thread? obj))
(define (process-links pid) (thread-specific pid))
(define (process-links-set! pid obj) (thread-specific-set! pid obj))
;; universal pid
(define-type upid
id: 9e096e09-8c66-4058-bddb-e061f2209838
tag
node)
;; nodes
(define-type node
id: 8992144e-4f3e-4ce4-9d01-077576f98bc5
read-only:
host
port)
;; tags
(define-type tag
id: efa4f5f8-c74c-465b-af93-720d44a08374
(uuid init: #f))
;; * Test whether 'obj' is a pid.
(define (pid? obj)
(or (process? obj) (upid? obj)))
;; ----------------------------------------------------------------------------
;; Logging functions
(define termite-log-fun (make-parameter #f))
(define (termite-log type message-list)
(and (termite-log-fun)
((termite-log-fun) type message-list)))
(define (warning . terms)
(termite-log 'warning terms))
(define (info . terms)
(termite-log 'info terms))
(define (debug . terms)
(termite-log 'debug terms))
;; ----------------------------------------------------------------------------
;; process manipulation primitives
;; * Get the pid of the current process.
(define self current-thread)
;; Base exception handler for Termite processes.
(define (base-exception-handler e)
(continuation-capture
(lambda (k)
(let ((log-crash
(lambda (e)
(termite-log
'error
(call-with-output-string
""
(lambda (port)
(display-exception-in-context e k port)))))))
(cond
;; Propagated Termite exception?
((termite-exception? e)
(if (not (eq? (termite-exception-reason e) 'normal))
(log-crash (termite-exception-object e)))
(for-each
(lambda (pid) (! pid e))
(process-links (self)))
(halt!))
;; Gambit exception in the current process
(else
(log-crash e)
(for-each
(lambda (pid)
(! pid (make-termite-exception (self) 'failure e)))
(process-links (self)))
(halt!)))))))
;; * Start a new process executing the code in 'thunk'.
(define (spawn thunk #!key (links '()) (name 'anonymous))
(let ((t (make-thread
(lambda ()
(with-exception-handler
base-exception-handler
thunk)
(shutdown!))
name)))
(thread-specific-set! t links)
(thread-start! t)
t))
(define (spawn-linked-to to thunk #!key (name 'anonymous-linked-to))
(spawn thunk links: (list to) name: name))
;; * Start a new process with a bidirectional link to the current
;; process.
(define (spawn-link thunk #!key (name 'anonymous-linked))
(let ((pid (spawn thunk links: (list (self)) name: name)))
(outbound-link pid)
pid))
;; * Start a new process on remote node 'node', executing the code
;; in 'thunk'.
(define (remote-spawn node thunk #!key (links '()) (name 'anonymous-remote))
(if (equal? node (current-node))
(spawn thunk links: links name: name)
(!? (remote-service 'spawner node)
(list 'spawn thunk links name))))
;; * Start a new process on remote node 'node', with a bidirectional
;; link to the current process.
(define (remote-spawn-link node thunk)
(let ((pid (remote-spawn node thunk links: (list (self)))))
(outbound-link pid)
pid))
;; * Cleanly stop the execution of the current process. Linked
;; processes will receive a "normal" exit message.
(define (shutdown!)
(for-each
(lambda (pid)
(! pid (make-termite-exception (self) 'normal #f)))
(process-links (self)))
(halt!))
;; this is *not* nice: it wont propagate the exit message to the other
;; processes
(define (halt!)
(thread-terminate! (current-thread)))
;; * Forcefully terminate a local process. Warning: it only works on
;; local processes! This should be used with caution.
(define (terminate! victim)
(thread-terminate! victim)
(for-each
(lambda (link)
(! link (make-termite-exception victim 'terminated #f)))
(process-links victim)))
;; TODO 'wait-for' and 'alive?' should be grouped in a more general
;; procedure able to determine the status of a process (alive, dead,
;; waiting, etc.) and the procedure should work on remote processes
;; * Wait for the end of a process 'pid'. Does not return anything.
;; Warning: will not work on remote processes.
(define (%wait-for pid)
(with-exception-catcher
(lambda (e)
(void))
(lambda ()
(thread-join! pid)
(void))))
;; Check whether the process 'pid' is still alive. Warning: will not
;; work on remote processes.
(define (%alive? pid)
(with-exception-catcher
(lambda (e)
(join-timeout-exception? e))
(lambda ()
(thread-join! pid 0)
#f)))
;; ----------------------------------------------------------------------------
;; Sending messages
;; * Send a message 'msg' to 'pid'. This means that the message will
;; be enqueued in the mailbox of the destination process.
;;
;; Delivery of the message is unreliable in theory, but in practice
;; local messages will always be delivered, and remote messages will
;; not be delivered only if the connection is currently broken to the
;; remote node, or if the remote node is down.
;;
;; Note that you will not get an error or an exception if the message
;; doesn't get there: you need to handle errors yourself.
(define (! to msg)
(cond
((process? to)
(thread-send to msg))
((upid? to)
(thread-send dispatcher (list 'relay to msg)))
(else
(error "invalid-message-destination" to))))
;; ----------------------------------------------------------------------------
;; Receiving messages
;; incorrect, because it doesn't handle exception messages
;; (define ? thread-receive)
;; * Retrieve the first message from the mailbox of the current
;; process. If no message is available, the process will block until
;; a message is received. If 'timeout' is specified, the process will
;; only block for that amount of time, and then raise an exception.
;; It is possible to also pass the 'default' argument to return a
;; value instead of raising an exception.
(define (? . opt) ;; TODO: inefficient, fix
(match opt
(()
(recv
(msg msg)))
((timeout)
(recv
(msg msg)
(after timeout (thread-receive 0))))
((timeout default)
(recv
(msg msg)
(after timeout default)))))
;; benchmark to see if faster...
;; (define (? #!optional (timeout +inf.0) (default (lambda (thread-receive 0))))
;; (with-exception-catcher
;; (lambda (exception)
;; (if (mailbox-receive-timeout-exception? exception)
;; (default)
;; (raise exception)))
;; (lambda ()
;; (thread-receive timeout))))
;; * Retrieve the first message from the mailbox of the current
;; process that satisfised the predicate 'pred?'. If no message
;; qualifies, the process will block until a message satisfying the
;; predicate is received. If 'timeout' is specified, the process will
;; only block for that amount of time, and then raise an exception.
;; It is possible to also pass the 'default' argument to return a
;; value instead of raising an exception.
;; TODO: inefficient, fix
(define (?? pred? . opt)
(match opt
(()
(recv
(msg (where (pred? msg)) msg)))
((timeout)
(recv
(msg (where (pred? msg)) msg)
(after timeout (thread-receive 0))))
((timeout default)
(recv
(msg (where (pred? msg)) msg)
(after timeout default)))))
;; ----------------------------------------------------------------------------
;; Higher-order concurrency primitives
;; * Send a "synchronous" message to a process. The message will be
;; annotated with a tag and the pid of the current process, therefore
;; sending a message of the form '(from tag msg)'. The server
;; receiving the message must specifically handle that format of
;; message, and reply with a message of the form '(tag reply)'.
;;
;; Like for the |?| and |??| message retrieving operators, it is
;; possible to specify a 'timeout' to limit the amount of time to wait
;; for a reply, and a 'default' value to return if no reply has been
;; received.
;; RPC
(define (!? pid msg . opt)
(let ((tag (make-tag)))
(! pid (list (self) tag msg))
(match opt
(()
(recv
((,tag reply) reply)))
((timeout)
(recv
((,tag reply) reply)
(after timeout (raise 'timeout))))
((timeout default)
(recv
((,tag reply) reply)
(after timeout default))))))
;; * Evaluate a 'thunk' on a remote node and return the result of that
;; evaluation. Just like for |!?|, |?| and |??|, it is possible to
;; specify a 'timeout' and a 'default' argument.
(define (on node thunk)
(let ((tag (make-tag))
(from (self)))
(remote-spawn node
(lambda ()
(! from (list tag (thunk)))))
(recv
((,tag reply) reply))))
;; ----------------------------------------------------------------------------
;; Links and exception handling
;; * Link the current process /to/ another process 'pid': any
;; exception not being caught by the current process will be
;; propagated to the remote process.
(define (outbound-link pid)
(let* ((links (process-links (self))))
(if (not (memq pid links))
(process-links-set! (self) (cons pid links)))))
;; ----------------------------------------------------------------------------
;; Termite I/O
;; Wraps 'pid's representing Gambit output ports.
(define-type termite-output-port
id: b0c30401-474c-4e83-94b4-d516e00fe363
unprintable:
pid)
;; Wraps 'pid's representing Gambit input ports.
(define-type termite-input-port
id: ebb22fcb-ca61-4765-9896-49e6716471c3
unprintable:
pid)
;; Start a process representing a Gambit output port.
(define (spawn-output-port port #!optional (serialize? #f))
(output-port-readtable-set!
port
(readtable-sharing-allowed?-set
(output-port-readtable port)
serialize?))
(make-termite-output-port
(spawn
(lambda ()
(let loop ()
(recv
(proc
(where (procedure? proc))
(proc port))
(x (warning "unknown message sent to output port: " x)))
(loop))))
name: 'termite-output-port))
;; Start a process representing a Gambit input port.
(define (spawn-input-port port #!optional (serialize? #f))
(input-port-readtable-set!
port
(readtable-sharing-allowed?-set
(input-port-readtable port)
serialize?))
(make-termite-input-port
(spawn
(lambda ()
(let loop ()
(recv
((from token proc)
(where (procedure? proc))
(! from (list token (proc port))))
(x (warning "unknown message sent to input port: " x)))
(loop))))
name: 'termite-input-port))
;; IO parameterization
;; (define current-termite-input-port (make-parameter #f))
;; (define current-termite-output-port (make-parameter #f))
;; insert IO overrides
;; (include "termiteio.scm")
;; ----------------------------------------------------------------------------
;; Distribution
;; Convert a 'pid'
(define (pid->upid obj)
(mutex-lock! *global-mutex*)
(cond
((table-ref *local->foreign* obj #f)
=> (lambda (x)
(mutex-unlock! *global-mutex*)
x))
(else
(let ((upid (make-upid (make-uuid) (current-node))))
(table-set! *local->foreign* obj upid)
(table-set! *foreign->local* upid obj)
(mutex-unlock! *global-mutex*)
upid))))
(define (tag->utag obj)
(mutex-lock! *global-mutex*)
(cond
((tag-uuid obj)
(mutex-unlock! *global-mutex*)
obj)
(else
(let ((uuid (make-uuid)))
(tag-uuid-set! obj uuid)
(table-set! *uuid->tag* uuid obj)
(mutex-unlock! *global-mutex*)
obj))))
(define (serialize-hook obj)
(cond
((process? obj)
(pid->upid obj))
((tag? obj)
(tag->utag obj))
;; unserializable objects, so instead of crashing we set them to #f
((or (port? obj))
#f)
(else obj)))
(define (upid->pid obj)
(cond
((table-ref *foreign->local* obj #f)
=> (lambda (pid) pid))
((and (symbol? (upid-tag obj))
(resolve-service (upid-tag obj)))
=> (lambda (pid)
pid))
(else
(error "don't know how to upid->pid"))))
(define (utag->tag obj)
(let ((uuid (tag-uuid obj)))
(cond
((table-ref *uuid->tag* uuid #f)
=> (lambda (tag) tag))
(else obj))))
(define (deserialize-hook obj)
(cond
((and (upid? obj)
(equal? (upid-node obj)
(current-node)))
(upid->pid obj))
((tag? obj)
(utag->tag obj))
(else obj)))
(define (serialize obj port)
(let* ((serialized-obj
(object->u8vector obj serialize-hook))
(len
(u8vector-length serialized-obj))
(serialized-len
(u8vector (bitwise-and len #xff)
(bitwise-and (arithmetic-shift len -8) #xff)
(bitwise-and (arithmetic-shift len -16) #xff)
(bitwise-and (arithmetic-shift len -24) #xff))))
(begin
(write-subu8vector serialized-len 0 4 port)
(write-subu8vector serialized-obj 0 len port))))
(define (deserialize port)
(let* ((serialized-len
(u8vector 0 0 0 0))
(n
(read-subu8vector serialized-len 0 4 port)))
(cond ((= 0 n)
#!eof)
((not (= 4 n))
(error "deserialization error"))
(else
(let* ((len
(+ (u8vector-ref serialized-len 0)
(arithmetic-shift (u8vector-ref serialized-len 1) 8)
(arithmetic-shift (u8vector-ref serialized-len 2) 16)
(arithmetic-shift (u8vector-ref serialized-len 3) 24)))
(serialized-obj
(make-u8vector len))
(n
(read-subu8vector serialized-obj 0 len port)))
(if (not (eqv? len n))
(begin
(error "deserialization error"
(list len: len n: n)))
(let ((obj (u8vector->object serialized-obj deserialize-hook)))
(if (vector? obj)
(vector->list obj)
obj))))))))
(define (start-serializing-output-port port)
(spawn-link
(lambda ()
(let loop ()
(recv
(('write data)
;; (debug out: data)
(serialize data port)
(force-output port)) ;; io override
(msg
(warning "serializing-output-port ignored message: " msg)))
(loop)))
name: 'termite-serializing-output-port))
(define (start-serializing-active-input-port port receiver)
(spawn-link
(lambda ()
(let loop ()
(let ((data (deserialize port)))
;; to receive exceptions...
(? 0 'ok)
;; (debug in: data)
(if (eof-object? data) (shutdown!))
(! receiver (list (self) data))
(loop))))
name: 'termite-serializing-active-input-port))
;; a tcp server listens on a certain port for new tcp connection
;; requests, and call ON-CONNECT to deal with those new connections.
(define (start-tcp-server tcp-port-number on-connect)
(let ((tcp-server-port
(open-tcp-server (list
port-number: tcp-port-number
coalesce: #f))))
(spawn
(lambda ()
(let loop ()
(on-connect (read tcp-server-port)) ;; io override
(loop)))
name: 'termite-tcp-server)))
;; MESSENGERs act as proxies for sockets to other nodes
;; initiate a new bidirectional connection to another node important:
;; caller is responsible for registering it with the dispatcher
(define (initiate-messenger node)
;; (print "OUTBOUND connection established\n")
(spawn
(lambda ()
(with-exception-catcher
(lambda (e)
(! dispatcher (list 'unregister (self)))
(shutdown!))
(lambda ()
(let ((socket (open-tcp-client
(list server-address: (node-host node)
port-number: (node-port node)
coalesce: #f))))
;; the real interesting part
(let ((in (start-serializing-active-input-port socket (self)))
(out (start-serializing-output-port socket)))
(! out (list 'write (current-node)))
(messenger-loop node in out))))))
name: 'termite-outbound-messenger))
;; start a MESSENGER for an 'inbound' connection (another node
;; initiated the bidirectional connection, see initiate-messenger)
(define (start-messenger socket)
;; (print "INBOUND connection established\n")
(spawn
(lambda ()
(with-exception-catcher
(lambda (e)
(! dispatcher (list 'unregister (self)))
(shutdown!))
(lambda ()
(let ((in (start-serializing-active-input-port socket (self)))
(out (start-serializing-output-port socket)))
(recv
((,in node)
;; registering messenger to local dispatcher
(! dispatcher (list 'register (self) node))
(messenger-loop node in out)))))))
name: 'termite-inbound-messenger))
(define (messenger-loop node in out)
(recv
;; incoming message
((,in ('relay id message))
(let ((to (upid->pid (make-upid id (current-node)))))
(! to message)))
;; outgoing message
(('relay to message)
;; 'to' is a upid
(let* ((id (upid-tag to))
;; (node (upid-node to))
;; (host (node-host node))
;; (port (node-id node))
)
(! out (list 'write (list 'relay id message)))))
;; unknown message
(msg
(warning "messenger-loop ignored message: " msg)))
(messenger-loop node in out))
;; the DISPATCHER dispatches messages to the right MESSENGER, it keeps
;; track of known remote nodes
(define dispatcher
(spawn
(lambda ()
;; the KNOWN-NODES of the DISPATCHER LOOP is an a-list of NODE => MESSENGER
(let loop ((known-nodes '()))
(recv
(('register messenger node)
(loop (cons (cons node messenger) known-nodes)))
(('unregister messenger)
(loop (remove (lambda (m) (equal? (cdr m) messenger)) known-nodes)))
(('relay upid message)
(let ((node (upid-node upid)))
(cond
;; the message should be sent locally (ideally should not happen
;; for performance reasons, but if the programmer wants to do
;; that, then OK...)
((equal? node (current-node))
(! (upid->pid upid) message)
(loop known-nodes))
;; the message is destined to a pid on a known node
((assoc node known-nodes)
=> (lambda (messenger)
(! (cdr messenger) (list 'relay upid message))
(loop known-nodes)))
;; unconnected node, must connect
(else
(let ((messenger (initiate-messenger node)))
(! messenger (list 'relay upid message))
(loop (cons (cons node messenger) known-nodes)))))))
(msg
(warning "dispatcher ignored message: " msg) ;; uh...
(loop known-nodes)))))
name: 'termite-dispatcher))