Archive for May, 2013

Implementation of “Don’t Lose Your ets Tables”

May 8th, 2013  |  Published in erlang, reliability  |  Bookmark on

A couple years ago I wrote about how to avoid losing your ets tables if the Erlang processes that own those tables crash. The original post resulted from accidentally losing an ets table full of video subscriber data during a debug session on a live customer production site. Oops.

If you’re looking for some code that implements what that post describes, look no further than DeadZen‘s etsgive repository. DeadZen is a very experienced Erlang developer, so it’s no surprise that his example code is straightforward and clean.

Just as described in my post, DeadZen’s supervisor code starts two workers, one that manages the table and one that uses it. The table manager process takes the following steps:

  1. traps exits
  2. links to the table user worker process (the supervisor starts the table user worker first)
  3. creates the ets table
  4. sets itself as the table heir process
  5. gives ownership of the table away to the table user worker process

The table user worker process first handles the transfer message resulting from the manager giving it ownership of the table. Once it becomes the owner, it can handle a call to increment a counter in the table and a call to check the table contents. It can also handle a call to die, which you would issue interactively from an Erlang shell to exercise the code. This causes the worker process to die, which means the table manager regains ownership of the table because it’s the table’s heir. The table manager then waits for the supervisor to start a a new table user worker process, and once it’s up and running, the manager links to it and then transfers table ownership to it.

This written description might be difficult to work through, but fear not, DeadZen also supplies an example Erlang shell session showing how it all works. Clone his repository and try it out for yourself!