forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3958 from atlanhq/mesh-316-support-unarchive
MESH-316 | Supporting Unarchiving of Products
- Loading branch information
Showing
6 changed files
with
377 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
.../main/java/org/apache/atlas/repository/migration/SoftDeletionProductMigrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package org.apache.atlas.repository.migration; | ||
|
||
import org.apache.atlas.exception.AtlasBaseException; | ||
import org.apache.atlas.model.instance.AtlasEntity; | ||
import org.apache.atlas.repository.graph.GraphHelper; | ||
import org.apache.atlas.repository.graphdb.AtlasEdge; | ||
import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; | ||
import org.apache.atlas.repository.graphdb.AtlasGraph; | ||
import org.apache.atlas.repository.graphdb.AtlasVertex; | ||
import org.apache.atlas.repository.store.graph.v2.TransactionInterceptHelper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import static org.apache.atlas.model.instance.AtlasEntity.Status.ACTIVE; | ||
import static org.apache.atlas.model.instance.AtlasEntity.Status.DELETED; | ||
import static org.apache.atlas.repository.Constants.EDGE_LABELS_FOR_HARD_DELETION; | ||
import static org.apache.atlas.repository.Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY; | ||
import static org.apache.atlas.repository.graph.GraphHelper.getStatus; | ||
|
||
public class SoftDeletionProductMigrationService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(SoftDeletionProductMigrationService.class); | ||
|
||
private final AtlasGraph graph; | ||
private final Set<String> productGuids; | ||
private final GraphHelper graphHelper; | ||
private final TransactionInterceptHelper transactionInterceptHelper; | ||
|
||
public SoftDeletionProductMigrationService(AtlasGraph graph, Set<String> productGuids, GraphHelper graphHelper, TransactionInterceptHelper transactionInterceptHelper) { | ||
this.graph = graph; | ||
this.productGuids = productGuids; | ||
this.graphHelper = graphHelper; | ||
this.transactionInterceptHelper = transactionInterceptHelper; | ||
} | ||
|
||
public void startEdgeMigration() throws AtlasBaseException { | ||
try { | ||
int count = 0; | ||
int totalUpdatedCount = 0; | ||
for (String productGuid: productGuids) { | ||
LOG.info("Removing edges for Product: {}", productGuid); | ||
|
||
if (productGuid != null && !productGuid.trim().isEmpty()) { | ||
AtlasVertex productVertex = graphHelper.getVertexForGUID(productGuid); | ||
|
||
if (productVertex == null) { | ||
LOG.info("ProductGUID with no vertex found: {}", productGuid); | ||
} else { | ||
AtlasEntity.Status vertexStatus = getStatus(productVertex); | ||
|
||
if (ACTIVE.equals(vertexStatus)) { | ||
boolean isCommitRequired = deleteEdgeForActiveProduct(productVertex); | ||
if (isCommitRequired) { | ||
count++; | ||
totalUpdatedCount++; | ||
} | ||
} | ||
|
||
if (DELETED.equals(vertexStatus)) { | ||
boolean isCommitRequired = deleteEdgeForArchivedProduct(productVertex); | ||
if (isCommitRequired) { | ||
count++; | ||
totalUpdatedCount++; | ||
} | ||
} | ||
|
||
if (count == 20) { | ||
LOG.info("Committing batch of 20 products..."); | ||
commitChanges(); | ||
count = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (count > 0) { | ||
LOG.info("Committing remaining {} products...", count); | ||
commitChanges(); | ||
} | ||
|
||
LOG.info("Total products updated: {}", totalUpdatedCount); | ||
} catch (Exception e) { | ||
LOG.error("Error while restoring state for Products: {}", productGuids, e); | ||
throw new AtlasBaseException(e); | ||
} | ||
} | ||
|
||
|
||
public boolean deleteEdgeForActiveProduct(AtlasVertex productVertex) { | ||
boolean isCommitRequired = false; | ||
try { | ||
Iterator<AtlasEdge> existingEdges = productVertex.getEdges(AtlasEdgeDirection.BOTH, (String[]) EDGE_LABELS_FOR_HARD_DELETION.toArray(new String[0])).iterator(); | ||
|
||
if (existingEdges == null || !existingEdges.hasNext()) { | ||
return isCommitRequired; | ||
} | ||
|
||
while (existingEdges.hasNext()) { | ||
AtlasEdge edge = existingEdges.next(); | ||
|
||
AtlasEntity.Status edgeStatus = getStatus(edge); | ||
|
||
if (DELETED.equals(edgeStatus)) { | ||
graph.removeEdge(edge); | ||
isCommitRequired = true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Error while deleting soft edges for Active Product: {}", productVertex, e); | ||
throw new RuntimeException(e); | ||
} | ||
return isCommitRequired; | ||
} | ||
|
||
|
||
private boolean deleteEdgeForArchivedProduct(AtlasVertex productVertex) { | ||
boolean isCommitRequired = false; | ||
try { | ||
Long updatedTime = productVertex.getProperty(MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); | ||
Iterator<AtlasEdge> existingEdges = productVertex.getEdges(AtlasEdgeDirection.BOTH, (String[]) EDGE_LABELS_FOR_HARD_DELETION.toArray(new String[0])).iterator(); | ||
|
||
if (existingEdges == null || !existingEdges.hasNext()) { | ||
return isCommitRequired; | ||
} | ||
|
||
while (existingEdges.hasNext()) { | ||
AtlasEdge edge = existingEdges.next(); | ||
Long modifiedEdgeTimestamp = edge.getProperty(MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); | ||
|
||
if (!updatedTime.equals(modifiedEdgeTimestamp)) { | ||
LOG.info("Removing edge with different timestamp: {}", edge); | ||
graph.removeEdge(edge); | ||
isCommitRequired = true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Error while deleting edges for Archived Product: {}", productVertex, e); | ||
throw new RuntimeException(e); | ||
} | ||
return isCommitRequired; | ||
} | ||
|
||
public void commitChanges() throws AtlasBaseException { | ||
try { | ||
transactionInterceptHelper.intercept(); | ||
LOG.info("Committed a entity to the graph"); | ||
} catch (Exception e) { | ||
LOG.error("Failed to commit asset: ", e); | ||
throw e; | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
...main/java/org/apache/atlas/repository/migration/ValidateProductEdgesMigrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.apache.atlas.repository.migration; | ||
|
||
import org.apache.atlas.exception.AtlasBaseException; | ||
import org.apache.atlas.model.instance.AtlasEntity; | ||
import org.apache.atlas.repository.graph.GraphHelper; | ||
import org.apache.atlas.repository.graphdb.AtlasEdge; | ||
import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; | ||
import org.apache.atlas.repository.graphdb.AtlasGraph; | ||
import org.apache.atlas.repository.graphdb.AtlasVertex; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import static org.apache.atlas.model.instance.AtlasEntity.Status.ACTIVE; | ||
import static org.apache.atlas.model.instance.AtlasEntity.Status.DELETED; | ||
import static org.apache.atlas.repository.Constants.EDGE_LABELS_FOR_HARD_DELETION; | ||
import static org.apache.atlas.repository.Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY; | ||
import static org.apache.atlas.repository.graph.GraphHelper.getStatus; | ||
|
||
public class ValidateProductEdgesMigrationService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ValidateProductEdgesMigrationService.class); | ||
|
||
private final Set<String> productGuids; | ||
private final GraphHelper graphHelper; | ||
|
||
public ValidateProductEdgesMigrationService(Set<String> productGuids, GraphHelper graphHelper) { | ||
this.productGuids = productGuids; | ||
this.graphHelper = graphHelper; | ||
} | ||
|
||
public boolean validateEdgeMigration() throws AtlasBaseException { | ||
try { | ||
int count = 0; | ||
int totalProductChecked = 0; | ||
boolean redundantEdgesFound = false; | ||
|
||
for (String productGuid: productGuids) { | ||
LOG.info("Validating edges for Product: {}", productGuid); | ||
|
||
if (productGuid != null && !productGuid.trim().isEmpty()) { | ||
AtlasVertex productVertex = graphHelper.getVertexForGUID(productGuid); | ||
|
||
if (productVertex == null) { | ||
LOG.info("ProductGUID with no vertex found: {}", productGuid); | ||
} else { | ||
AtlasEntity.Status vertexStatus = getStatus(productVertex); | ||
|
||
if (ACTIVE.equals(vertexStatus)) { | ||
boolean softDeletedEdgesFound = validateEdgeForActiveProduct(productVertex); | ||
if (softDeletedEdgesFound) { | ||
count++; | ||
totalProductChecked++; | ||
} else { | ||
totalProductChecked++; | ||
} | ||
} | ||
|
||
if (DELETED.equals(vertexStatus)) { | ||
boolean edgeWithDifferentTimeStampFound = validateEdgeForArchivedProduct(productVertex); | ||
if (edgeWithDifferentTimeStampFound) { | ||
count++; | ||
totalProductChecked++; | ||
} else { | ||
totalProductChecked++; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (count > 0) { | ||
redundantEdgesFound = true; | ||
LOG.info("Found {} products with redundant edges....", count); | ||
} | ||
|
||
LOG.info("Total products checked: {}", totalProductChecked); | ||
|
||
return redundantEdgesFound; | ||
} catch (Exception e) { | ||
LOG.error("Error while validating edges for Products: {}", productGuids, e); | ||
throw new AtlasBaseException(e); | ||
} | ||
} | ||
|
||
public boolean validateEdgeForActiveProduct (AtlasVertex productVertex) { | ||
boolean softDeletedEdgesFound = false; | ||
|
||
try { | ||
Iterator<AtlasEdge> existingEdges = productVertex.getEdges(AtlasEdgeDirection.BOTH, (String[]) EDGE_LABELS_FOR_HARD_DELETION.toArray(new String[0])).iterator(); | ||
|
||
if (existingEdges == null || !existingEdges.hasNext()) { | ||
LOG.info("No edges found for Product: {}", productVertex); | ||
return softDeletedEdgesFound; | ||
} | ||
|
||
while (existingEdges.hasNext()) { | ||
AtlasEdge edge = existingEdges.next(); | ||
|
||
AtlasEntity.Status edgeStatus = getStatus(edge); | ||
|
||
if (DELETED.equals(edgeStatus)) { | ||
softDeletedEdgesFound = true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Error while validating edges for Active Product: {}", productVertex, e); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return softDeletedEdgesFound; | ||
} | ||
|
||
public boolean validateEdgeForArchivedProduct (AtlasVertex productVertex) { | ||
boolean edgeWithDifferentTimeStampFound = false; | ||
try { | ||
Long updatedTime = productVertex.getProperty(MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); | ||
Iterator<AtlasEdge> existingEdges = productVertex.getEdges(AtlasEdgeDirection.BOTH, (String[]) EDGE_LABELS_FOR_HARD_DELETION.toArray(new String[0])).iterator(); | ||
|
||
if (existingEdges == null || !existingEdges.hasNext()) { | ||
LOG.info("No edges found for Product: {}", productVertex); | ||
return edgeWithDifferentTimeStampFound; | ||
} | ||
|
||
while (existingEdges.hasNext()) { | ||
AtlasEdge edge = existingEdges.next(); | ||
Long modifiedEdgeTimestamp = edge.getProperty(MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); | ||
|
||
if (!updatedTime.equals(modifiedEdgeTimestamp)) { | ||
LOG.info("Found edge with different timestamp: {}", edge); | ||
edgeWithDifferentTimeStampFound = true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.error("Error while validating edges for Archived Product: {}", productVertex, e); | ||
throw new RuntimeException(e); | ||
} | ||
return edgeWithDifferentTimeStampFound; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.