Commit d1109efe by Sarath Subramanian

ATLAS-3377: Update AtlasPatchRegistry to use graph query instead of index query

parent 2223c82c
...@@ -22,7 +22,9 @@ import org.apache.atlas.RequestContext; ...@@ -22,7 +22,9 @@ import org.apache.atlas.RequestContext;
import org.apache.atlas.model.patches.AtlasPatch; import org.apache.atlas.model.patches.AtlasPatch;
import org.apache.atlas.model.patches.AtlasPatch.AtlasPatches; import org.apache.atlas.model.patches.AtlasPatch.AtlasPatches;
import org.apache.atlas.model.patches.AtlasPatch.PatchStatus; import org.apache.atlas.model.patches.AtlasPatch.PatchStatus;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasGraphQuery;
import org.apache.atlas.repository.graphdb.AtlasIndexQuery; import org.apache.atlas.repository.graphdb.AtlasIndexQuery;
import org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result; import org.apache.atlas.repository.graphdb.AtlasIndexQuery.Result;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
...@@ -45,8 +47,6 @@ import java.util.Map; ...@@ -45,8 +47,6 @@ import java.util.Map;
import static org.apache.atlas.model.patches.AtlasPatch.PatchStatus.FAILED; import static org.apache.atlas.model.patches.AtlasPatch.PatchStatus.FAILED;
import static org.apache.atlas.model.patches.AtlasPatch.PatchStatus.UNKNOWN; import static org.apache.atlas.model.patches.AtlasPatch.PatchStatus.UNKNOWN;
import static org.apache.atlas.repository.Constants.*; import static org.apache.atlas.repository.Constants.*;
import static org.apache.atlas.repository.graph.AtlasGraphProvider.getGraphInstance;
import static org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer.TYPEDEF_PATCH_TYPE;
import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.getEncodedProperty; import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.getEncodedProperty;
import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.getIndexSearchPrefix; import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.getIndexSearchPrefix;
import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.setEncodedProperty; import static org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2.setEncodedProperty;
...@@ -96,20 +96,20 @@ public class AtlasPatchRegistry { ...@@ -96,20 +96,20 @@ public class AtlasPatchRegistry {
} }
public void updateStatus(String patchId, PatchStatus patchStatus) { public void updateStatus(String patchId, PatchStatus patchStatus) {
AtlasVertex patchVertex = findByPatchId(patchId); try {
AtlasVertex patchVertex = findByPatchId(patchId);
if (patchVertex == null) {
return;
}
setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString());
setEncodedProperty(patchVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime());
setEncodedProperty(patchVertex, MODIFIED_BY_KEY, getCurrentUser());
setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString());
patchNameStatusMap.put(patchId, patchStatus); if (patchVertex != null) {
setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString());
setEncodedProperty(patchVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime());
setEncodedProperty(patchVertex, MODIFIED_BY_KEY, getCurrentUser());
setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString());
}
} finally {
graph.commit();
graph.commit(); patchNameStatusMap.put(patchId, patchStatus);
}
} }
private static String getId(String incomingId, String patchFile, int index) { private static String getId(String incomingId, String patchFile, int index) {
...@@ -128,20 +128,27 @@ public class AtlasPatchRegistry { ...@@ -128,20 +128,27 @@ public class AtlasPatchRegistry {
private void createOrUpdatePatchVertex(AtlasGraph graph, String patchId, String description, private void createOrUpdatePatchVertex(AtlasGraph graph, String patchId, String description,
String patchType, String action, PatchStatus patchStatus) { String patchType, String action, PatchStatus patchStatus) {
boolean isPatchRegistered = MapUtils.isNotEmpty(patchNameStatusMap) && patchNameStatusMap.containsKey(patchId); try {
AtlasVertex patchVertex = isPatchRegistered ? findByPatchId(patchId) : graph.addVertex(); AtlasVertex patchVertex = findByPatchId(patchId);
setEncodedProperty(patchVertex, PATCH_ID_PROPERTY_KEY, patchId); if (patchVertex == null) {
setEncodedProperty(patchVertex, PATCH_DESCRIPTION_PROPERTY_KEY, description); patchVertex = graph.addVertex();
setEncodedProperty(patchVertex, PATCH_TYPE_PROPERTY_KEY, patchType); }
setEncodedProperty(patchVertex, PATCH_ACTION_PROPERTY_KEY, action);
setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString()); setEncodedProperty(patchVertex, PATCH_ID_PROPERTY_KEY, patchId);
setEncodedProperty(patchVertex, TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); setEncodedProperty(patchVertex, PATCH_DESCRIPTION_PROPERTY_KEY, description);
setEncodedProperty(patchVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); setEncodedProperty(patchVertex, PATCH_TYPE_PROPERTY_KEY, patchType);
setEncodedProperty(patchVertex, CREATED_BY_KEY, AtlasTypeDefGraphStoreV2.getCurrentUser()); setEncodedProperty(patchVertex, PATCH_ACTION_PROPERTY_KEY, action);
setEncodedProperty(patchVertex, MODIFIED_BY_KEY, AtlasTypeDefGraphStoreV2.getCurrentUser()); setEncodedProperty(patchVertex, PATCH_STATE_PROPERTY_KEY, patchStatus.toString());
setEncodedProperty(patchVertex, TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime());
graph.commit(); setEncodedProperty(patchVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime());
setEncodedProperty(patchVertex, CREATED_BY_KEY, AtlasTypeDefGraphStoreV2.getCurrentUser());
setEncodedProperty(patchVertex, MODIFIED_BY_KEY, AtlasTypeDefGraphStoreV2.getCurrentUser());
} finally {
graph.commit();
patchNameStatusMap.put(patchId, patchStatus);
}
} }
private static Map<String, PatchStatus> getPatchNameStatusForAllRegistered(AtlasGraph graph) { private static Map<String, PatchStatus> getPatchNameStatusForAllRegistered(AtlasGraph graph) {
...@@ -170,7 +177,7 @@ public class AtlasPatchRegistry { ...@@ -170,7 +177,7 @@ public class AtlasPatchRegistry {
while (results != null && results.hasNext()) { while (results != null && results.hasNext()) {
AtlasVertex patchVertex = results.next().getVertex(); AtlasVertex patchVertex = results.next().getVertex();
AtlasPatch patch = toAtlasPatch(patchVertex); AtlasPatch patch = toAtlasPatch(patchVertex);
ret.add(patch); ret.add(patch);
} }
...@@ -180,10 +187,10 @@ public class AtlasPatchRegistry { ...@@ -180,10 +187,10 @@ public class AtlasPatchRegistry {
} }
} catch (Throwable t) { } catch (Throwable t) {
LOG.warn("getAllPatches(): Returned empty result!"); LOG.warn("getAllPatches(): Returned empty result!");
} finally {
graph.commit();
} }
graph.commit();
return new AtlasPatches(ret); return new AtlasPatches(ret);
} }
...@@ -204,20 +211,11 @@ public class AtlasPatchRegistry { ...@@ -204,20 +211,11 @@ public class AtlasPatchRegistry {
return ret; return ret;
} }
private static AtlasVertex findByPatchId(String patchId) { public AtlasVertex findByPatchId(String patchId) {
AtlasVertex ret = null; AtlasGraphQuery query = graph.query().has(Constants.PATCH_ID_PROPERTY_KEY, patchId);
String indexQuery = getIndexSearchPrefix() + "\"" + PATCH_ID_PROPERTY_KEY + "\" : (" + patchId + ")"; Iterator<AtlasVertex> results = query.vertices().iterator();
Iterator<Result<Object, Object>> results = getGraphInstance().indexQuery(VERTEX_INDEX, indexQuery).vertices();
while (results != null && results.hasNext()) {
ret = results.next().getVertex();
if (ret != null) { return results.hasNext() ? results.next() : null;
break;
}
}
return ret;
} }
private static PatchStatus getPatchStatus(AtlasVertex vertex) { private static PatchStatus getPatchStatus(AtlasVertex vertex) {
......
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