Glossary¶
There’s a lot of similar terms and proper nouns – let’s try to untangle them!
General concepts¶
- crash-only software
-
The core idea of crash-only software is that software gets significantly simpler and more robust if you focus on fast recovery after a crash instead of trying to handle all possible errors everywhere.
With that, initialization is recovery. It’s “Have You Tried Turning It Off And On Again?” for applications.
In bgt, a supervised service can crash anytime, as long as its work factory knows how to initialize all necessary resources.
The concept is closely related to the Let It Crash philosophy in the – famously fault-tolerant – Erlang ecosystem. The term was coined by George Candea and Armando Fox in their Crash-Only Software paper.
- microreboot
-
Microreboots make crash-only software more practical in the real world.
Usually, you don’t want your whole application to crash because one of ten HTTP connection pools went bad. But recovering a resource pool can be difficult or impossible (for example, with stuck resources).
So you “reboot” only a part of your application and reinitialize the resource from scratch.
Supervised services with work factories lend themselves to microreboots.
As with crash-only software, there’s a paper by Candea et al. on it.
Services¶
- service
- A background thread running a
Servicein a loop. - worker
- The process that runs bgt’s threads and services.
Loops and progress¶
- loop
- A long-running, blocking
Loop.run()that a supervisor drives. Within bgt: a service. - loop cycle
-
One pass through a loop’s body: wait, then act.
A service cycle waits for its wakeup and runs work units. A loop reports its progress through its
has_completed_cycleattribute. - loop run
-
One execution of a loop’s
run(), from start until it returns or crashes. The supervisor starts a new loop run after a backoff.Therefore, a loop run consists of zero (crashed or stopped before first loop cycle) to infinite (never crashes, never exits) loop cycles.
- interval
- The upper bound on a loop cycle’s wait, if not interrupted by a wakeup. It’s the maximum wait time between loop cycles.
Work¶
- work unit
- One call of
do_work, time-bounded by contract. Whiledo_workreturnsTrue, the next work unit runs back-to-back within the same loop cycle. do_work- The callable that performs one work unit per call.
It’s your code and the reason why bgt exists.
Its signature is the
DoWorkprotocol. - work factory
- Makes a loop run’s
do_workand cleans up when the loop run ends. It is called at the start of every loop run, so setup is also recovery.as_work_factorywraps a plaindo_workthat needs no setup.as_async_work_factorywraps an asyncdo_workinstead.
Waking¶
- wakeup
- The object a service
wait()s on between loop cycles. It can be anything that satisfiesWakeup. bgt shipsIntervalOnlyWakeup, which has no external wake source. pgbg’sSubscriptionis one that fires on PostgreSQLNOTIFYevents. - wake
- The pending signal that a wakeup’s
wait()consumes by returningTrue.Wakeup.wake()delivers one, and any number of pending wakes coalesce into one.
Supervision¶
- supervisor
- Owns the thread, the backoff, and the restart policy for one loop.
Usually a
Supervisor. - crash
- A loop run that ends with an exception.
- backoff
- The exponentially growing delay between a crash and the next loop run. It resets after a loop run’s first completed loop cycle.
- stop
- The cooperative shutdown request.
SupervisedService.stop()sets the loop’s stop event and wakes it. - handle
- A batteries-included supervised facade:
SupervisedService. It offersstop(),is_running, and a context manager.