5 ms·
How about something like this? I don't understand the code you wrote exactly, and I smooshed the processor into the controller, because it simplifies things.
by toast0 7y ago
How about something like this? I don't understand the code you wrote exactly, and I smooshed the processor into the controller, because it simplifies things.
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.
- ProfHewitt 7y agoThe signature for a controller to implement is as follows: 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]
- toast0 7y agoOh, I think I see. How about 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.
- ProfHewitt 7y agoLooks like you are still not implementing the signature of a Controller, which as follows: 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 A boot request checks the item which is a Package with code and packageVersion and then runs the code. The boot request returns a termination report that results from running the code. However, when the boot request is still operating, the controller might receive a shutDown request, which will cause the cancellation of the running code that will caught within the run request and turned into a termination report. Note that there is no timing error in the Actor implementation because, a shutDown request cannot be received until code.run has released the region of mutual exclusion.
- deleted 7y ago[deleted]
- toast0 7y agoOK, so this isn't really {become, F}, this is {run, F}. The function F, is expected to return results and terminates, so it is no longer running (but, it could have spawned a process or otherwise changed the world state; Erlang is generous with respect to side effects) I don't really understand the version number means in this case? You can only run functions with the same or greater versions than had previously been run? I think you're somehow asking for this to be both synchronous, in the sense that the result of running the function is returned directly and asynchronous in that you'd like to be able to cancel it. If you want the return directly, I can't also have previously returned a cancellation id. If you just want to run a function, and get the results back, that's not that hard; but before I write the code for that, I want to really be sure what you're asking for.
- ProfHewitt 7y agoWith respect to your question: "I don't really understand the version number means in this case?" An item can be booted only if its version number has not regressed in to prevent replay attacks. There are no cancellation ids in the Actor implementation, however a cancellation exception can be thrown when an activity is cancelled.
- ProfHewitt 7y agotoast0: Doesn't look like your Erlang code implements the required signature for a Controller? Also, I don't see where it updates the version number and whether the code is running? Furthermore, where does it perform code.run?
- toast0 7y agoFunction(PublicKey) runs the code, which presumably knows its new version number. As mentioned otherwise, I had assumed this was in the context of {become, F}; but perhaps you had something else in mind.
- ProfHewitt 7y agoThe code is just binary code, which can only be run. The Actor implementation of a Controller just runs the code and so the controller does not have to itself perform a become. The variables currentVersion and the crowd running hold the state of the changing behavior of the controller. A tricky part for Erlang is that the variable running must updated even if code.run is cancelled.
- toast0 7y agoThe Erlang version doesn't need to become to run code either. It is just a fun thing to do. It's straightforward to just run the code as well; if you get a function F, you just do F(), assuming the function has no arguments, because you didn't include any; you can also use erlang:apply(F, Args), if you had passed a list of arguments. Honestly, I think we're going back and forth here, because we're not speaking the same language. I don't know what a crowd is, and I don't know what cancelled means. Those aren't concepts in Erlang. I'm not sure if you mean to have a distinction between 'boot' and 'run', you're using them both, and I think to mean the same thing you send a boot request when you want to run a piece of signed code? Erlang is foundationally about independent processes with ordered mailboxes. The two ways to interpret cancel in Erlang would be a) send a message 'cancel', and hope the process receives it before completing the work, or b) kill the process. A process can set a flag so that most kills will be turned into sending a message, but there's also an untrappable kill. Processes can also be linked and/or monitored for propagation of kills. You would typically model something with state like this as a process. The process would essentially be looping on a single function, passing any state it needed to keep to itself. In this case, you'd send it a boot request with the code to run etc, and your process id and a reference to get a response, and potentially a cancellation request, and maybe a get current version, and get running status (you didn't spec it, but they're both useful). The state in this case, that would be the current version, the public key, the current boot request (if any), and tracking details for the asynchronous signature checking, and tracking the running of the code. If you want to be able to kill the running code, without killing this process that has the state, then you'd need to spawn a new process to run the code, and you'd track that. If you don't really need that, you could just run the code in this process, and when it returns send back a result.