aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/titan/TitanGraphClient.java
blob: 9d5ff9d226828755908c1ceb5ae0471973f00246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.titan;

import com.thinkaurelius.titan.core.*;
import com.thinkaurelius.titan.core.schema.ConsistencyModifier;
import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.thinkaurelius.titan.core.util.TitanCleanup;
import com.thinkaurelius.titan.diskstorage.ResourceUnavailableException;
import com.thinkaurelius.titan.diskstorage.locking.PermanentLockingException;
import com.thinkaurelius.titan.graphdb.database.idassigner.IDPoolExhaustedException;
import fj.data.Either;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.DAOTitanStrategy;
import org.openecomp.sdc.be.dao.TitanClientStrategy;
import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.*;


@Component("titan-client")
public class TitanGraphClient {
	private static Logger logger = LoggerFactory.getLogger(TitanGraphClient.class.getName());
	private static Logger healthLogger = LoggerFactory.getLogger("titan.healthcheck");

	private static final String HEALTH_CHECK = GraphPropertiesDictionary.HEALTH_CHECK.getProperty();
	private static final String OK = "GOOD";

	public TitanGraphClient() {
	}

	private class HealthCheckTask implements Callable<Vertex> {
		@Override
		public Vertex call() {

			TitanVertex v = (TitanVertex) graph.query().has(HEALTH_CHECK, OK).vertices().iterator().next();
			TitanVertexProperty<String> property = v.property("healthcheck", OK + "_" + System.currentTimeMillis());
			healthLogger.trace("Health Check Node Found...{}", v.property(HEALTH_CHECK));
			graph.tx().commit();

			return v;
		}
	}

	private class HealthCheckScheduledTask implements Runnable {
		@Override
		public void run() {
			healthLogger.trace("Executing TITAN Health Check Task - Start");
			boolean healthStatus = isGraphOpen();
			healthLogger.trace("Executing TITAN Health Check Task - Status = {}", healthStatus);
			if (healthStatus != lastHealthState) {
				logger.trace("TITAN  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 Titan...");
			if (graph == null) {
				createGraph(titanCfgFile);
			}
		}
	}

	private TitanGraph graph;

	// Health Check Variables

	/**
	 * This executor will execute the health check task on a callable task that can be executed with a timeout.
	 */
	ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
		@Override
		public Thread newThread(Runnable r) {
			return new Thread(r, "Titan-Health-Check-Thread");
		}
	});
	private long healthCheckReadTimeout = 2;
	HealthCheckTask healthCallableTask = new HealthCheckTask();
	HealthCheckScheduledTask healthCheckScheduledTask = new HealthCheckScheduledTask();
	boolean lastHealthState = false;

	// Reconnection variables
	private ScheduledExecutorService reconnectScheduler = null;
	private ScheduledExecutorService healthCheckScheduler = null;
	private Runnable reconnectTask = null;
	private long reconnectInterval = 3;
	@SuppressWarnings("rawtypes")
	private Future reconnectFuture;

	private String titanCfgFile = null;
	TitanClientStrategy titanClientStrategy;

	public TitanGraphClient(TitanClientStrategy titanClientStrategy) {
		super();
		this.titanClientStrategy = titanClientStrategy;

		// Initialize a single threaded scheduler for health-check
		this.healthCheckScheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				return new Thread(r, "Titan-Health-Check-Task");
			}
		});

		healthCheckReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanHealthCheckReadTimeout(2);
		reconnectInterval = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanReconnectIntervalInSeconds(3);

		logger.info("** TitanGraphClient created");
	}

	@PostConstruct
	public TitanOperationStatus createGraph() {

		logger.info("** createGraph started **");

		if (ConfigurationManager.getConfigurationManager().getConfiguration().getTitanInMemoryGraph()) {
			BaseConfiguration conf = new BaseConfiguration();
			conf.setProperty("storage.backend", "inmemory");
			graph = TitanFactory.open(conf);
            createTitanSchema(); 
			logger.info("** in memory graph created");
			return TitanOperationStatus.OK;
		} else {
			this.titanCfgFile = titanClientStrategy.getConfigFile();
			if (titanCfgFile == null || titanCfgFile.isEmpty()) {
				titanCfgFile = "config/titan.properties";
			}

			// yavivi
			// In case connection failed on init time, schedule a reconnect task
			// in the BG
			TitanOperationStatus status = createGraph(titanCfgFile);
			logger.debug("Create Titan graph status {}", status);
			if (status != TitanOperationStatus.OK) {
				this.startReconnectTask();
			}

			return status;
		}
	}

	private void startHealthCheckTask() {
		this.healthCheckScheduler.scheduleAtFixedRate(healthCheckScheduledTask, 0, reconnectInterval, TimeUnit.SECONDS);
	}

	/**
	 * This method will be invoked ONLY on init time in case Titan storage is down.
	 */
	private void startReconnectTask() {
		this.reconnectTask = new ReconnectTask();
		// Initialize a single threaded scheduler
		this.reconnectScheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				return new Thread(r, "Titan-Reconnect-Task");
			}
		});

		logger.info("Scheduling reconnect task {} with interval of {} seconds", reconnectTask, reconnectInterval);
		reconnectFuture = this.reconnectScheduler.scheduleAtFixedRate(this.reconnectTask, 0, this.reconnectInterval, TimeUnit.SECONDS);
	}

	public void cleanupGraph() {
		if (graph != null) {
			// graph.shutdown();
			graph.close();
			TitanCleanup.clear(graph);
		}
	}

	private boolean graphInitialized(){
		TitanManagement graphMgmt = graph.openManagement();
		return graphMgmt.containsPropertyKey(HEALTH_CHECK) && graphMgmt.containsGraphIndex(HEALTH_CHECK);
	}
	

	public TitanOperationStatus createGraph(String titanCfgFile) {
		logger.info("** open graph with {} started", titanCfgFile);
		try {
			logger.info("openGraph : try to load file {}", titanCfgFile);
			graph = TitanFactory.open(titanCfgFile);
			if (graph.isClosed() || !graphInitialized()) {
				logger.error("titan graph was not initialized");
				return TitanOperationStatus.NOT_CREATED;
			}

		} catch (Exception e) {
			this.graph = null;
			logger.info("createGraph : failed to open Titan graph with configuration file: {}", titanCfgFile);
			logger.debug("createGraph : failed with exception.", e);
			return TitanOperationStatus.NOT_CONNECTED;
		}

		logger.info("** Titan graph created ");

		// Do some post creation actions
		this.onGraphOpened();

		return TitanOperationStatus.OK;
	}

	private void onGraphOpened() {
		// if a reconnect task is running, cancel it.
		if (this.reconnectFuture != null) {
			logger.info("** Cancelling Titan 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...");
			Vertex healthCheckNode = graph.addVertex();
			healthCheckNode.property(HEALTH_CHECK, OK);
			logger.trace("Healthcheck node created successfully. ID={}", healthCheckNode.property(T.id.getAccessor()));
			graph.tx().commit();
		} else {
			logger.trace("Skipping Healthcheck Singleton node creation. Already exist...");
		}
		this.startHealthCheckTask();
	}


	public Either<TitanGraph, TitanOperationStatus> getGraph() {
		if (graph != null) {
			return Either.left(graph);
		} else {
			return Either.right(TitanOperationStatus.NOT_CREATED);
		}
	}

	public TitanOperationStatus commit() {
		if (graph != null) {
			try {
				graph.tx().commit();
				return TitanOperationStatus.OK;
			} catch (Exception e) {
				return handleTitanException(e);
			}
		} else {
			return TitanOperationStatus.NOT_CREATED;
		}
	}

	public TitanOperationStatus rollback() {
		if (graph != null) {
			try {
				// graph.rollback();
				graph.tx().rollback();
				return TitanOperationStatus.OK;
			} catch (Exception e) {
				return handleTitanException(e);
			}
		} else {
			return TitanOperationStatus.NOT_CREATED;
		}
	}

	public static TitanOperationStatus handleTitanException(Exception e) {
		if (e instanceof TitanConfigurationException) {
			return TitanOperationStatus.TITAN_CONFIGURATION;
		}
		if (e instanceof SchemaViolationException) {
			return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
		}
		if (e instanceof PermanentLockingException) {
			return TitanOperationStatus.TITAN_SCHEMA_VIOLATION;
		}
		if (e instanceof IDPoolExhaustedException) {
			return TitanOperationStatus.GENERAL_ERROR;
		}
		if (e instanceof InvalidElementException) {
			return TitanOperationStatus.INVALID_ELEMENT;
		}
		if (e instanceof InvalidIDException) {
			return TitanOperationStatus.INVALID_ID;
		}
		if (e instanceof QueryException) {
			return TitanOperationStatus.INVALID_QUERY;
		}
		if (e instanceof ResourceUnavailableException) {
			return TitanOperationStatus.RESOURCE_UNAVAILABLE;
		}
		if (e instanceof IllegalArgumentException) {
			// TODO check the error message??
			return TitanOperationStatus.ILLEGAL_ARGUMENT;
		}

		return TitanOperationStatus.GENERAL_ERROR;
	}

	public boolean getHealth() {
		return this.lastHealthState;
	}

	private boolean isGraphOpen() {
		healthLogger.trace("Invoking Titan health check ...");
		Vertex v = null;
		if (graph != null) {
			try {
				Future<Vertex> future = healthCheckExecutor.submit(healthCallableTask);
				v = future.get(this.healthCheckReadTimeout, TimeUnit.SECONDS);
				healthLogger.trace("Health Check Node Found... {}", v.property(HEALTH_CHECK));
				graph.tx().commit();
			} catch (Exception e) {
				String message = e.getMessage();
				if (message == null) {
					message = e.getClass().getName();
				}
				logger.error("Titan Health Check Failed. {}", message);
				return false;
			}
			return true;
		} else {
			return false;
		}
	}


	public static void main(String[] args) throws InterruptedException {
		TitanGraphClient client = new TitanGraphClient(new DAOTitanStrategy());
		client.createGraph();

		while (true) {
			boolean health = client.isGraphOpen();
			System.err.println("health=" + health);
			Thread.sleep(2000);
		}

	}


	private static final String TITAN_HEALTH_CHECK_STR = "titanHealthCheck";

	private void logAlarm() {
		if (lastHealthState) {
			BeEcompErrorManager.getInstance().logBeHealthCheckTitanRecovery(TITAN_HEALTH_CHECK_STR);
		} else {
			BeEcompErrorManager.getInstance().logBeHealthCheckTitanError(TITAN_HEALTH_CHECK_STR);
		}
	}
	
	private void createTitanSchema() {
		
		TitanManagement graphMgt = graph.openManagement();
		TitanGraphIndex index = null;
		for (GraphPropertiesDictionary prop : GraphPropertiesDictionary.values()) {
			PropertyKey propKey = null;
			if (!graphMgt.containsPropertyKey(prop.getProperty())) {
				Class<?> clazz = prop.getClazz();
				if (!clazz.isAssignableFrom(ArrayList.class) && !clazz.isAssignableFrom(HashMap.class)) {
					propKey = graphMgt.makePropertyKey(prop.getProperty()).dataType(prop.getClazz()).make();
				}
			} else {
				propKey = graphMgt.getPropertyKey(prop.getProperty());
			}
			if (prop.isIndexed()) {
				if (!graphMgt.containsGraphIndex(prop.getProperty())) {
					if (prop.isUnique()) {
						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();
					}
				}
			}
		}
		graphMgt.commit();
	}

}