diff options
Diffstat (limited to 'catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java')
-rw-r--r-- | catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java | 217 |
1 files changed, 93 insertions, 124 deletions
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java index 66a2ba02ad..73f41a3e87 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphClient.java @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.dao.janusgraph; import fj.data.Either; @@ -60,72 +59,23 @@ import org.springframework.stereotype.Component; @Component("janusgraph-client") public class JanusGraphClient { - private static Logger logger = LoggerFactory.getLogger(JanusGraphClient.class.getName()); - private static Logger healthLogger = LoggerFactory.getLogger("janusgraph.healthcheck"); private static final String HEALTH_CHECK = GraphPropertiesDictionary.HEALTH_CHECK.getProperty(); private static final String OK = "GOOD"; - - public JanusGraphClient() { - } - - @PreDestroy - public void closeSession() { - if (graph.isOpen()) { - graph.close(); - logger.info("** JanusGraphClient session closed"); - } - } - - private class HealthCheckTask implements Callable<Vertex> { - @Override - public Vertex call() { - JanusGraphVertex vertex = - (JanusGraphVertex) graph.query().has(HEALTH_CHECK, OK).vertices().iterator().next(); - healthLogger.trace("Health Check Node Found...{}", vertex.property(HEALTH_CHECK)); - graph.tx().commit(); - return vertex; - } - } - - private class HealthCheckScheduledTask implements Runnable { - @Override - public void run() { - healthLogger.trace("Executing janusGraph Health Check Task - Start"); - boolean healthStatus = isGraphOpen(); - healthLogger.trace("Executing janusGraph Health Check Task - Status = {}", healthStatus); - if (healthStatus != lastHealthState) { - logger.trace("janusGraph Health State Changed to {}. Issuing alarm / recovery alarm...", healthStatus); - lastHealthState = healthStatus; - logAlarm(); - } - } - } - - private class ReconnectTask implements Runnable { - @Override - public void run() { - logger.trace("Trying to reconnect to JanusGraph..."); - if (graph == null) { - createGraph(janusGraphCfgFile); - } - } - } - - private JanusGraph graph; - - // Health Check Variables - + private static final String JANUSGRAPH_HEALTH_CHECK = "janusgraphHealthCheck"; + private static Logger logger = LoggerFactory.getLogger(JanusGraphClient.class.getName()); + private static Logger healthLogger = LoggerFactory.getLogger("janusgraph.healthcheck"); /** * This executor will execute the health check task on a callable task that can be executed with a timeout. */ - ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor( - runnable -> new Thread(runnable, "JanusGraph-Health-Check-Thread")); - private long healthCheckReadTimeout = 2; + ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "JanusGraph-Health-Check-Thread")); HealthCheckTask healthCallableTask = new HealthCheckTask(); HealthCheckScheduledTask healthCheckScheduledTask = new HealthCheckScheduledTask(); boolean lastHealthState = false; - + JanusGraphClientStrategy janusGraphClientStrategy; + // Health Check Variables + private JanusGraph graph; + private long healthCheckReadTimeout = 2; // Reconnection variables private ScheduledExecutorService reconnectScheduler = null; private ScheduledExecutorService healthCheckScheduler = null; @@ -133,31 +83,62 @@ public class JanusGraphClient { private long reconnectInterval = 3; @SuppressWarnings("rawtypes") private Future reconnectFuture; - private String janusGraphCfgFile = null; - JanusGraphClientStrategy janusGraphClientStrategy; - + public JanusGraphClient() { + } public JanusGraphClient(JanusGraphClientStrategy janusGraphClientStrategy) { super(); this.janusGraphClientStrategy = janusGraphClientStrategy; - // Initialize a single threaded scheduler for health-check - this.healthCheckScheduler = Executors.newSingleThreadScheduledExecutor( - runnable -> new Thread(runnable, "JanusGraph-Health-Check-Task")); + this.healthCheckScheduler = Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, "JanusGraph-Health-Check-Task")); + healthCheckReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getJanusGraphHealthCheckReadTimeout(2); + reconnectInterval = ConfigurationManager.getConfigurationManager().getConfiguration().getJanusGraphReconnectIntervalInSeconds(3); + logger.info("** JanusGraphClient created"); + } - healthCheckReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration() - .getJanusGraphHealthCheckReadTimeout(2); - reconnectInterval = ConfigurationManager.getConfigurationManager().getConfiguration() - .getJanusGraphReconnectIntervalInSeconds(3); + public static JanusGraphOperationStatus handleJanusGraphException(Exception e) { + if (e instanceof JanusGraphConfigurationException) { + return JanusGraphOperationStatus.JANUSGRAPH_CONFIGURATION; + } + if (e instanceof SchemaViolationException) { + return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION; + } + if (e instanceof PermanentLockingException) { + return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION; + } + if (e instanceof IDPoolExhaustedException) { + return JanusGraphOperationStatus.GENERAL_ERROR; + } + if (e instanceof InvalidElementException) { + return JanusGraphOperationStatus.INVALID_ELEMENT; + } + if (e instanceof InvalidIDException) { + return JanusGraphOperationStatus.INVALID_ID; + } + if (e instanceof QueryException) { + return JanusGraphOperationStatus.INVALID_QUERY; + } + if (e instanceof ResourceUnavailableException) { + return JanusGraphOperationStatus.RESOURCE_UNAVAILABLE; + } + if (e instanceof IllegalArgumentException) { + // TODO check the error message?? + return JanusGraphOperationStatus.ILLEGAL_ARGUMENT; + } + return JanusGraphOperationStatus.GENERAL_ERROR; + } - logger.info("** JanusGraphClient created"); + @PreDestroy + public void closeSession() { + if (graph.isOpen()) { + graph.close(); + logger.info("** JanusGraphClient session closed"); + } } @PostConstruct public JanusGraphOperationStatus createGraph() { - logger.info("** createGraph started **"); - if (ConfigurationManager.getConfigurationManager().getConfiguration().getJanusGraphInMemoryGraph()) { BaseConfiguration conf = new BaseConfiguration(); conf.setProperty("storage.backend", "inmemory"); @@ -170,16 +151,16 @@ public class JanusGraphClient { if (janusGraphCfgFile == null || janusGraphCfgFile.isEmpty()) { janusGraphCfgFile = "config/janusgraph.properties"; } - // yavivi + // In case connection failed on init time, schedule a reconnect task + // in the BG JanusGraphOperationStatus status = createGraph(janusGraphCfgFile); logger.debug("Create JanusGraph graph status {}", status); if (status != JanusGraphOperationStatus.OK) { this.startReconnectTask(); } - return status; } } @@ -194,12 +175,9 @@ public class JanusGraphClient { private void startReconnectTask() { this.reconnectTask = new ReconnectTask(); // Initialize a single threaded scheduler - this.reconnectScheduler = Executors.newSingleThreadScheduledExecutor( - runnable -> new Thread(runnable, "JanusGraph-Reconnect-Task")); - + this.reconnectScheduler = Executors.newSingleThreadScheduledExecutor(runnable -> new Thread(runnable, "JanusGraph-Reconnect-Task")); logger.info("Scheduling reconnect task {} with interval of {} seconds", reconnectTask, reconnectInterval); - reconnectFuture = this.reconnectScheduler - .scheduleAtFixedRate(this.reconnectTask, 0, this.reconnectInterval, TimeUnit.SECONDS); + reconnectFuture = this.reconnectScheduler.scheduleAtFixedRate(this.reconnectTask, 0, this.reconnectInterval, TimeUnit.SECONDS); } public void cleanupGraph() { @@ -218,7 +196,6 @@ public class JanusGraphClient { return graphMgmt.containsPropertyKey(HEALTH_CHECK) && graphMgmt.containsGraphIndex(HEALTH_CHECK); } - public JanusGraphOperationStatus createGraph(String janusGraphCfgFile) { logger.info("** open graph with {} started", janusGraphCfgFile); try { @@ -228,19 +205,15 @@ public class JanusGraphClient { logger.error("janusgraph graph was not initialized"); return JanusGraphOperationStatus.NOT_CREATED; } - } catch (Exception e) { this.graph = null; logger.info("createGraph : failed to open JanusGraph graph with configuration file: {}", janusGraphCfgFile); logger.debug("createGraph : failed with exception.", e); return JanusGraphOperationStatus.NOT_CONNECTED; } - logger.info("** JanusGraph graph created "); - // Do some post creation actions this.onGraphOpened(); - return JanusGraphOperationStatus.OK; } @@ -250,7 +223,6 @@ public class JanusGraphClient { logger.info("** Cancelling JanusGraph reconnect task"); reconnectFuture.cancel(true); } - // create health-check node if (!graph.query().has(HEALTH_CHECK, OK).vertices().iterator().hasNext()) { logger.trace("Healthcheck Singleton node does not exist, Creating healthcheck node..."); @@ -264,7 +236,6 @@ public class JanusGraphClient { this.startHealthCheckTask(); } - public Either<JanusGraph, JanusGraphOperationStatus> getGraph() { if (graph != null) { return Either.left(graph); @@ -299,39 +270,6 @@ public class JanusGraphClient { } } - public static JanusGraphOperationStatus handleJanusGraphException(Exception e) { - if (e instanceof JanusGraphConfigurationException) { - return JanusGraphOperationStatus.JANUSGRAPH_CONFIGURATION; - } - if (e instanceof SchemaViolationException) { - return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION; - } - if (e instanceof PermanentLockingException) { - return JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION; - } - if (e instanceof IDPoolExhaustedException) { - return JanusGraphOperationStatus.GENERAL_ERROR; - } - if (e instanceof InvalidElementException) { - return JanusGraphOperationStatus.INVALID_ELEMENT; - } - if (e instanceof InvalidIDException) { - return JanusGraphOperationStatus.INVALID_ID; - } - if (e instanceof QueryException) { - return JanusGraphOperationStatus.INVALID_QUERY; - } - if (e instanceof ResourceUnavailableException) { - return JanusGraphOperationStatus.RESOURCE_UNAVAILABLE; - } - if (e instanceof IllegalArgumentException) { - // TODO check the error message?? - return JanusGraphOperationStatus.ILLEGAL_ARGUMENT; - } - - return JanusGraphOperationStatus.GENERAL_ERROR; - } - public boolean getHealth() { return this.lastHealthState; } @@ -359,8 +297,6 @@ public class JanusGraphClient { } } - private static final String JANUSGRAPH_HEALTH_CHECK = "janusgraphHealthCheck"; - private void logAlarm() { if (lastHealthState) { BeEcompErrorManager.getInstance().logBeHealthCheckJanusGraphRecovery(JANUSGRAPH_HEALTH_CHECK); @@ -370,7 +306,6 @@ public class JanusGraphClient { } private void createJanusGraphSchema() { - JanusGraphManagement graphMgt = graph.openManagement(); JanusGraphIndex index = null; for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) { @@ -385,13 +320,11 @@ public class JanusGraphClient { } if (prop.isIndexed() && !graphMgt.containsGraphIndex(prop.getProperty())) { if (prop.isUnique()) { - index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique() - .buildCompositeIndex(); + index = graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).unique().buildCompositeIndex(); // Ensures only one name per vertex graphMgt.setConsistency(propKey, ConsistencyModifier.LOCK); // Ensures name uniqueness in the graph graphMgt.setConsistency(index, ConsistencyModifier.LOCK); - } else { graphMgt.buildIndex(prop.getProperty(), Vertex.class).addKey(propKey).buildCompositeIndex(); } @@ -400,4 +333,40 @@ public class JanusGraphClient { graphMgt.commit(); } + private class HealthCheckTask implements Callable<Vertex> { + + @Override + public Vertex call() { + JanusGraphVertex vertex = (JanusGraphVertex) graph.query().has(HEALTH_CHECK, OK).vertices().iterator().next(); + healthLogger.trace("Health Check Node Found...{}", vertex.property(HEALTH_CHECK)); + graph.tx().commit(); + return vertex; + } + } + + private class HealthCheckScheduledTask implements Runnable { + + @Override + public void run() { + healthLogger.trace("Executing janusGraph Health Check Task - Start"); + boolean healthStatus = isGraphOpen(); + healthLogger.trace("Executing janusGraph Health Check Task - Status = {}", healthStatus); + if (healthStatus != lastHealthState) { + logger.trace("janusGraph Health State Changed to {}. Issuing alarm / recovery alarm...", healthStatus); + lastHealthState = healthStatus; + logAlarm(); + } + } + } + + private class ReconnectTask implements Runnable { + + @Override + public void run() { + logger.trace("Trying to reconnect to JanusGraph..."); + if (graph == null) { + createGraph(janusGraphCfgFile); + } + } + } } |