Commit 30284c9a by Sarath Subramanian Committed by Madhan Neethiraj

ATLAS-1685: fix for issues flagged by coverity scan

parent d5a5238b
......@@ -22,11 +22,10 @@ import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.typesystem.types.IDataType;
......@@ -261,7 +260,7 @@ public interface AtlasGraph<V, E> {
*
* @return script engine to execute Gremlin queries
*/
ScriptEngine getGremlinScriptEngine();
ScriptEngine getGremlinScriptEngine() throws AtlasBaseException;
/**
* Release an instance of the script engine obtained with getGremlinScriptEngine()
......@@ -280,7 +279,7 @@ public interface AtlasGraph<V, E> {
*
* @throws ScriptException
*/
Object executeGremlinScript(String query, boolean isPath) throws ScriptException;
Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException;
/**
* Executes a Gremlin script using a ScriptEngineManager provided by consumer, returns an object with the result.
......
......@@ -35,6 +35,8 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph;
......@@ -264,7 +266,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
}
@Override
public Object executeGremlinScript(String query, boolean isPath) throws ScriptException {
public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
Object result = executeGremlinScript(query);
return convertGremlinScriptResult(isPath, result);
......@@ -285,15 +287,17 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
}
@Override
public ScriptEngine getGremlinScriptEngine() {
public ScriptEngine getGremlinScriptEngine() throws AtlasBaseException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("gremlin-groovy");
//Do not cache script compilations due to memory implications
if (engine != null) {
engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE);
if (engine == null) {
throw new AtlasBaseException(AtlasErrorCode.FAILED_TO_OBTAIN_GREMLIN_SCRIPT_ENGINE, "gremlin-groovy");
}
//Do not cache script compilations due to memory implications
engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE);
return engine;
}
......@@ -321,7 +325,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
return convertGremlinScriptResult(isPath, result);
}
private Object executeGremlinScript(String gremlinQuery) throws ScriptException {
private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
Object result = null;
ScriptEngine engine = getGremlinScriptEngine();
......@@ -331,6 +335,8 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
bindings.put("g", getGraph());
result = engine.eval(gremlinQuery, bindings);
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally {
releaseGremlinScriptEngine(engine);
}
......
......@@ -30,6 +30,8 @@ import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph;
......@@ -316,13 +318,12 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> {
}
@Override
public Object executeGremlinScript(String query, boolean isPath) throws ScriptException {
public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
Object result = executeGremlinScript(query);
return convertGremlinValue(result);
}
private Object executeGremlinScript(String gremlinQuery) throws ScriptException {
private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
GremlinGroovyScriptEngine scriptEngine = getGremlinScriptEngine();
try {
......@@ -334,6 +335,8 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> {
Object result = scriptEngine.eval(gremlinQuery, bindings);
return result;
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally {
releaseGremlinScriptEngine(scriptEngine);
}
......
......@@ -96,9 +96,9 @@ public enum AtlasErrorCode {
FAILED_TO_OBTAIN_TYPE_UPDATE_LOCK(500, "ATLAS-500-00-005", "Failed to get the lock; another type update might be in progress. Please try again"),
FAILED_TO_OBTAIN_IMPORT_EXPORT_LOCK(500, "ATLAS-500-00-006", "Another import or export is in progress. Please try again"),
NOTIFICATION_FAILED(500, "ATLAS-500-00-007", "Failed to notify for change {0}"),
GREMLIN_GROOVY_SCRIPT_ENGINE_FAILED(500, "ATLAS-500-00-008", "scriptEngine cannot be initialized for: {0}"),
FAILED_TO_OBTAIN_GREMLIN_SCRIPT_ENGINE(500, "ATLAS-500-00-008", "Failed to obtain gremlin script engine: {0}"),
JSON_ERROR_OBJECT_MAPPER_NULL_RETURNED(500, "ATLAS-500-00-009", "ObjectMapper.readValue returned NULL for class: {0}"),
GREMLIN_SCRIPT_EXECUTION_FAILED(500, "ATLAS-500-00-00A", "Script execution failed for: {0}"),
GREMLIN_SCRIPT_EXECUTION_FAILED(500, "ATLAS-500-00-00A", "Gremlin script execution failed: {0}"),
CURATOR_FRAMEWORK_UPDATE(500, "ATLAS-500-00-00B", "ActiveInstanceState.update resulted in exception."),
QUICK_START(500, "ATLAS-500-00-00C", "Failed to run QuickStart: {0}"),
......
......@@ -94,53 +94,48 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Executing DSL query: {}", dslQuery);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Executing DSL query: {}", dslQuery);
}
Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
List queryResult = (List) result;
Object firstElement = queryResult.get(0);
if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
List queryResult = (List) result;
Object firstElement = queryResult.get(0);
if (firstElement instanceof AtlasVertex) {
for (Object element : queryResult) {
if (element instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
} else {
LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
}
if (firstElement instanceof AtlasVertex) {
for (Object element : queryResult) {
if (element instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
} else {
LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
}
} else if (firstElement instanceof Map &&
(((Map)firstElement).containsKey("theInstance") || ((Map)firstElement).containsKey("theTrait"))) {
for (Object element : queryResult) {
if (element instanceof Map) {
Map map = (Map)element;
}
} else if (firstElement instanceof Map &&
(((Map)firstElement).containsKey("theInstance") || ((Map)firstElement).containsKey("theTrait"))) {
for (Object element : queryResult) {
if (element instanceof Map) {
Map map = (Map)element;
if (map.containsKey("theInstance")) {
Object value = map.get("theInstance");
if (map.containsKey("theInstance")) {
Object value = map.get("theInstance");
if (value instanceof List && CollectionUtils.isNotEmpty((List)value)) {
Object entry = ((List)value).get(0);
if (value instanceof List && CollectionUtils.isNotEmpty((List)value)) {
Object entry = ((List)value).get(0);
if (entry instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)entry));
}
if (entry instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)entry));
}
}
} else {
LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
}
} else {
LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
}
} else if (gremlinQuery.hasSelectList()) {
ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
}
} else if (gremlinQuery.hasSelectList()) {
ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
}
} catch (ScriptException e) {
throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, gremlinQuery.queryStr());
}
return ret;
......
......@@ -87,48 +87,43 @@ public class EntityLineageService implements AtlasLineageService {
}
private AtlasLineageInfo getLineageInfo(String guid, LineageDirection direction, int depth) throws AtlasBaseException {
Map<String, AtlasEntityHeader> entities = new HashMap<String, AtlasEntityHeader>();
Set<LineageRelation> relations = new HashSet<LineageRelation>();
Map<String, AtlasEntityHeader> entities = new HashMap<>();
Set<LineageRelation> relations = new HashSet<>();
String lineageQuery = getLineageQuery(guid, direction, depth);
try {
List paths = (List) graph.executeGremlinScript(lineageQuery, true);
List paths = (List) graph.executeGremlinScript(lineageQuery, true);
if (CollectionUtils.isNotEmpty(paths)) {
for (Object path : paths) {
if (path instanceof List) {
List vertices = (List) path;
if (CollectionUtils.isNotEmpty(paths)) {
for (Object path : paths) {
if (path instanceof List) {
List vertices = (List) path;
if (CollectionUtils.isNotEmpty(vertices)) {
AtlasEntityHeader prev = null;
if (CollectionUtils.isNotEmpty(vertices)) {
AtlasEntityHeader prev = null;
for (Object vertex : vertices) {
if (!(vertex instanceof AtlasVertex)) {
continue;
}
for (Object vertex : vertices) {
if (!(vertex instanceof AtlasVertex)) {
continue;
}
AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader((AtlasVertex)vertex);
AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader((AtlasVertex)vertex);
if (!entities.containsKey(entity.getGuid())) {
entities.put(entity.getGuid(), entity);
}
if (!entities.containsKey(entity.getGuid())) {
entities.put(entity.getGuid(), entity);
}
if (prev != null) {
if (direction.equals(LineageDirection.INPUT)) {
relations.add(new LineageRelation(entity.getGuid(), prev.getGuid()));
} else if (direction.equals(LineageDirection.OUTPUT)) {
relations.add(new LineageRelation(prev.getGuid(), entity.getGuid()));
}
if (prev != null) {
if (direction.equals(LineageDirection.INPUT)) {
relations.add(new LineageRelation(entity.getGuid(), prev.getGuid()));
} else if (direction.equals(LineageDirection.OUTPUT)) {
relations.add(new LineageRelation(prev.getGuid(), entity.getGuid()));
}
prev = entity;
}
prev = entity;
}
}
}
}
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_LINEAGE_QUERY_FAILED, lineageQuery);
}
return new AtlasLineageInfo(guid, entities, relations, direction, depth);
......
......@@ -27,12 +27,12 @@ import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.script.ScriptException;
import org.apache.atlas.AtlasClient;
import org.apache.atlas.GraphTransaction;
import org.apache.atlas.discovery.DiscoveryException;
import org.apache.atlas.discovery.DiscoveryService;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.query.Expressions;
import org.apache.atlas.query.GremlinEvaluator;
import org.apache.atlas.query.GremlinQuery;
......@@ -197,8 +197,8 @@ public class GraphBackedDiscoveryService implements DiscoveryService {
try {
Object o = graph.executeGremlinScript(gremlinQuery, false);
return extractResult(o);
} catch (ScriptException se) {
throw new DiscoveryException(se);
} catch (AtlasBaseException e) {
throw new DiscoveryException(e);
}
}
......
......@@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Singleton;
import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.metrics.AtlasMetrics;
import org.apache.atlas.repository.graph.AtlasGraphProvider;
import org.apache.atlas.repository.graphdb.AtlasGraph;
......@@ -31,7 +32,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.script.ScriptException;
import java.util.List;
import java.util.Map;
......@@ -100,7 +100,7 @@ public class MetricsService {
LOG.debug("Executing query: {}", metricQuery);
}
executeGremlinQuery(metrics, metricQuery.group, metricQuery.name, metricQuery.query);
} catch (ScriptException e) {
} catch (AtlasBaseException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Gremlin execution failed for metric {}", metricQuery, e);
} else {
......@@ -120,7 +120,7 @@ public class MetricsService {
return cachedMetrics;
}
private void executeGremlinQuery(AtlasMetrics metrics, String type, String name, String query) throws ScriptException {
private void executeGremlinQuery(AtlasMetrics metrics, String type, String name, String query) throws AtlasBaseException {
Object result = atlasGraph.executeGremlinScript(query, false);
if (result instanceof Number) {
......
......@@ -17,6 +17,7 @@
*/
package org.apache.atlas.services;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.metrics.AtlasMetrics;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.type.AtlasTypeRegistry;
......@@ -26,7 +27,6 @@ import org.mockito.stubbing.Answer;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.script.ScriptException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
......@@ -49,7 +49,7 @@ public class MetricsServiceTest {
private Number mockCount = 10;
@BeforeClass
public void init() throws ScriptException {
public void init() throws AtlasBaseException {
Map<String, Object> mockMap = new HashMap<>();
mockMap.put("a", 1);
mockMap.put("b", 2);
......@@ -66,7 +66,7 @@ public class MetricsServiceTest {
metricsService = new MetricsService(mockConfig, mockGraph);
}
private void setupMockGraph() throws ScriptException {
private void setupMockGraph() throws AtlasBaseException {
if (mockGraph == null) mockGraph = mock(AtlasGraph.class);
when(mockGraph.executeGremlinScript(anyString(), eq(false))).thenAnswer(new Answer<Object>() {
@Override
......@@ -81,7 +81,7 @@ public class MetricsServiceTest {
}
@Test
public void testGetMetrics() throws InterruptedException, ScriptException {
public void testGetMetrics() throws InterruptedException, AtlasBaseException {
assertNotNull(metricsService);
AtlasMetrics metrics = metricsService.getMetrics(false);
assertNotNull(metrics);
......
......@@ -628,7 +628,7 @@ public class ExportService {
private int progressReportCount = 0;
ExportContext(AtlasExportResult result, ZipSink sink) {
ExportContext(AtlasExportResult result, ZipSink sink) throws AtlasBaseException {
this.result = result;
this.sink = sink;
......
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