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.util.UUID;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.onap.aai.champcore.ChampTransaction;
27 import org.onap.aai.champcore.exceptions.ChampTransactionException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class TinkerpopTransaction extends ChampTransaction {
33 private static final int COMMIT_RETRY_COUNT = 3;
35 /** Threaded Tinkerpop transaction. */
36 protected Graph threadedTransaction;
39 private static final Logger LOGGER = LoggerFactory.getLogger(TinkerpopTransaction.class);
41 protected TinkerpopTransaction() { }
45 * Creates a new transaction instance.
47 * @param aGraphInstance - Instance of the graph to request the transaction from.
49 public TinkerpopTransaction(Graph aGraphInstance) {
52 if(!aGraphInstance.features().graph().supportsTransactions()) {
53 throw new UnsupportedOperationException();
56 // Request a threaded transaction object from the graph.
57 this.threadedTransaction = aGraphInstance.tx().createThreadedTx();
59 LOGGER.info("Open transaction - id: " + id);
67 public Graph getGraphInstance() {
68 return threadedTransaction;
72 public void commit() throws ChampTransactionException {
74 LOGGER.debug("Commiting transaction - " + id);
76 final long initialBackoff = (int) (Math.random() * 50);
78 // If something goes wrong, we will retry a couple of times before
80 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
85 threadedTransaction.tx().commit();
86 LOGGER.info("Committed transaction - id: " + id);
89 } catch (Throwable e) {
91 LOGGER.debug("Transaction " + id + " failed to commit due to: " + e.getMessage());
93 // Have we used up all of our retries?
94 if (i == COMMIT_RETRY_COUNT - 1) {
96 LOGGER.error("Maxed out commit attempt retries, client must handle exception and retry", e);
97 threadedTransaction.tx().rollback();
98 throw new ChampTransactionException(e);
101 // Calculate how long we will wait before retrying...
102 final long backoff = (long) Math.pow(2, i) * initialBackoff;
103 LOGGER.warn("Caught exception while retrying transaction commit, retrying in " + backoff + " ms");
105 // ...and sleep before trying the commit again.
107 Thread.sleep(backoff);
109 } catch (InterruptedException ie) {
111 LOGGER.info("Interrupted while backing off on transaction commit");
112 Thread.interrupted();
120 public void rollback() throws ChampTransactionException {
122 long initialBackoff = (int) (Math.random() * 50);
125 // If something goes wrong, we will retry a couple of times before
127 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
131 threadedTransaction.tx().rollback();
132 LOGGER.info("Rolled back transaction - id: " + id);
135 } catch (Throwable e) {
137 LOGGER.debug("Transaction " + id + " failed to roll back due to: " + e.getMessage());
139 // Have we used up all of our retries?
140 if (i == COMMIT_RETRY_COUNT - 1) {
142 LOGGER.error("Maxed out rollback attempt retries, client must handle exception and retry", e);
143 throw new ChampTransactionException(e);
146 // Calculate how long we will wait before retrying...
147 final long backoff = (long) Math.pow(2, i) * initialBackoff;
148 LOGGER.warn("Caught exception while retrying transaction roll back, retrying in " + backoff + " ms");
150 // ...and sleep before trying the commit again.
152 Thread.sleep(backoff);
154 } catch (InterruptedException ie) {
156 LOGGER.info("Interrupted while backing off on transaction rollback");
157 Thread.interrupted();