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