by gmfawcett · 6y ago · 0 comments
You seem to be saying that Erlang can implement "Actor behaviour change", just not directly. In what sense is this a concern? i.e., what value does direct implementation add here?
Erlang not being able to directly express Actor behavior change means that Erlang programs are more obscure and convoluted.
For example, consider the following program:
ProcessorController[pk:PublicKey] *implements* controller
// initialize processor controller with a public key pk
currrentVersion ≔ 1 // processor current version number is initially 1
running ← Crowd [1] // running is a crowd of at most 1 running activities
boot[*itemIs* i *whichIs* Package[*imageIS* code, *versionIs* packageVersion],
signedIs s] ↦ // received boot request with item i and signed s
*IsEmpty* running *cases* // Check if the processor is running
True ⇾ // If not running, then
SigningChecker.[*itemIs* i, *signedIs* s, *publicKeyIs* pk] *cases*
// check that i was signed by s with private key for pk
True ⇾ // If signing check succeeds, then
currentVersion⩽packageVersion *cases*
// check processor current version is
// less or equal than package version
True ⇾ // If version check succeeds, then
(currentVersion ≔ packageVersion; // first update current version number,
code.run thru running)
// afterward run code in a hole of mutual exclusion passing
// thru running then returning a termination report
False ⇾ // If version check fails,
*Throw* BadVersionException[] // throw bad version exception
False ⇾ *Throw* BadSignatureException[] // If signature check fails,
False ⇾ // If already running, then
*Throw* AlreadyRunningException[] // throw already running exception
shutDown ↦ // received shut down request
*IsEmpty* running *cases* // Check if the processor is running
True ⇾ // If not running, then
*Throw* NotRunningException[] //throw not running exception
False ⇾ // If already running, then
*Cancel* running // cancel running returning Void
The challenge in Erlang is to implement the hole in the region of mutual exclusion in the above implementation. fun Self (PublicKey) ->
CurrentVersion = 1,
receive
{upgrade, Function, Version, Signature} ->
case sign_checker(PublicKey, Function, Version, Signature) of
true when Version > CurrentVersion -> Function(PublicKey);
true -> throw(bad_version);
false -> throw(bad_signature)
end;
shutdown -> ok;
_Other ->
% here's where you handle work
% But I don't know what work you wanted to do,
% so I'm just looping
Self(PublicKey)
end end.
You probably don't actually want to throw for these errors (why should the server stop if it got an invalid request?), and you probably want your messages to include a reply tag, so you can get the results (like gen_server:gen_call/2,3 does), but you know, examples on the interwebs; don't run them as-is. Controller *interface*
boot[*itemIs* Item, *signedIs* Signed] → TerminationReport
// boot request with an item and signed returns
// a termination report from the boot request
shutDown → Void // shut down request returns Void
The challenge in Erlang is to allow the shutDown request to be received even though the boot request has not yet returned with a termination report.The cancellation exception is caught within the implementation of the run request and turned into a termination report.
PS. The definition the type Item is as follows:
Item ≡ Package[*imageIs* Binary, *versionIs* Version] fun Self (State) ->
{PublicKey, Ref, Function} = case State of
{P, R, F} when is_ref(R), is_function(F) -> {P, R, F};
Other -> {Other, false, false}
end,
CurrentVersion = 1,
receive
{upgrade, Function, NewVersion, Signature} when Ref == false ->
SignRef = make_ref(),
async_sign_checker({self(), SignRef}, PublicKey, Function, NewVersion, Signature),
Self({PublicKey, SignRef, Function, NewVersion);
{upgrade, _, _, _} -> throw (no_concurrent_upgrades);
{sign_check, SignRef, true} when Version > CurrentVersion -> Function(PublicKey);
{sign_check, SignRef, true} -> throw(bad_version);
{sign_check, SignRef, false} -> throw(bad_signature);
{sign_check, _, _} -> throw(bad_sign_check);
shutdown -> ok;
_Other ->
% here's where you handle work
% But I don't know what work you wanted to do,
% so I'm just looping
Self(PublicKey)
end end.
assuming async_sign_check sends back something sensible {sign_check, SignRef, true}. async_sign_check would send the work to another process, if you want it to stop checking the signature in case of early shutdown, the processes could be linked.Although, I'm still not quite sure I understand the system you're trying to get to. This in the context of {become, F}, so I'm assuming you want the process receiving the boot request to become the requested function. But you also want a termination report; and I'm assuming the function isn't expected to terminate, it's expected to receive and process requests. But, you didn't tell us where you want these reports sent?
In my updated code, there is certainly a possible window where the signature checker finished near the same time as a shutdown request was sent. In that case, if the shutdown request is received first, the process would shutdown; if the signature result is received first, that would be processed, and if the signature result and versioning was good, the process would become the new function, and that function would receive the shutdown request. If the new function has some complicated setup, and you wanted to shut it down in the middle of that, you would most likely need to use erlang:exit(Pid, kill); although, a carefully written function could periodically check for a shutdown message in the middle of startup. I don't find that a terribly useful usecase --- if you started something, let it finish starting, or just brutally kill it; architecting a clean shutdown from a partial start is most likely to be wasted effort, instead, one should architect their start so it doesn't perturb the state of the world until it is fully started and can be expected to shutdown cleanly.
Also, I don't see where it updates the version number and whether the code is running?
Furthermore, where does it perform code.run?
Thanks for clarifying. I'm not familiar with your theory, but at least I understand why you raised the point -- some Erlang programs are needlessly complicated due to the language's (colloquial!) actor semantics.
Maybe you're referring to junior-to-BEAM programmers reaching to prove they can use gen_servers everywhere?
There are also obscure corners of OTP that are a bit of a mess, but I'd say that's because it's inherited a lot of C-style folkways and doesn't have hierarchical module aliasing in the same way that say Elixir has. And there's a substantial amount of using modules-like-factories that got picked up in certain corners of the ecosystem, probably from Java habits, but I don't see that as much in Elixir, which suggests that it's not the actor semantics so much as, just outdated programming trends that have accumulated in the erlang ecosystem by virtue of it being older, and a "less popular" language that other people try to shove their round pegs into (arguably elixir is the latest iteration of that but they seem to have made some really good choices, at least in my opinion).
What Erlang programs have you found to be needlessly complicated?