1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
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.
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.
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/>.
19 ;; XUDD inspired actor system
21 (define-module (8sync systems actors)
22 #:use-module (oop goops)
23 #:use-module (srfi srfi-9)
24 #:use-module (srfi srfi-9 gnu)
25 #:use-module (ice-9 control)
26 #:use-module (ice-9 format)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 pretty-print)
29 #:use-module (8sync agenda)
30 #:use-module (8sync repl)
31 #:export (;; utilities... ought to go in their own module
33 big-random-number-string
34 simple-message-id-generator
42 ;;; Commenting out the <address> type for now;
43 ;;; it may be back when we have better serializers
46 address-actor-id address-hive-id
53 simple-dispatcher build-actions make-action-dispatch
58 ;; There are more methods for the hive, but there's
59 ;; no reason for the outside world to look at them maybe?
61 hive-create-actor hive-create-actor*
63 create-actor create-actor*
68 message-to message-action message-from
69 message-id message-body message-in-reply-to
74 <- <-wait <-reply <-reply-wait
76 call-with-message msg-receive msg-val
81 serialize-message write-message
82 serialize-message-pretty pprint-message
83 read-message read-message-from-string))
87 (make-parameter (random-state-from-platform)))
89 ;; Same size as a uuid4 I think...
90 (define random-number-size (expt 2 128))
92 (define (big-random-number)
93 (random random-number-size (%random-state)))
95 ;; Would be great to get this base64 encoded instead.
96 (define (big-random-number-string)
97 ;; @@: This is slow. Using format here is wasteful.
98 (format #f "~x" (big-random-number)))
100 ;; @@: This is slow. A mere ~275k / second on my (old) machine.
101 ;; The main cost seems to be in number->string.
102 (define (simple-message-id-generator)
103 ;; Prepending this cookie makes message ids unique per hive
104 (let ((prefix (format #f "~x:" (big-random-number)))
107 (set! counter (1+ counter))
108 (string-append prefix (number->string counter)))))
116 ;; @@: We may want to add a deferred-reply to the below, similar to
117 ;; what we had in XUDD, for actors which do their own response
118 ;; queueing.... ie, that might receive messages but need to shelve
119 ;; them to be acted upon after something else is taken care of.
121 (define-record-type <message>
122 (make-message-intern id to from action
123 body in-reply-to wants-reply
129 (action message-action)
131 (in-reply-to message-in-reply-to)
132 (wants-reply message-wants-reply)
133 (replied message-replied set-message-replied!))
136 (define* (make-message id to from action body
137 #:key in-reply-to wants-reply
139 (make-message-intern id to from action body
140 in-reply-to wants-reply replied))
142 (define (message-auto-reply? message)
143 (eq? (message-action message) '*auto-reply*))
145 (define (message-needs-reply? message)
146 "See if this message needs a reply still"
147 (and (message-wants-reply message)
148 (not (message-replied message))))
151 (define (kwarg-list-to-alist args)
152 (let loop ((remaining args)
155 (((? keyword? key) val rest ...)
157 (cons (cons (keyword->symbol key) val)
160 (_ (throw 'invalid-kwarg-list
161 "Invalid keyword argument list"
165 ;;; See: https://web.archive.org/web/20081223021934/http://mumble.net/~jar/articles/oo-moon-weinreb.html
166 ;;; (also worth seeing: http://mumble.net/~jar/articles/oo.html )
168 (define (<- from-actor to-id action . message-body-args)
169 "Send a message from an actor to another actor"
170 (let* ((hive (actor-hive from-actor))
171 (message (make-message (hive-gen-message-id hive) to-id
172 (actor-id from-actor) action
174 (8sync (hive-process-message hive message))))
176 (define (<-wait from-actor to-id action . message-body-args)
177 "Send a message from an actor to another, but wait until we get a response"
178 (let* ((hive (actor-hive from-actor))
179 (abort-to (hive-prompt (actor-hive from-actor)))
180 (message (make-message (hive-gen-message-id hive) to-id
181 (actor-id from-actor) action
184 (abort-to-prompt abort-to from-actor message)))
186 ;; TODO: Intelligently ~propagate(ish) errors on -wait functions.
187 ;; We might have `send-message-wait-brazen' to allow callers to
188 ;; not have an exception thrown and instead just have a message with
189 ;; the appropriate '*error* message returned.
191 (define (<-reply from-actor original-message . message-body-args)
193 (set-message-replied! original-message #t)
194 (let* ((hive (actor-hive from-actor))
195 (new-message (make-message (hive-gen-message-id hive)
196 (message-from original-message)
197 (actor-id from-actor) '*reply*
199 #:in-reply-to (message-id original-message))))
200 (8sync (hive-process-message hive new-message))))
202 (define (<-auto-reply from-actor original-message)
203 "Auto-reply to a message. Internal use only!"
204 (set-message-replied! original-message #t)
205 (let* ((hive (actor-hive from-actor))
206 (new-message (make-message (hive-gen-message-id hive)
207 (message-from original-message)
208 (actor-id from-actor) '*auto-reply*
210 #:in-reply-to (message-id original-message))))
211 (8sync (hive-process-message hive new-message))))
213 (define (<-reply-wait from-actor original-message . message-body-args)
214 "Reply to a messsage, but wait until we get a response"
215 (set-message-replied! original-message #t)
216 (let* ((hive (actor-hive from-actor))
217 (abort-to (hive-prompt (actor-hive from-actor)))
218 (new-message (make-message (hive-gen-message-id hive)
219 (message-from original-message)
220 (actor-id from-actor) '*reply*
223 #:in-reply-to (message-id original-message))))
224 (abort-to-prompt abort-to from-actor new-message)))
228 ;;; Main actor implementation
229 ;;; =========================
231 (define (actor-inheritable-message-handler actor message)
232 (define action (message-action message))
233 (define (find-message-handler return)
234 (for-each (lambda (this-class)
236 (or (and (class-slot-definition this-class 'actions)
237 (class-slot-ref this-class 'actions))
239 (for-each (match-lambda
240 ((action-name . method)
241 (when (eq? action-name action)
244 (class-precedence-list (class-of actor)))
245 (throw 'action-not-found
246 "No appropriate action handler found for actor"
251 (call/ec find-message-handler))
252 (apply method actor message (message-body message)))
254 (define-class <actor> ()
256 (id #:init-keyword #:id
258 ;; The hive we're connected to.
259 ;; We need this to be able to send messages.
260 (hive #:init-keyword #:hive
261 #:accessor actor-hive)
262 ;; How we receive and process new messages
263 (message-handler #:init-value actor-inheritable-message-handler
264 ;; @@: There's no reason not to use #:class instead of
265 ;; #:each-subclass anywhere in this file, except for
266 ;; Guile bug #25211 (#:class is broken in Guile 2.2)
267 #:allocation #:each-subclass)
269 ;; This is the default, "simple" way to inherit and process messages.
270 (actions #:init-value '()
271 #:allocation #:each-subclass))
273 (define-method (actor-message-handler (actor <actor>))
274 (slot-ref actor 'message-handler))
276 ;;; So these are the nicer representations of addresses.
277 ;;; However, they don't serialize so easily with scheme read/write, so we're
278 ;;; using the simpler cons cell version below for now.
280 ;; (define-record-type <address>
281 ;; (make-address actor-id hive-id) ; @@: Do we want the trailing -id?
283 ;; (actor-id address-actor-id)
284 ;; (hive-id address-hive-id))
286 ;; (set-record-type-printer!
288 ;; (lambda (record port)
289 ;; (format port "<address: ~s@~s>"
290 ;; (address-actor-id record) (address-hive-id record))))
293 (define (make-address actor-id hive-id)
294 (cons actor-id hive-id))
296 (define (address-actor-id address)
299 (define (address-hive-id address)
302 (define (address->string address)
303 (string-append (address-actor-id address) "@"
304 (address-hive-id address)))
306 (define-method (actor-id-actor (actor <actor>))
307 "Get the actor id component of the actor-id"
308 (address-actor-id (actor-id actor)))
310 (define-method (actor-id-hive (actor <actor>))
311 "Get the hive id component of the actor-id"
312 (address-hive-id (actor-id actor)))
314 (define-method (actor-id-string (actor <actor>))
315 "Render the full actor id as a human-readable string"
316 (address->string (actor-id actor)))
318 (define %current-actor
326 (define (simple-dispatcher action-map)
327 (lambda (actor message)
328 (let* ((action (message-action message))
329 (method (assoc-ref action-map action)))
331 ;; @@: There's every possibility this should be handled in
332 ;; hive-process-message instead.
333 (throw 'action-not-found
334 "No appropriate action handler found for actor"
338 #:available-actions (map car action-map)))
339 (apply method actor message (message-body message)))))
341 (define-syntax %expand-action-item
343 ((_ (action-name handler))
344 (cons (quote action-name) handler))))
346 (define-syntax-rule (build-actions action-item ...)
347 "Build a mapping of actions. Same syntax as make-action-dispatch
348 but this doesn't build the dispatcher for you (you probably want to
349 pass it to simple-dispatcher).
351 The advantage here is that since this simply builds an alist, you can
352 compose it with other action maps."
353 (list (%expand-action-item action-item) ...))
355 (define-syntax make-action-dispatch
357 "Expand a list of action names and actions into an alist
359 You can use this like the following:
360 (make-action-dispatch
362 (lambda (actor message)
363 (display \"I love cookies!\n\")))
365 (lambda (actor message)
366 (display \"Life of the party!\"))))
368 Alternately, if you'd like to skip the lambda, you could use the slightly
369 more compact following syntax:
370 (make-action-dispatch
371 ((cookies actor message)
372 (display \"I love cookies!\n\"))
373 ((party actor message)
374 (display \"Life of the party!\")))"
375 ((make-action-dispatch action-item ...)
376 (simple-dispatcher (build-actions action-item ...)))))
378 (define-syntax-rule (define-simple-actor class actions ...)
379 (define-class class (<actor>)
381 #:init-value (make-action-dispatch actions ...)
382 #:allocation #:each-subclass)))
387 ;;; Every actor has a hive. The hive is a kind of "meta-actor"
388 ;;; which routes all the rest of the actors in a system.
390 (define-generic hive-handle-failed-forward)
392 (define-class <hive> (<actor>)
393 (actor-registry #:init-thunk make-hash-table
394 #:getter hive-actor-registry)
395 (msg-id-generator #:init-thunk simple-message-id-generator
396 #:getter hive-msg-id-generator)
397 ;; Ambassadors are used (or will be) for inter-hive communication.
398 ;; These are special actors that know how to route messages to other hives.
399 (ambassadors #:init-thunk make-weak-key-hash-table
400 #:getter hive-ambassadors)
401 ;; Waiting coroutines
402 ;; This is a map from cons cell of message-id
403 ;; to a cons cell of (actor-id . coroutine)
404 ;; @@: Should we have a <waiting-coroutine> record type?
405 ;; @@: Should there be any way to clear out "old" coroutines?
406 (waiting-coroutines #:init-thunk make-hash-table
407 #:getter hive-waiting-coroutines)
409 ;; When actors send messages to each other they abort to this prompt
410 ;; to send the message, then carry on their way
411 (prompt #:init-thunk make-prompt-tag
412 #:getter hive-prompt)
415 (make-action-dispatch
416 ;; This is in the case of an ambassador failing to forward a message...
417 ;; it reports it back to the hive
418 (*failed-forward* hive-handle-failed-forward))))
420 (define-method (hive-handle-failed-forward (hive <hive>) message)
421 "Handle an ambassador failing to forward a message"
424 (define* (make-hive #:key hive-id)
425 (let ((hive (make <hive>
428 (big-random-number-string))))))
429 ;; Set the hive's actor reference to itself
430 (set! (actor-hive hive) hive)
433 (define-method (hive-id (hive <hive>))
434 (actor-id-hive hive))
436 (define-method (hive-gen-actor-id (hive <hive>) cookie)
437 (make-address (if cookie
438 (string-append cookie "-" (big-random-number-string))
439 (big-random-number-string))
442 (define-method (hive-gen-message-id (hive <hive>))
443 "Generate a message id using HIVE's message id generator"
444 ((hive-msg-id-generator hive)))
446 (define-method (hive-resolve-local-actor (hive <hive>) actor-address)
447 (hash-ref (hive-actor-registry hive) actor-address))
449 (define-method (hive-resolve-ambassador (hive <hive>) ambassador-address)
450 (hash-ref (hive-ambassadors hive) ambassador-address))
452 (define-method (make-forward-request (hive <hive>) (ambassador <actor>) message)
453 (make-message (hive-gen-message-id hive) (actor-id ambassador)
454 ;; If we make the hive not an actor, we could either switch this
455 ;; to #f or to the original actor...?
456 ;; Maybe some more thinking should be done on what should
457 ;; happen in case of failure to forward? Handling ambassador failures
458 ;; seems like the primary motivation for the hive remaining an actor.
461 `((original . ,message))))
463 (define-method (hive-reply-with-error (hive <hive>) original-message
464 error-key error-args)
465 ;; We only supply the error-args if the original sender is on the same hive
466 (define (orig-actor-on-same-hive?)
467 (equal? (hive-id hive)
468 (address-hive-id (message-from original-message))))
469 (set-message-replied! original-message #t)
470 (let* ((new-message-body
471 (if (orig-actor-on-same-hive?)
472 `(#:original-message ,original-message
473 #:error-key ,error-key
474 #:error-args ,error-args)
475 `(#:original-message ,original-message
476 #:error-key ,error-key)))
477 (new-message (make-message (hive-gen-message-id hive)
478 (message-from original-message)
479 (actor-id hive) '*error*
481 #:in-reply-to (message-id original-message))))
482 ;; We only return a thunk, rather than run 8sync here, because if
483 ;; we ran 8sync in the middle of a catch we'd end up with an
484 ;; unresumable continuation.
485 (lambda () (hive-process-message hive new-message))))
487 (define-method (hive-process-message (hive <hive>) message)
488 "Handle one message, or forward it via an ambassador"
489 (define (maybe-autoreply actor)
490 ;; Possibly autoreply
491 (if (message-needs-reply? message)
492 (<-auto-reply actor message)))
494 (define (resolve-actor-to)
495 "Get the actor the message was aimed at"
496 (let ((actor (hive-resolve-local-actor hive (message-to message))))
498 (throw 'actor-not-found
499 (format #f "Message ~a from ~a directed to nonexistant actor ~a"
501 (address->string (message-from message))
502 (address->string (message-to message)))
506 (define (call-catching-coroutine thunk)
507 (define queued-error-handling-thunk #f)
508 (define (call-catching-errors)
509 ;; TODO: maybe parameterize (or attach to hive) and use
510 ;; maybe-catch-all from agenda.scm
511 ;; @@: Why not just use with-throw-handler and let the catch
512 ;; happen at the agenda? That's what we used to do, but
513 ;; it ended up with a SIGABRT. See:
514 ;; http://lists.gnu.org/archive/html/bug-guile/2016-05/msg00003.html
517 ;; In the actor model, we don't totally crash on errors.
519 ;; If an error happens, we raise it
521 (if (message-needs-reply? message)
522 ;; If the message is waiting on a reply, let them know
523 ;; something went wrong.
524 ;; However, we have to do it outside of this catch
525 ;; routine, or we'll end up in an unrewindable continuation
527 (set! queued-error-handling-thunk
528 (hive-reply-with-error hive message key args)))
529 ;; print error message
530 (apply print-error-and-continue key args)))
531 ;; @@: This is a kludge. See above for why.
532 (if queued-error-handling-thunk
533 (8sync (queued-error-handling-thunk))))
534 (call-with-prompt (hive-prompt hive)
536 (lambda (kont actor message)
537 ;; Register the coroutine
538 (hash-set! (hive-waiting-coroutines hive)
540 (cons (actor-id actor) kont))
541 ;; Send off the message
542 (8sync (hive-process-message hive message)))))
544 (define (process-local-message)
545 (let ((actor (resolve-actor-to)))
546 (call-catching-coroutine
548 (define message-handler (actor-message-handler actor))
549 ;; @@: Should a more general error handling happen here?
550 (parameterize ((%current-actor actor))
552 (message-handler actor message)))
553 (maybe-autoreply actor)
554 ;; Returning result allows actors to possibly make a run-request
555 ;; at the end of handling a message.
556 ;; ... We do want that, right?
559 (define (resume-waiting-coroutine)
561 ((or (eq? (message-action message) '*reply*)
562 (eq? (message-action message) '*auto-reply*))
563 (call-catching-coroutine
565 (match (hash-remove! (hive-waiting-coroutines hive)
566 (message-in-reply-to message))
567 ((_ . (resume-actor-id . kont))
568 (if (not (equal? (message-to message)
570 (throw 'resuming-to-wrong-actor
571 "Attempted to resume a coroutine to the wrong actor!"
572 #:expected-actor-id (message-to message)
573 #:got-actor-id resume-actor-id
575 (let (;; @@: How should we resolve resuming coroutines to actors who are
577 (actor (resolve-actor-to))
578 (result (kont message)))
579 (maybe-autoreply actor)
581 (#f (throw 'no-waiting-coroutine
582 "message in-reply-to tries to resume nonexistent coroutine"
584 ;; Yikes, we must have gotten an error or something back
586 ;; @@: Not what we want in the long run?
587 ;; What we'd *prefer* to do is to resume this message
588 ;; and throw an error inside the message handler
589 ;; (say, from send-mesage-wait), but that causes a SIGABRT (??!!)
590 (hash-remove! (hive-waiting-coroutines hive)
591 (message-in-reply-to message))
593 (if (eq? (message-action message) '*reply*)
594 "Won't resume coroutine; got an *error* as a reply"
595 "Won't resume coroutine because action is not *reply*")))
596 (throw 'hive-unresumable-coroutine
598 #:message message)))))
600 (define (process-remote-message)
601 ;; Find the ambassador
602 (let* ((remote-hive-id (hive-id (message-to message)))
603 (ambassador (hive-resolve-ambassador remote-hive-id))
604 (message-handler (actor-message-handler ambassador))
605 (forward-request (make-forward-request hive ambassador message)))
606 (message-handler ambassador forward-request)))
608 (let ((to (message-to message)))
609 ;; This seems to be an easy mistake to make, so check that addressing
612 (throw 'missing-addressee
613 "`to' field is missing on message"
615 (if (hive-actor-local? hive to)
616 (if (message-in-reply-to message)
617 (resume-waiting-coroutine)
618 (process-local-message))
619 (process-remote-message))))
621 (define-method (hive-actor-local? (hive <hive>) address)
622 (equal? (hive-id hive) (address-hive-id address)))
624 (define-method (hive-register-actor! (hive <hive>) (actor <actor>))
625 (hash-set! (hive-actor-registry hive) (actor-id actor) actor))
627 (define-method (%hive-create-actor (hive <hive>) actor-class
629 "Actual method called by hive-create-actor.
631 Since this is a define-method it can't accept fancy define* arguments,
632 so this gets called from the nicer hive-create-actor interface. See
633 that method for documentation."
634 (let* ((actor-id (hive-gen-actor-id hive id-cookie))
635 (actor (apply make actor-class
639 (hive-register-actor! hive actor)
640 ;; return the actor id
643 (define* (hive-create-actor hive actor-class #:rest init)
644 (%hive-create-actor hive actor-class
647 (define* (hive-create-actor* hive actor-class id-cookie #:rest init)
648 (%hive-create-actor hive actor-class
651 (define (call-with-message message proc)
652 "Applies message body arguments into procedure, with message as first
653 argument. Similar to call-with-values in concept."
654 (apply proc message (message-body message)))
656 ;; (msg-receive (<- bar baz)
660 ;; Emacs: (put 'msg-receive 'scheme-indent-function 2)
662 ;; @@: Or receive-msg or receieve-message or??
663 (define-syntax-rule (msg-receive arglist message body ...)
664 "Call body with arglist (which can accept arguments like lambda*)
665 applied from the message-body of message."
666 (call-with-message message
670 (define (msg-val message)
671 "Retrieve the first value from the message-body of message.
672 Like single value return from a procedure call. Probably the most
673 common case when waiting on a reply from some action invocation."
674 (call-with-message message
675 (lambda (_ val) val)))
678 ;;; Various API methods for actors to interact with the system
679 ;;; ==========================================================
681 ;; TODO: move send-message and friends here...?
683 (define* (create-actor from-actor actor-class #:rest init)
684 "Create an instance of actor-class. Return the new actor's id.
686 This is the method actors should call directly (unless they want
687 to supply an id-cookie, in which case they should use
689 (%hive-create-actor (actor-hive from-actor) actor-class
693 (define* (create-actor* from-actor actor-class id-cookie #:rest init)
694 "Create an instance of actor-class. Return the new actor's id.
696 Like create-actor, but permits supplying an id-cookie."
697 (%hive-create-actor (actor-hive from-actor) actor-class
701 (define (self-destruct actor)
702 "Remove an actor from the hive."
703 (hash-remove! (hive-actor-registry (actor-hive actor))
708 ;;; 8sync bootstrap utilities
709 ;;; =========================
711 (define* (ez-run-hive hive initial-tasks #:key repl-server)
712 "Start up an agenda and run HIVE in it with INITIAL-TASKS.
714 Should we start up a cooperative REPL for live hacking? REPL-SERVER
715 wants to know! You can pass it #t or #f, or if you want to specify a port,
717 (let* ((queue (list->q initial-tasks))
718 (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
721 ;; If repl-server is an integer, we'll use that as the port
722 ((integer? repl-server)
723 (spawn-and-queue-repl-server! agenda repl-server))
725 (spawn-and-queue-repl-server! agenda)))
726 (start-agenda agenda)))
728 (define (bootstrap-message hive to-id action . message-body-args)
730 (apply <- hive to-id action message-body-args)))
734 ;;; Basic readers / writers
735 ;;; =======================
737 (define (serialize-message message)
738 "Serialize a message for read/write"
742 (message-from message)
743 (message-action message)
744 (message-body message)
745 (message-in-reply-to message)
746 (message-wants-reply message)
747 (message-replied message)))
749 (define* (write-message message #:optional (port (current-output-port)))
750 "Write out a message to a port for easy reading later.
752 Note that if a sub-value can't be easily written to something
753 Guile's `read' procedure knows how to read, this doesn't do anything
754 to improve that. You'll need a better serializer for that.."
755 (write (serialize-message message) port))
757 (define (serialize-message-pretty message)
758 "Serialize a message in a way that's easy for humans to read."
760 (id ,(message-id message))
761 (to ,(message-to message))
762 (from ,(message-from message))
763 (action ,(message-action message))
764 (body ,(message-body message))
765 (in-reply-to ,(message-in-reply-to message))
766 (wants-reply ,(message-wants-reply message))
767 (replied ,(message-replied message))))
769 (define (pprint-message message)
770 "Pretty print a message."
771 (pretty-print (serialize-message-pretty message)))
773 (define* (read-message #:optional (port (current-input-port)))
774 "Read a message serialized via serialize-message from PORT"
776 ((id to from action body in-reply-to wants-reply replied)
778 id to from action body
779 in-reply-to wants-reply replied))
781 (throw 'message-read-bad-structure
782 "Could not read message from structure"
785 (define (read-message-from-string message-str)
786 "Read message from MESSAGE-STR"
787 (with-input-from-string message-str
789 (read-message (current-input-port)))))