f78abea1fa84e39493c38c3b53e675f1c84dd7e5
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / graph / impl / TinkerpopTransaction.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.aai.champcore.graph.impl;
22
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;
29
30 public class TinkerpopTransaction extends ChampTransaction {
31
32   private static final int COMMIT_RETRY_COUNT = 3;
33
34   /** Threaded Tinkerpop transaction. */
35   protected Graph threadedTransaction;
36
37
38   private static final Logger LOGGER = LoggerFactory.getLogger(TinkerpopTransaction.class);
39
40   protected TinkerpopTransaction() { }
41
42
43   /**
44    * Creates a new transaction instance.
45    *
46    * @param aGraphInstance - Instance of the graph to request the transaction from.
47    */
48   public TinkerpopTransaction(Graph aGraphInstance) {
49     super();
50
51     if(!aGraphInstance.features().graph().supportsTransactions()) {
52       throw new UnsupportedOperationException();
53     }
54
55     // Request a threaded transaction object from the graph.
56     this.threadedTransaction = aGraphInstance.tx().createThreadedTx();
57
58     LOGGER.info("Open transaction - id: " + id);
59   }
60
61   @Override
62   public String id() {
63     return id.toString();
64   }
65
66   public Graph getGraphInstance() {
67     return threadedTransaction;
68   }
69
70   @Override
71   public void commit() throws ChampTransactionException {
72
73     LOGGER.debug("Commiting transaction - " + id);
74
75     final long initialBackoff = (int)(new SecureRandom().nextDouble() * 50);
76
77     // If something goes wrong, we will retry a couple of times before
78     // giving up.
79     for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
80
81       try {
82
83         // Do the commit.
84         threadedTransaction.tx().commit();
85         LOGGER.info("Committed transaction - id: " + id);
86         return;
87
88       } catch (Throwable e) {
89
90         LOGGER.debug("Transaction " + id + " failed to commit due to: " + e.getMessage());
91
92         // Have we used up all of our retries?
93         if (i == COMMIT_RETRY_COUNT - 1) {
94
95           LOGGER.error("Maxed out commit attempt retries, client must handle exception and retry", e);
96           threadedTransaction.tx().rollback();
97           throw new ChampTransactionException(e);
98         }
99
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");
103
104         // ...and sleep before trying the commit again.
105         try {
106           Thread.sleep(backoff);
107
108         } catch (InterruptedException ie) {
109
110           LOGGER.info("Interrupted while backing off on transaction commit");
111           Thread.interrupted();
112           return;
113         }
114       }
115     }
116   }
117
118   @Override
119   public void rollback() throws ChampTransactionException {
120
121     long initialBackoff = (int)(new SecureRandom().nextDouble() * 50);
122
123
124     // If something goes wrong, we will retry a couple of times before
125     // giving up.
126     for (int i = 0; i < COMMIT_RETRY_COUNT; i++) {
127
128       try {
129
130         threadedTransaction.tx().rollback();
131         LOGGER.info("Rolled back transaction - id: " + id);
132         return;
133
134       } catch (Throwable e) {
135
136         LOGGER.debug("Transaction " + id + " failed to roll back due to: " + e.getMessage());
137
138         // Have we used up all of our retries?
139         if (i == COMMIT_RETRY_COUNT - 1) {
140
141           LOGGER.error("Maxed out rollback attempt retries, client must handle exception and retry", e);
142           throw new ChampTransactionException(e);
143         }
144
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");
148
149         // ...and sleep before trying the commit again.
150         try {
151           Thread.sleep(backoff);
152
153         } catch (InterruptedException ie) {
154
155           LOGGER.info("Interrupted while backing off on transaction rollback");
156           Thread.interrupted();
157           return;
158         }
159       }
160     }
161   }
162 }