fix(zdu): keep an entity on hard rollback when a newer version's row belongs to another run
Follows #20280. After a zero-downtime upgrade rollback, a hard rollback of an ingestion run keeps the entity the run created when a newer version's row on it belongs to another run, instead of deleting that row with the entity. Only applies with entity and aspect tolerance on, which is off by default.
What was wrong
- A hard rollback deletes an entity the run created only while the run still owns all of it.
EntityServiceImpl.ownedOnlyByRunmakes that check, and it reads only the aspects this version's registry knows. - After a rollback, the newer version can have stored an aspect this version doesn't know, written under another run. The check never sees that row, concludes the run owns the entity, and the delete removes every row of the urn, that row included.
- The dry run is built from the system-metadata index, which lists the newer version's aspect, so it says the entity is kept. The real rollback contradicted it, and contradicted the #20280 note that the newer version's rows stay in place.
- Where it applies: on MySQL/Postgres this predates #20280, because the delete is
DELETE … WHERE urn = ?. On Cassandra it was introduced by #20280, whose tolerant delete removes the whole partition so that a newer version's rows don't come back after re-upgrading.
Changes
AspectDao.getLatestRowsOfUrn(new): the latest row of every aspect stored for a urn, as stored, including aspects the registry doesn't know.- Ebean: raw SQL
WHERE urn = :urn AND version = 0, through the routing seam likebatchGet(table fromtableResolver, database fromresolveBatchGetDatabasewith write intent, since the caller decides a delete from it). - Cassandra: a select within the urn's partition, filtered to
version = 0.
- Ebean: raw SQL
EntityServiceImpl.ownedOnlyByRun: while entity and aspect tolerance is on, it also reads those rows for aspects the registry doesn't know. The entity is kept if any of them, other thanstatus, belongs to someone other than the run. Those rows are read without a lock; the rows the registry knows are still locked, as before. The run's own aspects are still rolled back one by one, as for any kept entity.docs/how/breaking-changes.md: an entry for the change.
Flags off
Gated on entity and aspect tolerance, so with it off nothing changes, including the MySQL behaviour that predates #20280. It only ever makes the rollback keep more, never delete more.
Testing
EntityServiceTest, run for Ebean (H2) and Cassandra (Testcontainers): another run's newer-version row keeps the entity and the row; the run's own newer-version row lets the entity be deleted, row included; with strict handling the entity is deleted as before. The "keeps" test fails without the fix.EbeanAspectDaoSeamRoutingTest: the new read resolves the table and transaction scope from the caller's context, and returns every latest row of the urn (including an unknown aspect) and no older versions or other urns.- Results:
EbeanEntityServiceTest180,CassandraEntityServiceTest82,CassandraAspectDaoTest32,AspectDaoTest13,EbeanAspectDaoSeamRoutingTest5,EbeanAspectDaoTransactionRetryTest6,EbeanTimelineServiceTest3,CassandraTimelineServiceTest3: all pass. - Live, local Docker quickstart (MySQL). The same probe was run against master and this branch, each with tolerance off and on. Run R1 ingests a dataset (creating it); run R2's row for an aspect only the newer version knows is inserted as that version writes it (SQL row plus system-metadata document). Then a dry run and a hard rollback of R1.
| master, off | branch, off | master, on | branch, on | |
|---|---|---|---|---|
| Dry run of R1 | entitiesDeleted: 0 | same | same | same |
| Hard rollback of R1 | entity deleted (entitiesDeleted: 1) | same | entity deleted | entity kept (entitiesDeleted: 0) |
| Rows left | none | none | none | datasetKey and R2's row |
| Control: R2 wrote an aspect this version knows | kept, R2's row stays | same | same | same |
With tolerance off, the branch matches master, including the MySQL behaviour that predates #20280. Cassandra is covered by the Testcontainers tests, not a live deployment.
Performance
With entity and aspect tolerance on: one extra read per hard rollback of a run-created entity, and only once every row the registry knows belongs to the run. On Cassandra the version = 0 filter needs ALLOW FILTERING, so it reads the urn's partition, older versions included.
Questions for reviewers
- Cassandra reads: on #20280 a reviewer asked that Cassandra hard deletes not read the urn's full history. This read does scan the partition, but only on this narrow path: tolerance on, a hard rollback of a run-created entity, and every row the registry knows already owned by the run. Is that acceptable, or should it use a lighter query?
- Gating: MySQL had this behaviour before #20280 and keeps it with the flags off. It's gated because the case only arises after a version rollback, and flags off promises unchanged behaviour. Should it be ungated instead?