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(dataproduct): optimize data product sideeffect #11961

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -9,6 +9,7 @@
import com.linkedin.dataproduct.DataProductAssociation;
import com.linkedin.dataproduct.DataProductAssociationArray;
import com.linkedin.dataproduct.DataProductProperties;
import com.linkedin.events.metadata.ChangeType;
import com.linkedin.metadata.aspect.RetrieverContext;
import com.linkedin.metadata.aspect.batch.ChangeMCP;
import com.linkedin.metadata.aspect.batch.MCLItem;
Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import lombok.Getter;
Expand Down Expand Up @@ -65,77 +67,108 @@ private static Stream<MCPItem> generatePatchRemove(
MCLItem mclItem, @Nonnull RetrieverContext retrieverContext) {

if (DATA_PRODUCT_PROPERTIES_ASPECT_NAME.equals(mclItem.getAspectName())) {
List<MCPItem> mcpItems = new ArrayList<>();

DataProductProperties dataProductProperties = mclItem.getAspect(DataProductProperties.class);
if (dataProductProperties == null) {
log.error("Unable to process data product properties for urn: {}", mclItem.getUrn());
return Stream.empty();
}
Map<String, List<GenericJsonPatch.PatchOp>> patchOpMap = new HashMap<>();
for (DataProductAssociation dataProductAssociation :
DataProductAssociationArray newDataProductAssociationArray =
Optional.ofNullable(dataProductProperties.getAssets())
.orElse(new DataProductAssociationArray())) {
RelatedEntitiesScrollResult result =
retrieverContext
.getGraphRetriever()
.scrollRelatedEntities(
null,
QueryUtils.newFilter(
"urn", dataProductAssociation.getDestinationUrn().toString()),
null,
EMPTY_FILTER,
ImmutableList.of("DataProductContains"),
QueryUtils.newRelationshipFilter(EMPTY_FILTER, RelationshipDirection.INCOMING),
Collections.emptyList(),
null,
10, // Should only ever be one, if ever greater than ten will decrease over time
// to become consistent
null,
null);
if (!result.getEntities().isEmpty()) {
for (RelatedEntities entity : result.getEntities()) {
if (!mclItem.getUrn().equals(UrnUtils.getUrn(entity.getSourceUrn()))) {
GenericJsonPatch.PatchOp patchOp = new GenericJsonPatch.PatchOp();
patchOp.setOp(PatchOperationType.REMOVE.getValue());
patchOp.setPath(String.format("/assets/%s", entity.getDestinationUrn()));
patchOpMap
.computeIfAbsent(entity.getSourceUrn(), urn -> new ArrayList<>())
.add(patchOp);
}
.orElse(new DataProductAssociationArray());

DataProductProperties previousDataProductProperties =
mclItem.getPreviousAspect(DataProductProperties.class);

if (!ChangeType.UPSERT.equals(mclItem.getChangeType())
|| previousDataProductProperties == null) {
// CREATE/CREATE_ENTITY/RESTATE
return generateUnsetMCPs(mclItem, newDataProductAssociationArray, retrieverContext);
} else {
// UPSERT with previous
DataProductAssociationArray oldDataProductAssociationArray =
Optional.ofNullable(previousDataProductProperties.getAssets())
.orElse(new DataProductAssociationArray());

DataProductAssociationArray additions =
newDataProductAssociationArray.stream()
.filter(association -> !oldDataProductAssociationArray.contains(association))
.collect(Collectors.toCollection(DataProductAssociationArray::new));

return generateUnsetMCPs(mclItem, additions, retrieverContext);
}
}
return Stream.empty();
}

private static Stream<MCPItem> generateUnsetMCPs(
@Nonnull MCLItem dataProductItem,
@Nonnull DataProductAssociationArray dataProductAssociations,
@Nonnull RetrieverContext retrieverContext) {
List<MCPItem> mcpItems = new ArrayList<>();
Map<String, List<GenericJsonPatch.PatchOp>> patchOpMap = new HashMap<>();

for (DataProductAssociation dataProductAssociation : dataProductAssociations) {
RelatedEntitiesScrollResult result =
retrieverContext
.getGraphRetriever()
.scrollRelatedEntities(
null,
QueryUtils.newFilter(
"urn", dataProductAssociation.getDestinationUrn().toString()),
null,
EMPTY_FILTER,
ImmutableList.of("DataProductContains"),
QueryUtils.newRelationshipFilter(EMPTY_FILTER, RelationshipDirection.INCOMING),
Collections.emptyList(),
null,
10, // Should only ever be one, if ever greater than ten will decrease over time
// to become consistent
null,
null);
if (!result.getEntities().isEmpty()) {
for (RelatedEntities entity : result.getEntities()) {
if (!dataProductItem.getUrn().equals(UrnUtils.getUrn(entity.getSourceUrn()))) {
GenericJsonPatch.PatchOp patchOp = new GenericJsonPatch.PatchOp();
patchOp.setOp(PatchOperationType.REMOVE.getValue());
patchOp.setPath(String.format("/assets/%s", entity.getDestinationUrn()));
patchOpMap
.computeIfAbsent(entity.getSourceUrn(), urn -> new ArrayList<>())
.add(patchOp);
}
}
}
for (String urn : patchOpMap.keySet()) {
EntitySpec entitySpec =
retrieverContext
.getAspectRetriever()
.getEntityRegistry()
.getEntitySpec(DATA_PRODUCT_ENTITY_NAME);
mcpItems.add(
PatchItemImpl.builder()
.urn(UrnUtils.getUrn(urn))
.entitySpec(
retrieverContext
.getAspectRetriever()
.getEntityRegistry()
.getEntitySpec(DATA_PRODUCT_ENTITY_NAME))
.aspectName(DATA_PRODUCT_PROPERTIES_ASPECT_NAME)
.aspectSpec(entitySpec.getAspectSpec(DATA_PRODUCT_PROPERTIES_ASPECT_NAME))
.patch(
GenericJsonPatch.builder()
.arrayPrimaryKeys(
Map.of(
DataProductPropertiesTemplate.ASSETS_FIELD_NAME,
List.of(DataProductPropertiesTemplate.KEY_FIELD_NAME)))
.patch(patchOpMap.get(urn))
.build()
.getJsonPatch())
.auditStamp(mclItem.getAuditStamp())
.systemMetadata(mclItem.getSystemMetadata())
.build(retrieverContext.getAspectRetriever().getEntityRegistry()));
}
return mcpItems.stream();
}
return Stream.empty();
for (String urn : patchOpMap.keySet()) {
EntitySpec entitySpec =
retrieverContext
.getAspectRetriever()
.getEntityRegistry()
.getEntitySpec(DATA_PRODUCT_ENTITY_NAME);
mcpItems.add(
PatchItemImpl.builder()
.urn(UrnUtils.getUrn(urn))
.entitySpec(
retrieverContext
.getAspectRetriever()
.getEntityRegistry()
.getEntitySpec(DATA_PRODUCT_ENTITY_NAME))
.aspectName(DATA_PRODUCT_PROPERTIES_ASPECT_NAME)
.aspectSpec(entitySpec.getAspectSpec(DATA_PRODUCT_PROPERTIES_ASPECT_NAME))
.patch(
GenericJsonPatch.builder()
.arrayPrimaryKeys(
Map.of(
DataProductPropertiesTemplate.ASSETS_FIELD_NAME,
List.of(DataProductPropertiesTemplate.KEY_FIELD_NAME)))
.patch(patchOpMap.get(urn))
.build()
.getJsonPatch())
.auditStamp(dataProductItem.getAuditStamp())
.systemMetadata(dataProductItem.getSystemMetadata())
.build(retrieverContext.getAspectRetriever().getEntityRegistry()));
}

return mcpItems.stream();
}
}
Loading
Loading