Computer Science: Smart Code Reloading

How do you reload code at a per-module level? How do you deal with the data that the module might contain?

Reloading code on the fly is something that the original Lisp machines were famous for. Erlang/OTP is famous for this too. In my own project, Aquarium, which is a Python Web application framework, I use to do this trick as well.

In Python, reloading code is relatively easy (with a bunch of caveats having to do with import "graphs" and inheritance hierarchies). However, what do you do with the data? When you reload the module, the old data in that module is lost.

I've always wondered how the Lisp guys did it. How did they cope with changes in the data format? If you have a list of tuples of length 3, what happens if the new code expects a list of tuples of length 4?

In Rails land, they have database migration scripts. Hence, you specify the entire schema as an iterative set of changes to the database, starting from an empty database. You can also back out a migration if things don't work out.

I'm going to make a hypothesis. I suspect Erlang/OTP already does it this way using Mnesia, their in-process, distributed database. First of all, you don't keep any state at the module level. In true functional style, data is on the "stack" (although how the language is implemented is something else). Data that needs to survive a module reload is stored in an in-process "database". Note, I'm using the term "database" loosely, and I'm definitely not talking about SQL. To change the data format of the data stored in the "database", you write a migration. Hence, when you reload a module, you get new code, and you migrate the old data.

(Thanks go to Alex Jacobson and Mike Cheponis for many stimulating discussions.)


Bob Ippolito said…
That's not really how Erlang does it at all. If you're using Mnesia and you want to make a change to the schema of a table you would create a new table and migrate the old table's data to the new one. I've very rarely had to do this.

At the lowest level, module reloading is quite simple and pleasant. The way it works is that the VM will keep up to two versions of a given module alive at a time (old and new). Reloading a module kills all processes that reference "old", mark "new" as old, and load the latest version of code as "new".

A fully qualified function call "foo:bar(baz)" will always use the "new" foo. A local function call "bar(baz)" will use the same module version as the caller. Basically you just write code that does fully qualified calls at strategic places.

This means that a process is going to see a consistent view of the module's code for as long as it needs to, while new processes use the latest and greatest. Servers will most likely do a fully-qualified call when it tail recurses so it will upgrade itself.

You can prevent an extremely idle process from getting killed over two successive code updates by using erlang:hibernate or proc_lib:hibernate to wipe the stack and put it to sleep until it gets a message.
LeCamarade said…
I'm quite interested in hotswapping. Because it is a future norm. All the more reason to do Erlang ... :o)
augustss said…
Just to add to what Bob say. If your function used to take a 3-tuple, but now expects a 4-tuple, then you have to code your function to accept both. Because when the new code gets called from the old code it will be called the way the old code was written, i.e., with a 3-tuple. So you provide the upgrade code right there in your module. Of course, once you have switched from old to new, you can remove the upgrade code from the new module if you want.

I think Erlang's solution to hot code loading is very nice.
Bob and augustss, thanks! That's exactly what I was looking for.