2021-05-27 08:36:35 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
2021-05-27 09:28:10 +00:00
|
|
|
(provide (except-out (struct-out entity) entity)
|
|
|
|
(rename-out [make-entity entity])
|
|
|
|
|
|
|
|
(struct-out ref)
|
|
|
|
parse-Ref
|
|
|
|
Ref->preserves
|
|
|
|
|
|
|
|
actor-system
|
|
|
|
|
|
|
|
actor?
|
|
|
|
actor-id
|
2021-05-28 08:33:02 +00:00
|
|
|
actor-daemon?
|
2021-05-27 09:28:10 +00:00
|
|
|
actor-exit-reason
|
|
|
|
actor-add-exit-hook!
|
2021-05-28 08:33:02 +00:00
|
|
|
actor-daemon!
|
2021-05-27 09:28:10 +00:00
|
|
|
|
|
|
|
facet?
|
|
|
|
facet-id
|
|
|
|
facet-actor
|
|
|
|
facet-parent
|
|
|
|
facet-live?
|
|
|
|
facet-inert?
|
|
|
|
facet-on-stop!
|
|
|
|
facet-prevent-inert-check!
|
|
|
|
|
|
|
|
turn?
|
|
|
|
turn-id
|
|
|
|
turn-active-facet
|
|
|
|
turn!
|
|
|
|
turn-freshen
|
|
|
|
turn-ref
|
|
|
|
turn-facet!
|
|
|
|
turn-stop!
|
|
|
|
turn-spawn!
|
|
|
|
turn-stop-actor!
|
|
|
|
turn-crash!
|
2021-05-31 11:05:37 +00:00
|
|
|
turn-field!
|
|
|
|
turn-dataflow!
|
|
|
|
turn-assert/dataflow!
|
2021-05-27 09:28:10 +00:00
|
|
|
turn-assert!
|
|
|
|
turn-assert!*
|
|
|
|
turn-retract!
|
|
|
|
turn-replace!
|
|
|
|
turn-retract!*
|
|
|
|
turn-sync!
|
|
|
|
turn-sync!*
|
|
|
|
turn-message!)
|
2021-05-27 08:36:35 +00:00
|
|
|
|
2021-05-31 11:05:37 +00:00
|
|
|
(require (only-in preserves preserve=?))
|
2021-05-27 08:36:35 +00:00
|
|
|
(require racket/match)
|
|
|
|
(require (only-in racket/exn exn->string))
|
|
|
|
(require struct-defaults)
|
|
|
|
|
|
|
|
(require "rewrite.rkt")
|
2021-05-27 09:28:10 +00:00
|
|
|
(require "engine.rkt")
|
2021-05-31 11:05:37 +00:00
|
|
|
(require "dataflow.rkt")
|
|
|
|
(require "field.rkt")
|
2021-05-27 08:36:35 +00:00
|
|
|
(require "support/counter.rkt")
|
|
|
|
|
|
|
|
(struct entity (assert retract message sync))
|
|
|
|
(define-struct-defaults make-entity entity
|
|
|
|
(#:assert [entity-assert #f]
|
|
|
|
#:retract [entity-retract #f]
|
|
|
|
#:message [entity-message #f]
|
|
|
|
#:sync [entity-sync #f]))
|
|
|
|
|
|
|
|
(struct ref (relay target attenuation) #:transparent)
|
|
|
|
(define (parse-Ref r) (if (ref? r) r eof))
|
|
|
|
(define (Ref->preserves r) r)
|
|
|
|
|
|
|
|
(struct outbound-assertion (handle peer [established? #:mutable]))
|
|
|
|
|
|
|
|
(struct actor (id
|
2021-05-27 09:28:10 +00:00
|
|
|
engine
|
2021-05-28 08:33:02 +00:00
|
|
|
[daemon? #:mutable]
|
2021-05-31 11:05:37 +00:00
|
|
|
dataflow
|
2021-05-27 08:36:35 +00:00
|
|
|
[root #:mutable]
|
|
|
|
[exit-reason #:mutable] ;; #f -> running, #t -> terminated OK, exn -> error
|
|
|
|
[exit-hooks #:mutable])
|
2021-05-27 09:28:10 +00:00
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc a port mode)
|
|
|
|
(fprintf port "#<actor:~a>" (actor-id a)))])
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
(struct facet (id
|
|
|
|
actor
|
|
|
|
parent
|
|
|
|
children
|
|
|
|
outbound
|
|
|
|
[shutdown-actions #:mutable]
|
|
|
|
[live? #:mutable]
|
|
|
|
[inert-check-preventers #:mutable])
|
2021-05-27 09:28:10 +00:00
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc f port mode)
|
|
|
|
(local-require (only-in racket/string string-join))
|
|
|
|
(fprintf port "#<facet:~a:~a>"
|
|
|
|
(actor-id (facet-actor f))
|
|
|
|
(string-join (let loop ((f f))
|
|
|
|
(if (facet-parent f)
|
|
|
|
(cons (number->string (facet-id f)) (loop (facet-parent f)))
|
|
|
|
'()))
|
|
|
|
"/")))])
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
(struct turn (id
|
|
|
|
active-facet
|
|
|
|
[queues #:mutable]))
|
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define generate-actor-id (make-counter))
|
|
|
|
(define generate-turn-id (make-counter))
|
|
|
|
(define generate-handle (make-counter))
|
|
|
|
|
|
|
|
(define-logger syndicate/actor)
|
|
|
|
|
|
|
|
;;--------------------------------------------------------------------------
|
|
|
|
|
2021-05-27 09:28:10 +00:00
|
|
|
(define (actor-system boot-proc)
|
|
|
|
(define e (make-engine 1))
|
2021-05-28 08:33:02 +00:00
|
|
|
(make-actor e #t boot-proc (make-hash))
|
2021-05-27 09:28:10 +00:00
|
|
|
(adjust-inhabitant-count! e -1)
|
|
|
|
(thread-wait (engine-thread e)))
|
|
|
|
|
2021-05-28 08:33:02 +00:00
|
|
|
(define (make-actor engine daemon? boot-proc initial-assertions)
|
2021-05-27 08:36:35 +00:00
|
|
|
(define ac (actor (generate-actor-id)
|
2021-05-27 09:28:10 +00:00
|
|
|
engine
|
2021-05-28 08:33:02 +00:00
|
|
|
daemon?
|
2021-05-31 11:05:37 +00:00
|
|
|
(make-dataflow-graph)
|
2021-05-27 08:36:35 +00:00
|
|
|
'uninitialized
|
|
|
|
#f
|
|
|
|
'()))
|
2021-05-28 08:33:02 +00:00
|
|
|
(when (not daemon?)
|
|
|
|
(adjust-inhabitant-count! engine +1))
|
2021-05-27 08:36:35 +00:00
|
|
|
(set-actor-root! ac (make-facet ac #f initial-assertions))
|
|
|
|
(turn! (make-facet ac (actor-root ac))
|
|
|
|
(stop-if-inert-after boot-proc))
|
2021-05-27 09:28:10 +00:00
|
|
|
(log-syndicate/actor-info "~a created" ac)
|
2021-05-27 08:36:35 +00:00
|
|
|
ac)
|
|
|
|
|
|
|
|
(define (actor-add-exit-hook! ac hook)
|
|
|
|
(set-actor-exit-hooks! ac (cons hook (actor-exit-hooks ac))))
|
|
|
|
|
2021-05-28 08:33:02 +00:00
|
|
|
(define (actor-daemon! ac daemon?)
|
|
|
|
(when (not (eq? daemon? (actor-daemon? ac)))
|
|
|
|
(set-actor-daemon?! ac daemon?)
|
|
|
|
(adjust-inhabitant-count! (actor-engine ac) (if daemon? -1 +1))))
|
|
|
|
|
2021-05-27 08:36:35 +00:00
|
|
|
(define (actor-terminate! turn ac reason)
|
|
|
|
(when (not (actor-exit-reason ac))
|
|
|
|
(set-actor-exit-reason! ac reason)
|
|
|
|
(if (eq? reason #t)
|
2021-05-27 09:28:10 +00:00
|
|
|
(log-syndicate/actor-info "~a terminated OK" ac)
|
|
|
|
(log-syndicate/actor-error "~a terminated with exception:\n~a"
|
|
|
|
ac
|
2021-05-27 08:36:35 +00:00
|
|
|
(exn->string reason)))
|
|
|
|
(for [(h (in-list (reverse (actor-exit-hooks ac))))] (h turn))
|
2021-05-27 09:28:10 +00:00
|
|
|
(queue-task! (actor-engine ac)
|
|
|
|
(lambda () (turn! (actor-root ac)
|
2021-05-28 08:33:02 +00:00
|
|
|
(lambda (turn)
|
|
|
|
(facet-terminate! turn (actor-root ac) (eq? reason #t)))
|
2021-05-27 09:28:10 +00:00
|
|
|
#t)))
|
2021-05-28 08:33:02 +00:00
|
|
|
(when (not (actor-daemon? ac))
|
|
|
|
(adjust-inhabitant-count! (actor-engine ac) -1))))
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define (make-facet ac parent [initial-assertions (make-hash)])
|
|
|
|
(define f (facet (generate-actor-id)
|
|
|
|
ac
|
|
|
|
parent
|
|
|
|
(make-hasheq)
|
|
|
|
initial-assertions
|
|
|
|
'()
|
|
|
|
#t
|
|
|
|
0))
|
|
|
|
(when parent
|
|
|
|
(hash-set! (facet-children parent) f #t))
|
|
|
|
f)
|
|
|
|
|
|
|
|
(define (facet-on-stop! f action)
|
|
|
|
(set-facet-shutdown-actions! f (cons action (facet-shutdown-actions f))))
|
|
|
|
|
|
|
|
(define (facet-inert? f)
|
|
|
|
(and (hash-empty? (facet-children f))
|
|
|
|
(hash-empty? (facet-outbound f))
|
|
|
|
(zero? (facet-inert-check-preventers f))))
|
|
|
|
|
|
|
|
(define (facet-prevent-inert-check! f)
|
|
|
|
(define armed #t)
|
2021-05-27 09:28:10 +00:00
|
|
|
(set-facet-inert-check-preventers! f (+ (facet-inert-check-preventers f) 1))
|
2021-05-27 08:36:35 +00:00
|
|
|
(lambda ()
|
|
|
|
(when armed
|
|
|
|
(set! armed #f)
|
2021-05-27 09:28:10 +00:00
|
|
|
(set-facet-inert-check-preventers! f (- (facet-inert-check-preventers f) 1)))))
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
(define (facet-terminate! turn f orderly?)
|
|
|
|
(when (facet-live? f)
|
2021-05-28 08:33:02 +00:00
|
|
|
(log-syndicate/actor-debug "~a stopping (~a)" f (if orderly? "orderly" "disorderly"))
|
2021-05-27 08:36:35 +00:00
|
|
|
(set-facet-live?! f #f)
|
|
|
|
|
|
|
|
(define parent (facet-parent f))
|
|
|
|
(when parent (hash-remove! (facet-children parent) f))
|
|
|
|
|
2021-05-31 10:25:44 +00:00
|
|
|
(with-active-facet turn f
|
|
|
|
(lambda (turn)
|
|
|
|
(for [(c (in-hash-keys (facet-children f)))] (facet-terminate! turn c orderly?))
|
|
|
|
(when orderly? (for [(h (in-list (reverse (facet-shutdown-actions f))))] (h turn)))
|
|
|
|
(for [(a (in-hash-values (facet-outbound f)))] (turn-retract!* turn a))
|
|
|
|
(when orderly?
|
|
|
|
(queue-task!
|
|
|
|
(actor-engine (facet-actor f))
|
|
|
|
(lambda ()
|
|
|
|
(if parent
|
|
|
|
(when (facet-inert? parent)
|
|
|
|
(turn! parent (lambda (turn) (facet-terminate! turn parent #t))))
|
|
|
|
(turn! (actor-root (facet-actor f))
|
|
|
|
(lambda (turn) (actor-terminate! turn (facet-actor f) #t))
|
|
|
|
#t)))))))))
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define (turn! f action [zombie-turn? #f])
|
2021-05-31 11:05:37 +00:00
|
|
|
(define ac (facet-actor f))
|
|
|
|
(when (or zombie-turn? (and (not (actor-exit-reason ac)) (facet-live? f)))
|
2021-05-27 08:36:35 +00:00
|
|
|
(let ((turn (turn (generate-turn-id) f (make-hasheq))))
|
|
|
|
(with-handlers ([exn? (lambda (e)
|
2021-05-31 11:05:37 +00:00
|
|
|
(turn! (actor-root ac)
|
2021-05-27 08:36:35 +00:00
|
|
|
(lambda (turn)
|
2021-05-31 11:05:37 +00:00
|
|
|
(actor-terminate! turn ac e))))])
|
2021-05-27 08:36:35 +00:00
|
|
|
(action turn)
|
2021-05-31 11:05:37 +00:00
|
|
|
(dataflow-repair-damage! (actor-dataflow ac) (lambda (action) (action turn)))
|
2021-05-27 08:36:35 +00:00
|
|
|
(for [((ff qq) (in-hash (turn-queues turn)))]
|
2021-05-27 09:28:10 +00:00
|
|
|
(queue-task! (actor-engine (facet-actor ff))
|
2021-05-31 10:06:44 +00:00
|
|
|
(lambda ()
|
|
|
|
(turn! ff (lambda (turn)
|
|
|
|
(for [(a (in-list (reverse qq)))] (a turn)))))))
|
2021-05-27 08:36:35 +00:00
|
|
|
(set-turn-queues! turn #f)))))
|
|
|
|
|
2021-05-31 10:25:44 +00:00
|
|
|
(define (with-active-facet outer-turn f action)
|
2021-05-27 08:36:35 +00:00
|
|
|
(let ((inner-turn (turn (generate-turn-id) f (turn-queues outer-turn))))
|
|
|
|
(action inner-turn)
|
|
|
|
(set-turn-queues! inner-turn #f)))
|
|
|
|
|
|
|
|
(define (turn-enqueue! turn f action)
|
|
|
|
(define qs (turn-queues turn))
|
|
|
|
(when (not qs) (error 'turn-enqueue! "Attempt to reuse a committed turn"))
|
|
|
|
(hash-update! qs f (lambda (actions) (cons action actions)) '()))
|
|
|
|
|
2021-05-27 09:41:11 +00:00
|
|
|
(define (turn-ref turn entity [attenuation '()])
|
|
|
|
(ref (turn-active-facet turn) entity attenuation))
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
(define (turn-facet! turn boot-proc)
|
|
|
|
(let ((new-facet (make-facet (facet-actor (turn-active-facet turn)) (turn-active-facet turn))))
|
2021-05-31 10:25:44 +00:00
|
|
|
(with-active-facet turn new-facet (stop-if-inert-after boot-proc))
|
2021-05-27 08:36:35 +00:00
|
|
|
new-facet))
|
|
|
|
|
|
|
|
(define (turn-stop! turn [f (turn-active-facet turn)] [continuation void])
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(facet-parent f)
|
|
|
|
(lambda (turn)
|
|
|
|
(facet-terminate! turn f #t)
|
|
|
|
(continuation turn))))
|
|
|
|
|
2021-05-28 08:33:02 +00:00
|
|
|
(define (turn-spawn! turn boot-proc [initial-assertions (make-hash)] #:daemon? [daemon? #f])
|
2021-05-27 08:36:35 +00:00
|
|
|
(define f (turn-active-facet turn))
|
|
|
|
(define o (facet-outbound f))
|
|
|
|
(turn-enqueue! turn
|
|
|
|
f
|
|
|
|
(lambda (turn)
|
|
|
|
(define new-outbound (make-hash))
|
|
|
|
(for [(handle (in-hash-keys initial-assertions))]
|
|
|
|
(hash-set! new-outbound handle (hash-ref o handle))
|
|
|
|
(hash-remove! o handle))
|
2021-05-27 09:28:10 +00:00
|
|
|
(define engine (actor-engine (facet-actor f)))
|
2021-05-28 08:33:02 +00:00
|
|
|
(queue-task! engine (lambda () (make-actor engine daemon? boot-proc new-outbound))))))
|
2021-05-27 08:36:35 +00:00
|
|
|
|
|
|
|
(define (turn-stop-actor! turn)
|
|
|
|
(define ac (facet-actor (turn-active-facet turn)))
|
|
|
|
(turn-enqueue! turn (actor-root ac) (lambda (turn) (actor-terminate! turn ac #t))))
|
|
|
|
|
|
|
|
(define (turn-crash! turn exn)
|
|
|
|
(define ac (facet-actor (turn-active-facet turn)))
|
|
|
|
(turn-enqueue! turn (actor-root ac) (lambda (turn) (actor-terminate! turn ac exn))))
|
|
|
|
|
2021-05-31 11:05:37 +00:00
|
|
|
(define (turn-field! turn name initial-value)
|
|
|
|
(field (actor-dataflow (facet-actor (turn-active-facet turn))) name initial-value))
|
|
|
|
|
|
|
|
(define (turn-dataflow! turn action)
|
|
|
|
(parameterize ((current-dataflow-subject-id action))
|
|
|
|
(action turn)))
|
|
|
|
|
|
|
|
(define (turn-assert/dataflow! turn peer assertion-action)
|
|
|
|
(define handle #f)
|
|
|
|
(define assertion (void))
|
|
|
|
(turn-dataflow! turn
|
|
|
|
(lambda (turn)
|
|
|
|
(define new-assertion (assertion-action turn))
|
|
|
|
(when (not (preserve=? assertion new-assertion))
|
|
|
|
(set! handle (turn-replace! turn peer handle new-assertion))))))
|
|
|
|
|
2021-05-27 08:36:35 +00:00
|
|
|
(define (turn-assert! turn peer assertion)
|
|
|
|
(define handle (generate-handle))
|
|
|
|
(turn-assert!* turn peer assertion handle)
|
|
|
|
handle)
|
|
|
|
|
|
|
|
(define (turn-assert!* turn peer assertion handle)
|
|
|
|
(match (run-rewrites (ref-attenuation peer) assertion)
|
|
|
|
[(? void?) (void)]
|
|
|
|
[rewritten
|
|
|
|
(define a (outbound-assertion handle peer #f))
|
|
|
|
(hash-set! (facet-outbound (turn-active-facet turn)) handle a)
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(ref-relay peer)
|
|
|
|
(lambda (turn)
|
|
|
|
(set-outbound-assertion-established?! a #t)
|
|
|
|
(deliver (entity-assert (ref-target peer)) turn rewritten handle)))]))
|
|
|
|
|
|
|
|
(define (turn-retract! turn handle)
|
|
|
|
(when handle
|
|
|
|
(define a (hash-ref (facet-outbound (turn-active-facet turn)) handle #f))
|
|
|
|
(when a (turn-retract!* turn a))))
|
|
|
|
|
|
|
|
(define (turn-replace! turn peer old-handle assertion)
|
|
|
|
(define new-handle (if (void? assertion) #f (turn-assert! turn peer assertion)))
|
|
|
|
(turn-retract! turn old-handle)
|
|
|
|
new-handle)
|
|
|
|
|
|
|
|
(define (turn-retract!* turn a)
|
|
|
|
(hash-remove! (facet-outbound (turn-active-facet turn)) (outbound-assertion-handle a))
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(ref-relay (outbound-assertion-peer a))
|
|
|
|
(lambda (turn)
|
|
|
|
(when (outbound-assertion-established? a)
|
|
|
|
(set-outbound-assertion-established?! a #f)
|
|
|
|
(deliver (entity-retract (ref-target (outbound-assertion-peer a)))
|
|
|
|
turn
|
|
|
|
(outbound-assertion-handle a))))))
|
|
|
|
|
|
|
|
(define (turn-sync! turn peer k)
|
|
|
|
(turn-sync!* turn peer (turn-ref turn (make-entity #:message k))))
|
|
|
|
|
|
|
|
(define (turn-sync!* turn peer-to-sync-with peer-k)
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(ref-relay peer-to-sync-with)
|
|
|
|
(lambda (turn)
|
|
|
|
(deliver (or (entity-sync (ref-target peer-to-sync-with))
|
|
|
|
(lambda (turn peer-k) (turn-message! turn peer-k #t)))
|
|
|
|
turn
|
|
|
|
peer-k))))
|
|
|
|
|
|
|
|
(define (turn-message! turn peer assertion)
|
|
|
|
(match (run-rewrites (ref-attenuation peer) assertion)
|
|
|
|
[(? void?) (void)]
|
|
|
|
[rewritten
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(ref-relay peer)
|
|
|
|
(lambda (turn)
|
|
|
|
(deliver (entity-message (ref-target peer)) turn rewritten)))]))
|
|
|
|
|
|
|
|
(define (turn-freshen turn action)
|
|
|
|
(when (turn-queues turn) (error 'turn-freshen "Attempt to freshen a non-stale turn"))
|
|
|
|
(turn! (turn-active-facet turn) action))
|
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
(define (stop-if-inert-after action)
|
|
|
|
(lambda (turn)
|
|
|
|
(action turn)
|
|
|
|
(turn-enqueue! turn
|
|
|
|
(turn-active-facet turn)
|
|
|
|
(lambda (turn)
|
|
|
|
(define f (turn-active-facet turn))
|
|
|
|
(when (or (and (facet-parent f) (not (facet-live? (facet-parent f))))
|
|
|
|
(facet-inert? f))
|
|
|
|
(turn-stop! turn))))))
|
|
|
|
|
|
|
|
(define (deliver maybe-proc . args)
|
|
|
|
(when maybe-proc
|
|
|
|
(apply maybe-proc args)))
|