-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lhs
2080 lines (1706 loc) · 63.5 KB
/
main.lhs
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% Commands for TeXCount
%TC:macro \cite [option:text,text]
%TC:macro \citep [option:text,text]
%TC:macro \citet [option:text,text]
%TC:envir table 0 1
%TC:envir table* 0 1
%TC:envir tabular [ignore] word
%TC:envir displaymath 0 word
%TC:envir math 0 word
%TC:envir comment 0 0
\documentclass[sigplan,screen]{acmart}
\usepackage{cleveref}
\usepackage{enumitem} % style lists
\usepackage{caption} % align captions globally
\usepackage{tikz}
\usepackage{pifont}
\usepackage{svg}
\usepackage{subcaption}
% make numbered lists use parenthesized numerals
\renewcommand{\labelenumi}{(\arabic{enumi})}
% left align captions
\captionsetup{ singlelinecheck = false }
%%%% lhs2Tex (*.lhs) document
\let\Bbbk\undefined
%include polycode.fmt
\long\def\ignore#1{}
%%%% deindent comments
\renewcommand\onelinecommentchars{-{}- }
\visiblecomments
%%%% run traces
%options ghci -threaded -rtsopts -with-rtsopts=-N
%%%% prettier formatting
%format <$> = "\mathbin{\langle\$\rangle}"
%format <*> = "\mathbin{\langle*\rangle}"
%format ++ = "\mathbin{+\hspace{-0.2em}+}"
%format >>= = "\mathbin{>\hspace{-0.4em}>\hspace{-0.3em}=}"
%format >> = "\mathbin{>\hspace{-0.4em}>}"
%format =<< = "\mathbin{=\hspace{-0.3em}<\hspace{-0.4em}<}"
%format << = "\mathbin{<\hspace{-0.4em}<}"
%format ^ = "^"
%format [ = "["
%format { = "\!\{"
%%%%format ] = "]"
%%%%format } = "\}"
%%%%format (,) = "(,\!)"
%%% The following is specific to Haskell '23 and the paper
%%% 'An Exceptional Actor System (Functional Pearl)'
%%% by Patrick Redmond and Lindsey Kuper.
%%%
\setcopyright{rightsretained}
\acmPrice{}
\acmDOI{10.1145/3609026.3609728}
\acmYear{2023}
\copyrightyear{2023}
\acmSubmissionID{icfpws23haskellmain-id51-p}
\acmISBN{979-8-4007-0298-3/23/09}
\acmConference[Haskell '23]{Proceedings of the 16th ACM SIGPLAN International Haskell Symposium}{September 8--9, 2023}{Seattle, WA, USA}
\acmBooktitle{Proceedings of the 16th ACM SIGPLAN International Haskell Symposium (Haskell '23), September 8--9, 2023, Seattle, WA, USA}
\received{2023-06-01}
\received[accepted]{2023-07-04}
%% The majority of ACM publications use numbered citations and
%% references. The command \citestyle{authoryear} switches to the
%% "author year" style.
%% If you are preparing content for an event
%% sponsored by ACM SIGGRAPH, you must use the "author year" style of
%% citations and references.
%% Uncommenting the next command will enable that style.
%% \citestyle{acmauthoryear}
%% This command is an attempt to fix issues with text running into margins.
\sloppy
\begin{document}
\title{An Exceptional Actor System (Functional Pearl)}
\author{Patrick Redmond}
\orcid{0000-0001-5702-0860}
\affiliation{
\institution{University of California, Santa Cruz}
\country{USA}
}
\author{Lindsey Kuper}
\orcid{0000-0002-1374-7715}
\affiliation{
\institution{University of California, Santa Cruz}
\country{USA}
}
\begin{abstract}
The Glasgow Haskell Compiler is known for its feature-laden runtime system
(RTS), which includes lightweight threads, asynchronous exceptions, and a
slew of other features.
%
Their combination is powerful enough that a programmer may
complete the same task in many different ways --- some more advisable than
others.
We present a user-accessible actor framework hidden in plain sight within
the RTS and demonstrate it on a classic example from the distributed
systems literature.
%
We then extend both the framework and example to the realm of dynamic
types.
%
Finally, we raise questions about how RTS features intersect and possibly
subsume one another, and suggest that GHC can guide good practice by
constraining the use of some features.
\end{abstract}
\begin{CCSXML}
<ccs2012>
<concept>
<concept_id>10011007.10011006.10011008.10011024.10011034</concept_id>
<concept_desc>Software and its engineering~Concurrent programming structures</concept_desc>
<concept_significance>500</concept_significance>
</concept>
</ccs2012>
\end{CCSXML}
\ccsdesc[500]{Software and its engineering~Concurrent programming structures}
\keywords{
actor framework,
asynchronous exceptions,
runtime system
}
\maketitle
\section{Introduction}
Together with its runtime system (RTS), the Glasgow Haskell Compiler (GHC) is
the most commonly used implementation of Haskell \cite{fausak2022}.
%
The RTS is featureful and boasts support for lightweight threads, two kinds of
profiling, transactional memory, asynchronous exceptions, and more.
%
Combined with the \verb|base| package, a programmer can get a lot
done without ever reaching into the extensive set of community packages on
Hackage.
In that spirit,
we noticed that there is nothing really stopping one from
abusing the tools \verb|throwTo| and \verb|catch|
to pass data between threads.
%
Any user-defined datatype can be made into an asynchronous exception.
%
Why not implement message-passing algorithms on that substrate?
We pursued this line of thought, and in this paper we present an actor
framework hidden just under the surface of the RTS.
%
The paper is organized as follows:
\begin{itemize}[leftmargin=1.5em]
\item[--] \Cref{sec:background} provides a concise summary of asynchronous
exceptions in GHC and the actor model of programming.
\item[--] \Cref{sec:actor-framework} details the implementation of our
actor framework. We first show how actors receive messages of a single
type, and then extend the framework to support dynamically typed actors,
which receive messages of more than one type.
\item[--] \Cref{sec:ring-impl} shows an implementation of a classic
protocol for leader election using our actor framework. We then extend the actors with an
additional message type and behavior without changing the original
implementation.
\item[--] We reflect on whether this was a good idea in
\Cref{sec:what-have-we-wrought},
by considering the practicality and performance of our framework,
and conclude in \Cref{sec:conclusion} that asynchronous exceptions
might be more constrained.
\end{itemize}
This paper is a literate Haskell program.\footnote{
We use \verb|GHC 9.0.2| and \verb|base-4.15.1.0|.
%
Our actor framework imports \verb|Control.Exception| and
\verb|Control.Concurrent|, and we use the extensions \verb|NamedFieldPuns|
and \verb|DuplicateRecordFields| for convenience of presentation.
%
The leader election example of \Cref{sec:ring-impl} additionally imports the module \verb|System.Random|
and uses the \verb|ViewPatterns| extension.
%
The appendices have other imports, which we do not describe here.
}
% Imports for the haskell program:
\ignore{
\begin{code}
{-# LANGUAGE NamedFieldPuns #-} -- Section 2
{-# LANGUAGE DuplicateRecordFields #-} -- Section 3.2
{-# LANGUAGE ViewPatterns #-} -- Section 3.3
-- Section 2.1, 2.2
import Control.Exception (Exception(..), throwTo, catch, mask_)
import Control.Concurrent (ThreadId, myThreadId, threadDelay)
import Control.Exception (getMaskingState, MaskingState(..))
-- Section 2.3
import Control.Exception (TypeError(..))
-- Section 3.2
import Control.Exception (SomeException)
import Control.Concurrent (forkIO, killThread)
import System.Random (RandomGen, randomR, getStdRandom)
-- Trace appendix
import System.IO (hSetBuffering, stdout, BufferMode(..))
-- Perf eval appendix
import Control.Exception (assert)
import System.Environment (lookupEnv)
import qualified Control.Concurrent.Chan as Ch
import Control.Concurrent.MVar (MVar, newEmptyMVar, takeMVar, putMVar)
import qualified Criterion.Main as Cr
\end{code}
} % end ignore
\section{Brief Background}
\label{sec:background}
In this section, we briefly review the status of asynchronous exceptions in GHC
(\Cref{subsec:async-exceptions}) and the actor model of programming
(\Cref{sec:actor-model}); readers already familiar with these topics may wish
to skip this section.
Readers unfamiliar with the behavior of \verb|throwTo|, \verb|catch|, or
\verb|mask| from the \verb|Control.Exception| module may wish to first scan the
documentation of \verb|throwTo|~\citep{controlDotException}.
\subsection{Asynchronous Exceptions in GHC}
\label{subsec:async-exceptions}
The Glasgow Haskell Compiler (GHC) is unusual in its support for
\emph{asynchronous exceptions}.
%
Unlike synchronous exceptions,
which are thrown as a result of executing code in the current thread,
asynchronous exceptions are thrown by threads distinct from the current one,
or by the RTS itself.
%
They are used to communicate conditions that may require the current thread to
terminate: thread cancellation, user interrupts, or memory limits.
Asynchronous exceptions allow syntactically-distant parts of a program
to interact in unexpected ways, much like mutable references.
%
A thread needs only the \verb|ThreadId| of another
to throw a \verb|ThreadKilled| exception to it.
The standard library function \verb|killThread|
is even implemented as \verb|(\x -> throwTo x ThreadKilled)|.\footnote{
These identifiers are variously defined in \texttt{Control.Concurrent} and
\texttt{Control.Exception} in \texttt{base-4.15.1.0}.
}
%
There is no permission or capability required to access this powerful feature.
Asynchronous exceptions are peculiar because they aren't constrained to their
stated purpose of ``signaling (or killing) one
thread by another'' \cite{marlow2001async}.
%
A thread may throw any exception to any thread for any reason.
%
This absence of restrictions means that standard exceptions may be reused for
any purpose, such as to extend greetings:
\verb|(\x -> throwTo x $ AssertionFailed "hello")|.
%
Even
user-defined datatypes may be thrown as asynchronous exceptions by
declaring an empty instance of \verb|Exception| \cite{marlow2006extensible}.
%
For example, with the declarations in \Cref{fig:greet}, it is possible to greet
in vernacular: \verb|(\x -> throwTo x Hi)|.
Asynchronous exceptions may be caught by the receiving thread for
either cleanup or, surprisingly, recovery.
%
An example of recovery includes ``inform[ing] the program when memory is
running out [so] it can take remedial action'' \cite{marlow2001async}.
%
The ability to recover from a termination signal seems innocuous, but
it leaves asynchronous exceptions open to being repurposed.
\subsection{The Actor Model}
\label{sec:actor-model}
The actor model is a computational paradigm characterized by message passing.
%
\citet{hewitt1973actors} write that ``an actor can be thought of as a kind of
virtual processor that is never `busy' [in the sense that it cannot be sent a
message].''
%
In our setting, we interpret an actor to be a green thread\footnote{
A \emph{green thread} (also ``lightweight thread'' or ``userspace thread'')
is a thread not bound to an OS thread, but dynamically mapped to a CPU by a
language-level scheduler.
%
As opposed to heavier-weight OS threads, green threads simplify the implementation of a practical actor framework that supports large numbers of actors.
} with some state and an inbox.
%
When a message is received by an actor,
it is handled by that actor's \emph{intent function}.
%
An intent function may perform some actions:
send a message, update state, create a new actor, destroy an actor, or
terminate itself.
%
Unless terminated, the actor then waits to process the next message in its
inbox.
%
We will approximate this model with Haskell's asynchronous exceptions as the
mechanism for message passing.
More concretely, we think of an actor framework as
having the characteristics of a
\emph{concurrency-oriented programming language} (COPL),
a notion due to \citet{armstrong2003}.
%
After describing our framework, we will make the case (in \Cref{sec:almost-copl}) that it has many of the
characteristics of a COPL.
%
To summarize \citet{armstrong2003}, a COPL
(1) has processes,
(2) which are strongly isolated,
(3) with a unique hidden identifier,
(4) without shared state,
(5) that communicate via unreliable message passing,
and
(6) can detect when another process halts.
%
Additionally,
(5a) message passing is asynchronous so that no stuck recipient may cause a sender to become stuck,
(5b) receiving a response is the only way to know that a prior message was sent,
and
(5c) messages between two processes obey FIFO ordering.
%
While an actor system within an instance of the RTS cannot satisfy all of these
requirements (e.g., termination of the main thread is not strongly isolated
from the child threads), we will show that our framework satisfies many requirements of
being a COPL with relatively little effort.
\begin{figure}
%\raggedright
\begin{spec}
data Greet = Hi | Hello deriving Show
instance Exception Greet
\end{spec}
\caption{
\verb|Show| and \verb|Exception| instances are all that is required to
become an asynchronous exception.
}
\label{fig:greet}
\end{figure}
\section{Actor Framework Implementation}
\label{sec:actor-framework}
In our framework, an actor is a Haskell thread running a
provided main loop function.
%
The main loop function mediates message receipt and makes calls to a
user-defined intent function.
%
Here we describe the minimal abstractions around such threads that realize the
actor model.
%
These abstractions are so minimal as to seem unnecessary; we have sought to
keep them minimal to underscore our point.
\subsection{Sending (Throwing) Messages}
\label{sec:sending-throwing}
To send a message, we will throw an exception to the recipient's thread
identifier.
%
So that the recipient may respond, we define a self-addressed envelope data
type in \Cref{fig:envelope-and-intent} and declare the required instances.
\Cref{fig:static-impl} defines a send function, \verb|sendStatic|,
which reads the current thread identifier, constructs a self-addressed
envelope, and throws it to the specified recipient.
%
For the purpose of explication in this paper, it also prints an execution trace.
\subsection{Receiving (Catching) Messages}
\label{sec:receiving-catching}
An actor is defined by how it behaves in response to messages.
%
A user-defined intent function, with the type \verb|Intent| shown in
\Cref{fig:envelope-and-intent},
encodes behavior as a state transition that takes a self-addressed envelope
argument.
Every actor thread will run a provided main loop function to manage message
receipt and processing.
%
The main loop function installs an exception handler to accumulate messages in
an inbox and calls a user-defined intent function on each.
%
\Cref{fig:static-impl} defines a main loop, \verb|runStatic|, that
takes an \verb|Intent| function and its initial state and does not return.
%
It masks asynchronous exceptions so they will only be raised at well-defined
points within the loop: during \verb|threadDelay| or possibly during the
\verb|Intent| function.
The loop in \Cref{fig:static-impl} has two pieces of state: that of the intent
function, and an inbox of messages to be processed.
%
The loop body is divided roughly into three cases by an exception
handler and a case-split on the inbox list:
%
\begin{enumerate}[leftmargin=2em]
\item If the inbox is empty, sleep for an arbitrary length of time and then
recurse on the unchanged actor state and the empty inbox.
\item If the inbox has a message, call the intent function and recurse on
the updated actor state and the remainder of the inbox.
\item If, during cases (1) or (2), an \verb|Envelope| exception is received,
recurse on the unchanged actor state and an inbox with the new envelope
appended to the end.
\end{enumerate}
%
In the normal course of things, an actor will start with an empty inbox and go
to sleep.
%
If a message is received during sleep, the actor will wake (because
\verb|threadDelay| is defined to be \emph{interruptible}), add the message to
its inbox, and recurse.
%
On the next loop iteration, the actor will process that message and once again
have an empty inbox.
%
Exceptions are masked (using \texttt{mask\_}\footnote{
It is good practice to use \texttt{mask} instead of \texttt{mask\_}, and
``restore'' the prior masking state of the context before calling a
user-defined callback function.
%
Such functions may be written with the expectation to catch asynchronous
exceptions, for reasons mentioned in \Cref{subsec:async-exceptions} or
\citet{marlow2001async}.
%
For our purpose here, \texttt{mask\_} is acceptable.
}) outside of interruptible actions so that the bookkeeping
of recursing with updated state through the loop is not disrupted.
\paragraph{Unsafety}
Before moving forward, let us acknowledge that this is \emph{not safe}.
%
An exception may arrive while executing the intent function.
%
Despite our use of \texttt{mask\_},
if the intent function executes an interruptible action, then
it will be preempted.
%
In this case the intent function's work will be unfinished.
%
Without removing the message currently being processed, the loop
will continue on an inbox extended with the new message.
%
The next iteration will process the same message as the preempted
iteration, effecting a double-send.
To avoid the possibility of a double-send, a careful implementor of an actor
program might follow the documented recommendations for code in the presence of
asynchronous exceptions:
use software transactional memory (STM),
avoid interruptible actions,
or apply \verb|uninterruptibleMask|.
%
However, recall that message sends are implemented with \verb|throwTo|, which is
``\emph{always} interruptible, even if it does not actually block''
\cite{controlDotException}.
%
A solution is obtained ``by forking a new thread'' \cite{marlow2001async} each
time we run an intent function, but this sacrifices serializable
executions --- an actor must be safe to run concurrently with itself.
%
We opt for the simple presentation in \Cref{fig:static-impl}
and recommend that users write idempotent intent functions.
\begin{figure}
\raggedright
\begin{code}
data Envelope a = Envelope { sender :: ThreadId, message :: a }
deriving Show
instance Exception a => Exception (Envelope a)
type Intent st msg = st -> Envelope msg -> IO st
\end{code}
\caption{
Message values are contained in a self-addressed envelope.
%
Actor behavior is encoded as a transition system.
}
\label{fig:envelope-and-intent}
\end{figure}
\begin{figure}
\raggedright
\begin{code}
sendStatic :: Exception a => ThreadId -> a -> IO ()
sendStatic recipient message = do
sender <- myThreadId
putStrLn (show sender ++ " send " ++ show message
++ " to " ++ show recipient)
throwTo recipient Envelope{sender, message}
runStatic :: Exception a => Intent s a -> s -> IO ()
runStatic intent initialState = mask_ $ loop (initialState, [])
where
loop (state, inbox) =
catch
(case inbox of
[] -> threadDelay 60000000 {-"\hfill(1)"-}
>> return (state, inbox)
x:xs ->
(,{-"\!"-}) <$> intent state x <*> return xs) {-"\hfill(2)"-}
(\e@Envelope{} ->
return (state, inbox ++ [e])) {-"\hfill(3)"-}
>>= loop
\end{code}
\caption{
Message sends are implemented by throwing an exception.
%
Actor threads run a main loop to receive messages.
}
\label{fig:static-impl}
\end{figure}
\subsection{Dynamic Types}
\label{sec:dynamic-types}
The actor main loop in \Cref{fig:static-impl} constrains an actor
thread to handle messages of a single type.
%
An envelope containing the wrong message type will not be caught by the
exception handler, causing the receiving actor to crash.
%
We think the recipient should not crash when another actor sends an incorrect
message.\footnote{
Sending a message not handled by the recipient is like calling a function
with wrong argument types, which would cause the thread to crash in a
dynamically typed language. However, here both caller and callee are
persistent, and we choose to locate the mistake in the caller.
}
In this section, we correct this issue by extending the framework to support
actors that may receive messages of different types.
%
With this extension, our framework could be thought of as dynamically typed in
the sense that a single actor can process multiple message types.
%
This is similar to the dynamic types support in the
\verb|Data.Dynamic| module.
Furthermore, any actor may be extended by wrapping it (``has-a'' style) with an
actor that uses a distinct message type and branches on the type of a received
message, delegating to the wrapped actor where desired.\footnote{
It is not sufficient to wrap a message type in a sum and write an actor
that takes the sum as its message.
%
Such an actor will fail to receive messages sent as the un-wrapped type.
%
To correct for this, one would need to change existing actors to wrap their
outgoing messages in the sum.
%
\Cref{sec:dynamic-types} generalizes this
correction without requiring changes to existing actors.
}
%
It may seem natural to encapsulate such actor-wrapping in combinators that
generalize the patterns by which an actor is given additional behavior.
%
However, here our goal is not to lean into the utility of a dynamically
typed actor framework, but to point out how little scaffolding is required to
obtain one from the RTS.
\subsubsection{Sending Dynamic Messages}
Instead of sending an \verb|Envelope|
of some application-specific message type
we convert messages to the ``any type''
in Haskell's exception hierarchy,
\verb|SomeException| \cite{marlow2006extensible}.
%
\Cref{fig:dyn-impl} defines a new \verb|send| function that converts messages,
so that all inflight messages will have the type \verb|Envelope
SomeException|.
\subsubsection{Receiving Dynamic Messages}
\label{sec:dynamic-recv-loop}
On the receiving side, messages must now be downcast to the \verb|Intent|
function's message type.
%
This is an opportunity to treat messages of the wrong type specially.
%
In \Cref{fig:dyn-impl} we define a new main loop, \verb|runDyn|,
that lifts any intent function to one that can
receive envelopes containing \verb|SomeException|.
%
If the message downcast fails, instead of the recipient crashing, it performs a
``return to sender.''
%
Specifically, it throws an exception (not an envelope) using the built-in
\verb|TypeError| exception.\footnote{
The extensions \texttt{ScopedTypeVariables}, \texttt{TypeApplications}, and
the function \texttt{Data.Typeable.typeOf} can be used to construct a
helpful type error message for debugging actor programs.
}
These changes do not directly empower actor intent functions to
deal with messages of different types.
%
We have only removed
application-specific type parameters from envelopes.
%
Actors intending to receive messages of different types will do so by
downcasting from \verb|SomeException| themselves.
%
Such actors will use an intent function handling messages of type
\verb|SomeException|.
%%%, and will work equally well with \verb|runStatic| or \verb|run|.
%
We will see an example of this usage pattern in \Cref{sec:dyn-ring}.
\begin{figure}
\raggedright
\begin{code}
send :: Exception a => ThreadId -> a -> IO ()
send recipient = sendStatic recipient . toException
runDyn :: Exception a => Intent s a -> s -> IO ()
runDyn intentStatic = runStatic intentDyn
where
intentDyn state e@Envelope{sender, message} =
case fromException message of
Just m -> intentStatic state e{message=m}
Nothing
-> throwTo sender (TypeError "...")
>> return state
\end{code}
\caption{
The dynamically typed framework
upcasts before sending
and downcasts before processing.
}
\label{fig:dyn-impl}
\end{figure}
\subsection{Safe Initialization}
\label{sec:safe-fork}
When creating an actor thread, it is important that no exception arrive before
the actor main loop (\verb|runStatic| in \Cref{fig:static-impl})
installs its exception handler.
%
If this happened, the exception would cause the newly created thread to die.
%
To avoid this, the fork prior to entering the main loop must be
masked (in addition to the mask within the main loop).
\Cref{fig:run} defines the main loop wrapper we will use for examples in
\Cref{sec:ring-impl}.
%
It performs a best-effort check and issues a helpful reminder to mask the
creation of actor threads.\footnote{
We do not define a wrapper around \texttt{forkIO} to perform this masking
because actors that perform initialization steps can currently do so
before calling \texttt{run}. \Cref{sec:main2-init} is an example of this.
}
\begin{figure}
\raggedright
\begin{code}
run :: Exception a => Intent s a -> s -> IO ()
run intent state = do
ms <- getMaskingState
case ms of
MaskedInterruptible -> runDyn intent state
_ -> error "mask the forking of actor threads"
\end{code}
\caption{Remind users to prevent initialization errors by masking forks.}
\label{fig:run}
\end{figure}
\section{Example: Ring Leader Election}
\label{sec:ring-impl}
The problem of \emph{ring leader election} is to designate one node
among a network of communicating nodes organized in a ring topology.
%
Each node has a unique identity, and identities are totally ordered.
%
Nodes know their immediate successor, or ``next'' node, but do not know the
number or identities of the other nodes in the ring.
%
A correct solution will result in exactly one node being designated the leader.
%
This classic problem from the distributed
systems literature serves to illustrate our actor framework,
despite leader election being unnecessary in the context of threads in a process.
\citet{chang1979decentralextrema} describe a solution to the ring leader election problem that begins with every
node sending a message to its successor to nominate itself as the leader
(\Cref{fig:ring-election-visual}).
%
Upon receiving a nomination,
a node forwards the nomination to its successor
if the identity of the nominee is
greater than its own identity.
%
Otherwise, the nomination is ignored.
%
We implement and extend that solution below.
\begin{figure}
\include{ring.tex}
\caption{
In-progress ring leader election with seven nodes
(\citeauthor{chang1979decentralextrema}'
\citeyear{chang1979decentralextrema} solution).
%
The node identities are unique and randomly distributed.
%
Two nomination chains are shown:
%
Node 5 nominated itself and was accepted by nodes 3, 1, and 4; next node 4
will nominate 5 to node 6 (who will reject it).
%
Concurrently, node 6 nominated itself and was accepted by node 2 but
rejected by node 7.
%
For this election to result in a leader, node 7 must nominate itself.
}
\label{fig:ring-election-visual}
\end{figure}
\subsection{Implementing a Leader Election}
Each node begins uninitialized, and later becomes a member of the ring when
it learns the identity of its successor.
%
To represent this we define two constructors in \Cref{fig:node-types} for node
state type, \verb|Node|.
\noindent
Three messages (also defined in \Cref{fig:node-types} as type, \verb|Msg|) will
be used to run the election:
\begin{itemize}[leftmargin=1.5em]
\item[--] \verb|Init|: After creating nodes, the main thread initializes
the ring by informing each node of its successor.
%
\item[--] \verb|Start|: The main thread rapidly instructs every node to start
the leader election.
%
\item[--] \verb|Nominate|: The nodes carry out the election by sending and
receiving nominations.
\end{itemize}
%
\begin{figure}
\raggedright
\begin{code}
data Node = Uninitialized | Member {next::ThreadId}
data Msg
= Init{next::ThreadId}
| Start
| Nominate{nominee::ThreadId}
deriving Show
instance Exception Msg
\end{code}
\caption{
Election nodes can be in one of two states,
and they accept three different messages.
}
\label{fig:node-types}
\end{figure}
\subsubsection{Election Termination}
\label{sec:election-termination}
The node with the greatest identity that nominates itself will eventually
receive its own nomination after it has circulated the entire ring.
%
That same node will ignore every other nomination.
%
Therefore the algorithm will terminate because node identities are unique
and only one nomination can circumnavigate the ring.\footnote{
In the context of this paper, termination is guaranteed because we have
reliable message passing (see \Cref{sec:almost-copl}).
%
In the context of a distributed system, with unreliable message passing, it
is possible that no nomination makes it all the way around the ring.
%
In such a situation, the algorithm could terminate without a
winner.
}
\subsubsection{Node-Actor Behavior}
\label{sec:ring-intent-fun}
The intent function for a node actor will have state of type \verb|Node| and
receive messages of type \verb|Msg|, as defined in \Cref{fig:node-types}.
%
We show its implementation and describe each case below.
%
\begin{code}
node :: Intent Node Msg
\end{code}
%
When an uninitialized node receives an \verb|Init| message, it becomes a member
of the ring and remembers its successor.
%
\begin{code}
node Uninitialized
Envelope{message=Init{next}} = do
return Member{next}
\end{code}
%
When a member of the ring receives a \verb|Start| message,
it nominates itself to its successor in the ring.
%
\begin{code}
node state@Member{next}
Envelope{message=Start} = do
self <- myThreadId
send next $ Nominate self
return state
\end{code}
%
When a member of the ring receives a \verb|Nominate| message, it compares the
nominee to its own identity.
%
If they are equal, then the member wins and the algorithm stops.
%
If the nominee is greater, then the member forwards the nomination to its
successor.
%
\begin{code}
node state@Member{next}
Envelope{message=Nominate n} = do
self <- myThreadId
case () of
_ | self == n -> putStrLn (show self ++ ": I win")
| self < n -> send next (Nominate n)
| otherwise -> putStrLn "Ignored nomination"
return state
\end{code}
%
\ignore{
\begin{code}
node _ _ = error "node: unhandled"
\end{code}
}
\subsubsection{Election Initialization}
\label{sec:main1-init}
The election initialization function
is implemented in \Cref{fig:ringElection}.
%
It takes the size of the ring
and an unevaluated \verb|IO| action representing node behavior,
and takes the following steps to start the election:\footnote{
The implementation shown doesn't handle rings of size 0 or 1.
%
Also, we do not show thread cleanup.
}
%
\begin{enumerate}[leftmargin=2em]
\item Create actors (with asynchronous exceptions masked).
\item Randomize the order of actor \verb|ThreadId|s.\footnote{The implementation of \verb|permute| is in \Cref{apx:permute-impl}.}
\item Inform each actor of the \verb|ThreadId| that follows it in the
random order (its successor) with an \verb|Init| message.
\item Send each actor the \verb|Start| message to kick things off.
\end{enumerate}
%
To call the election initialization function, we construct an \verb|IO| action
by passing the node intent function and the initial node state to the actor
main loop from \Cref{fig:run}:
%
\ignore{
\begin{code}
main1 :: Int -> IO ()
main1 count = do
ring <-
\end{code}
}
%
\begin{center}
\begin{code}
ringElection count $ run node Uninitialized
\end{code}
\end{center}
%
\ignore{
\begin{code}
threadDelay 1000000
mapM_ killThread ring
\end{code}
}
%
An election execution trace appears in \Cref{fig:main1-trace}.
\begin{figure}
\raggedright
\begin{code}
ringElection :: Int -> IO () -> IO [ThreadId]
ringElection n actor = do
nodes <- sequence . replicate n . mask_ $ forkIO actor {-"\quad\hfill (1)"-}
ring <- getStdRandom $ permute nodes {-"\quad\quad\hfill (2)"-}
mapM_
(\(t, next) -> send t Init{next}) {-"\quad\quad\hfill (3)"-}
(zip ring $ tail ring ++ [head ring])
mapM_ (\t -> send t Start) ring {-"\quad\quad\hfill (4)"-}
return ring
\end{code}
\caption{Ring leader election initialization.}
\label{fig:ringElection}
\end{figure}
\begin{figure}[h]
\raggedright
\scriptsize