2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champcore.graph.impl;
24 import java.util.UUID;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.onap.aai.champcore.ChampTransaction;
28 import org.onap.aai.champcore.exceptions.ChampTransactionException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class TinkerpopTransaction extends ChampTransaction {
34 private static final int COMMIT_RETRY_COUNT = 3;
36 /** Threaded Tinkerpop transaction. */
37 protected Graph threadedTransaction;
40 private static final Logger LOGGER = LoggerFactory.getLogger(TinkerpopTransaction.class);
42 protected TinkerpopTransaction() { }
46 * Creates a new transaction instance.
48 * @param aGraphInstance - Instance of the graph to request the transaction from.
50 public TinkerpopTransaction(Graph aGraphInstance) {
53 if(!aGraphInstance.features().graph().supportsTransactions()) {
54 throw new UnsupportedOperationException();
57 // Request a threaded transaction object from the graph.
58 this.threadedTransaction = aGraphInstance.tx().createThreadedTx();
60 LOGGER.info("Open transaction - id: " + id);
68 public Graph getGraphInstance() {
69 return threadedTransaction;
73 public void commit() throws ChampTransactionException {
75 LOGGER.debug("Commiting transaction - " + id);
77 final long initialBackoff = (int) (Math.random() * 50);
79 // If something goes wrong, we will retry a couple of times before
81 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
86 threadedTransaction.tx().commit();
87 LOGGER.info("Committed transaction - id: " + id);
90 } catch (Throwable e) {
92 LOGGER.debug("Transaction " + id + " failed to commit due to: " + e.getMessage());
94 // Have we used up all of our retries?
95 if (i == COMMIT_RETRY_COUNT - 1) {
97 LOGGER.error("Maxed out commit attempt retries, client must handle exception and retry", e);
98 threadedTransaction.tx().rollback();
99 throw new ChampTransactionException(e);
102 // Calculate how long we will wait before retrying...
103 final long backoff = (long) Math.pow(2, i) * initialBackoff;
104 LOGGER.warn("Caught exception while retrying transaction commit, retrying in " + backoff + " ms");
106 // ...and sleep before trying the commit again.
108 Thread.sleep(backoff);
110 } catch (InterruptedException ie) {
112 LOGGER.info("Interrupted while backing off on transaction commit");
113 Thread.interrupted();
121 public void rollback() throws ChampTransactionException {
123 long initialBackoff = (int) (Math.random() * 50);
126 // If something goes wrong, we will retry a couple of times before
128 for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
132 threadedTransaction.tx().rollback();
133 LOGGER.info("Rolled back transaction - id: " + id);
136 } catch (Throwable e) {
138 LOGGER.debug("Transaction " + id + " failed to roll back due to: " + e.getMessage());
140 // Have we used up all of our retries?
141 if (i == COMMIT_RETRY_COUNT - 1) {
143 LOGGER.error("Maxed out rollback attempt retries, client must handle exception and retry", e);
144 throw new ChampTransactionException(e);
147 // Calculate how long we will wait before retrying...
148 final long backoff = (long) Math.pow(2, i) * initialBackoff;
149 LOGGER.warn("Caught exception while retrying transaction roll back, retrying in " + backoff + " ms");
151 // ...and sleep before trying the commit again.
153 Thread.sleep(backoff);
155 } catch (InterruptedException ie) {
157 LOGGER.info("Interrupted while backing off on transaction rollback");
158 Thread.interrupted();