Tuesday, November 28, 2006

On thread persistence and migration

Having contacted the Hop authors, I got a good feeling on their progresses, and I'm looking forward for the next (beta?) version. Also, they suggested me a PhD thesis, "A Portable Mechanism for Thread Persistence and Migration", whose details can be found in Wei Tao's Ph.D. Research Page.



Her dissertation is a good explanation on how a Java program may be adapted into having support for thread suspension and resume. She based her work on a complete code re-writing (both at source and byte-code level), following a (theoretically correct) set of transformations. In short, a program must emulate the creation of a continuation on each code sentence, using an exception mechanism to capture the state of the running program. This exception usage is handy, because after catching an exception the code may throw it again, or store (persistently) the execution state. Obviously there's the problem of non serializable resources having to be saved and restored, and the local, temporary variables. Tao, cleverly leaves this issue to the application creator, with two resolution strategies:

  • Creating a wrapper class to the resource, that knows how to close and recreate it (e.g., close a file port and re-opening it a few days later);
  • Setting the an existing object field to the resource placed in a local variable;
It's relevant to note that Tao relies on the use of the transient modifier, that makes such a field null when the object is serialized - having to be re-created on deserialization.



The process is heavily focused on the Java Virtual Machine, and the transformation is not proved to make such sense in other programming languages/environments. Nevertheless, the byte-code transformation strategy is well documented and several performance considerations are made throughout the dissertation. I, however, believe that this approach may be simplified - particularly the exception-based technique used to save/restore execution state - when the application is written in some other languages, like Scheme, that natively support first-class continuations.



Her motivation to this technique is well founded, focusing both portability (she may employ this transformations on several applications, and even with other programming languages, as long as their core is closely related to Java's) and code mobility (inevitably the best reason to save thread execution state and to be able to restore/resume it later, although "Moving long running applications from desktops to laptops and back also has clear appeal").



One interesting point she states from the beginning of the report is that making a thread persistently saved is quite similar to having it migrated to another place, the difference being that the store is the run-time environment of another process/machine.



If you're interested in execution-flow migration, thread state, and similar issues, I advise you to read this report, or at least to skim through it.



4 comments:

Unknown said...

An interesting project to attain thread persistence in jave is javaflow (http://jakarta.apache.org/commons/sandbox/javaflow/). I used it in the past to code a lightweight mobile agent platform with strong migration and it was very fun to use it for that purpose. The library provides thread persistence by means of continuations, a well-known feture provided by languages like scheme.

Edgar Gonçalves said...

Hmm, I haven't kept up with Javaflow's progresses. I'll take a look into it soon, make some experiments, and will post back my thoughts on it. Thanks for the tip!

Unknown said...

If you want some help, its main developer is Torsten Curdt (http://vafer.org/blog/) from Apache Software Foundation. We wrote an article with him in the past about this topic, but finally it were not published.

Edgar Gonçalves said...

Is that paper publicly available somewhere? It seems the Javaflow website only refers the short tutorial and the API documentation - I'm sure that your paper, albeit not having been published, would be a more interesting read on this subject!