Postmortem: I made issue filing scan the whole project for every new number
For about a day and a half, filing an issue in our tracker mostly failed. The tool that files issues kept returning:
No number was assigned in zone
TASK, so nothing was written: the controller did not answer for a number.
It happened in every zone. Filing is the main way work gets into the system, so for that stretch sessions and operators could only add work by retrying until a request happened to get through. Sessions that needed to file a follow-up before closing their own task hit the same wall.
I wrote the code that caused this. Below is what I did wrong, what I should have written, and what I’m changing.
What happened
Each new issue gets a number from the controller. The controller keeps one counter per project and zone, assigns numbers in increasing order and never reuses one. The agent asks for a number and gives up after a fixed 10 seconds.
The controller’s health check stayed green the whole time. The controller was up. It was just too slow to answer this one request.
What I got wrong
1. I re-derived the counter on every request
The design said the controller keeps each counter “at or above the highest number its readings hold.” Readings are the stored copies of every issue file in every checkout on every machine. I took that to mean the code should compare against all the readings every time it answered.
So each number request did this:
- opened a transaction,
- read every stored reading for the project (tens of thousands of rows on our largest project) to find one maximum per zone,
- updated the counter,
- committed.
None of that was needed. The same module already raised the counter every time a reading was recorded. Any counter that existed was already at or above the readings. The scan just recomputed a value the counter already held.
2. I ran the scan on the single serialized writer
All writes in the store go through one connection, and I ran this scan inside a transaction on it. For the few seconds it took, every write from every machine in the fleet waited behind it. And each number request was itself waiting behind whatever writes the fleet already had queued.
3. I made the wait fixed and let the cost grow
The agent’s 10-second limit never changes, but the cost of answering grew with the project and with fleet activity. When the project was small and the fleet was quiet, it worked. As both grew, it started failing now and then, and then most of the time. The first failure succeeded on the third try, which made it look like flakiness when it was really a scaling problem.
4. The design didn’t bound the cost
The design described where the number comes from and what it must stay consistent with. It never said how expensive an answer could be, even though the other side of the request has a hard timeout. I implemented what was written and didn’t ask the obvious question.
5. We retried instead of investigating
When the first refusal appeared, the response was to note “retry once or twice; if it keeps failing, report it” and keep going. It kept failing for about 36 hours before anyone looked for the cause. The controller’s slow-statement log had this exact query at the top the whole time.
What I should have written
The number should come from the counter alone, in one statement:
UPDATE issue_zone_counter
SET last_number = last_number + 1
WHERE ...
RETURNING last_number;
That statement is committed before answering. The readings should only be read once, to start a counter for a zone that doesn’t have one yet. That’s the fix now in the nightly build. All existing numbering tests pass without changes, which shows the scan never contributed to the behaviour they check.
The design now says it directly: the number is answered from the zone’s counter alone, and the readings are read only to start a zone with no counter, so an answer arrives before the agent stops waiting, however big the project gets.
At the time of writing the fix is merged but not yet deployed everywhere, so some filing may still fail until it rolls out.
What I’m changing
In design: If a requirement makes a caller wait a fixed amount of time, whatever it waits on must state a cost that doesn’t grow with the data. A fixed timeout on one side and an unbounded cost on the other is a bug waiting for enough data.
In review: I’m adding two checks, and I’d like other reviewers to use them too:
- Flag any store method that runs inside a transaction on the single writer and calls something documented as reading “every” row of a project, machine or fleet, especially on a request path with a fixed client-side timeout.
- Flag any requirement that states a fixed wait without bounding the cost of what is being waited for.
In implementation: Before re-deriving an invariant on a hot path, I’ll check whether something already maintains it. Here the code a few functions away already did.
In operations: If the primary intake path refuses requests, we find the root cause the same day. “Retry and move on” is fine for a single blip. It isn’t a plan for a refusal that keeps coming back, and a green health check doesn’t mean a specific path is healthy. We already had the slow-statement log, and we should have read it on day one.
I’m sorry for the lost day and a half. This came from treating a consistency rule as something to recompute on every request. It should have been maintained once, when readings were recorded, and the counter trusted after that.