Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(entity-service): prevent mutation of systemMetdata on prev #12081

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2536,16 +2536,12 @@ private UpdateAspectResult ingestAspectToLocalDB(
Optional<SystemMetadata> latestSystemMetadataDiff =
systemMetadataDiff(
txContext,
writeItem.getUrn(),
previousBatchAspect.getSystemMetadata(),
writeItem.getSystemMetadata(),
databaseAspect == null ? null : databaseAspect.getSystemMetadata());

if (latestSystemMetadataDiff.isPresent()) {
// Update previous version since that is what is re-written
previousBatchAspect
.getEntityAspect()
.setSystemMetadata(RecordUtils.toJsonString(latestSystemMetadataDiff.get()));

// Inserts & update order is not guaranteed, flush the insert for potential updates within
// same tx
if (databaseAspect == null && txContext != null) {
Expand All @@ -2560,13 +2556,25 @@ private UpdateAspectResult ingestAspectToLocalDB(
conditionalLogLevel(
txContext,
String.format(
"Update aspect with name %s, urn %s, txContext: %s, databaseAspect: %s, newAspect: %s",
"Update aspect with name %s, urn %s, txContext: %s, databaseAspect: %s, newMetadata: %s newSystemMetadata: %s",
previousBatchAspect.getAspectName(),
previousBatchAspect.getUrn(),
txContext != null,
databaseAspect == null ? null : databaseAspect.getEntityAspect(),
previousBatchAspect.getEntityAspect()));
aspectDao.saveAspect(txContext, previousBatchAspect.getEntityAspect(), false);
previousBatchAspect.getEntityAspect().getMetadata(),
latestSystemMetadataDiff.get()));

aspectDao.saveAspect(
txContext,
previousBatchAspect.getUrnRaw(),
previousBatchAspect.getAspectName(),
previousBatchAspect.getMetadataRaw(),
previousBatchAspect.getCreatedBy(),
null,
previousBatchAspect.getCreatedOn(),
RecordUtils.toJsonString(latestSystemMetadataDiff.get()),
previousBatchAspect.getVersion(),
false);

// metrics
aspectDao.incrementWriteMetrics(
Expand Down Expand Up @@ -2661,13 +2669,14 @@ private static boolean shouldAspectEmitChangeLog(@Nonnull final AspectSpec aspec

private static Optional<SystemMetadata> systemMetadataDiff(
@Nullable TransactionContext txContext,
@Nonnull Urn urn,
@Nullable SystemMetadata previous,
@Nonnull SystemMetadata current,
@Nullable SystemMetadata database) {

SystemMetadata latestSystemMetadata = GenericRecordUtils.copy(previous, SystemMetadata.class);

latestSystemMetadata.setLastRunId(previous.getRunId(), SetMode.REMOVE_IF_NULL);
latestSystemMetadata.setLastRunId(latestSystemMetadata.getRunId(), SetMode.REMOVE_IF_NULL);
latestSystemMetadata.setLastObserved(current.getLastObserved(), SetMode.IGNORE_NULL);
latestSystemMetadata.setRunId(current.getRunId(), SetMode.REMOVE_IF_NULL);

Expand All @@ -2677,7 +2686,8 @@ private static Optional<SystemMetadata> systemMetadataDiff(
conditionalLogLevel(
txContext,
String.format(
"systemMetdataDiff: %s != %s AND %s",
"systemMetdataDiff urn %s, %s != %s AND %s",
urn,
RecordUtils.toJsonString(latestSystemMetadata),
previous == null ? null : RecordUtils.toJsonString(previous),
database == null ? null : RecordUtils.toJsonString(database)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public void testReingestLineageAspect() throws Exception {
restateChangeLog.setSystemMetadata(futureSystemMetadata);
restateChangeLog.setPreviousAspectValue(aspect);
restateChangeLog.setPreviousSystemMetadata(
simulatePullFromDB(futureSystemMetadata, SystemMetadata.class));
simulatePullFromDB(initialSystemMetadata, SystemMetadata.class));
restateChangeLog.setEntityKeyAspect(
GenericRecordUtils.serializeAspect(
EntityKeyUtils.convertUrnToEntityKey(
Expand Down Expand Up @@ -705,8 +705,7 @@ public void testReingestLineageProposal() throws Exception {
restateChangeLog.setAspect(genericAspect);
restateChangeLog.setSystemMetadata(futureSystemMetadata);
restateChangeLog.setPreviousAspectValue(genericAspect);
restateChangeLog.setPreviousSystemMetadata(
simulatePullFromDB(futureSystemMetadata, SystemMetadata.class));
restateChangeLog.setPreviousSystemMetadata(simulatePullFromDB(metadata1, SystemMetadata.class));

Map<String, RecordTemplate> latestAspects =
_entityServiceImpl.getLatestAspectsForUrn(
Expand Down
Loading