From a96463efef0039bd3f964093948135180d07efec Mon Sep 17 00:00:00 2001 From: gdatahub Date: Tue, 24 Nov 2026 18:00:55 +0530 Subject: [PATCH 1/2] fix(zdu): keep an entity on hard rollback when a newer version's row belongs to another run A hard rollback deletes an entity the run created only while the run still owns all of it. EntityServiceImpl.ownedOnlyByRun reads only the aspects this version's registry knows, so after a rollback it missed an aspect only the newer version knows, written under another run: the entity was deleted with that row, although the dry run (built from the system-metadata index) said it would be kept. With entity and aspect tolerance on, ownedOnlyByRun now also reads the latest row of every aspect stored for the urn (new AspectDao.getLatestRowsOfUrn; Ebean through the routing seam, Cassandra within the urn's partition) and keeps the entity if one of the unknown aspects other than status belongs to someone else. With tolerance off nothing changes. --- docs/how/breaking-changes.md | 11 +++ .../linkedin/metadata/entity/AspectDao.java | 8 ++ .../metadata/entity/EntityServiceImpl.java | 27 ++++- .../entity/cassandra/CassandraAspectDao.java | 21 ++++ .../metadata/entity/ebean/EbeanAspectDao.java | 28 ++++++ .../metadata/entity/AspectDaoTest.java | 5 + .../metadata/entity/EntityServiceTest.java | 98 +++++++++++++++++++ .../ebean/EbeanAspectDaoSeamRoutingTest.java | 29 ++++++ 8 files changed, 222 insertions(+), 5 deletions(-) diff --git a/docs/how/breaking-changes.md b/docs/how/breaking-changes.md index 1eaccfe29df9..29e223e2a934 100644 --- a/docs/how/breaking-changes.md +++ b/docs/how/breaking-changes.md @@ -89,6 +89,17 @@ null, so no data is lost, but a query that selects the old shape, such as `applications { total relationships { entity { urn } } }`, now fails validation. Select `applications { application { urn properties { name } } }` instead. +### Hard rollback of an ingestion run after a zero-downtime rollback + +This only applies with entity and aspect tolerance on (`UNKNOWN_DATA_TOLERANCE_ENABLED` or +`UNKNOWN_ENTITY_ASPECT_TOLERANCE_ENABLED`, off by default; see +[#20280](https://github.com/datahub-project/datahub/pull/20280)). With it off nothing changes. + +A hard rollback of an ingestion run keeps an entity the run created when anything other than the run +wrote one of its aspects, including an aspect only the newer version knows. Before, the check that +decides whether the run owns the whole entity didn't see aspects the running version doesn't know, so +the entity was deleted with the other run's row, although the dry run said it would be kept. + ### Semantic search template sends an entity URN to similarity ([#21474](https://github.com/datahub-project/datahub/pull/21474)) System update now writes version 19 of the shipped search templates. `urn:li:searchTemplate:semantic` diff --git a/metadata-io/src/main/java/com/linkedin/metadata/entity/AspectDao.java b/metadata-io/src/main/java/com/linkedin/metadata/entity/AspectDao.java index 0eff6219e93c..780179abe69b 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/entity/AspectDao.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/entity/AspectDao.java @@ -476,6 +476,14 @@ Integer countAspect( /** Rows the urn has in primary storage, every aspect and every version; -1 when not supported. */ int countUrnRows(@Nonnull OperationContext opContext, @Nonnull String urn); + /** + * The latest row of every aspect stored for the urn, as stored: rows of aspects this version's + * registry doesn't know are included and nothing is converted. For checks that must see what a + * newer version wrote (read after a rollback), e.g. which run last wrote each aspect. + */ + @Nonnull + List getLatestRowsOfUrn(@Nonnull OperationContext opContext, @Nonnull String urn); + /** * Stream latest-version aspect rows matching {@code args}, ordered by URN/aspect, and hand the * lazily-fetched {@link PartitionedStream} to {@code consumer} to process. diff --git a/metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java b/metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java index 8a09d1e620f8..17f81eec01b5 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/entity/EntityServiceImpl.java @@ -4756,7 +4756,9 @@ private boolean withinCeiling( /** * Whether every latest aspect of the entity, other than its status, was last written by {@code * runId}. A write by anyone else, another run or a write with no run id such as a UI edit, means - * rolling back the run must not delete the entity. Locks the rows, like {@link #withinCeiling}. + * rolling back the run must not delete the entity. Locks the rows of the aspects the registry + * knows, like {@link #withinCeiling}; while unknown data is tolerated it also reads, without + * locking, the rows a newer version stored for aspects the registry doesn't know. */ private boolean ownedOnlyByRun( @Nonnull OperationContext opContext, @Nonnull Urn urn, @Nonnull String runId) { @@ -4770,12 +4772,27 @@ private boolean ownedOnlyByRun( if (!latest.containsKey(opContext.getKeyAspectName(urn))) { return false; } - return latest.entrySet().stream() - .filter(row -> !STATUS_ASPECT_NAME.equals(row.getKey())) + final boolean registryRowsOwned = + latest.entrySet().stream() + .filter(row -> !STATUS_ASPECT_NAME.equals(row.getKey())) + .allMatch( + row -> + row.getValue().getSystemMetadata() != null + && runId.equals(row.getValue().getSystemMetadata().getRunId(GetMode.NULL))); + if (!registryRowsOwned || !handling().toleratesUnknownEntityAspect()) { + return registryRowsOwned; + } + // A newer version (read after a rollback) can store aspects this registry doesn't know; the + // read above can't see them. Deleting the entity would delete such a row written by another + // run too, so it counts like any other aspect. + return aspectDao.getLatestRowsOfUrn(opContext, urn.toString()).stream() + .filter(row -> !latest.containsKey(row.getAspect())) + .filter(row -> !STATUS_ASPECT_NAME.equals(row.getAspect())) .allMatch( row -> - row.getValue().getSystemMetadata() != null - && runId.equals(row.getValue().getSystemMetadata().getRunId(GetMode.NULL))); + runId.equals( + SystemMetadataUtils.parseSystemMetadata(row.getSystemMetadata()) + .getRunId(GetMode.NULL))); } private static long createdOnMillis(@Nonnull SystemAspect row) { diff --git a/metadata-io/src/main/java/com/linkedin/metadata/entity/cassandra/CassandraAspectDao.java b/metadata-io/src/main/java/com/linkedin/metadata/entity/cassandra/CassandraAspectDao.java index 83183e750780..7f60c6926162 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/entity/cassandra/CassandraAspectDao.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/entity/cassandra/CassandraAspectDao.java @@ -720,6 +720,27 @@ public Integer countAspect(OperationContext operationContext, final RestoreIndic return -1; } + @Nonnull + @Override + public List getLatestRowsOfUrn( + @Nonnull OperationContext opContext, @Nonnull String urn) { + validateConnection(); + // One partition (urn is the partition key); version follows aspect in the clustering key, so + // filtering on it alone needs ALLOW FILTERING, bounded to this partition's rows. + final SimpleStatement ss = + selectFrom(CassandraAspect.TABLE_NAME) + .all() + .whereColumn(CassandraAspect.URN_COLUMN) + .isEqualTo(literal(urn)) + .whereColumn(CassandraAspect.VERSION_COLUMN) + .isEqualTo(literal(ASPECT_LATEST_VERSION)) + .allowFiltering() + .build(); + return _cqlSession.execute(ss).all().stream() + .map(CassandraAspect::rowToEntityAspect) + .collect(Collectors.toList()); + } + @Override public int countUrnRows(@Nonnull OperationContext opContext, @Nonnull String urn) { // Not implemented diff --git a/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanAspectDao.java b/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanAspectDao.java index 157ed29fb003..2d839a5a157e 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanAspectDao.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanAspectDao.java @@ -1499,6 +1499,34 @@ public Integer countAspect(@Nonnull OperationContext opContext, final RestoreInd return txnFactory.runInScope(opContext, () -> buildExpressionList(args, true).findCount()); } + @Nonnull + @Override + public List getLatestRowsOfUrn( + @Nonnull OperationContext opContext, @Nonnull String urn) { + validateConnection(); + // Through the routing seam, like batchGet: the resolved aspect table, on primary (write intent) + // since callers decide a delete from these rows. + final RawSql rawSql = + parseBatchGetRawSql( + "SELECT urn, aspect, version, metadata, systemMetadata, createdOn, createdBy, createdFor " + + "FROM" + + tableResolver.aspectTable(opContext, EbeanAspectV2.TABLE_NAME) + + "WHERE urn = :urn AND version = :version"); + final Query query = + resolveBatchGetDatabase(opContext, true) + .find(EbeanAspectV2.class) + .setRawSql(rawSql) + .setParameter("urn", urn) + .setParameter("version", ASPECT_LATEST_VERSION); + return txnFactory.runInScope( + opContext, + () -> { + final List rows = query.findList(); + syncKeyScalarsFromEmbeddedId(rows); + return rows.stream().map(EbeanAspectV2::toEntityAspect).collect(Collectors.toList()); + }); + } + @Override public int countUrnRows(@Nonnull OperationContext opContext, @Nonnull String urn) { return txnFactory.runInScope( diff --git a/metadata-io/src/test/java/com/linkedin/metadata/entity/AspectDaoTest.java b/metadata-io/src/test/java/com/linkedin/metadata/entity/AspectDaoTest.java index b2080eec6122..b0ced910d8a1 100644 --- a/metadata-io/src/test/java/com/linkedin/metadata/entity/AspectDaoTest.java +++ b/metadata-io/src/test/java/com/linkedin/metadata/entity/AspectDaoTest.java @@ -507,6 +507,11 @@ public int countUrnRows(OperationContext opContext, String urn) { return 0; } + @Override + public List getLatestRowsOfUrn(OperationContext opContext, String urn) { + return List.of(); + } + @Override public R streamAspectBatches( OperationContext opContext, diff --git a/metadata-io/src/test/java/com/linkedin/metadata/entity/EntityServiceTest.java b/metadata-io/src/test/java/com/linkedin/metadata/entity/EntityServiceTest.java index 69b728613c17..e56e6e92a114 100644 --- a/metadata-io/src/test/java/com/linkedin/metadata/entity/EntityServiceTest.java +++ b/metadata-io/src/test/java/com/linkedin/metadata/entity/EntityServiceTest.java @@ -1,6 +1,7 @@ package com.linkedin.metadata.entity; import static com.linkedin.metadata.Constants.*; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import static org.testng.Assert.*; @@ -55,6 +56,7 @@ import com.linkedin.metadata.aspect.CorpUserAspect; import com.linkedin.metadata.aspect.CorpUserAspectArray; import com.linkedin.metadata.aspect.EntityAspect; +import com.linkedin.metadata.aspect.SystemAspect; import com.linkedin.metadata.aspect.VersionedAspect; import com.linkedin.metadata.aspect.batch.BatchItem; import com.linkedin.metadata.aspect.batch.MCPItem; @@ -83,6 +85,7 @@ import com.linkedin.metadata.utils.EntityApiUtils; import com.linkedin.metadata.utils.EntityKeyUtils; import com.linkedin.metadata.utils.GenericRecordUtils; +import com.linkedin.metadata.utils.StrictHandling; import com.linkedin.metadata.utils.SystemMetadataUtils; import com.linkedin.metadata.utils.TolerantHandling; import com.linkedin.metadata.utils.UnknownDataHandlingsForTests; @@ -1403,6 +1406,77 @@ public void testHardRollbackOfAKeptEntityStillRemovesTheRunsOwnAspects() { assertNull(_entityServiceImpl.getLatestAspect(opContext, urn, editableName)); } + @Test + public void testHardRollbackKeepsEntityWithAnotherRunsAspectOnlyANewerVersionKnows() { + // Read after a rollback: a newer version wrote an aspect this registry doesn't know, under + // another run. Deleting the entity would delete that row too, so the entity stays. + Urn urn = UrnUtils.getUrn("urn:li:corpuser:rollbackKeepsNewerVersionsRow"); + String keyName = opContext.getKeyAspectName(urn); + String infoName = AspectGenerationUtils.getAspectName(new CorpUserInfo()); + SystemMetadata runA = AspectGenerationUtils.createSystemMetadata(1625792689, "run-a-newer"); + ingestKeyForRollback(urn, runA); + ingestForRollback(urn, infoName, AspectGenerationUtils.createCorpUserInfo("a@test.com"), runA); + insertRowOnlyANewerVersionKnows(urn, "run-b-newer"); + + _entityServiceImpl.rollbackRun( + opContext, + ImmutableList.of( + rollbackRow(urn, keyName, "run-a-newer"), rollbackRow(urn, infoName, "run-a-newer")), + "run-a-newer", + true); + + assertNotNull(_entityServiceImpl.getLatestAspect(opContext, urn, keyName)); + assertNotNull( + _aspectDao.getAspect( + opContext, urn.toString(), ASPECT_ONLY_A_NEWER_VERSION_KNOWS, ASPECT_LATEST_VERSION)); + } + + @Test + public void testHardRollbackDeletesEntityWhenTheAspectOnlyANewerVersionKnowsIsTheRunsOwn() { + Urn urn = UrnUtils.getUrn("urn:li:corpuser:rollbackDeletesOwnNewerVersionsRow"); + String keyName = opContext.getKeyAspectName(urn); + String infoName = AspectGenerationUtils.getAspectName(new CorpUserInfo()); + SystemMetadata runA = AspectGenerationUtils.createSystemMetadata(1625792689, "run-a-own-newer"); + ingestKeyForRollback(urn, runA); + ingestForRollback(urn, infoName, AspectGenerationUtils.createCorpUserInfo("a@test.com"), runA); + insertRowOnlyANewerVersionKnows(urn, "run-a-own-newer"); + + _entityServiceImpl.rollbackRun( + opContext, + ImmutableList.of( + rollbackRow(urn, keyName, "run-a-own-newer"), + rollbackRow(urn, infoName, "run-a-own-newer")), + "run-a-own-newer", + true); + + assertNull(_entityServiceImpl.getLatestAspect(opContext, urn, keyName)); + assertNull( + _aspectDao.getAspect( + opContext, urn.toString(), ASPECT_ONLY_A_NEWER_VERSION_KNOWS, ASPECT_LATEST_VERSION)); + } + + @Test + public void testHardRollbackStrictHandlingDeletesEntityDespiteANewerVersionsRow() { + // With unknown data not tolerated, the check sees only the registry's aspects, as before. + UnknownDataHandlingsForTests.use(StrictHandling.INSTANCE); + Urn urn = UrnUtils.getUrn("urn:li:corpuser:rollbackStrictIgnoresNewerVersionsRow"); + String keyName = opContext.getKeyAspectName(urn); + String infoName = AspectGenerationUtils.getAspectName(new CorpUserInfo()); + SystemMetadata runA = AspectGenerationUtils.createSystemMetadata(1625792689, "run-a-strict"); + ingestKeyForRollback(urn, runA); + ingestForRollback(urn, infoName, AspectGenerationUtils.createCorpUserInfo("a@test.com"), runA); + insertRowOnlyANewerVersionKnows(urn, "run-b-strict"); + + _entityServiceImpl.rollbackRun( + opContext, + ImmutableList.of( + rollbackRow(urn, keyName, "run-a-strict"), rollbackRow(urn, infoName, "run-a-strict")), + "run-a-strict", + true); + + assertNull(_entityServiceImpl.getLatestAspect(opContext, urn, keyName)); + } + @Test public void testSoftRollbackMarksEntityRemovedEvenWithAnAspectFromAnotherRun() { Urn urn = UrnUtils.getUrn("urn:li:corpuser:softRollbackWithOtherRun"); @@ -1425,6 +1499,30 @@ public void testSoftRollbackMarksEntityRemovedEvenWithAnAspectFromAnotherRun() { assertTrue(status.isRemoved()); } + private static final String ASPECT_ONLY_A_NEWER_VERSION_KNOWS = "aspectFromANewerVersion"; + + /** A latest row a newer version wrote for an aspect this registry doesn't know. */ + private void insertRowOnlyANewerVersionKnows(Urn urn, String runId) { + // No aspect spec exists for the name, so the row is handed to the store as written; both + // stores insert what withVersion returns. + SystemAspect row = mock(SystemAspect.class); + when(row.withVersion(anyLong())) + .thenAnswer( + invocation -> + EntityAspect.builder() + .urn(urn.toString()) + .aspect(ASPECT_ONLY_A_NEWER_VERSION_KNOWS) + .version(invocation.getArgument(0)) + .metadata("{}") + .createdBy(TEST_AUDIT_STAMP.getActor().toString()) + .createdOn(new Timestamp(TEST_AUDIT_STAMP.getTime())) + .systemMetadata( + RecordUtils.toJsonString( + AspectGenerationUtils.createSystemMetadata(1645792689, runId))) + .build()); + _aspectDao.insertAspect(opContext, null, row, ASPECT_LATEST_VERSION); + } + private void ingestForRollback( Urn urn, String aspectName, RecordTemplate aspect, SystemMetadata systemMetadata) { _entityServiceImpl.ingestAspects( diff --git a/metadata-io/src/test/java/com/linkedin/metadata/entity/ebean/EbeanAspectDaoSeamRoutingTest.java b/metadata-io/src/test/java/com/linkedin/metadata/entity/ebean/EbeanAspectDaoSeamRoutingTest.java index ea7d9fec7d4c..3aa56d72cc95 100644 --- a/metadata-io/src/test/java/com/linkedin/metadata/entity/ebean/EbeanAspectDaoSeamRoutingTest.java +++ b/metadata-io/src/test/java/com/linkedin/metadata/entity/ebean/EbeanAspectDaoSeamRoutingTest.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.mockito.ArgumentCaptor; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; @@ -54,6 +55,7 @@ public class EbeanAspectDaoSeamRoutingTest { private static final String URN_BATCH_A = "urn:li:corpuser:seamBatchA"; private static final String URN_BATCH_B = "urn:li:corpuser:seamBatchB"; private static final String URN_RANGE = "urn:li:corpuser:seamVersionRange"; + private static final String URN_LATEST_ROWS = "urn:li:corpuser:seamLatestRows"; private Database server; private AspectTableResolver tableResolverSpy; @@ -192,6 +194,33 @@ public void rawSqlVersionRangeRoutesCallerContextAndAggregatesCorrectly() { assertSame(scoped.getValue(), rawSqlContext); } + @Test + public void rawSqlLatestRowsOfUrnRoutesCallerContextAndReturnsEveryAspect() { + // Every latest row of the urn, including an aspect the registry doesn't know; older versions + // and other urns are left out. + insertRow(URN_LATEST_ROWS, STATUS_ASPECT_NAME, ASPECT_LATEST_VERSION, "status-v0"); + insertRow(URN_LATEST_ROWS, STATUS_ASPECT_NAME, 3L, "status-v3"); + insertRow(URN_LATEST_ROWS, "aspectFromANewerVersion", ASPECT_LATEST_VERSION, "newer-v0"); + insertRow(URN_RANGE, STATUS_ASPECT_NAME, ASPECT_LATEST_VERSION, "other-urn"); + + List rows = testDao.getLatestRowsOfUrn(rawSqlContext, URN_LATEST_ROWS); + + assertEquals( + rows.stream() + .map(row -> row.getAspect() + "=" + row.getMetadata()) + .collect(Collectors.toSet()), + Set.of(STATUS_ASPECT_NAME + "=status-v0", "aspectFromANewerVersion=newer-v0")); + assertTrue(rows.stream().allMatch(row -> URN_LATEST_ROWS.equals(row.getUrn()))); + + ArgumentCaptor resolved = ArgumentCaptor.forClass(OperationContext.class); + verify(tableResolverSpy).aspectTable(resolved.capture(), eq(EbeanAspectV2.TABLE_NAME)); + assertSame(resolved.getValue(), rawSqlContext); + + ArgumentCaptor scoped = ArgumentCaptor.forClass(OperationContext.class); + verify(txnFactorySpy).runInScope(scoped.capture(), any()); + assertSame(scoped.getValue(), rawSqlContext); + } + private void insertRow(String urn, String aspect, long version, String metadata) { // Written outside the DAO so the seam spies record only the invocations under test. server.save(newRow(urn, aspect, version, metadata)); From 9d0e626eaf536fa468bc4c76628d473b429fc35d Mon Sep 17 00:00:00 2001 From: gdatahub Date: Tue, 24 Nov 2026 18:03:12 +0530 Subject: [PATCH 2/2] docs(zdu): link the breaking-changes entry to its PR --- docs/how/breaking-changes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/how/breaking-changes.md b/docs/how/breaking-changes.md index 29e223e2a934..7068bd303217 100644 --- a/docs/how/breaking-changes.md +++ b/docs/how/breaking-changes.md @@ -89,7 +89,7 @@ null, so no data is lost, but a query that selects the old shape, such as `applications { total relationships { entity { urn } } }`, now fails validation. Select `applications { application { urn properties { name } } }` instead. -### Hard rollback of an ingestion run after a zero-downtime rollback +### Hard rollback of an ingestion run after a zero-downtime rollback ([#21572](https://github.com/datahub-project/datahub/pull/21572)) This only applies with entity and aspect tolerance on (`UNKNOWN_DATA_TOLERANCE_ENABLED` or `UNKNOWN_ENTITY_ASPECT_TOLERANCE_ENABLED`, off by default; see