views:

369

answers:

8

I'm currently brainstorming over the idea how to upgrade a program while it is running. (Not while debugging, a "production" system.)

But one thing that is required for it, is to actually submit the changed source code or compiled byte code into the running process.

Pseudo Code

var method = typeof(MyClass).GetMethod("Method1");
var content = //get it from a database (bytecode or source code)
  SELECT content FROM methods WHERE id=? AND version=?
method.SetContent(content);

At first, I want to achieve the system to work without the complexity of object-orientation. That leads to the following requirements:

  • change source code or byte code of function
  • drop functions
  • add new functions
  • change the signature of a function

With .NET (and others) I could inject a class via an IoC and could thus change the source code. But the loading would be cumbersome, because everything has to be in an Assembly or created via Emit. Maybe with Java this would be easier? The whole ClassLoader is replacable, I think.

With JavaScript I could achieve many of the goals. Simply eval a new function (MyMethod_V25) and assign it to MyClass.prototype.MyMethod. I think one can also drop functions somehow with "del"

Which general-purpose platform can handle such things?

+1  A: 

With JavaScript it can be done. Whats amazing is that Google's V8 engine is open source and is easy to implement into any C++ program.

http://code.google.com/p/v8/

Of course you will have to write a bit of a library to have functionality exposed and loading of the script from inside the JavaScript. It will depend on what you are wanting to do.

Daniel A. White
+1  A: 

Most of dynamic languages have this capability. Take a look at Ruby: you can modify existing methods etc. at runtime. When IronRuby is out, you will be able to do this also in .Net platform.

bbmud
Tcl is another language where this is pretty simple.
Bryan Oakley
Ruby is indeed a dynamic language -- if you have the console open you can redefine methods. However, it is not clear to me how you would do this in practice... For example, if you are running Mongrel as a web server... how would you get access to an in-process shell?
David James
+2  A: 

In Java, you have the OSGi project, which facilitates upgrading and changing modules of your application without touching other modules.

If you don't mind learning something different, the Erlang programming language was designed from the ground up with this type of application in mind.

Gerco Dries
+2  A: 

My impression is that right now Erlang is very visible as a language which has this capability. That said, my father-in-law (a master programmer, in my opinion) has told me that he's implemented hot-swappable code on a somewhat older platform -- assembler for what they now call z/OS (OS/390 before that).

Personally, I've been looking for ways to do this in the Java space, where the vast majority of my professional work is currently done. In Javaland, the best publicized effort to provide hot-unloading (as far as I know) is the work done by the OSGi Alliance. That said, this solution necessarily involves some classloader magic because of how some common Java libraries are architected (example: JDBC DriverManager). If you choose to go down the OSGI route, your code will likely require extensive auditing and testing to ensure that it will be usable with the OSGi architecture.

As an alternative to implementing hot-swappable code, perhaps you could implement a system which appears to have this capability using the potentially simpler mechanism of request queueing. For example, if you need to hot-swap the piece of your system which processes large, backend requests, why not send these requests through an intermediary which can dispatch them to the backend component if it is running and accumulate them in a queue if the component is down? This might allow you to upgrade the backend component independently of the rest of your system without redeploying as we say in the industry "the whole shebang".

Paul Morie
A: 

thank's for the very good answers.

  • I'll check out OSGI first, but I'm skeptikal, because it's all about components (each component one file?)
  • dynamic languages: I will try PHP, Python and Ruby: I think adding and replacing code will be simple, but unloading code the hard part
  • JavaScript: maybe it's also an option, additionally to the C++ hook mentioned, COM offers you the door to many existing components
  • Erlang: until now I heard it only in the context of parallel computing, I'll take a look at the manual!

I'm still not very sure what can be done with such a system ( I have no urgent practical requirement for it), but I have the feeling that it's extremely powerfull for:

  • very easy deployment updates
  • highly efficient servers that host many application versions to many different customers
  • customizing (of installations)
  • integration
  • development: compiling a 2MB assembly is pretty inefficient, when you changed only a small piece of code (Okay, interpeted languages have this advantage anyway)
  • large systems (SAP uses such an approach, and no one would think of compiling the whole SAP-Installation when changing a function)
SO is not a discussion board, don't put comments as answers, add them to the original question or as a comment to an answer
fuzzy lollipop
+1 what fuzzy lollipop said
David James
+1  A: 

But one thing that is required for it, is to actually submit the changed source code or compiled byte code into the running process. Which general-purpose platform can handle such things?

Erlang was already mentioned, and it uses explicit "synchronization points" where a running routine may explicitly update itself by doing a "self" call using "?MODULE:routine()."

This is another important thing to keep in mind: you don't just need the capability in the VM to replace running code, but the running code also needs a way to respond to such updates and adjust accordingly.

You may also want to look into UpgradeJ which is a language that was specifically designed with this requirement (code hot swapping) mind.

none
+1  A: 

I think any image-based language would support this. I know Common Lisp does, since it's probably one of the most common ways to deploy Lisp web apps, but I suspect it would work pretty much the same in, say, Smalltalk.

Ken
A: 

Erlang can do everything you are looking for, and it doesn't rely on any bolt-on libraries (unless you count OTP in that category) or coding work-arounds. It can maintain state (i.e. "values of variables" in imperative-language-speak) across the reload, and does not require transactions to be buffered or retransmitted. If you write your code in the OTP style then you can have your application programmers write straightforward sequential code that will have some degree of ability to execute in parallel, and to support hot reload, and all this without themhaving to worry about the details of how it's done. It just works.

Tim