184 points · by cprecioso · 6y ago · 54 comments
When he sends the "{become, F}" message, and F is a function pointer or closure, how is that dispatched across a network? Do the code definitions need to be present remotely too? If so, I'm not sure that quite qualifies as a "universal" server, only a server for locally defined functions. Still cool though.
BTW, I've used a similar technique to create a pool of worker goroutines in Golang. Had good performance from it too.
In erlang, if you want, you can compile whole modules from source constructed at runtime and they behave just like normal code. Typical usage is hot-loading new modules from disk or from other nodes, you load your patch on one node and distribute it over network and if you don't make something stupid, your code is updated in whole system without dropping any tcp connections or the like. Everything happens automatically and every process using old code is updated as soon as possible to new version when possible.
With another ~25 years of collective experience, if somebody took a shot at it today I think we could make some improvements, but it was pretty far ahead of its time, and can still pull some stunts almost nothing else can, or at least not without creating "an ad-hoc, informally-specified, bug-ridden, slow implementation of half of [Erlang]", to tweak Greenspun a bit.
https://news.ycombinator.com/item?id=9512955
What I understood was that objects/function are referred by hash of the source/dependency tree so that updates can be propagated transparently.
[1] http://erlang.org/doc/apps/erts/erl_ext_dist.html#export_ext
def adder(v):
return lambda x: v + x
Can I then send the result of (say) "adder(5)" over the network and have it work?On a erlang shell in node A, do:
register('test', self()).
receive F -> ok end.
(this gives the shell a registered name, and then waits for a message)On a debug shell on node A do:
AddrFactory = fun (X) -> fun (Y) -> Y + X end end.
{test, NodeA} ! AddrFactory(5).
Then, back on node A, do F(10).
And you'll get 15For more fun, you could have node A send the value to go into the closure, etc.
NodeA is something like 'demo@hosta.example.org'
NodeA would be started with something like erl -name demo@hosta.example.org -setcookie demo
and NodeB started like erl -name demo@hostb.example.org -setcookie demo
variable = 5
lambda = &(&1 + variable)
File.write!("pickle", :erlang.term_to_binary(lambda))
close terminal, open new shell unpickled = "pickle" |> File.read! |> :erlang.binary_to_term
unpickled.(5)
yields 10.You could, of course, send that file over the network (via email, tcp/ip, carrier pigeon, whatever), and it's even easier if you use built-in erlang distribution primitives, which indeed do this under the hood.
Keep in mind that by default values are immutable, so when you're building that lambda it takes out a fixed-in-time reference to that 5 (or whatever, you could even make it user input). In other languages, if that variable changes... Something strange might happen, depending on what that underlying datatype is. In particular, side effects can exist for lists but not scalars in python.
In elixir, you can rebind that variable 5 to something else but it won't change the lambda (erlang doesn't allow you to do that at all, except in the repl, sort of). You could also do that with, say, a list datatype, and you get the same deal.
for example:
list = [1,2,3]
lambda = fn -> length(list) end
lambda.() #==> 3
list = list ++ [4, 5]
list #==> [1,2,3,4,5]
lambda.() #==> 3
which is what lets you have a lambda with no expectation on the mutability of the bound-in values. What you get is what you expect, no matter how far away or disconnected or even dead the source is. And that respect for the physics of causality and relativity, at the core, is what makes the BEAM languages so great for distributed systems.https://erlang.org/doc/apps/ssl/ssl_distribution.html#specif...
Dependencies are also just atom (universal, global, enum) references, so there's no pointers involved.
Do you mind telling more about this? Or link, if it’s on Github
http://marcio.io/2015/07/handling-1-million-requests-per-min...
but made more generic. The 'task' passed to each worker was a closure that was executed by the worker.
https://news.ycombinator.com/item?id=12396420 (38 comments)
https://news.ycombinator.com/item?id=8807660 (2 comments)
Consequently, Erlang cannot directly implement Actor behavior change. A factorial server is implemented below:
FactorialServer[ ] *implements* ProcedureServer<[NaturalNumber], NaturalNumber>
[i] |-> // received message [i]
Factorial.[i] // return result of sending [i] to Factorial
In order to implement Actor behavior change, Erlang must resort to using helper processes that trampoline back so that helper processes can return the desired change.See the following for direct implementation of Actor behavior change:
https://papers.ssrn.com/abstract=3418003
PS. The type ProcedureServer above can be defined as follows: ProcedureServer<t1, t2> ≡ // Procedure server with parameters types t1 and t2 is defined to be
t1 -> t2 // type of procedure from t1 into t2You 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.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.
https://www.youtube.com/watch?v=uORIxJhOjkI
I miss him dearly.