I wrote some library code recently, triggered by a post I read. The gist of the author's post was to compare Scala and Erlang. I actually agree with him. I want to bring your attention to where I come into this, Hot code swapping.
I created a library capable of swapping out dynamic code, particularly run on a server. If I want to change behavior of some code without having to recompile the code and reboot the server, I should be able to do this.
My first attempt at this was more of a proof of concept, but it does work. Now, it's more of a pseudo hot swap solution, involving remote actors in the internal Scala library.
Here's some example of the client code in question.
import com.philipcali.hotcode.HotSwap._ def log(s: String) { // logs text to file appender } for(i <- 1 to 10) { println("Iteration %d" format(i)) dynamic('main) { log("Potentially dynamic code") } // Sleep for a second so I can inject some code Thread.sleep(1000) }
The control construct dynamic
is what we zero in on. The code inside that
block is stored on a RemoteActor
. Now, a few seconds later,
we want to change it.
import com.philipcali.hotcode.HotSwap._ def log(s: String) { // logs text to file appender } update('main) { log("Just been injected!") }
The call to update
here, replaces what's currently running with whatever is in
the update code block. Under the covers, nothing is lost. The RemoteActor
is
actually storing revisions of the dynamic code, which can be replaced at any
given time.
If we look at the resulting output in our logged file:
Potentially dynamic code Potentially dynamic code Potentially dynamic code Just been injected! Just been injected! Just been injected! Just been injected! Just been injected! Just been injected! Just been injected!
So there you have it: pseudo hot swapping. The dynamic code here is hardly dynamic, because everything is compiled, but changing behavior to an application should be fairly painless. Now, to do what the author wanted in that comparison post (which was to simply add a log line), you don't have to shutdown the server!
No comments:
Post a Comment