Workbench / The TiffinBox Build
S41 · V3·40
Starting and Stopping
Volume 3, chapter 40. Started on a port another process held, the TiffinBox server printed a BindException and then hung: main had died, but the order log's writer is a non-daemon thread, so the JVM stayed up, holding the log's lock and serving nothing. A damaged record made the log's constructor throw, keeping its file lock and three descriptors, so a second attempt failed with OverlappingFileLockException. This stage makes startup fail fast with a reason and an exit code, reads the jar's class files to check what was compiled (finding that stage 38's clock rule missed method references), and measures how far the first request is from the ten-thousandth.
Stage 41: starting and stopping
The brief
What you'll build.
- Startup that fails cleanly:
OrderLog's constructor releases its lock and files when it can't finish;- a record that can't be read raises
LogDamagedException, naming the file, the record number and the byte offset; Mainexits 65 (damaged data) or 75 (port in use) with one sentence an operator can act on, and closes the log if the server can't start.tools/ClassScan.java: a class file reader with no libraries (magic, version, constant pool), run withjava tools/ClassScan.java <jar>. It checks that every class is version 65 and that no class but the entry point refers to a method that reads the wall clock.--jdklists the JDK classes the code names that read the clock themselves.- LangLint
ambient-timefor member references:Instant::nowandSystem::currentTimeMillisare flagged like the calls. - Release-check steps: the class scan, and a busy port that must exit 75 rather than hang.
- Tests:
anUnreadableKnownTagNamesTheRecordAndReleasesTheLock, and the damaged-log test updated for the new exception.
Why a real company needs this. A service that hangs instead of exiting defeats every supervisor: systemd and Kubernetes restart processes that exit, not ones that sit there. An error that names the record and byte lets an operator decide in minutes instead of reading a stack trace through the night.
Why now. Chapter 40 is JVM internals: the life of a Java program, bytecode and the constant pool, class loading, memory, the JIT and garbage collection. This stage applies its first three parts: how a JVM starts and ends, and what a class file contains.
What you'll have at the end.
- a server that, whatever stops it starting, exits within a second with a reason and a code;
- a check on the compiled classes that complements the source rules;
- warm-up numbers for TiffinBox's own pages;
- ADR-0043: startup failures and exit codes.
Theory to own
- When a JVM exits. What ends it, besides
mainreturning? (The chapter, "The life of a Java program"; Companion, Gaps 1 and Code walkthrough.) - Constructors and resources. Who closes what a constructor opened, if it throws? (Companion, Gaps 2.)
- The constant pool. What does a class file record about the methods it calls? (The chapter, "Bytecode".)
- Warm-up. Why is a page that renders in 14 µs slow for its first users? (The chapter, "Tiered compilation"; Companion, Gaps 4.)
Build it
Step 1: start on a busy port
Hold port 18994 with another process and run serve on it, with timeout 20.
Checkpoint. exp/41/busy-port-before.txt.
Step 2: a damaged record
Write exp/41/Corrupt.java: three good records and one whose checksum is valid but whose idempotency-key bytes (tag 1) are 6f1c2a. Open it twice in one JVM, count descriptors still open on the directory, then run serve on it.
Checkpoint. exp/41/corrupt-before.out, serve-before.txt.
Step 3: fail cleanly
OrderLog: wrap opening and recovery so that, if they didn't finish,finallycloses the log channel, releases the lock and closes its channel. It's afinally, not acatch: LangLint'sbroad-catchrule refusescatch (RuntimeException), and afinallyreleases whatever the failure was.recover: throwsLogDamagedException(file, record, offset, why, cause)for an unreadable key and for damage followed by good records.Main.serve: mapsLogDamagedExceptionto exit 65 andBindExceptionto exit 75, closing the log if the server didn't start.
Checkpoint. exp/41/corrupt-after.out, serve-after.txt, busy-port-after.txt; core 52 tests.
Step 4: read the class files
Write tools/ClassScan.java and run it on the server jar, then on a jar of exp/41/Sneaky.java (a class holding Instant::now and System::currentTimeMillis as method references). Then add visitMemberReference to LangLint, with cases in test/Time.java.
Checkpoint. exp/41/classscan.out, exp/41/method-refs.out; langlint: 3 test files, every expected warning and no others.
Step 5: from cold to warm
Write exp/41/Warm.java: render /menu in-process 30,000 times from a cold JVM, printing requests 1, 2, 10, 100, 1,000, 10,000 and 30,000; run it with default flags, -XX:TieredStopAtLevel=1 and -Xint, twice each.
Checkpoint. exp/41/warm.out.
Senior and architect lens
1. Every exit path is part of the interface. Exit codes, the first line on stderr, and what is left locked are read by supervisors and operators at the worst moment. Test them like an API: the release check now starts on a busy port and checks for 75.
2. ADR-0043: startup failures and exit codes. Write it with these points:
- Codes: 65 for data that can't be read (never repaired automatically), 75 for a temporary condition (port in use), 1 for anything unexpected.
- Messages: one sentence on stderr that says what, where (file, record, byte) and the choices.
- Resources: constructors release what they took when they fail; the entry point closes the log if the server doesn't start.
- Threads: non-daemon threads only for work that must finish (the log writer), and they're stopped on every exit path.
- Bytecode check:
ClassScanruns in the release check, as a backstop for the source rules.
3. Check the artefact, not only the source. The compiler, generated code and dependencies all stand between source and what runs. The jar is what ships, and it can be read with a hundred lines of code.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| a slow page after each deploy | readiness before warm-up | stage 48 (Spring Boot: readiness and warm-up) |
| more threads (jobs, schedulers) | more ways to outlive main |
stage 52 (jobs: lifecycle and shutdown) |
| repairing a damaged log by hand | operators editing bytes | stage 44 (build systems: a tbx log repair tool, with a backup first) |
| the clock scan's "names, not calls" | false alarms from dependencies | stage 44 (build systems: a call-graph check) |
Evidence
exp/41/busy-port-before.txt(hung, 124) andbusy-port-after.txt(exit 75 in 0 s).exp/41/corrupt-before.out(3 descriptors,OverlappingFileLockException) andcorrupt-after.out(0, the same clear error twice).exp/41/classscan.out,method-refs.out; LangLint's test file covers member references.exp/41/warm.out,hook.out.- Core 52, server 46 and tbx 21 tests;
golden master: 47 exchanges, identical. The release check passes, including the two new steps (withNG_NODEset for the Angular build). - ADR-0043 is committed.
Carry forward
- Exit codes 65 and 75, and the message format, for every startup failure later stages add.
ClassScanfor any new rule about what the code may call.- Warm-up numbers as the baseline for stage 48's readiness.