Skip to main content

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.)

Comments

Unknown 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.
Anonymous 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.
jjinux said…
Bob and augustss, thanks! That's exactly what I was looking for.

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;)

In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3, Erlang, and the original Lisp machine. The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts.

Now, I'm sure that many of these ideas have already been envisioned within Tunes.org, LLVM, Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway.

Rather than wax philosophical, let me just dump out some ideas:Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code." I bet it&…