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 (define-module (8sync actors)
20 #:use-module (oop goops)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (ice-9 control)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 pretty-print)
27 #:use-module (8sync agenda)
28 #:export (;; utilities... ought to go in their own module
30 big-random-number-string
31 simple-message-id-generator
39 ;;; Commenting out the <address> type for now;
40 ;;; it may be back when we have better serializers
43 address-actor-id address-hive-id
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 <-wait* <-reply <-reply-wait <-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
126 (id message-id) ; id of this message
127 (to message-to) ; actor id this is going to
128 (from message-from) ; actor id of sender
129 (action message-action) ; action (a symbol) to be handled
130 (body message-body) ; argument list "body" of message
131 (in-reply-to message-in-reply-to) ; message id this is in reply to, if any
132 (wants-reply message-wants-reply) ; whether caller is waiting for reply
133 (replied message-replied ; was this message replied to?
134 set-message-replied!))
137 (define* (make-message id to from action body
138 #:key in-reply-to wants-reply
140 (make-message-intern id to from action body
141 in-reply-to wants-reply replied))
143 (define (message-auto-reply? message)
144 (eq? (message-action message) '*auto-reply*))
146 (define (message-needs-reply? message)
147 "See if this message needs a reply still"
148 (and (message-wants-reply message)
149 (not (message-replied message))))
152 (define (kwarg-list-to-alist args)
153 (let loop ((remaining args)
156 (((? keyword? key) val rest ...)
158 (cons (cons (keyword->symbol key) val)
161 (_ (throw 'invalid-kwarg-list
162 "Invalid keyword argument list"
166 ;;; See: https://web.archive.org/web/20081223021934/http://mumble.net/~jar/articles/oo-moon-weinreb.html
167 ;;; (also worth seeing: http://mumble.net/~jar/articles/oo.html )
169 ;; This is the internal, generalized message sending method.
170 ;; Users shouldn't use it! Use the <-foo forms instead.
172 ;; @@: Could we get rid of some of the conditional checks through
175 (define-inlinable (send-message send-options from-actor to-id action
176 replying-to-message wants-reply?
178 (if replying-to-message
179 (set-message-replied! replying-to-message #t))
180 (let* ((hive (actor-hive from-actor))
182 (make-message (hive-gen-message-id hive) to-id
183 (actor-id from-actor) action
185 #:wants-reply wants-reply?
187 (if replying-to-message
188 (message-id replying-to-message)
191 (abort-to-prompt (hive-prompt (actor-hive from-actor))
192 from-actor new-message send-options)
193 ;; @@: It might be that eventually we pass in send-options
194 ;; here too. Since <-wait and <-reply-wait are the only ones
195 ;; that use it yet, for now it kind of just makes things
197 (8sync (hive-process-message hive new-message)))))
200 (define (<- from-actor to-id action . message-body-args)
201 "Send a message from an actor to another actor"
202 (send-message '() from-actor to-id action
203 #f #f message-body-args))
205 (define (<-wait* send-options from-actor to-id action . message-body-args)
206 "Like <-wait, but allows extra parameters, for example whether to
208 (apply wait-maybe-handle-errors
209 (send-message send-options from-actor to-id action
210 #f #t message-body-args)
213 (define (<-wait from-actor to-id action . message-body-args)
214 "Send a message from an actor to another, but wait until we get a response"
215 (apply <-wait* '() from-actor to-id action message-body-args))
217 ;; TODO: Intelligently ~propagate(ish) errors on -wait functions.
218 ;; We might have `send-message-wait-brazen' to allow callers to
219 ;; not have an exception thrown and instead just have a message with
220 ;; the appropriate '*error* message returned.
222 (define (<-reply from-actor original-message . message-body-args)
224 (send-message '() from-actor (message-from original-message) '*reply*
225 original-message #f message-body-args))
227 (define (<-auto-reply from-actor original-message)
228 "Auto-reply to a message. Internal use only!"
229 (send-message '() from-actor (message-from original-message) '*auto-reply*
230 original-message #f '()))
232 (define (<-reply-wait* send-options from-actor original-message
234 "Reply to a messsage, but wait until we get a response"
235 (apply wait-maybe-handle-errors
236 (send-message send-options from-actor
237 (message-from original-message) '*reply*
238 original-message #t message-body-args)
241 (define (<-reply-wait from-actor original-message . message-body-args)
242 "Reply to a messsage, but wait until we get a response"
243 (apply <-reply-wait* '() from-actor original-message message-body-args))
245 (define* (wait-maybe-handle-errors message
248 "Before returning a message to a waiting caller, see if we need to
249 raise an exception if an error."
250 (define action (message-action message))
251 (cond ((and (eq? action '*error*)
253 (throw 'hive-unresumable-coroutine
254 "Won't resume coroutine; got an *error* as a reply"
260 ;;; Main actor implementation
261 ;;; =========================
263 (define (actor-inheritable-message-handler actor message)
264 (define action (message-action message))
265 (define (find-message-handler return)
266 (for-each (lambda (this-class)
268 (or (and (class-slot-definition this-class 'actions)
269 (class-slot-ref this-class 'actions))
271 (for-each (match-lambda
272 ((action-name . method)
273 (when (eq? action-name action)
276 (class-precedence-list (class-of actor)))
277 (throw 'action-not-found
278 "No appropriate action handler found for actor"
283 (call/ec find-message-handler))
284 (apply method actor message (message-body message)))
286 (define-syntax-rule (build-actions (symbol method) ...)
287 "Construct an alist of (symbol . method), where the method is wrapped
288 with wrap-apply to facilitate live hacking and allow the method definition
289 to come after class definition."
292 (wrap-apply method)) ...))
294 (define-class <actor> ()
296 (id #:init-keyword #:id
298 ;; The hive we're connected to.
299 ;; We need this to be able to send messages.
300 (hive #:init-keyword #:hive
301 #:accessor actor-hive)
302 ;; How we receive and process new messages
303 (message-handler #:init-value actor-inheritable-message-handler
304 ;; @@: There's no reason not to use #:class instead of
305 ;; #:each-subclass anywhere in this file, except for
306 ;; Guile bug #25211 (#:class is broken in Guile 2.2)
307 #:allocation #:each-subclass
308 #:getter actor-message-handler)
310 ;; This is the default, "simple" way to inherit and process messages.
311 (actions #:init-value (build-actions
312 ;; Default clean-up method is to do nothing.
313 (*clean-up* (const #f)))
314 #:allocation #:each-subclass))
316 ;;; So these are the nicer representations of addresses.
317 ;;; However, they don't serialize so easily with scheme read/write, so we're
318 ;;; using the simpler cons cell version below for now.
320 ;; (define-record-type <address>
321 ;; (make-address actor-id hive-id) ; @@: Do we want the trailing -id?
323 ;; (actor-id address-actor-id)
324 ;; (hive-id address-hive-id))
326 ;; (set-record-type-printer!
328 ;; (lambda (record port)
329 ;; (format port "<address: ~s@~s>"
330 ;; (address-actor-id record) (address-hive-id record))))
333 (define (make-address actor-id hive-id)
334 (vector actor-id hive-id))
336 (define (address-actor-id address)
337 (vector-ref address 0))
339 (define (address-hive-id address)
340 (vector-ref address 1))
342 (define (address->string address)
343 (string-append (address-actor-id address) "@"
344 (address-hive-id address)))
346 (define-method (actor-id-actor (actor <actor>))
347 "Get the actor id component of the actor-id"
348 (address-actor-id (actor-id actor)))
350 (define-method (actor-id-hive (actor <actor>))
351 "Get the hive id component of the actor-id"
352 (address-hive-id (actor-id actor)))
354 (define-method (actor-id-string (actor <actor>))
355 "Render the full actor id as a human-readable string"
356 (address->string (actor-id actor)))
358 (define %current-actor
361 (define (actor-am-i-alive? actor)
362 (hive-resolve-local-actor (actor-hive actor) (actor-id actor)))
369 (define-syntax-rule (define-simple-actor class action ...)
370 (define-class class (<actor>)
371 (actions #:init-value (build-actions action ...)
372 #:allocation #:each-subclass)))
377 ;;; Every actor has a hive. The hive is a kind of "meta-actor"
378 ;;; which routes all the rest of the actors in a system.
380 (define-generic hive-handle-failed-forward)
382 (define-class <hive> (<actor>)
383 (actor-registry #:init-thunk make-hash-table
384 #:getter hive-actor-registry)
385 (msg-id-generator #:init-thunk simple-message-id-generator
386 #:getter hive-msg-id-generator)
387 ;; Ambassadors are used (or will be) for inter-hive communication.
388 ;; These are special actors that know how to route messages to other hives.
389 (ambassadors #:init-thunk make-weak-key-hash-table
390 #:getter hive-ambassadors)
391 ;; Waiting coroutines
392 ;; This is a map from cons cell of message-id
393 ;; to a cons cell of (actor-id . coroutine)
394 ;; @@: Should we have a <waiting-coroutine> record type?
395 ;; @@: Should there be any way to clear out "old" coroutines?
396 (waiting-coroutines #:init-thunk make-hash-table
397 #:getter hive-waiting-coroutines)
399 ;; When actors send messages to each other they abort to this prompt
400 ;; to send the message, then carry on their way
401 (prompt #:init-thunk make-prompt-tag
402 #:getter hive-prompt)
403 (actions #:allocation #:each-subclass
406 ;; This is in the case of an ambassador failing to forward a
407 ;; message... it reports it back to the hive
408 (*failed-forward* hive-handle-failed-forward)
409 (*clean-up-all* hive-handle-clean-up-all))))
411 (define-method (hive-handle-failed-forward (hive <hive>) message)
412 "Handle an ambassador failing to forward a message"
415 (define-method (hive-handle-clean-up-all (hive <hive>) message)
416 "Send a message to all actors in our registry to clean themselves up."
417 ;; Unfortunately we have to do this hack and run over the list
418 ;; twice, because hash-for-each would result in an unrewindable
421 (hash-map->list (lambda (actor-id actor) actor-id)
422 (hive-actor-registry hive)))
423 (for-each (lambda (actor-id)
424 (<- hive actor-id '*clean-up*))
427 (define* (make-hive #:key hive-id)
428 (let ((hive (make <hive>
431 (big-random-number-string))))))
432 ;; Set the hive's actor reference to itself
433 (set! (actor-hive hive) hive)
434 ;; Register the actor with itself
435 (hive-register-actor! hive hive)
438 (define-method (hive-id (hive <hive>))
439 (actor-id-hive hive))
441 (define-method (hive-gen-actor-id (hive <hive>) cookie)
442 (make-address (if cookie
443 (string-append cookie "-" (big-random-number-string))
444 (big-random-number-string))
447 (define-method (hive-gen-message-id (hive <hive>))
448 "Generate a message id using HIVE's message id generator"
449 ((hive-msg-id-generator hive)))
451 (define-method (hive-resolve-local-actor (hive <hive>) actor-address)
452 (hash-ref (hive-actor-registry hive) actor-address))
454 (define-method (hive-resolve-ambassador (hive <hive>) ambassador-address)
455 (hash-ref (hive-ambassadors hive) ambassador-address))
457 (define-method (make-forward-request (hive <hive>) (ambassador <actor>) message)
458 (make-message (hive-gen-message-id hive) (actor-id ambassador)
459 ;; If we make the hive not an actor, we could either switch this
460 ;; to #f or to the original actor...?
461 ;; Maybe some more thinking should be done on what should
462 ;; happen in case of failure to forward? Handling ambassador failures
463 ;; seems like the primary motivation for the hive remaining an actor.
466 `((original . ,message))))
468 (define-method (hive-reply-with-error (hive <hive>) original-message
469 error-key error-args)
470 ;; We only supply the error-args if the original sender is on the same hive
471 (define (orig-actor-on-same-hive?)
472 (equal? (hive-id hive)
473 (address-hive-id (message-from original-message))))
474 (set-message-replied! original-message #t)
475 (let* ((new-message-body
476 (if (orig-actor-on-same-hive?)
477 `(#:original-message ,original-message
478 #:error-key ,error-key
479 #:error-args ,error-args)
480 `(#:original-message ,original-message
481 #:error-key ,error-key)))
482 (new-message (make-message (hive-gen-message-id hive)
483 (message-from original-message)
484 (actor-id hive) '*error*
486 #:in-reply-to (message-id original-message))))
487 ;; We only return a thunk, rather than run 8sync here, because if
488 ;; we ran 8sync in the middle of a catch we'd end up with an
489 ;; unresumable continuation.
490 (lambda () (hive-process-message hive new-message))))
492 (define-record-type <waiting-on-reply>
493 (make-waiting-on-reply actor-id kont send-options)
495 (actor-id waiting-on-reply-actor-id)
496 (kont waiting-on-reply-kont)
497 (send-options waiting-on-reply-send-options))
500 (define-method (hive-process-message (hive <hive>) message)
501 "Handle one message, or forward it via an ambassador"
502 (define (maybe-autoreply actor)
503 ;; Possibly autoreply
504 (if (message-needs-reply? message)
505 (<-auto-reply actor message)))
507 (define (resolve-actor-to)
508 "Get the actor the message was aimed at"
509 (let ((actor (hive-resolve-local-actor hive (message-to message))))
511 (throw 'actor-not-found
512 (format #f "Message ~a from ~a directed to nonexistant actor ~a"
514 (address->string (message-from message))
515 (address->string (message-to message)))
519 (define (call-catching-coroutine thunk)
520 (define queued-error-handling-thunk #f)
521 (define (call-catching-errors)
522 ;; TODO: maybe parameterize (or attach to hive) and use
523 ;; maybe-catch-all from agenda.scm
524 ;; @@: Why not just use with-throw-handler and let the catch
525 ;; happen at the agenda? That's what we used to do, but
526 ;; it ended up with a SIGABRT. See:
527 ;; http://lists.gnu.org/archive/html/bug-guile/2016-05/msg00003.html
530 ;; In the actor model, we don't totally crash on errors.
532 ;; If an error happens, we raise it
534 (if (message-needs-reply? message)
535 ;; If the message is waiting on a reply, let them know
536 ;; something went wrong.
537 ;; However, we have to do it outside of this catch
538 ;; routine, or we'll end up in an unrewindable continuation
540 (set! queued-error-handling-thunk
541 (hive-reply-with-error hive message key args)))
542 ;; print error message
543 (apply print-error-and-continue key args)))
544 ;; @@: This is a kludge. See above for why.
545 (if queued-error-handling-thunk
546 (8sync (queued-error-handling-thunk))))
547 (call-with-prompt (hive-prompt hive)
549 (lambda (kont actor message send-options)
550 ;; Register the coroutine
551 (hash-set! (hive-waiting-coroutines hive)
553 (make-waiting-on-reply
554 (actor-id actor) kont send-options))
555 ;; Send off the message
556 (8sync (hive-process-message hive message)))))
558 (define (process-local-message)
559 (let ((actor (resolve-actor-to)))
560 (call-catching-coroutine
562 (define message-handler (actor-message-handler actor))
563 ;; @@: Should a more general error handling happen here?
564 (parameterize ((%current-actor actor))
566 (message-handler actor message)))
567 (maybe-autoreply actor)
568 ;; Returning result allows actors to possibly make a run-request
569 ;; at the end of handling a message.
570 ;; ... We do want that, right?
573 (define (resume-waiting-coroutine)
574 (case (message-action message)
575 ;; standard reply / auto-reply
576 ((*reply* *auto-reply* *error*)
577 (call-catching-coroutine
579 (match (hash-remove! (hive-waiting-coroutines hive)
580 (message-in-reply-to message))
582 (if (not (equal? (message-to message)
583 (waiting-on-reply-actor-id waiting)))
584 (throw 'resuming-to-wrong-actor
585 "Attempted to resume a coroutine to the wrong actor!"
586 #:expected-actor-id (message-to message)
587 #:got-actor-id (waiting-on-reply-actor-id waiting)
589 (let* (;; @@: How should we resolve resuming coroutines to actors who are
591 (actor (resolve-actor-to))
592 (kont (waiting-on-reply-kont waiting))
593 (result (kont message)))
594 (maybe-autoreply actor)
596 (#f (throw 'no-waiting-coroutine
597 "message in-reply-to tries to resume nonexistent coroutine"
599 ;; Unhandled action for a reply!
601 (throw 'hive-unresumable-coroutine
602 "Won't resume coroutine, nonsense action on reply message"
603 #:action (message-action message)
604 #:message message))))
606 (define (process-remote-message)
607 ;; Find the ambassador
608 (let* ((remote-hive-id (hive-id (message-to message)))
609 (ambassador (hive-resolve-ambassador remote-hive-id))
610 (message-handler (actor-message-handler ambassador))
611 (forward-request (make-forward-request hive ambassador message)))
612 (message-handler ambassador forward-request)))
614 (let ((to (message-to message)))
615 ;; This seems to be an easy mistake to make, so check that addressing
618 (throw 'missing-addressee
619 "`to' field is missing on message"
621 (if (hive-actor-local? hive to)
622 (if (message-in-reply-to message)
623 (resume-waiting-coroutine)
624 (process-local-message))
625 (process-remote-message))))
627 (define-method (hive-actor-local? (hive <hive>) address)
628 (equal? (hive-id hive) (address-hive-id address)))
630 (define-method (hive-register-actor! (hive <hive>) (actor <actor>))
631 (hash-set! (hive-actor-registry hive) (actor-id actor) actor))
633 (define-method (%hive-create-actor (hive <hive>) actor-class
635 "Actual method called by hive-create-actor.
637 Since this is a define-method it can't accept fancy define* arguments,
638 so this gets called from the nicer hive-create-actor interface. See
639 that method for documentation."
640 (let* ((actor-id (hive-gen-actor-id hive id-cookie))
641 (actor (apply make actor-class
645 (hive-register-actor! hive actor)
646 ;; return the actor id
649 (define* (hive-create-actor hive actor-class #:rest init)
650 (%hive-create-actor hive actor-class
653 (define* (hive-create-actor* hive actor-class id-cookie #:rest init)
654 "Create an actor, but also add a 'cookie' to the name for debugging"
655 (%hive-create-actor hive actor-class
658 (define (call-with-message message proc)
659 "Applies message body arguments into procedure, with message as first
660 argument. Similar to call-with-values in concept."
661 (apply proc message (message-body message)))
663 ;; (msg-receive (<- bar baz)
667 ;; Emacs: (put 'msg-receive 'scheme-indent-function 2)
669 ;; @@: Or receive-msg or receieve-message or??
670 (define-syntax-rule (msg-receive arglist message body ...)
671 "Call body with arglist (which can accept arguments like lambda*)
672 applied from the message-body of message."
673 (call-with-message message
677 (define (msg-val message)
678 "Retrieve the first value from the message-body of message.
679 Like single value return from a procedure call. Probably the most
680 common case when waiting on a reply from some action invocation."
681 (call-with-message message
682 (lambda (_ val) val)))
685 ;;; Various API methods for actors to interact with the system
686 ;;; ==========================================================
688 ;; TODO: move send-message and friends here...?
690 (define* (create-actor from-actor actor-class #:rest init)
691 "Create an instance of actor-class. Return the new actor's id.
693 This is the method actors should call directly (unless they want
694 to supply an id-cookie, in which case they should use
696 (%hive-create-actor (actor-hive from-actor) actor-class
700 (define* (create-actor* from-actor actor-class id-cookie #:rest init)
701 "Create an instance of actor-class. Return the new actor's id.
703 Like create-actor, but permits supplying an id-cookie."
704 (%hive-create-actor (actor-hive from-actor) actor-class
708 (define* (self-destruct actor #:key (clean-up #t))
709 "Remove an actor from the hive.
711 Unless #:clean-up is set to #f, this will first have the actor handle
712 its '*clean-up* action handler."
714 (<-wait actor (actor-id actor) '*clean-up*))
715 (hash-remove! (hive-actor-registry (actor-hive actor))
720 ;;; 8sync bootstrap utilities
721 ;;; =========================
723 (define* (run-hive hive initial-tasks
725 "Start up an agenda and run HIVE in it with INITIAL-TASKS."
729 (let* ((queue (list->q initial-tasks))
730 (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
732 (start-agenda agenda)))
736 (run-hive-clean-up hive)))))
738 (define (run-hive-clean-up hive)
739 (let ((queue (list->q (list (bootstrap-message hive (actor-id hive)
742 (make-agenda #:queue queue))))
744 (define (bootstrap-message hive to-id action . message-body-args)
746 (apply <- hive to-id action message-body-args)))
750 ;;; Basic readers / writers
751 ;;; =======================
753 (define (serialize-message message)
754 "Serialize a message for read/write"
758 (message-from message)
759 (message-action message)
760 (message-body message)
761 (message-in-reply-to message)
762 (message-wants-reply message)
763 (message-replied message)))
765 (define* (write-message message #:optional (port (current-output-port)))
766 "Write out a message to a port for easy reading later.
768 Note that if a sub-value can't be easily written to something
769 Guile's `read' procedure knows how to read, this doesn't do anything
770 to improve that. You'll need a better serializer for that.."
771 (write (serialize-message message) port))
773 (define (serialize-message-pretty message)
774 "Serialize a message in a way that's easy for humans to read."
776 (id ,(message-id message))
777 (to ,(message-to message))
778 (from ,(message-from message))
779 (action ,(message-action message))
780 (body ,(message-body message))
781 (in-reply-to ,(message-in-reply-to message))
782 (wants-reply ,(message-wants-reply message))
783 (replied ,(message-replied message))))
785 (define (pprint-message message)
786 "Pretty print a message."
787 (pretty-print (serialize-message-pretty message)))
789 (define* (read-message #:optional (port (current-input-port)))
790 "Read a message serialized via serialize-message from PORT"
792 ((id to from action body in-reply-to wants-reply replied)
794 id to from action body
795 in-reply-to wants-reply replied))
797 (throw 'message-read-bad-structure
798 "Could not read message from structure"
801 (define (read-message-from-string message-str)
802 "Read message from MESSAGE-STR"
803 (with-input-from-string message-str
805 (read-message (current-input-port)))))