Our recent (March 30, April 3, and April 6) explorations into Christian Sunesson’s fork of iserve taught us much about OTP/Erlang functionality and conventions, including directory structures, supervision, and gen-server functionality.

In the April 6 post we explored how iserve’s start-up logic created two child processes, a supervisor, iserve_server_sup, and a gen_server, iserve_master.

Now we’re in a position to plunge deeper into the heart of iserve. So let’s poke into iserve_server_sup.

There’s repetition of material from earlier posts here. Think of it as cram review before your midterm.

And, all due cautions here: beware possible Newbie misunderstandings or misinterpretation in what follows. As always, I invite corrections.

First, note the -behavior(supervisor) directive. This tells us that iserve_server_sup is an interface to the supervisor pattern in OTP/Erlang.

iserve_server_sup exports four functions: start_link/0, start_link/1, add_server/2, and init/1.

Recall that during the iserve start_up sequence, iserve_sup created a supervisory process registered as iserve_sup. iserve_sup’s initialization process, iserve_sup:init/1, in turn, called iserve_server_sup:start_link/1, passing in iserve_sup’s PID in the variable Supervisor.

The function iserve_server_sup:start_link/1 creates and registers a new second-level supervisory process called iserve_server_sup. The name is defined in the macro SERVER declared in the -define directive at the top of the module source listing.

When the supervisory process iserve_server_sup is created, supervisor:start_link/3 calls init/1, which initializes the new supervisory process.

iserve_server_sup:init/1 first instantiates the variable AChild with a tuple that defines a call to iserve_server, then fires off the message {ok, {{simple_one_for_one,10,300},[AChild]}}.

So who’s waiting with abated breath for this message?

The OTP/Erlang generic supervisor. The message defines a simple_one_for_one restart strategy for iserve_server_sup, with no more than 10 restarts within 300 seconds. AChild defines which child processes iserve_server_sup should start and monitor.

The value of AChild is:

{iserve_server,{iserve_server,start_link,[iserve_master]},
temporary,2000,worker,[iserve_server]}

So how do we interpret all this? We’ve gone through this before, but let’s it again (nothing like repetition to sharpen the Newbie mind):

iserve_server – a name used to identify the child specification by the supervisor.

iserve_server, start_link,[iserve_master] – Start function in form of M,F,A; e.g. iserve_server:start_link(iserve_master).

temporary – restart function.

2000 – Shutdown. The supervisor will tell the child process to terminate by calling exit(Child,shutdown) and then wait for an exit signal with reason shutdown back from the child process. If no exit signal is received within the specified time, 2000 seconds in our case, the child process is unconditionally terminated using exit(Child,kill).

worker – child type

iserve_server – Module; used by the release handler during code replacement to determine which processes are using a certain module. Module is the callback module, if the child process is a supervisor, gen_server or gen_fsm.

(April 22, 2009 — Mea Culpa!: I think I stepped off the track when I wrote the following. Don’t believe a word of it. I’ll revisit as soon as I’m more confident that I know what I’m talking about. LRP)

So it’s looking like the supervisor iserve_server_sup is going to be looking after the gen_server iserve master.

Let’s check it out by poking into iserve_master.

First off, Christian tells us that iserve_master provides “Bookkeeping of running servers.”

Next, the -behavior(gen_server) tells us that it provides the callback functions for gen_server.

I hesitate to plow through the bloody details of gen_server. Been there. Done that. So I won’t. You can read about it here.

For our purposes, the things to note are the internal functions call_add_server and call_del_server.

Now these look interesting, but by now you’re undoubtedly bored to tears and I’m running out of pep.

So let’s pick it up another day.

Leave a Reply