@@ -972,10 +972,13 @@ This condition is specifically for an older conditional write ingestAspectIfNotP
972
972
*/
973
973
if (overwrite || databaseAspect == null ) {
974
974
result =
975
- ingestAspectToLocalDB (txContext , writeItem , databaseSystemAspect )
976
- .toBuilder ()
977
- .request (writeItem )
978
- .build ();
975
+ Optional .ofNullable (
976
+ ingestAspectToLocalDB (
977
+ txContext , writeItem , databaseSystemAspect ))
978
+ .map (
979
+ optResult ->
980
+ optResult .toBuilder ().request (writeItem ).build ())
981
+ .orElse (null );
979
982
980
983
} else {
981
984
RecordTemplate oldValue = databaseSystemAspect .getRecordTemplate ();
@@ -996,49 +999,56 @@ This condition is specifically for an older conditional write ingestAspectIfNotP
996
999
997
1000
return result ;
998
1001
})
1002
+ .filter (Objects ::nonNull )
999
1003
.collect (Collectors .toList ());
1000
1004
1001
- // commit upserts prior to retention or kafka send, if supported by impl
1002
- if (txContext != null ) {
1003
- txContext .commitAndContinue ();
1004
- }
1005
- long took = TimeUnit .NANOSECONDS .toMillis (ingestToLocalDBTimer .stop ());
1006
- if (took > DB_TIMER_LOG_THRESHOLD_MS ) {
1007
- log .info ("Ingestion of aspects batch to database took {} ms" , took );
1008
- }
1009
-
1010
- // Retention optimization and tx
1011
- if (retentionService != null ) {
1012
- List <RetentionService .RetentionContext > retentionBatch =
1013
- upsertResults .stream ()
1014
- // Only consider retention when there was a previous version
1015
- .filter (
1016
- result ->
1017
- batchAspects .containsKey (result .getUrn ().toString ())
1018
- && batchAspects
1019
- .get (result .getUrn ().toString ())
1020
- .containsKey (result .getRequest ().getAspectName ()))
1021
- .filter (
1022
- result -> {
1023
- RecordTemplate oldAspect = result .getOldValue ();
1024
- RecordTemplate newAspect = result .getNewValue ();
1025
- // Apply retention policies if there was an update to existing aspect
1026
- // value
1027
- return oldAspect != newAspect
1028
- && oldAspect != null
1029
- && retentionService != null ;
1030
- })
1031
- .map (
1032
- result ->
1033
- RetentionService .RetentionContext .builder ()
1034
- .urn (result .getUrn ())
1035
- .aspectName (result .getRequest ().getAspectName ())
1036
- .maxVersion (Optional .of (result .getMaxVersion ()))
1037
- .build ())
1038
- .collect (Collectors .toList ());
1039
- retentionService .applyRetentionWithPolicyDefaults (opContext , retentionBatch );
1005
+ if (!upsertResults .isEmpty ()) {
1006
+ // commit upserts prior to retention or kafka send, if supported by impl
1007
+ if (txContext != null ) {
1008
+ txContext .commitAndContinue ();
1009
+ }
1010
+ log .info (
1011
+ "Ingestion of aspects batch to database took {} in {} ms" ,
1012
+ upsertResults .size (),
1013
+ TimeUnit .NANOSECONDS .toMillis (ingestToLocalDBTimer .stop ()));
1014
+
1015
+ // Retention optimization and tx
1016
+ if (retentionService != null ) {
1017
+ List <RetentionService .RetentionContext > retentionBatch =
1018
+ upsertResults .stream ()
1019
+ // Only consider retention when there was a previous version
1020
+ .filter (
1021
+ result ->
1022
+ batchAspects .containsKey (result .getUrn ().toString ())
1023
+ && batchAspects
1024
+ .get (result .getUrn ().toString ())
1025
+ .containsKey (result .getRequest ().getAspectName ()))
1026
+ .filter (
1027
+ result -> {
1028
+ RecordTemplate oldAspect = result .getOldValue ();
1029
+ RecordTemplate newAspect = result .getNewValue ();
1030
+ // Apply retention policies if there was an update to existing
1031
+ // aspect
1032
+ // value
1033
+ return oldAspect != newAspect
1034
+ && oldAspect != null
1035
+ && retentionService != null ;
1036
+ })
1037
+ .map (
1038
+ result ->
1039
+ RetentionService .RetentionContext .builder ()
1040
+ .urn (result .getUrn ())
1041
+ .aspectName (result .getRequest ().getAspectName ())
1042
+ .maxVersion (Optional .of (result .getMaxVersion ()))
1043
+ .build ())
1044
+ .collect (Collectors .toList ());
1045
+ retentionService .applyRetentionWithPolicyDefaults (opContext , retentionBatch );
1046
+ } else {
1047
+ log .warn ("Retention service is missing!" );
1048
+ }
1040
1049
} else {
1041
1050
log .warn ("Retention service is missing!" );
1051
+ log .warn ("Empty transaction detected. {}" , inputBatch );
1042
1052
}
1043
1053
1044
1054
return upsertResults ;
@@ -2506,7 +2516,7 @@ private Map<EntityAspectIdentifier, EnvelopedAspect> getEnvelopedAspects(
2506
2516
* @param databaseAspect The aspect as it exists in the database.
2507
2517
* @return result object
2508
2518
*/
2509
- @ Nonnull
2519
+ @ Nullable
2510
2520
private UpdateAspectResult ingestAspectToLocalDB (
2511
2521
@ Nullable TransactionContext txContext ,
2512
2522
@ Nonnull final ChangeMCP writeItem ,
@@ -2520,6 +2530,9 @@ private UpdateAspectResult ingestAspectToLocalDB(
2520
2530
.setLastRunId (writeItem .getSystemMetadata ().getRunId (GetMode .NULL ), SetMode .IGNORE_NULL );
2521
2531
2522
2532
// 2. Compare the latest existing and new.
2533
+ final RecordTemplate databaseValue =
2534
+ databaseAspect == null ? null : databaseAspect .getRecordTemplate ();
2535
+
2523
2536
final EntityAspect .EntitySystemAspect previousBatchAspect =
2524
2537
(EntityAspect .EntitySystemAspect ) writeItem .getPreviousSystemAspect ();
2525
2538
final RecordTemplate previousValue =
@@ -2528,7 +2541,7 @@ private UpdateAspectResult ingestAspectToLocalDB(
2528
2541
// 3. If there is no difference between existing and new, we just update
2529
2542
// the lastObserved in system metadata. RunId should stay as the original runId
2530
2543
if (previousValue != null
2531
- && DataTemplateUtil .areEqual (previousValue , writeItem .getRecordTemplate ())) {
2544
+ && DataTemplateUtil .areEqual (databaseValue , writeItem .getRecordTemplate ())) {
2532
2545
2533
2546
SystemMetadata latestSystemMetadata = previousBatchAspect .getSystemMetadata ();
2534
2547
latestSystemMetadata .setLastObserved (writeItem .getSystemMetadata ().getLastObserved ());
@@ -2564,45 +2577,49 @@ private UpdateAspectResult ingestAspectToLocalDB(
2564
2577
}
2565
2578
2566
2579
// 4. Save the newValue as the latest version
2567
- log .debug (
2568
- "Ingesting aspect with name {}, urn {}" , writeItem .getAspectName (), writeItem .getUrn ());
2569
- String newValueStr = EntityApiUtils .toJsonAspect (writeItem .getRecordTemplate ());
2570
- long versionOfOld =
2571
- aspectDao .saveLatestAspect (
2572
- txContext ,
2573
- writeItem .getUrn ().toString (),
2574
- writeItem .getAspectName (),
2575
- previousBatchAspect == null ? null : EntityApiUtils .toJsonAspect (previousValue ),
2576
- previousBatchAspect == null ? null : previousBatchAspect .getCreatedBy (),
2577
- previousBatchAspect == null
2578
- ? null
2579
- : previousBatchAspect .getEntityAspect ().getCreatedFor (),
2580
- previousBatchAspect == null ? null : previousBatchAspect .getCreatedOn (),
2581
- previousBatchAspect == null ? null : previousBatchAspect .getSystemMetadataRaw (),
2582
- newValueStr ,
2583
- writeItem .getAuditStamp ().getActor ().toString (),
2584
- writeItem .getAuditStamp ().hasImpersonator ()
2585
- ? writeItem .getAuditStamp ().getImpersonator ().toString ()
2586
- : null ,
2587
- new Timestamp (writeItem .getAuditStamp ().getTime ()),
2588
- EntityApiUtils .toJsonAspect (writeItem .getSystemMetadata ()),
2589
- writeItem .getNextAspectVersion ());
2590
-
2591
- // metrics
2592
- aspectDao .incrementWriteMetrics (
2593
- writeItem .getAspectName (), 1 , newValueStr .getBytes (StandardCharsets .UTF_8 ).length );
2594
-
2595
- return UpdateAspectResult .builder ()
2596
- .urn (writeItem .getUrn ())
2597
- .oldValue (previousValue )
2598
- .newValue (writeItem .getRecordTemplate ())
2599
- .oldSystemMetadata (
2600
- previousBatchAspect == null ? null : previousBatchAspect .getSystemMetadata ())
2601
- .newSystemMetadata (writeItem .getSystemMetadata ())
2602
- .operation (MetadataAuditOperation .UPDATE )
2603
- .auditStamp (writeItem .getAuditStamp ())
2604
- .maxVersion (versionOfOld )
2605
- .build ();
2580
+ if (!DataTemplateUtil .areEqual (databaseValue , writeItem .getRecordTemplate ())) {
2581
+ log .debug (
2582
+ "Ingesting aspect with name {}, urn {}" , writeItem .getAspectName (), writeItem .getUrn ());
2583
+ String newValueStr = EntityApiUtils .toJsonAspect (writeItem .getRecordTemplate ());
2584
+ long versionOfOld =
2585
+ aspectDao .saveLatestAspect (
2586
+ txContext ,
2587
+ writeItem .getUrn ().toString (),
2588
+ writeItem .getAspectName (),
2589
+ previousBatchAspect == null ? null : EntityApiUtils .toJsonAspect (previousValue ),
2590
+ previousBatchAspect == null ? null : previousBatchAspect .getCreatedBy (),
2591
+ previousBatchAspect == null
2592
+ ? null
2593
+ : previousBatchAspect .getEntityAspect ().getCreatedFor (),
2594
+ previousBatchAspect == null ? null : previousBatchAspect .getCreatedOn (),
2595
+ previousBatchAspect == null ? null : previousBatchAspect .getSystemMetadataRaw (),
2596
+ newValueStr ,
2597
+ writeItem .getAuditStamp ().getActor ().toString (),
2598
+ writeItem .getAuditStamp ().hasImpersonator ()
2599
+ ? writeItem .getAuditStamp ().getImpersonator ().toString ()
2600
+ : null ,
2601
+ new Timestamp (writeItem .getAuditStamp ().getTime ()),
2602
+ EntityApiUtils .toJsonAspect (writeItem .getSystemMetadata ()),
2603
+ writeItem .getNextAspectVersion ());
2604
+
2605
+ // metrics
2606
+ aspectDao .incrementWriteMetrics (
2607
+ writeItem .getAspectName (), 1 , newValueStr .getBytes (StandardCharsets .UTF_8 ).length );
2608
+
2609
+ return UpdateAspectResult .builder ()
2610
+ .urn (writeItem .getUrn ())
2611
+ .oldValue (previousValue )
2612
+ .newValue (writeItem .getRecordTemplate ())
2613
+ .oldSystemMetadata (
2614
+ previousBatchAspect == null ? null : previousBatchAspect .getSystemMetadata ())
2615
+ .newSystemMetadata (writeItem .getSystemMetadata ())
2616
+ .operation (MetadataAuditOperation .UPDATE )
2617
+ .auditStamp (writeItem .getAuditStamp ())
2618
+ .maxVersion (versionOfOld )
2619
+ .build ();
2620
+ }
2621
+
2622
+ return null ;
2606
2623
}
2607
2624
2608
2625
private static boolean shouldAspectEmitChangeLog (@ Nonnull final AspectSpec aspectSpec ) {
0 commit comments