xmpp_sup boots xmpp_session_registry (Child 1) followed by xmpp_router (Child 2) under rest_for_one.
Milestone 1: The Root OTP Supervisor Hierarchy
Why This Matters in Production
In production telecom switches, distributed brokers, and real-time chat backends (WhatsApp, ejabberd, MongooseIM), faults are inevitable: sockets drop mid-frame, out-of-spec client payloads raise exceptions, and memory pressure hits individual workers.
Rather than burying every line inside defensive try/catch blocks, Erlang/OTP embraces "Let It Crash": workers fail cleanly inside isolated processes while a Supervision Tree deterministically restores a known-good topology around them.
Architectural Dependency: xmpp_router queries the in-memory ETS session table owned by xmpp_session_registry. Because child startup order defines dependency direction in OTP, xmpp_sup must guarantee the registry always starts first and any registry restart resets the downstream router.
---
Deep Concept Breakdown & Under the Hood
| Strategy | Crash on Child #1 (registry) | Crash on Child #2 (router) | Verdict for XMPP Core |
|---|---|---|---|
one_for_one | Restarts #1 only; #2 holds stale table state | Restarts #2 only | Unsafe on #1 reset |
one_for_all | Restarts #1 and #2 | Kills healthy #1 unnecessarily | Disruptive on #2 crash |
rest_for_one | Terminates #2 first, restarts #1 then #2 | Restarts #2 only (#1 stays alive) | Ideal dependency isolation |
Under the hood, the OTP supervisor terminates downstream siblings in reverse start order before starting recovered children in forward start order, using shutdown timeouts so each terminate/2 runs cleanly before forced termination. If restarts exceed intensity within period seconds, the supervisor gives up and propagates failure upward rather than spinning in an infinite crash loop.
---
Official OTP Supervisor API Reference
1%% Official OTP Signatures (stdlib / supervisor)2supervisor:start_link(SupName, Module, Args) -> StartLinkRet3 Types: SupName = {local, Name :: atom()} | {global, term()} | {via, module(), term()}4 Module = module(), Args = term()5 StartLinkRet = {ok, pid()} | ignore | {error, term()}6 7supervisor:stop(SupRef) -> ok8 Types: SupRef = pid() | atom() | {global, term()} | {via, module(), term()} | {atom(), node()}Attach to local process registration with whereis/1 when implementing stop/0 so calling stop/0 on an already-stopped supervisor returns ok instead of raising noproc (see starter export stubs and the attached OTP supervisor card below).
---
Assignment Specification
Implement src/xmpp_sup.erl with -behaviour(supervisor), exporting start_link/0, stop/0, and init/1:
1. start_link()
- Input: None.
- Behavior: Starts the OTP supervisor locally registered as
?MODULEwith initialization arguments[]. - Examples:
xmpp_sup:start_link()boots both children and returns{ok, SupPid}whereis_pid(SupPid)istrue.
2. stop()
- Input: None.
- Behavior: Looks up the locally registered
?MODULEprocess; if unregistered (undefined), returnsokidempotently; if alive, stops the supervisor cleanly and returnsoksowhereis(xmpp_sup)becomesundefined. - Examples:
xmpp_sup:stop()returnsokboth when running and when already stopped.
3. init([])
- Input: Empty list
[]. - Behavior: Returns
{ok, {SupFlags, ChildSpecs}}configuring: SupFlags:rest_for_onestrategy, max restart intensity5, sliding period10seconds.ChildSpecs: Twopermanentworkerchild maps in strict startup order withshutdown => 2000:
id => xmpp_session_registry, start MFA callingxmpp_session_registry:start_link/0, modules[xmpp_session_registry].id => xmpp_router, start MFA callingxmpp_router:start_link/0, modules[xmpp_router].
- Examples:
xmpp_sup:init([])yields child IDs[xmpp_session_registry, xmpp_router]in exact order underrest_for_one.
---
Rules & Verification Constraints
- Compile cleanly with
erlc -Wall(zero warnings under-behaviour(supervisor)). start_link/0,stop/0, andinit/1must all be exported.- Child specification order MUST place
xmpp_session_registryfirst andxmpp_routersecond aspermanentworkers (shutdown => 2000). - Live boot verification executes
xmpp_sup:start_link(),supervisor:which_children(SupPid), andxmpp_sup:stop().
In xmpp_sup, why is xmpp_session_registry ordered before xmpp_router under rest_for_one?
#{strategy => rest_for_one, intensity => 5, period => 10}
[RegistryChildSpec, RouterChildSpec]start_link/0stop/0init/1Call xmpp_sup:start_link(), inspect child processes with supervisor:which_children(xmpp_sup) to confirm two permanent workers, and verify xmpp_sup:stop() shuts down cleanly.
Relevant Official Erlang / OTP Reference Cards
supervisor:start_link(Module, Args) -> {ok, Pid} | {error, Reason}gen_statem:start_link(Module, Args, Opts) -> {ok, Pid} | {error, Reason}ets:lookup(Tab, Key) -> [Object]erlang:monitor(process, Item) -> MonitorRefNo Verification Run Yet
Tap Submit Capstone below to grade all 5 OTP modules and the live Chaos Monkey resilience test.