2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
21 package org.onap.aai.champcore.graph.impl;
23 import java.security.SecureRandom;
24 import org.apache.tinkerpop.gremlin.structure.Graph;
25 import org.onap.aai.champcore.ChampTransaction;
26 import org.onap.aai.champcore.exceptions.ChampTransactionException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class TinkerpopTransaction extends ChampTransaction {
32 private static final int COMMIT_RETRY_COUNT = 3;
34 /** Threaded Tinkerpop transaction. */
35 protected Graph threadedTransaction;
38 private static final Logger LOGGER = LoggerFactory.getLogger(TinkerpopTransaction.class);
40 protected TinkerpopTransaction() { }
44 * Creates a new transaction instance.
46 * @param aGraphInstance - Instance of the graph to request the transaction from.
48 public TinkerpopTransaction(Graph aGraphInstance) {
51 if(!aGraphInstance.features().graph().supportsTransactions()) {
52 throw new UnsupportedOperationException();
55 // Request a threaded transaction object from the graph.
56 this.threadedTransaction = aGraphInstance.tx().createThreadedTx();
58 LOGGER.info("Open transaction - id: " + id);
66 public Graph getGraphInstance() {
67 return threadedTransaction;
71 public void commit() throws ChampTransactionException {
73 LOGGER.debug("Commiting transaction - " + id);
75 final long initialBackoff = (int)(new SecureRandom().nextDouble() * 50);
77 // If something goes wrong, we will retry a couple of times before
79 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
84 threadedTransaction.tx().commit();
85 LOGGER.info("Committed transaction - id: " + id);
88 } catch (Throwable e) {
90 LOGGER.debug("Transaction " + id + " failed to commit due to: " + e.getMessage());
92 // Have we used up all of our retries?
93 if (i == COMMIT_RETRY_COUNT - 1) {
95 LOGGER.error("Maxed out commit attempt retries, client must handle exception and retry", e);
96 threadedTransaction.tx().rollback();
97 throw new ChampTransactionException(e);
100 // Calculate how long we will wait before retrying...
101 final long backoff = (long) Math.pow(2, i) * initialBackoff;
102 LOGGER.warn("Caught exception while retrying transaction commit, retrying in " + backoff + " ms");
104 // ...and sleep before trying the commit again.
106 Thread.sleep(backoff);
108 } catch (InterruptedException ie) {
110 LOGGER.info("Interrupted while backing off on transaction commit");
111 Thread.interrupted();
119 public void rollback() throws ChampTransactionException {
121 long initialBackoff = (int)(new SecureRandom().nextDouble() * 50);
124 // If something goes wrong, we will retry a couple of times before
126 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
130 threadedTransaction.tx().rollback();
131 LOGGER.info("Rolled back transaction - id: " + id);
134 } catch (Throwable e) {
136 LOGGER.debug("Transaction " + id + " failed to roll back due to: " + e.getMessage());
138 // Have we used up all of our retries?
139 if (i == COMMIT_RETRY_COUNT - 1) {
141 LOGGER.error("Maxed out rollback attempt retries, client must handle exception and retry", e);
142 throw new ChampTransactionException(e);
145 // Calculate how long we will wait before retrying...
146 final long backoff = (long) Math.pow(2, i) * initialBackoff;
147 LOGGER.warn("Caught exception while retrying transaction roll back, retrying in " + backoff + " ms");
149 // ...and sleep before trying the commit again.
151 Thread.sleep(backoff);
153 } catch (InterruptedException ie) {
155 LOGGER.info("Interrupted while backing off on transaction rollback");
156 Thread.interrupted();