0 XP
CH12 CAPSTONE6-Milestone Production Blueprint
0/6 Milestones Verified
CH12 · Milestone 1 of 6Production Capstone
src/xmpp_sup.erl
XMPP Server 5-Module Architecture
1/4
M1: xmpp_sup
rest_for_one · intensity: 5/10s
M3: session_registry
ETS · monitor/2 cleanup
ETS query
M5: xmpp_router
PID ! Xml · Offline Queue
TCP Socket
M2: stream_parser
binary:copy/1 unpin
M4: c2s_statem
4-State FSM · SASL
Step 1: Supervision Tree Boot (M1)

xmpp_sup boots xmpp_session_registry (Child 1) followed by xmpp_router (Child 2) under rest_for_one.

supervisor:start_link/3 · rest_for_one (5 restarts / 10s)

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.

Important

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

StrategyCrash on Child #1 (registry)Crash on Child #2 (router)Verdict for XMPP Core
one_for_oneRestarts #1 only; #2 holds stale table stateRestarts #2 onlyUnsafe on #1 reset
one_for_allRestarts #1 and #2Kills healthy #1 unnecessarilyDisruptive on #2 crash
rest_for_oneTerminates #2 first, restarts #1 then #2Restarts #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) -> StartLinkRet
3 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) -> ok
8 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 ?MODULE with initialization arguments [].
  • Examples:
  • xmpp_sup:start_link() boots both children and returns {ok, SupPid} where is_pid(SupPid) is true.

2. stop()

  • Input: None.
  • Behavior: Looks up the locally registered ?MODULE process; if unregistered (undefined), returns ok idempotently; if alive, stops the supervisor cleanly and returns ok so whereis(xmpp_sup) becomes undefined.
  • Examples:
  • xmpp_sup:stop() returns ok both when running and when already stopped.

3. init([])

  • Input: Empty list [].
  • Behavior: Returns {ok, {SupFlags, ChildSpecs}} configuring:
  • SupFlags: rest_for_one strategy, max restart intensity 5, sliding period 10 seconds.
  • ChildSpecs: Two permanent worker child maps in strict startup order with shutdown => 2000:
  1. id => xmpp_session_registry, start MFA calling xmpp_session_registry:start_link/0, modules [xmpp_session_registry].
  2. id => xmpp_router, start MFA calling xmpp_router:start_link/0, modules [xmpp_router].
  • Examples:
  • xmpp_sup:init([]) yields child IDs [xmpp_session_registry, xmpp_router] in exact order under rest_for_one.

---

Rules & Verification Constraints

  • Compile cleanly with erlc -Wall (zero warnings under -behaviour(supervisor)).
  • start_link/0, stop/0, and init/1 must all be exported.
  • Child specification order MUST place xmpp_session_registry first and xmpp_router second as permanent workers (shutdown => 2000).
  • Live boot verification executes xmpp_sup:start_link(), supervisor:which_children(SupPid), and xmpp_sup:stop().
Quick Mental Model CheckPredict Before Coding

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]
Functions to Implement & Export:
start_link/0stop/0init/1
Verification Hint:

Call 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

OTP Refsupervisor:start_link
supervisor:start_link(Module, Args) -> {ok, Pid} | {error, Reason}
OTP Refgen_statem:start_link
gen_statem:start_link(Module, Args, Opts) -> {ok, Pid} | {error, Reason}
OTP Refets:lookup
ets:lookup(Tab, Key) -> [Object]
OTP Referlang:monitor
erlang:monitor(process, Item) -> MonitorRef