Commit 0571fa42 by Ashutosh Mestry

ATLAS-3797: Refactoring: Improve Edge Creation.

parent 3de30f55
......@@ -26,6 +26,7 @@ import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.AtlasException;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasGraphIndexClient;
......@@ -85,6 +86,7 @@ import java.util.Set;
import static org.apache.atlas.repository.Constants.INDEX_SEARCH_VERTEX_PREFIX_DEFAULT;
import static org.apache.atlas.repository.Constants.INDEX_SEARCH_VERTEX_PREFIX_PROPERTY;
import static org.apache.atlas.repository.graphdb.janus.AtlasJanusGraphDatabase.getGraphInstance;
import static org.apache.atlas.type.Constants.STATE_PROPERTY_KEY;
/**
* Janus implementation of AtlasGraph.
......@@ -174,12 +176,11 @@ public class AtlasJanusGraph implements AtlasGraph<AtlasJanusVertex, AtlasJanusE
@Override
public AtlasEdge getEdgeBetweenVertices(AtlasVertex fromVertex, AtlasVertex toVertex, String edgeLabel) {
GraphTraversal gt = V(fromVertex.getId()).outE(edgeLabel).where(__.otherV().hasId(toVertex.getId()));
Object o = gt.hasNext() ? gt.next() : null;
if (o == null) {
return null;
}
return GraphDbObjectFactory.createEdge(this, (Edge) o);
Edge gremlinEdge = getFirstActiveEdge(gt);
return (gremlinEdge != null)
? GraphDbObjectFactory.createEdge(this, gremlinEdge)
: null;
}
@Override
......@@ -567,9 +568,22 @@ public class AtlasJanusGraph implements AtlasGraph<AtlasJanusVertex, AtlasJanusE
private final class ConvertGremlinValueFunction implements Function<Object, Object> {
@Override
public Object apply(Object input) {
return convertGremlinValue(input);
}
}
private Edge getFirstActiveEdge(GraphTraversal gt) {
while (gt.hasNext()) {
Edge gremlinEdge = (Edge) gt.next();
if (gremlinEdge != null && gremlinEdge.property(STATE_PROPERTY_KEY).isPresent() &&
gremlinEdge.property(STATE_PROPERTY_KEY).value().equals(AtlasEntity.Status.ACTIVE.toString())
) {
return gremlinEdge;
}
}
return null;
}
}
......@@ -150,23 +150,20 @@ public final class GraphHelper {
}
public AtlasEdge getOrCreateEdge(AtlasVertex outVertex, AtlasVertex inVertex, String edgeLabel) throws RepositoryException {
AtlasPerfMetrics.MetricRecorder metric = RequestContext.get().startMetricRecord("getOrCreateEdge");
for (int numRetries = 0; numRetries < maxRetries; numRetries++) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Running edge creation attempt {}", numRetries);
}
Iterator<AtlasEdge> edges = getAdjacentEdgesByLabel(inVertex, AtlasEdgeDirection.IN, edgeLabel);
while (edges.hasNext()) {
AtlasEdge edge = edges.next();
if (edge.getOutVertex().getId().equals(outVertex.getId())) {
Id.EntityState edgeState = getState(edge);
if (edgeState == null || edgeState == Id.EntityState.ACTIVE) {
if (inVertex.hasEdges(AtlasEdgeDirection.IN, edgeLabel) && outVertex.hasEdges(AtlasEdgeDirection.OUT, edgeLabel)) {
AtlasEdge edge = graph.getEdgeBetweenVertices(outVertex, inVertex, edgeLabel);
if (edge != null) {
return edge;
}
}
}
return addEdge(outVertex, inVertex, edgeLabel);
} catch (Exception e) {
......@@ -186,6 +183,8 @@ public final class GraphHelper {
}
}
}
RequestContext.get().endMetricRecord(metric);
return null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment