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