The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S38 · V3·37

One Clock

Volume 3, chapter 37. The pricing rules were pure: they saw the time only through Facts. But the review page built its preview with Instant.now(), so with the server's clock fixed at 08:00 and a breakfast discount, the review said ₹179.00 and the receipt said ₹159.00. This stage adds a compiler rule that bans reading the system clock anywhere but the entry point. It found four places: the review page, the CSP report log, the entry point itself, and the kitchen event log's epoch, which turned out to be an identity made from a time. 9,991 of 10,000 logs made back to back shared one.

6 parts · ~4 min to read
Updated Suggest a correction

Stage 38: one clock

The brief

What you'll build.

  • A LangLint rule, ambient-time: a warning (an error with -Werror) for Instant.now(), LocalDate.now() and the other java.time now()s without a Clock, System.currentTimeMillis(), Clock.systemUTC() and its siblings, and new Date(). now(clock) and System.nanoTime() (for durations) are allowed;
  • fixes for everything it finds: the review page and the CSP log read the server's clock (Server.now()), the entry point keeps the one real clock with a reviewed suppression, and the event log's epoch becomes a random identity;
  • tests: the rule's own test file, and PagesTest.theReviewIsPricedByTheServersClock, which fixes the server's clock 12 hours away from the real one.

Why a real company needs this. Clocks are injected so that tests, replays and investigations can choose the time. One direct read of the system clock defeats that silently: everything passes in production, where both clocks agree, and fails exactly when someone is trying to reproduce a problem. A compile-time rule is cheaper than finding each one in an incident.

Why now. Chapter 37 is functional programming: functions as values, pure functions, streams and laziness, Optional and monads, side effects and parallelism, and a functional core inside an imperative shell with an injected Clock. This stage applies its first idea, purity, to the inputs the code doesn't show.

What you'll have at the end.

  • one clock per server, read through one method;
  • a review page and a receipt that agree at any server time;
  • a build that refuses new direct reads of the system clock;
  • ADR-0040: time is an input.

Theory to own

  1. Purity and hidden inputs. Interpreter.price(rules, facts) is pure. Was the review page's pricing pure? (The chapter, "First principles"; Companion, Gaps 1.)
  2. Functional core, imperative shell. Which part of the server should decide what time it is? (The chapter, "Functional style in a Spring service".)
  3. Identity against time. What is the event log's epoch for, and does it need to be a time? (Stage 17.)
  4. Static checks with types. Why is a compiler plugin better here than grep 'now()'? (Stage 34.)

Build it

Step 1: see the two prices

Write exp/38/Review.java: a server with Clock.fixed at 08:00 in Chennai and the rule rule "Breakfast" when hour < 11 then ₹20 off; request the review page for two masala dosas, place the order through /checkout, and print the review's total and the receipt's.

Checkpoint. exp/38/review-before.out.

Step 2: the rule

Add ambient-time to LangLint (tools/lint) with test/Time.java: each banned call marked // expect: ambient-time, and the allowed ones (now(clock), ZonedDateTime.now(clock.withZone(...)), nanoTime(), new Date(0L), a suppressed entry point) unmarked. Build core, server and tbx with it.

Checkpoint. langlint: 3 test files, every expected warning and no others; exp/38/lint-before.txt.

Step 3: one clock

Add Instant now() to Server, read it in Pages.review and in the CSP log, and annotate Main.serve with @SuppressWarnings("langlint:ambient-time") and a comment. Add theReviewIsPricedByTheServersClock: a server clock 12 hours from the real one, a rule hour = H for the server's hour, and the review and receipt both at ₹159.00.

Checkpoint. exp/38/review-after.out; 43 server tests.

Step 4: an identity, not a time

Write exp/38/Epochs.java: create 10,000 pairs of KitchenEvents back to back and count pairs whose ids share an epoch; then simulate a restart (two events on one log, three on a new one) and ask the new log what a client with the old id missed. Then make the epoch eight random base-36 digits.

Checkpoint. epochs-before.out and epochs-after.out; golden master: 47 exchanges, identical.

Senior and architect lens

1. Make the hidden input a parameter, then make the parameter the only way in. The fix (s.now()) took one line; keeping it fixed takes the rule. Tests catch the cases someone thought of; the rule catches the next Instant.now() someone writes.

2. ADR-0040: time is an input. Write it with these points:

  • One clock: Main creates it; the server reads it through now(); core functions take an Instant or Facts, never a clock.
  • One reading per decision: a request that makes several decisions reads the time once and passes it on (the review reads it once for its preview).
  • The rule: ambient-time is on in every module; a suppression needs a comment saying why (today: the entry point only).
  • Durations: System.nanoTime() for measuring elapsed time; it's not a time of day and isn't banned.
  • Identities: never derived from the time; random or sequential.

3. A bug that hides in production hides from the people debugging it. Production's clock agreed with the system's, so the mismatch existed only in tests and replays, the tools used to investigate production. Code that behaves differently under the investigation tools than in production makes every investigation slower.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
a library that reads the clock itself (a scheduler, a cache's expiry) the rule sees only our code stage 41 (JVM: what libraries do at startup), stage 53 (resilience: timeouts under a test clock)
a request that spans two reads of now() two decisions at different times stage 49 (Spring Web: a request context carries one instant)
time zones and DST in the business day BUSINESS_ZONE is a constant stage 47 (Spring Core: configuration), Volume 23 (internationalisation)
tests that need time to pass Clock.fixed doesn't move stage 55 (backend testing: a settable test clock)

Evidence

  • exp/38/review-before.out shows ₹179.00 against ₹159.00; review-after.out shows ₹159.00 on both.
  • exp/38/lint-before.txt lists four findings; the build now has none.
  • exp/38/epochs-before.out and epochs-after.out: 9,991 against 0.
  • LangLint: 3 test files; core 51, server 43 tests; golden master: 47 exchanges, identical; the suite passes 44.
  • ADR-0040 is committed.

Carry forward

  • Server.now() is the server's only way to read the time.
  • ambient-time joins LangLint's rules for every module, including the ones later stages add.
  • Facts and values in, decisions out is the shape for the pricing and validation cores later stages extract (stage 46).