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; ...@@ -22,11 +22,10 @@ import java.io.OutputStream;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.script.Bindings;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException; import javax.script.ScriptException;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.groovy.GroovyExpression; import org.apache.atlas.groovy.GroovyExpression;
import org.apache.atlas.typesystem.types.IDataType; import org.apache.atlas.typesystem.types.IDataType;
...@@ -261,7 +260,7 @@ public interface AtlasGraph<V, E> { ...@@ -261,7 +260,7 @@ public interface AtlasGraph<V, E> {
* *
* @return script engine to execute Gremlin queries * @return script engine to execute Gremlin queries
*/ */
ScriptEngine getGremlinScriptEngine(); ScriptEngine getGremlinScriptEngine() throws AtlasBaseException;
/** /**
* Release an instance of the script engine obtained with getGremlinScriptEngine() * Release an instance of the script engine obtained with getGremlinScriptEngine()
...@@ -280,7 +279,7 @@ public interface AtlasGraph<V, E> { ...@@ -280,7 +279,7 @@ public interface AtlasGraph<V, E> {
* *
* @throws ScriptException * @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. * Executes a Gremlin script using a ScriptEngineManager provided by consumer, returns an object with the result.
......
...@@ -35,6 +35,8 @@ import javax.script.ScriptEngine; ...@@ -35,6 +35,8 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
import javax.script.ScriptException; 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.groovy.GroovyExpression;
import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
...@@ -264,7 +266,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { ...@@ -264,7 +266,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
} }
@Override @Override
public Object executeGremlinScript(String query, boolean isPath) throws ScriptException { public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
Object result = executeGremlinScript(query); Object result = executeGremlinScript(query);
return convertGremlinScriptResult(isPath, result); return convertGremlinScriptResult(isPath, result);
...@@ -285,14 +287,16 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { ...@@ -285,14 +287,16 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
} }
@Override @Override
public ScriptEngine getGremlinScriptEngine() { public ScriptEngine getGremlinScriptEngine() throws AtlasBaseException {
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("gremlin-groovy"); ScriptEngine engine = manager.getEngineByName("gremlin-groovy");
if (engine == null) {
throw new AtlasBaseException(AtlasErrorCode.FAILED_TO_OBTAIN_GREMLIN_SCRIPT_ENGINE, "gremlin-groovy");
}
//Do not cache script compilations due to memory implications //Do not cache script compilations due to memory implications
if (engine != null) {
engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE); engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE);
}
return engine; return engine;
} }
...@@ -321,7 +325,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { ...@@ -321,7 +325,7 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
return convertGremlinScriptResult(isPath, result); return convertGremlinScriptResult(isPath, result);
} }
private Object executeGremlinScript(String gremlinQuery) throws ScriptException { private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
Object result = null; Object result = null;
ScriptEngine engine = getGremlinScriptEngine(); ScriptEngine engine = getGremlinScriptEngine();
...@@ -331,6 +335,8 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> { ...@@ -331,6 +335,8 @@ public class Titan0Graph implements AtlasGraph<Titan0Vertex, Titan0Edge> {
bindings.put("g", getGraph()); bindings.put("g", getGraph());
result = engine.eval(gremlinQuery, bindings); result = engine.eval(gremlinQuery, bindings);
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally { } finally {
releaseGremlinScriptEngine(engine); releaseGremlinScriptEngine(engine);
} }
......
...@@ -30,6 +30,8 @@ import javax.script.Bindings; ...@@ -30,6 +30,8 @@ import javax.script.Bindings;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptException; 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.groovy.GroovyExpression;
import org.apache.atlas.repository.graphdb.AtlasEdge; import org.apache.atlas.repository.graphdb.AtlasEdge;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
...@@ -316,13 +318,12 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> { ...@@ -316,13 +318,12 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> {
} }
@Override @Override
public Object executeGremlinScript(String query, boolean isPath) throws ScriptException { public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
Object result = executeGremlinScript(query); Object result = executeGremlinScript(query);
return convertGremlinValue(result); return convertGremlinValue(result);
} }
private Object executeGremlinScript(String gremlinQuery) throws ScriptException { private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
GremlinGroovyScriptEngine scriptEngine = getGremlinScriptEngine(); GremlinGroovyScriptEngine scriptEngine = getGremlinScriptEngine();
try { try {
...@@ -334,6 +335,8 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> { ...@@ -334,6 +335,8 @@ public class Titan1Graph implements AtlasGraph<Titan1Vertex, Titan1Edge> {
Object result = scriptEngine.eval(gremlinQuery, bindings); Object result = scriptEngine.eval(gremlinQuery, bindings);
return result; return result;
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
} finally { } finally {
releaseGremlinScriptEngine(scriptEngine); releaseGremlinScriptEngine(scriptEngine);
} }
......
...@@ -96,9 +96,9 @@ public enum AtlasErrorCode { ...@@ -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_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"), 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}"), 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}"), 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."), 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}"), QUICK_START(500, "ATLAS-500-00-00C", "Failed to run QuickStart: {0}"),
......
...@@ -94,7 +94,6 @@ public class EntityDiscoveryService implements AtlasDiscoveryService { ...@@ -94,7 +94,6 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL); AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset); GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
try {
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Executing DSL query: {}", dslQuery); LOG.debug("Executing DSL query: {}", dslQuery);
} }
...@@ -139,10 +138,6 @@ public class EntityDiscoveryService implements AtlasDiscoveryService { ...@@ -139,10 +138,6 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
} }
} }
} catch (ScriptException e) {
throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, gremlinQuery.queryStr());
}
return ret; return ret;
} }
......
...@@ -87,11 +87,10 @@ public class EntityLineageService implements AtlasLineageService { ...@@ -87,11 +87,10 @@ public class EntityLineageService implements AtlasLineageService {
} }
private AtlasLineageInfo getLineageInfo(String guid, LineageDirection direction, int depth) throws AtlasBaseException { private AtlasLineageInfo getLineageInfo(String guid, LineageDirection direction, int depth) throws AtlasBaseException {
Map<String, AtlasEntityHeader> entities = new HashMap<String, AtlasEntityHeader>(); Map<String, AtlasEntityHeader> entities = new HashMap<>();
Set<LineageRelation> relations = new HashSet<LineageRelation>(); Set<LineageRelation> relations = new HashSet<>();
String lineageQuery = getLineageQuery(guid, direction, depth); 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)) { if (CollectionUtils.isNotEmpty(paths)) {
...@@ -127,10 +126,6 @@ public class EntityLineageService implements AtlasLineageService { ...@@ -127,10 +126,6 @@ public class EntityLineageService implements AtlasLineageService {
} }
} }
} catch (ScriptException e) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_LINEAGE_QUERY_FAILED, lineageQuery);
}
return new AtlasLineageInfo(guid, entities, relations, direction, depth); return new AtlasLineageInfo(guid, entities, relations, direction, depth);
} }
......
...@@ -27,12 +27,12 @@ import java.util.Map; ...@@ -27,12 +27,12 @@ import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.script.ScriptException;
import org.apache.atlas.AtlasClient; import org.apache.atlas.AtlasClient;
import org.apache.atlas.GraphTransaction; import org.apache.atlas.GraphTransaction;
import org.apache.atlas.discovery.DiscoveryException; import org.apache.atlas.discovery.DiscoveryException;
import org.apache.atlas.discovery.DiscoveryService; import org.apache.atlas.discovery.DiscoveryService;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.query.Expressions; import org.apache.atlas.query.Expressions;
import org.apache.atlas.query.GremlinEvaluator; import org.apache.atlas.query.GremlinEvaluator;
import org.apache.atlas.query.GremlinQuery; import org.apache.atlas.query.GremlinQuery;
...@@ -197,8 +197,8 @@ public class GraphBackedDiscoveryService implements DiscoveryService { ...@@ -197,8 +197,8 @@ public class GraphBackedDiscoveryService implements DiscoveryService {
try { try {
Object o = graph.executeGremlinScript(gremlinQuery, false); Object o = graph.executeGremlinScript(gremlinQuery, false);
return extractResult(o); return extractResult(o);
} catch (ScriptException se) { } catch (AtlasBaseException e) {
throw new DiscoveryException(se); throw new DiscoveryException(e);
} }
} }
......
...@@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting; ...@@ -21,6 +21,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import org.apache.atlas.ApplicationProperties; import org.apache.atlas.ApplicationProperties;
import org.apache.atlas.AtlasException; import org.apache.atlas.AtlasException;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.metrics.AtlasMetrics; import org.apache.atlas.model.metrics.AtlasMetrics;
import org.apache.atlas.repository.graph.AtlasGraphProvider; import org.apache.atlas.repository.graph.AtlasGraphProvider;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
...@@ -31,7 +32,6 @@ import org.slf4j.Logger; ...@@ -31,7 +32,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.inject.Inject; import javax.inject.Inject;
import javax.script.ScriptException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -100,7 +100,7 @@ public class MetricsService { ...@@ -100,7 +100,7 @@ public class MetricsService {
LOG.debug("Executing query: {}", metricQuery); LOG.debug("Executing query: {}", metricQuery);
} }
executeGremlinQuery(metrics, metricQuery.group, metricQuery.name, metricQuery.query); executeGremlinQuery(metrics, metricQuery.group, metricQuery.name, metricQuery.query);
} catch (ScriptException e) { } catch (AtlasBaseException e) {
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Gremlin execution failed for metric {}", metricQuery, e); LOG.debug("Gremlin execution failed for metric {}", metricQuery, e);
} else { } else {
...@@ -120,7 +120,7 @@ public class MetricsService { ...@@ -120,7 +120,7 @@ public class MetricsService {
return cachedMetrics; 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); Object result = atlasGraph.executeGremlinScript(query, false);
if (result instanceof Number) { if (result instanceof Number) {
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
*/ */
package org.apache.atlas.services; package org.apache.atlas.services;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.metrics.AtlasMetrics; import org.apache.atlas.model.metrics.AtlasMetrics;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
...@@ -26,7 +27,6 @@ import org.mockito.stubbing.Answer; ...@@ -26,7 +27,6 @@ import org.mockito.stubbing.Answer;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import javax.script.ScriptException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
...@@ -49,7 +49,7 @@ public class MetricsServiceTest { ...@@ -49,7 +49,7 @@ public class MetricsServiceTest {
private Number mockCount = 10; private Number mockCount = 10;
@BeforeClass @BeforeClass
public void init() throws ScriptException { public void init() throws AtlasBaseException {
Map<String, Object> mockMap = new HashMap<>(); Map<String, Object> mockMap = new HashMap<>();
mockMap.put("a", 1); mockMap.put("a", 1);
mockMap.put("b", 2); mockMap.put("b", 2);
...@@ -66,7 +66,7 @@ public class MetricsServiceTest { ...@@ -66,7 +66,7 @@ public class MetricsServiceTest {
metricsService = new MetricsService(mockConfig, mockGraph); metricsService = new MetricsService(mockConfig, mockGraph);
} }
private void setupMockGraph() throws ScriptException { private void setupMockGraph() throws AtlasBaseException {
if (mockGraph == null) mockGraph = mock(AtlasGraph.class); if (mockGraph == null) mockGraph = mock(AtlasGraph.class);
when(mockGraph.executeGremlinScript(anyString(), eq(false))).thenAnswer(new Answer<Object>() { when(mockGraph.executeGremlinScript(anyString(), eq(false))).thenAnswer(new Answer<Object>() {
@Override @Override
...@@ -81,7 +81,7 @@ public class MetricsServiceTest { ...@@ -81,7 +81,7 @@ public class MetricsServiceTest {
} }
@Test @Test
public void testGetMetrics() throws InterruptedException, ScriptException { public void testGetMetrics() throws InterruptedException, AtlasBaseException {
assertNotNull(metricsService); assertNotNull(metricsService);
AtlasMetrics metrics = metricsService.getMetrics(false); AtlasMetrics metrics = metricsService.getMetrics(false);
assertNotNull(metrics); assertNotNull(metrics);
......
...@@ -628,7 +628,7 @@ public class ExportService { ...@@ -628,7 +628,7 @@ public class ExportService {
private int progressReportCount = 0; private int progressReportCount = 0;
ExportContext(AtlasExportResult result, ZipSink sink) { ExportContext(AtlasExportResult result, ZipSink sink) throws AtlasBaseException {
this.result = result; this.result = result;
this.sink = sink; 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