Recompute flat entity maps before skipping a stored formula backfill
Why
A stored formula field created within about 10 seconds of another metadata change ends up with no values. Its compute_storedFormulaFields operation completes in a few milliseconds, the row is deleted, and nothing is backfilled. There is no error and no FAILED row.
Seen during 2.68 QA on twenty-main: three formula fields created back to back had their operation complete in 6 to 9 ms with every record left empty.
Two things combine:
WorkspaceCacheServicememoizes each read for 10 seconds per process. An invalidation only clears that memo in the process that invalidates. The worker keeps serving the maps it read for the previous job, without checking the Redis hash.- The formula handler treated "formula not in the maps" as "formula gone" and returned
completed.
The row is also committed before the migration invalidates the cache, but that window is a few milliseconds. The memo is what makes this happen in practice.
Reproduced locally on main with a server and a worker: 6 of 8 isolated creations, and 15 of 30 parallel ones, were left with no value. A later backfill on the same object hides it, because the dirty pass recomputes every formula of the records it touches.
This is the same failure as #32290 (build_index, validate_foreignKey, build_recordPermissionIndexes, rebuild_object). The formula handler was not converted then.
Fix
- The job run marks the worker's local cache stale before loading the maps (
markWorkspaceLocalCacheStale, as the access group jobs do since #31703). The next read compares hashes with Redis and refetches only what changed. Nothing is written to Redis and other pods are not affected. This covers every deferred operation, not only formulas. - The formula handler returns
retryWithFreshMetadatawhen a formula field of its payload is not in the workspace context. The runner then recomputes the maps once from the database and runs the operation again. If the field is still missing it was really deleted and the operation is skipped, as before. payloadEntityNotFoundis renamedretryWithFreshMetadata, which says what the runner does with it.
flowchart TD
A[Job reads<br/>PENDING rows] --> B[Mark local<br/>cache stale]:::new
B --> C[Load maps,<br/>hash checked]
C --> D[Run handler]
D -->|completed| E[Delete row]
D -->|retryWithFresh<br/>Metadata| F[Recompute maps<br/>from DB, once]
F --> G[Run handler<br/>again]
G -->|completed| E
G -->|still missing| H[Skip: entity<br/>deleted]
H --> E
classDef new fill:#d4f7d4,stroke:#2a7a2a
| Local run, 12 isolated + 36 parallel creations | Fields left empty | Redis-wide recomputes |
|---|---|---|
| main (earlier run: 8 isolated + 30 parallel) | 21 of 38 | 0 |
| Handler change only | 0 | 23 |
| Handler change + stale mark | 0 | 0 |
The recompute stays as a fallback for a row committed before its migration has invalidated the cache, and for entities that were really deleted.
Not covered
A formula switched from computed on read to stored inside the commit window is in the cache with its old evaluation, so it is still skipped. That needs a different signal than "not in the maps".
Screenshots
No UI change.
Test
Typecheck and lint. QA on a local server and worker, scratch database, final code:
| Case | Result |
|---|---|
| 12 isolated and 36 parallel stored formula creations | All backfilled, 0 Redis-wide recomputes |
| Same build, stale mark disabled | All backfilled, 22 Redis-wide recomputes |
| Same build, stale mark and retry disabled (main behavior) | 26 of 48 fields left empty |
| Formula expression updated | Every record recomputed |
| Field deleted before its job runs | One recompute, operation skipped, row removed |
| Redis still holding the maps from before the creation | One recompute, every record backfilled |
Redis cache traffic for the same 48 creations, server and worker together:
| Variant | Hash reads | Data reads | Writes | Data written |
|---|---|---|---|---|
| main | 1,192 | 96 | 1,855 | ~4.0 GB |
| Handler change only | 1,375 | 139 | 2,532 | ~4.7 GB |
| This PR | 1,143 | 100 | 1,746 | ~3.6 GB |
The stale mark costs at most a few hash reads per job run and no write. Each recompute it avoids is about 33 MB written on this workspace.