forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 9
MESH-645 : Repair API for daapOutputPortGuids #5481
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65cabd2
mesh-645: Repair API for daapOutputPortGuids
PRATHAM2002-DS edc4a37
mesh-645: corrected query param validation
PRATHAM2002-DS 06c7741
mesh-645: removed unused imports
PRATHAM2002-DS 97999d6
mesh-645: Resolved edge case
PRATHAM2002-DS 1e669d8
mesh-645: Resolved PR comments
PRATHAM2002-DS db53377
mesh-645: Resolved PR comments
PRATHAM2002-DS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...n/java/org/apache/atlas/repository/store/graph/v2/repair/AtlasRepairAttributeService.java
This file contains hidden or 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,70 @@ | ||
package org.apache.atlas.repository.store.graph.v2.repair; | ||
|
||
import org.apache.atlas.exception.AtlasBaseException; | ||
import org.apache.atlas.AtlasErrorCode; | ||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class AtlasRepairAttributeService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AtlasRepairAttributeService.class); | ||
|
||
private final RepairAttributeFactory repairAttributeFactory; | ||
|
||
@Inject | ||
public AtlasRepairAttributeService(RepairAttributeFactory repairAttributeFactory) { | ||
this.repairAttributeFactory = repairAttributeFactory; | ||
} | ||
|
||
public void repairAttributes(String attributeName, String repairType, Set<String> entityGuids) | ||
throws AtlasBaseException { | ||
|
||
validateRequest(attributeName, repairType, entityGuids); | ||
|
||
LOG.info("Starting attribute repair - Type: {}, Attribute: {}, Entities: {}", | ||
repairType, attributeName, entityGuids.size()); | ||
|
||
try { | ||
AtlasRepairAttributeStrategy strategy = repairAttributeFactory.getStrategy(repairType, entityGuids); | ||
|
||
strategy.validate(entityGuids, attributeName); | ||
strategy.repair(entityGuids, attributeName); | ||
|
||
LOG.info("Successfully completed attribute repair - Type: {}, Attribute: {}, Entities: {}", | ||
repairType, attributeName, entityGuids.size()); | ||
|
||
} catch (Exception e) { | ||
LOG.error("Error during attribute repair - Type: {}, Attribute: {}, Entities: {}", | ||
repairType, attributeName, entityGuids.size(), e); | ||
throw e; | ||
} | ||
} | ||
|
||
private void validateRequest(String attributeName, String repairType, Set<String> entityGuids) | ||
throws AtlasBaseException { | ||
|
||
if (StringUtils.isEmpty(attributeName)) { | ||
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Attribute name cannot be empty"); | ||
} | ||
|
||
if (StringUtils.isEmpty(repairType)) { | ||
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Repair type cannot be empty"); | ||
} | ||
|
||
if (CollectionUtils.isEmpty(entityGuids)) { | ||
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Entity GUIDs cannot be empty"); | ||
} | ||
|
||
if (entityGuids.size() > 1000) { | ||
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, | ||
"Too many entities. Maximum allowed: 1000, provided: " + entityGuids.size()); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java/org/apache/atlas/repository/store/graph/v2/repair/AtlasRepairAttributeStrategy.java
This file contains hidden or 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,34 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.atlas.repository.store.graph.v2.repair; | ||
|
||
|
||
import org.apache.atlas.exception.AtlasBaseException; | ||
|
||
import java.util.Set; | ||
|
||
public interface AtlasRepairAttributeStrategy { | ||
|
||
String getRepairType(); | ||
void repair(Set<String> entityGuids, String attributeName) throws AtlasBaseException; | ||
void validate (Set<String> entityGuids, String attributeName) throws AtlasBaseException; | ||
|
||
} | ||
|
||
|
149 changes: 149 additions & 0 deletions
149
...a/org/apache/atlas/repository/store/graph/v2/repair/RemoveInvalidGuidsRepairStrategy.java
This file contains hidden or 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,149 @@ | ||
package org.apache.atlas.repository.store.graph.v2.repair; | ||
|
||
import org.apache.atlas.AtlasErrorCode; | ||
import org.apache.atlas.exception.AtlasBaseException; | ||
import org.apache.atlas.repository.graphdb.AtlasVertex; | ||
import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2; | ||
import org.apache.atlas.repository.store.graph.v2.EntityGraphRetriever; | ||
import org.apache.atlas.repository.store.graph.v2.TransactionInterceptHelper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
|
||
import static org.apache.atlas.repository.store.graph.v2.preprocessor.PreProcessorUtils.OUTPUT_PORT_GUIDS_ATTR; | ||
|
||
public class RemoveInvalidGuidsRepairStrategy implements AtlasRepairAttributeStrategy { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RemoveInvalidGuidsRepairStrategy.class); | ||
|
||
private final EntityGraphRetriever entityRetriever; | ||
|
||
|
||
private final TransactionInterceptHelper transactionInterceptHelper; | ||
|
||
private static final String REPAIR_TYPE = "REMOVE_INVALID_GUIDS"; | ||
|
||
public RemoveInvalidGuidsRepairStrategy(EntityGraphRetriever entityRetriever, TransactionInterceptHelper transactionInterceptHelper) { | ||
this.entityRetriever = entityRetriever; | ||
this.transactionInterceptHelper = transactionInterceptHelper; | ||
} | ||
|
||
@Override | ||
public String getRepairType() { | ||
return REPAIR_TYPE; | ||
} | ||
|
||
@Override | ||
public void validate(Set<String> entityGuids, String attributeName) throws AtlasBaseException { | ||
for (String entityGuid : entityGuids) { | ||
AtlasVertex entityVertex = entityRetriever.getEntityVertex(entityGuid); | ||
|
||
if (entityVertex == null) { | ||
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, "Entity vertex not found for guid: " + entityGuid); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void repair(Set<String> entityGuids, String attributeName) throws AtlasBaseException { | ||
try { | ||
int count = 0; | ||
int totalUpdatedCount = 0; | ||
|
||
for (String entityGuid : entityGuids) { | ||
AtlasVertex entityVertex = entityRetriever.getEntityVertex(entityGuid); | ||
|
||
if (entityVertex == null) { | ||
LOG.error("Entity vertex not found for guid: {}", entityGuid); | ||
continue; | ||
} | ||
|
||
if (!entityVertex.getPropertyKeys().contains(attributeName)) { | ||
LOG.info("Attribute: {} not found for entity: {}. Skipping repair for this entity.", attributeName, entityGuid); | ||
continue; | ||
} | ||
|
||
if (OUTPUT_PORT_GUIDS_ATTR.equals(attributeName)) { | ||
boolean isCommitRequired = repairAttr(entityVertex); | ||
if (isCommitRequired){ | ||
count++; | ||
totalUpdatedCount++; | ||
} else { | ||
LOG.info("No changes to commit for entity: {}", entityGuid); | ||
} | ||
|
||
if (count == 50) { | ||
LOG.info("Committing batch of 50 entities..."); | ||
commitChanges(); | ||
count = 0; | ||
} | ||
} | ||
} | ||
|
||
if (count > 0) { | ||
LOG.info("Committing remaining {} entities...", count); | ||
commitChanges(); | ||
} | ||
|
||
LOG.info("Total Vertex updated: {}", totalUpdatedCount); | ||
|
||
} catch(Exception e){ | ||
LOG.error("Error while performing repair: {}", entityGuids, e); | ||
throw e; | ||
} | ||
} | ||
|
||
private boolean repairAttr(AtlasVertex vertex) throws AtlasBaseException { | ||
try{ | ||
boolean isCommitRequired = false; | ||
|
||
List<String> outputPortGuids = vertex.getMultiValuedProperty(OUTPUT_PORT_GUIDS_ATTR, String.class); | ||
if (outputPortGuids == null || outputPortGuids.isEmpty()) { | ||
LOG.info("No guids found in attribute: {} for entity: {}. Skipping repair for this entity.", OUTPUT_PORT_GUIDS_ATTR, vertex.getProperty("guid", String.class)); | ||
return false; | ||
} | ||
|
||
List<String> validGuids = new ArrayList<>(); | ||
List<String> invalidGuids = new ArrayList<>(); | ||
|
||
for (String guid : outputPortGuids) { | ||
AtlasVertex portVertex = entityRetriever.getEntityVertex(guid); | ||
if (portVertex != null) { | ||
validGuids.add(guid); | ||
} else { | ||
invalidGuids.add(guid); | ||
} | ||
} | ||
|
||
if (!invalidGuids.isEmpty()) { | ||
LOG.info("Removing invalid guids: {} from attribute: {} for entity: {}", invalidGuids, OUTPUT_PORT_GUIDS_ATTR, vertex.getProperty("guid", String.class)); | ||
|
||
vertex.removeProperty(OUTPUT_PORT_GUIDS_ATTR); | ||
|
||
for (String validGuid : validGuids) { | ||
AtlasGraphUtilsV2.addEncodedProperty(vertex, OUTPUT_PORT_GUIDS_ATTR, validGuid); | ||
} | ||
PRATHAM2002-DS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
isCommitRequired = true; | ||
} else { | ||
LOG.info("All guids in attribute: {} for entity: {} are valid. No repair needed.", OUTPUT_PORT_GUIDS_ATTR, vertex.getProperty("guid", String.class)); | ||
} | ||
|
||
return isCommitRequired; | ||
} catch (Exception e) { | ||
LOG.error("Failed to repair attribute for entity: ", e); | ||
throw e; | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
PRATHAM2002-DS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
38 changes: 38 additions & 0 deletions
38
...c/main/java/org/apache/atlas/repository/store/graph/v2/repair/RepairAttributeFactory.java
This file contains hidden or 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,38 @@ | ||
package org.apache.atlas.repository.store.graph.v2.repair; | ||
|
||
import org.apache.atlas.exception.AtlasBaseException; | ||
import org.apache.atlas.AtlasErrorCode; | ||
import org.apache.atlas.repository.store.graph.v2.EntityGraphRetriever; | ||
import org.apache.atlas.repository.store.graph.v2.TransactionInterceptHelper; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class RepairAttributeFactory { | ||
|
||
private final EntityGraphRetriever entityRetriever; | ||
private final TransactionInterceptHelper transactionInterceptHelper; | ||
|
||
@Inject | ||
public RepairAttributeFactory(EntityGraphRetriever entityRetriever, | ||
TransactionInterceptHelper transactionInterceptHelper) { | ||
this.entityRetriever = entityRetriever; | ||
this.transactionInterceptHelper = transactionInterceptHelper; | ||
} | ||
|
||
public AtlasRepairAttributeStrategy getStrategy(String repairType, Set<String> entityGuids) throws AtlasBaseException { | ||
switch (repairType) { | ||
case "REMOVE_INVALID_GUIDS": | ||
return new RemoveInvalidGuidsRepairStrategy(entityRetriever, transactionInterceptHelper); | ||
default: | ||
throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, | ||
"Unsupported repair type: " + repairType + ". Supported types: [REMOVE_INVALID_GUIDS]"); | ||
} | ||
} | ||
|
||
public boolean isValidRepairType(String repairType) { | ||
return "REMOVE_INVALID_GUIDS".equals(repairType); | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.