Big refactor for 8sync on fibers in progress.
[8sync.git] / 8sync / actors.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright © 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This file is part of 8sync.
5 ;;;
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU Lesser General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync.  If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (8sync actors)
20   #:use-module (oop goops)
21   #:use-module (srfi srfi-9)
22   #:use-module (ice-9 control)
23   #:use-module (ice-9 format)
24   #:use-module (ice-9 match)
25   #:use-module (ice-9 atomic)
26   #:use-module (ice-9 pretty-print)
27   #:use-module (ice-9 receive)
28   #:use-module (fibers)
29   #:use-module (fibers channels)
30   #:use-module (fibers conditions)
31   #:use-module (fibers operations)
32   #:use-module (8sync agenda)
33   #:use-module (8sync inbox)
34   #:use-module (8sync rmeta-slot)
35   #:export (;; utilities... ought to go in their own module
36             big-random-number
37             big-random-number-string
38
39             <actor>
40             actor-id
41             actor-message-handler
42
43             ;;; Commenting out the <address> type for now;
44             ;;; it may be back when we have better serializers
45             ;; <address>
46             make-address address?
47             address-actor-id address-hive-id
48
49             actor-init! actor-cleanup!
50
51             address->string
52             actor-id-actor
53             actor-id-hive
54             actor-id-string
55
56             actor-alive?
57
58             build-actions
59
60             define-actor
61
62             ;; <hive>
63             ;; make-hive
64             ;; ;; There are more methods for the hive, but there's
65             ;; ;; no reason for the outside world to look at them maybe?
66             ;; hive-id
67             bootstrap-actor bootstrap-actor*
68
69             create-actor create-actor*
70             self-destruct
71
72             <message>
73             make-message message?
74             message-to message-action message-from
75             message-id message-body message-in-reply-to
76             message-wants-reply
77
78             message-auto-reply?
79
80             <- <-wait
81
82             spawn-hive run-hive))
83
84 ;; For ids
85 (set! *random-state* (random-state-from-platform))
86
87 ;; Same size as a uuid4 I think...
88 (define random-number-size (expt 2 128))
89
90 (define (big-random-number)
91   (random random-number-size))
92
93 ;; Would be great to get this base64 encoded instead.
94 (define (big-random-number-string)
95   ;; @@: This is slow.  Using format here is wasteful.
96   (format #f "~x" (big-random-number)))
97
98 ;; @@: This is slow-ish.  A mere ~275k / second on my (old) machine.
99 ;;   The main cost seems to be in number->string.
100 (define (simple-message-id-generator)
101   ;; Prepending this cookie makes message ids unique per hive
102   (let ((prefix (format #f "~x:" (big-random-number)))
103         (counter 0))
104     (lambda ()
105       (set! counter (1+ counter))
106       (string-append prefix (number->string counter)))))
107
108
109 \f
110 ;;; Messages
111 ;;; ========
112
113 (define-record-type <message>
114   (make-message-intern id to from action
115                        body in-reply-to wants-reply)
116   message?
117   ;; @@: message-ids are removed.  They could be re-enabled
118   ;;   if we had thread-safe promises...
119   (id message-id)                    ; id of this message
120   (to message-to)                    ; actor id this is going to
121   (from message-from)                ; actor id of sender
122   (action message-action)            ; action (a symbol) to be handled
123   (body message-body)                ; argument list "body" of message
124   (in-reply-to message-in-reply-to)  ; message id this is in reply to, if any
125   (wants-reply message-wants-reply)) ; whether caller is waiting for reply
126
127
128 (define* (make-message id to from action body
129                        #:key in-reply-to wants-reply)
130   (make-message-intern id to from action body
131                        in-reply-to wants-reply))
132
133 (define (kwarg-list-to-alist args)
134   (let loop ((remaining args)
135              (result '()))
136     (match remaining
137       (((? keyword? key) val rest ...)
138        (loop rest
139              (cons (cons (keyword->symbol key) val) 
140                    result)))
141       (() result)
142       (_ (throw 'invalid-kwarg-list
143                 "Invalid keyword argument list"
144                 args)))))
145
146
147 ;;; See: https://web.archive.org/web/20081223021934/http://mumble.net/~jar/articles/oo-moon-weinreb.html
148 ;;;   (also worth seeing: http://mumble.net/~jar/articles/oo.html )
149
150 ;; This is the internal, generalized message sending method.
151 ;; Users shouldn't use it!  Use the <-foo forms instead.
152
153 (define-inlinable (%<- wants-reply from-actor to action args message-id in-reply-to)
154   ;; Okay, we need to deal with message ids.
155   ;; Could we get rid of them? :\
156   ;; It seems if we can use eq? and have messages be immutable then
157   ;; it should be possible to identify follow-up replies.
158   ;; If we need to track replies across hive boundaries we could
159   ;; register unique ids across the ambassador barrier.
160   (match to
161     (#(_ _ (? channel? channel) dead?)
162      (let ((message (make-message message-id to
163                                   (and from-actor (actor-id from-actor))
164                                   action args
165                                   #:wants-reply wants-reply
166                                   #:in-reply-to in-reply-to)))
167        (perform-operation
168         (choice-operation
169          (put-operation channel message)
170          (wait-operation dead?)))))
171     ;; TODO: put remote addresses here.
172     (#(actor-id hive-id #f #f)
173      ;; Here we'd make a call to our hive...
174      'TODO)
175     ;; A message sent to nobody goes nowhere.
176     ;; TODO: Should we display a warning here, probably?
177     (#f #f)))
178
179 (define (<- to action . args)
180   (define from-actor (*current-actor*))
181   (%<- #f from-actor to action args
182        (or (and from-actor
183                 ((actor-msg-id-generator from-actor)))
184            (big-random-number-string))
185        #f))
186
187 ;; TODO: this should abort to the prompt, then check for errors
188 ;;   when resuming.
189
190 (define (<-wait to action . args)
191   (define prompt (*actor-prompt*))
192   (when (not prompt)
193     (error "Tried to <-wait without being in an actor's context..."))
194
195   (let ((reply (abort-to-prompt prompt to action args)))
196     (cond ((eq? action '*error*)
197            (throw 'hive-unresumable-coroutine
198                   "Won't resume coroutine; got an *error* as a reply"
199                   #:message reply))
200           (else (apply values (message-body reply))))))
201
202 \f
203 ;;; Main actor implementation
204 ;;; =========================
205
206 (define (actor-inheritable-message-handler actor message)
207   (define action (message-action message))
208   (define method
209     (class-rmeta-ref (class-of actor) 'actions action
210                      #:equals? eq? #:cache-set! hashq-set!
211                      #:cache-ref hashq-ref))
212   (unless method
213     (throw 'action-not-found
214            "No appropriate action handler found for actor"
215            #:action action
216            #:actor actor
217            #:message message))
218   (apply method actor message (message-body message)))
219
220 (define-syntax-rule (build-actions (symbol method) ...)
221   "Construct an alist of (symbol . method), where the method is wrapped
222 with wrap-apply to facilitate live hacking and allow the method definition
223 to come after class definition."
224   (build-rmeta-slot
225    (list (cons (quote symbol)
226                (wrap-apply method)) ...)))
227
228 (define-class <actor> ()
229   ;; An address object... a vector of #(actor-id hive-id inbox-channel dead?)
230   ;;  - inbox-channel is the receiving channel (as opposed to actor-inbox-deq)
231   ;;  - dead? is a fibers condition variable which is set once this actor
232   ;;    kicks the bucket
233   (id #:init-keyword #:address
234       #:getter actor-id)
235   ;; The connection to the hive we're connected to.
236   (hive-channel #:init-keyword #:hive-channel
237                 #:accessor actor-hive-channel)
238
239   ;; Our queue to send/receive messages on
240   (inbox-deq #:init-thunk make-channel
241              #:accessor actor-inbox-deq)
242
243   (msg-id-generator #:init-thunk simple-message-id-generator
244                     #:getter actor-msg-id-generator)
245
246   ;; How we receive and process new messages
247   (message-handler #:init-value actor-inheritable-message-handler
248                    ;; @@: There's no reason not to use #:class instead of
249                    ;;   #:each-subclass anywhere in this file, except for
250                    ;;   Guile bug #25211 (#:class is broken in Guile 2.2)
251                    #:allocation #:each-subclass
252                    #:getter actor-message-handler)
253
254   ;; valid values are:
255   ;;  - #t as in, send the init message, but don't wait (default)
256   ;;  - 'wait, as in wait on the init message
257   ;;  - #f as in don't bother to init
258   (should-init #:init-value #t
259                #:getter actor-should-init
260                #:allocation #:each-subclass)
261
262   ;; This is the default, "simple" way to inherit and process messages.
263   (actions #:init-thunk (build-actions)
264            #:allocation #:each-subclass))
265
266 ;;; Actors may specify an "init" action that occurs before the actor
267 ;;; actually begins to run.
268 ;;; During actor-init!, an actor may send a message to itself or others
269 ;;; via <- but *may not* use <-wait.
270 (define-method (actor-init! (actor <actor>))
271   'no-op)
272
273 (define-method (actor-cleanup! (actor <actor>))
274   'no-op)
275
276 ;;; Addresses are vectors where the first part is the actor-id and
277 ;;; the second part is the hive-id.  This works well enough... they
278 ;;; look decent being pretty-printed.
279
280 (define (make-address actor-id hive-id channel dead?)
281   (vector actor-id hive-id channel dead?))
282
283 (define (address-actor-id address)
284   (vector-ref address 0))
285
286 (define (address-hive-id address)
287   (vector-ref address 1))
288
289 (define (address-channel address)
290   (vector-ref address 2))
291
292 (define (address-dead? address)
293   (vector-ref address 3))
294
295 (define (address->string address)
296   (string-append (address-actor-id address) "@"
297                  (address-hive-id address)))
298
299 (define (address-equal? address1 address2)
300   "Check whether or not the two addresses are equal.
301
302 This compares the actor-id and hive-id but ignores the channel and
303 dead? condition."
304   (match address1
305     (#(actor-id-1 hive-id-1 _ _)
306      (match address2
307        (#(actor-id-2 hive-id-2)
308         (and (equal? actor-id-1 actor-id-2)
309              (and (equal? hive-id-1 hive-id-2))))
310        (_ #f)))
311     (_ #f)))
312
313 (define (actor-id-actor actor)
314   "Get the actor id component of the actor-id"
315   (address-actor-id (actor-id actor)))
316
317 (define (actor-id-hive actor)
318   "Get the hive id component of the actor-id"
319   (address-hive-id (actor-id actor)))
320
321 (define (actor-id-string actor)
322   "Render the full actor id as a human-readable string"
323   (address->string (actor-id actor)))
324
325 (define (actor-inbox-enq actor)
326   (address-channel (actor-id actor)))
327
328 (define *current-actor*
329   (make-parameter #f))
330
331 (define *actor-prompt*
332   (make-parameter #f))
333
334 (define (actor-main-loop actor)
335   "Main loop of the actor.  Loops around, pulling messages off its queue
336 and handling them."
337   ;; @@: Maybe establish some sort of garbage collection routine for these...
338   (define waiting
339     (make-hash-table))
340   (define message-handler
341     (actor-message-handler actor))
342   (define dead?
343     (address-dead? (actor-id actor)))
344   (define prompt (make-prompt-tag (actor-id-actor actor)))
345
346   (define (handle-message message)
347     (catch #t
348       (lambda ()
349         (call-with-values
350             (lambda ()
351               (message-handler actor message))
352           (lambda vals
353             ;; Return reply if necessary
354             (when (message-wants-reply message)
355               (when (message-wants-reply message)
356                 (%<- #f actor (message-from message) '*reply*
357                      vals ((actor-msg-id-generator actor))
358                      (message-id message)))))))
359       (const #t)
360       (let ((err (current-error-port)))
361         (lambda (key . args)
362           (false-if-exception
363            (let ((stack (make-stack #t 4)))
364              (format err "Uncaught exception when handling message ~a:\n"
365                      message)
366              (display-backtrace stack err)
367              (print-exception err (stack-ref stack 0)
368                               key args)
369              (newline err)
370              ;; If the other actor is waiting on a reply, let's let them
371              ;; know there was an error...
372              (when (message-wants-reply message)
373                (%<- #f actor (message-from message) '*error*
374                     (list key) ((actor-msg-id-generator actor))
375                     (message-id message)))))))))
376   
377   (define (resume-handler message)
378     (define in-reply-to (message-in-reply-to message))
379     (cond
380      ((hash-ref waiting in-reply-to) =>
381       (lambda (kont)
382         (hash-remove! waiting in-reply-to)
383         (kont message)))
384      (else
385       (format (current-error-port)
386               "Tried to resume nonexistant message: ~a\n"
387               (message-id message)))))
388
389   (define halt-or-handle-message
390     ;; It would be nice if we could give priorities to certain operations.
391     ;; halt should always win over getting a message...
392     (choice-operation
393      (wrap-operation (wait-operation dead?)
394                      (const #f))  ; halt and return
395      (wrap-operation (get-operation (actor-inbox-deq actor))
396                      (lambda (message)
397                        (call-with-prompt prompt
398                          (lambda ()
399                            (if (message-in-reply-to message)
400                                ;; resume a continuation which was waiting on a reply
401                                (resume-handler message)
402                                ;; start handling a new message
403                                (handle-message message)))
404                          ;; Here's where we abort to if we're doing <-wait
405                          ;; @@: maybe use match-lambda if we're going to end up
406                          ;;   handling multiple ~commands
407                          (lambda (kont to action message-args)
408                            (define message-id
409                              ((actor-msg-id-generator actor)))
410                            (hash-set! waiting message-id kont)
411                            (%<- #t actor to action message-args message-id #f)))
412                        #t))))   ; loop again
413
414   ;; Mutate the parameter; this should be fine since each fiber
415   ;; runs in its own dynamic state with with-dynamic-state.
416   ;; See with-dynamic-state discussion in
417   ;;   https://wingolog.org/archives/2017/06/27/growing-fibers
418   (*current-actor* actor)
419   ;; We temporarily set the *actor-prompt* to #f to make sure that
420   ;; actor-init! doesn't try to do a <-wait message (and not accidentally use
421   ;; the parent fiber's *actor-prompt* either.)
422   (*actor-prompt* #f)
423   (actor-init! actor)
424   (*actor-prompt* prompt)
425
426   (let loop ()
427     (and (perform-operation halt-or-handle-message)
428          (loop))))
429
430 \f
431 ;;; Actor utilities
432 ;;; ===============
433
434 (define-syntax-rule (define-actor class inherits
435                       (action ...)
436                       slots ...)
437   (define-class class inherits
438     (actions #:init-thunk (build-actions action ...)
439              #:allocation #:each-subclass)
440     slots ...))
441
442 \f
443 ;;; The Hive
444 ;;; ========
445 ;;;   Every actor has a hive, which keeps track of other actors, manages
446 ;;;   cleanup, and performs inter-hive communication.
447
448 (define-class <hive> ()
449   (id #:init-keyword #:id
450       #:getter hive-id)
451   (actor-registry #:init-thunk make-hash-table
452                   #:getter hive-actor-registry)
453   ;; TODO: Rename "ambassadors" to "relays"
454   ;; Ambassadors are used (or will be) for inter-hive communication.
455   ;; These are special actors that know how to route messages to other
456   ;; hives.
457   (ambassadors #:init-thunk make-weak-key-hash-table
458                #:getter hive-ambassadors)
459   (channel #:init-thunk make-channel
460            #:getter hive-channel)
461   (halt? #:init-thunk make-condition
462          #:getter hive-halt?))
463
464 (define* (make-hive #:key hive-id)
465   (make <hive> #:id (or hive-id
466                         (big-random-number-string))))
467
468 (define (gen-actor-id cookie)
469   (if cookie
470       (string-append cookie ":" (big-random-number-string))
471       (big-random-number-string)))
472
473 (define (hive-main-loop hive)
474   "The main loop of the hive.  This listens for messages on the hive-channel
475 for certain actions to perform.
476
477 `messages' here is not the same as a <message> object; these are a list of
478 values, the first value being a symbol"
479   (define channel (hive-channel hive))
480   (define halt? (hive-halt? hive))
481   (define registry (hive-actor-registry hive))
482
483   ;; not the same as a <message> ;P
484   (define handle-message
485     (match-lambda
486       (('register-actor actor-id address actor)
487        (hash-set! registry actor-id (vector address actor)))
488       ;; Remove the actor from hive
489       (('remove-actor actor-id)
490        (hash-remove! (hive-actor-registry hive) actor-id))
491       (('register-ambassador hive-id ambassador-actor-id)
492        'TODO)
493       (('unregister-ambassador hive-id ambassador-actor-id)
494        'TODO)
495       (('forward-message from-actor-id message)
496        'TODO)))
497
498   (define halt-or-handle
499     (choice-operation
500      (wrap-operation (get-operation channel)
501                      (lambda (msg)
502                        (handle-message msg)
503                        #t))
504      (wrap-operation (wait-operation halt?)
505                      (const #f))))
506
507   (let lp ()
508     (and (perform-operation halt-or-handle)
509          (lp))))
510
511 (define *current-hive* (make-parameter #f))
512
513 (define* (spawn-hive proc #:key (hive (make-hive)))
514   "Spawn a hive in a fiber running PROC, passing it the fresh hive"
515   (spawn-fiber (lambda () (hive-main-loop hive)))
516   (proc hive))
517
518 (define (run-hive proc . args)
519   "Spawn a hive and run it in run-fibers.  Takes a PROC as would be passed
520 to spawn-hive... all remaining arguments passed to run-fibers."
521   (apply run-fibers
522          (lambda ()
523            (spawn-hive proc))
524          args))
525
526 (define (%create-actor hive-channel hive-id
527                        actor-class init-args id-cookie send-init?)
528   (let* ((actor-id (gen-actor-id id-cookie))
529          (dead? (make-condition))
530          (inbox-enq (make-channel))
531          (address (make-address actor-id hive-id
532                                 inbox-enq dead?))
533          (actor (apply make actor-class
534                        #:hive-channel hive-channel
535                        #:address address
536                        init-args))
537          (should-init (actor-should-init actor)))
538
539     ;; start the main loop
540     (spawn-fiber (lambda ()
541                    ;; start the inbox loop
542                    (spawn-fiber
543                     (lambda ()
544                       (delivery-agent inbox-enq (actor-inbox-deq actor)
545                                       dead?))
546                     ;; this one is decidedly non-parallel, because we want
547                     ;; the delivery agent to be in the same thread as its actor
548                     #:parallel? #f)
549
550                    (actor-main-loop actor))
551                  #:parallel? #t)
552
553     (put-message hive-channel (list 'register-actor actor-id address actor))
554     
555     ;; return the address
556     address))
557
558 (define* (bootstrap-actor hive actor-class #:rest init-args)
559   "Create an actor on HIVE using ACTOR-CLASS passing in INIT-ARGS args"
560   (%create-actor (hive-channel hive) (hive-id hive) actor-class
561                  init-args (symbol->string (class-name actor-class))
562                  #f))
563
564 (define* (bootstrap-actor* hive actor-class id-cookie #:rest init-args)
565   "Create an actor, but also allow customizing a 'cookie' added to the id
566 for debugging"
567   (%create-actor (hive-channel hive) (hive-id hive) actor-class
568                  init-args id-cookie
569                  #f))
570
571 (define* (create-actor from-actor actor-class #:rest init-args)
572   "Create an instance of actor-class.  Return the new actor's id.
573
574 This is the method actors should call directly (unless they want
575 to supply an id-cookie, in which case they should use
576 create-actor*)."
577   (%create-actor (actor-hive-channel from-actor) (actor-id-hive from-actor)
578                  actor-class init-args #f #t))
579
580
581 (define* (create-actor* from-actor actor-class id-cookie #:rest init-args)
582   "Create an instance of actor-class.  Return the new actor's id.
583
584 Like create-actor, but permits supplying an id-cookie."
585   (%create-actor (actor-hive-channel from-actor) (actor-id-hive from-actor)
586                  actor-class init-args id-cookie #t))
587
588 (define* (self-destruct actor #:key (cleanup #t))
589   "Remove an actor from the hive.
590
591 Unless #:cleanup is set to #f, this will first have the actor handle
592 its '*cleanup* action handler."
593   (signal-condition! (address-dead? (actor-id actor)))
594   (put-message (actor-hive-channel actor) (list 'remove-actor (actor-id-actor actor)))
595   ;; Set *actor-prompt* to nothing to prevent actor-cleanup! from sending
596   ;; a message with <-wait
597   (*actor-prompt* #f)
598   (actor-cleanup! actor))
599
600 ;; From a patch I sent to Fibers...
601 (define (condition-signalled? cvar)
602   "Return @code{#t} if @var{cvar} has already been signalled.
603
604 In general you will want to use @code{wait} or @code{wait-operation} to
605 wait on a condition.  However, sometimes it is useful to see whether or
606 not a condition has already been signalled without blocking."
607   (atomic-box-ref ((@@ (fibers conditions) condition-signalled?) cvar)))
608
609 (define (actor-alive? actor)
610   (condition-signalled? (address-dead? (actor-id actor))))