You know you’re getting old when you walk in on your 50th high school reunion to find wall-to-wall gray hairs.

You really know you’re getting old when it takes three days to recover from the ravages of a red-eye flight from San Francisco to Boston.

Where did all those years go? And where the vim, vigor, and vitality?

Yes, I can hear the young’uns smirking away so smugly. All I can say is, your time is coming.

Well, I’m back now and continuing my exploration of Webmachine.

First thing I noted as I eased back into the saddle was changes to the quick start page.. So, ignore much of what I said in my last post and simply follow the quick start instructions. Works like a charm for me this time around.

As I started delving deeper into the documentation, however, newbie anxieties began rising up out of the pit of my stomach. It’s all there, I’m sure, everything you need to know to build awesome websites on top of Webmachine. But some of the plain English was turning to Greek before my very eyes.

What to do? Maybe hacking out a few examples to challenge and verify my feeble understanding of the Webmachine docs would help. So, in this spirit, I decided to start with URI dispatch configuration and work my way down into more esoteric matters.

All that follows may bore wizards silly. But with all good hopes of helping along a newbie or two, here’s what I did:

At the tail end of the quick start page Justin suggests that you modify the resource /tmp/mywebdemo/src/mywebdemo_resource.erl. Rather than modify mywebdemo_resource.erl, I made six copies — res1.erl … res6.erl.

I then opened each copy and updated the module name: e.g. -module(mywebdemo_resource) to -module(res1), etc.

I also modified the html payload: e.g. {“Hello, new world”, ReqData, State} to {“Hello, res1.”, ReqData, State}, etc.

Now I have seven web pages in my demo site. But before I can access them, I need to update :/tmp/mywebdemo/priv/dispatch.conf.

Here’s what dispatch.conf looked like when I was done:

{[], mywebdemo_resource, []}.
{["res1"], res1, []}.
{["res2"], res2, []}.
{["res3"], res3, []}.
{["res4"], res4, []}.
{["res5"], res5, []}.
{["res6"], res6, []}.

Finally, I killed the Webmachine webserver, executed /tmp/mywebdemo$ make, and restarted the webserver.

So now I have a test bed for exploring Justin’s URI dispatch configuration examples.

Here’s how to access each page:

http://localhost:8000/
http://localhost:8000/res1/

http://localhost:8000/res6/

… but you knew that.

I’ll share my next steps in my next post.

See ya’ll.

OTP and Erlang in Action

August 17, 2009

Martin Logan is one of the many bright people I met at Erlang Factory Palo Alto.

Martin has been programming Erlang for more than 10 years — full time for the last five. He’s the core developer at Erlware and the primary developer of the Faxien package management system.

During one of the Erlang Factory breaks Martin patiently defined “referential transparency” for me. Fancy phrase for a fairly simple concept it turns out — an expression that always returns the same result given the same input is said to be “referentially transparent.”

Anyway, I recently purchased from Manning Publications the MEAP edition of OTP and Erlang in Action by Martin Logan, Eric Merritt, and Richard Carlson. And it’s sure to be an Erlang classic.

The cool thing about MEAP is that you pop in your credit card number and get back an instant *.pdf download of the book in progress. As the authors add content, you get e-mail notification of updates from Manning. You can order hard copy of the work, of course, but you won’t get it until the formal publication date. But, I say that OTP and Erlang in Action is too important to wait. If you’re striving for professional excellence in Erlang, order both *.pdf and hard copy today.

Joe Armstrong’s Programming Erlang is, of course, the Erlang gold standard. But OTP and Erlang in Action (OTP for short) belongs right beside it. And, arguably, you might find skimming through it before you even crack open Armstrong quite profitable. Most certainly, once you’ve digested Armstrong, you’ll want to read and re-read OTP, and enter code as you go along, until the pages fall off.

OTP and Erlang in Action first leads you through the core concepts of Erlang — processes and concurrency, process communication, fault tolerance, distributed Erlang, functional programming, higher order functions, iteration vs. recursion, single assignment, and pattern matching.

All these fancy shmancy words — sounds like a trudge. But OTP is written in lucid and lively English — just the kind of plain-talk accessible language that tells you that the writers really know their stuff.

Chapter 2 introduces language basics. It’s not nearly as thorough as the Erlang tutorials or Armstrong, but it complements them nicely and, more to the point, sets you up for some dazzling hands-on programming.

To wit, the remaining 11 chapters, guide you through solid OTP production-level code — a telnet server all packaged up as an OTP application and a distributed caching system. This is code you can build and use. Along the way you learn in great detail about OTP packaging and organization, processes and linking, building a production system, distributed Erlang, tracking and managing distributed code, packaging and deployment, hot code loading and upgrading, non-native Erlang distribution, drivers and multi language interfaces — all the good stuff that was left to your imagination in other texts.

As a newbie, I’ve got to say, this book is a real find. Can’t wait for the authors to finish it.

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.

Just spotted this on Christian Suneson’s blog

“I probably broke iserve build-simplicity tonight.”

Don’t know if Christian has fixed it yet. But if you have trouble bringing up iserve, it’s a good guess that Christian’s on the case.

In our last two posts, March 30 and April 3, we poked into Erlang/OTP conventions, supervisor processes, and application resource files.

Yes, I know, this is supposed to be a Newbie Test. Well, it is. But the opportunity to study how iserve is set up as an Erlang/OTP application is just too good to pass up.

Newbies need to nibble down nifty knowledge at every pass.

iserve’s application resource file, iserve.app, tells us that we must poke into iserve_sup.erl to see how iserve is fired up.

As noted in our March 30 post, you can recognize iserve_sup.erl as a supervisor callback module by its name, *_sup.erl, and the -behviour(supevisor) directive near the top of the iserve_sup.erl module.

iserve_sup.erl exports two functions: start_link/0 and init/1.

Here’s start_link/0.

start_link() -/>
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

supervisor:start_link/3 creates a supervisor process as part of a supervision tree and ensures that the supervisor is linked to the calling process (its supervisor).

Note that the first parameter of supervisor:start_link/3 is of the form {local,Name}.

This means that the supervisor is registered locally as Name using register/2.

register/2 expects a name, Name, and a process identity, P. It associates Name with the port or process identity P.

The second parameter is the name of the callback module, in this case, iserve_sup.erl.

The third parameter is an argument passed to init/1.

So, we fire off iserve:start_link/0. This kicks off a locally registered supervisor process with the name iserve_sup. Our new supervisor now calls init/1 to find out about restart strategy, maximum restart frequency and child processes.

In our case, init/1 starts two new child processes, a supervisors, iserve_server_sup, and a gen_server, iserve_master.

We looked at how supervisor processes are configured in our March 30 post.

Christian kindly provides source comments to help us understand the functions of these two processes:

    %% The servers supervisor is for keeping individual servers,
    %% adding and removing them dynamically

    %% The master is for book keeping all running servers and
    %% to hold information about them.

So what do we find when we poke into iserve_server_sup?

Well, my eyes are too blurry to find out. But I’ll try to get back to this when the dust settles. Promise.

Back anon.

Note to wizards: corrections/comments welcomed.

March 30 Poking Around Erlang touched on iserve’s application resource file, iserve.app.

If you’re an Erlang newbie, it’s worth studying this file closely. Much can be gleaned about the application you’re setting out to understand.

At risk of boring wizards beyond tears, here is iserve.ap in all it’s glory:

{application, iserve,
[{description, "Web Server"},
{vsn, "%ISERVE_VSN%"},
{modules, [ iserve_sup,
iserve_app,
iserve_server,
iserve_socket
]},

{registered, [ iserve_sup]},
{applications, [kernel, stdlib, sasl]},
{mod, {iserve_app, []}},
{env, [{port, 8080}, {callback, iserve_test}]}]}.

Why is this file important and what does it tell us?

The Erlang app man page is a good place to go for answers.

Erlang/OTP conventions call for one application resource file for each application in the system. The file should be named Application.app, where Application is the name of the application. It should be located in the application’s ebin directory.

The application resource file is read by the application controller when an application is loaded/started. It is also used by functions in systools when generating start scripts.

Note that the structure of the iserve’s resource specification is a series of comma-delimited tuples, Each tuple is prefaced with an atom, italicized below.

I’d hoped to have decoder rings made up, but the following will have to do in the interim:

application (name of the application): iserve

description (one-line description of the application): Web Server

version (version of the application): “%ISERVE_VSN%” NOTE: instantiated elsewhere

modules (all modules introduced by the application): iserve_sup, iserve_app,
iserve_server, iserve_socket

registered (all names of registered processes started in this application): iserve_sup

applications (all applications which must be started before this application is allowed to be started): kernel, stdlib, sasl

mod (application callback module and start argument): iserve_app, []

env (configuration parameters used by the application): port, 8080; callback, iserve_test

So, now we know that among all the files in the iserve directories, we must pay closest attention to iserve_server and iserve_socket to understand functionality. We must start up kernel, stdlib, and sasl before we start iserve. To start iserve, we need to call iserve_sup.

Useful overview for one short file. And the good news is that you’ll find a similar file in the ebin directory of every Erlang/OTP application.

My March 20 post pointed out how Christian Sunesson’s fork of iserve incorporates OTP system principles.

This must have seemed obvious and a tad tedious to Erlang wizards, but it’s been useful for me as a newbie to stand the rather abstract statements of principle in the official Erlang OTP docs against an actual implementation.

In my experience, you can’t have too many examples to drive an abstract idea home. If I have one generic criticism of the otherwise admirable official Erlang documentation it would be just this: too few code-level examples.

I also found the comparison of iserve source against the OTP docs useful in another way: As a newbie poking into a non-trivial Erlang system, it’s a challenge to know where to start among the plethora of directories, modules, and supporting files as one strives to understand what’s going on.

An understanding of OTP conventions whittles down the challenge considerably.

So, today I’m poking a bit deeper into the mysteries of OTP and iserve. Then, in another post or two I’ll try to lay out my understanding of how iserve works and, maybe, point out a few interesting code phases and idioms that I’ve noticed along the way.

My usual caution: I’m a newbie. If I get something wrong feel free to scold me, but better, please help us rise to better understanding with detailed what and why.

Let’s look first at two files in the iserve src directory: iserve_sup.erl and iserve_server_sup.erl.

When you dive into OTP Design Principles, the first thing you learn is that the supervision tree is a basic concept in Erlang/OTP.

Since you can read as well as I, no need to go too deeply into it here other than to identify iserve_sup.erl and iserve_server_sup.erl as examples of callback functions that tell a more generic OTP supervisor module what to supervise and how. The clue? Note the *_sup.erl module names and the -behaviour(supervisor) directives in each of the modules.

If you open iserve_sup.erl you’ll note that the init/1 function starts and supervises two worker processes, iserve_server_sup.erl and iserve_master.erl.

With guidance from the OTP docs we can further decipher what’s going on:

Here’s specification of the first worker process:

Servers = {servers_sup,
{iserve_server_sup, start_link, [Supervisor]},
permanent, 2000, worker, [iserve_server_sup]},

- servers_sup is the id of the first worker process
- iserve_server_sup, start_link tells the supervisor how start the worker
- Supervisor … frankly, this puzzle me, perhaps you can clear it up for us
- permanent says that the child function should always be restarted
- the integer 2000 is a timeout value.

    The OTP docs tells that: “An integer timeout value means that the supervisor tells the child process to terminate by calling exit(Child, shutdown) and then waits for an exit signal back. If no exit signal is received within the specified time, the child process is unconditionally terminated using exit(Child, kill). “

- worker tells the supervising process that the child function is a worker process
- iserve_server_sup is the name of the callback module

Here’s the other child process specified in iserver_sup.erl:

Master = {master,
{iserve_master, start_link, [iserve_server_sup]},
permanent, 2000, worker, [iserve_master]},

See if you can decode it. Now open iserver_server_sup.erl and see if the code doesn’t make more sense.

Now let’s look at iserve.app and iserve_app.erl.

The key to understanding these two files may also be found in the Erlang/OTP docs.

When you check it out, you’ll find that iserve_app.erl is an application callback module which specifies how to start and stop the application and iserve.app is an application resource file which specifies which modules the application consists of and the name of the callback module.

So, between the supervision and application files we have a fine road map for understanding the rest of the application.

But, enough for today. My bride will be home soon and it’s my night to bring up dinner.

Best to all.

Webmachine

Thanks to alert correspondent Jamaal we’ve just learned that Justin Sheehy has released version 1.0 of Webmachine.

Justin positions Webmachine as “an application layer that adds HTTP semantic awareness on top of the excellent bit-pushing and HTTP syntax-management provided by mochiweb, and provides a simple and clean way to connect that to your application’s behavior.”

He further notes “From the way that Webmachine’s decision core works, it follows that Webmachine’s HTTP behavior is transactional. Each HTTP Request is fully received, and the resulting HTTP Response is then fully constructed before being returned. This means that while Webmachine is suitable for a great many Web applications it is not a good fit for an application that will gradually or continuously stream responses back to clients inside the context of a single HTTP Request.”

Jamaal is smitten by Webmachine, finds it easy to work with. He’s promised notes on his experiences to date; we’re eagerly looking forward to them.

I’m thinking about moving Webmachine up the list as a Newbie Test candidate, but first need to finish up explorations of iserve.

Erlang and Ngnix and ???

Nick Gerakines is experimenting with a C/C++ Ngnix module as a front-end to Erlang.

JLouse says “Pre-process the data in other languages and make them into erlang terms which can be read fast by erlang.”

And adds “… don’t get bitten by the mnesia bug, please. It is almost always the case that mnesia and ETS are the wrong way to go.”

As a Newbie, one the attractions of Erlang was the impression that I didn’t have to pile heterogeneous software environments atop one another to get a job done. But these writers, and others, suggest that Erlang is not up to my hopes and expectations.

So what gives? What’s a Newbie to make of this? Is Erlang:

    1) Immature? Give it time and it’ll deliver on the promise.
    2) Incomplete? Give it time and it’ll embrace ever more functionality.
    3) Specialized? It is what is; can’t be all things to all people; live with it.
    4) OK as is? These guys may have a point, but by and large it’s sour grapes.

Love to hear from the gray-beard wizard contingent on this topic.

Best Practices

While we’re on the subject of Nick Gerakines, bookmark his list of Erlang Library Best Practices.

Can’t tell you how much this Newbie appreciates words of wisdom from the wise.

Formatting Dates in Erlang

Here’s another one to bookmark… a handy little function for formatting dates.

A newbie could do worse then spend a few days studying iserve source code.

This newbie is not wise enough to second guess Sean Hinde, Torbjörn Törnkvist, and Christian Sunesson on code quality. But taken as a whole, the iserve project provides a non-trivial model of web server construction. Of perhaps greater newbie value, it demonstrates Erlang and Erlang OTP conventions that every Erlang newbie should understand and emulate.

One could start with Sean Hinde’s original www.trapexit.org posting. But, as a first step, review of Christian Sunesson’s updated source side-by-side with the OTP system and design principles documentation may well be more productive.

Erlang OTP System Principles Section 1.5 lists the file types defined in Erlang OTP. An understanding of the form and function of these file types goes far toward understanding any Erlang OTP project.

Section 7.4 of the Erlang OTP Design Principles documentation suggests a directory structure for OTP applications. This structure makes it easier to make your way around OTP project source listings.

If you download and compile the latest version of iserve, you’ll note the Christian follows the OTP conventions.

We have an ebin directory plump with *.beam object files created during the compilation process.

Ignore the *.beam files in ebin, however, and you note the iserve.app file. This is an Application Resource File described in OTP Design Principles Section 7.3. In this file you’ll find the name of the application, a short description, version, modules, and other useful information.

In the include directory we find iserve.hrl file. See Section 7.1 of the Erlang Reference Manual for include file details. If you open iserve.hrl, you’ll find a record structure that defines a connection between a browser and a server. Sean provides more details.

For more on records, check Section 8 of the Erlang Reference Manual.

We peek now in the src directory and see a handful of *.erl Erlang source files.

But, hold the phone! There’s a second iserve.app file in src. We open it to find that it’s identical to the iserve.app file in the include directory. Why this should be and which is operative… beats me. Maybe one of our wizards can explain.

Among the *.erl files we find two supervision files, iserve_server_sup.erl and iserve_sup.erl. The structure and function of supervision files is documented in Section 5 of OTP Design Principles. Again, beats me why we need two. Is our wizard still around?

Another really really important thing to note can be found in iserve_sup.erl and iserve_server.erl. Grok in on the lines -behavior(xxxx). These lines tell you that you’re looking at callback modules that export pre-defined functions to to implement a common code pattern, in our case, supervision and server functionality.

There’s more to learn here, but the shadows are long and I’m headed out to the movies with my love.

Back soon fair winds willing.

Mochiweb

In case it slipped by, Jamaal posted a comment the other day that brings to our attention a meaty Mochiweb tutorial:

Design and Analysis of Mochiweb

An English language translation is available.

Unfortunately for this Newbie the garbled machine-translated English is almost as challenging as Mochiweb itself.

My how I would love to see a fluent English-speaking human take on the task of translation, since this tutorial promises much. Even more, I’d love to be able to read the original Chinese.

Most of all, I’d love to see an in-depth English language user guide to Mochiweb that shows us how to build beautiful apps on the Mochiweb foundation.

Some days I fear that Erlang fluency may be as far off for me as mastery of Chinese.

Many thanks for the links Jamaal.

And thanks to refactor for sharing your insights into Mochiweb.

Nitrogen

Nitrogen develop Rusty Klophaus tossed off this provocative comment in the Nitrogen Google Group the other day:

“Overall, Nitrogen is rapidly maturing as a framework. The interfaces
are much less volatile than they were even a few months ago.

“Within the next few month I hope to start labeling releases with major
and minor version numbers. At that point, interfaces would only change
as part of a major release, and the changes will be well documented.”

May the Force be with you, Rusty!