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.
- traps exits
- links to the table user worker process (the supervisor starts the table user worker first)
- creates the
- sets itself as the table heir process
- 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!