/*
 * Copyright (C) 2015 hops.io.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.hops.transaction.handler;

import io.hops.exception.TransientStorageException;
import io.hops.log.NDCWrapper;

import java.io.IOException;

public abstract class LightWeightRequestHandler extends RequestHandler {

  public LightWeightRequestHandler(OperationType opType) {
    super(opType);
  }

  @Override
  protected Object execute(Object info) throws IOException {
    int tryCount = 0;
    long totalTime = 0;

    while (tryCount <= RETRY_COUNT) {
      boolean rollback = false;
      exponentialBackoff();
      tryCount++;
      try {
        NDCWrapper.push(opType
            .toString()); // Defines a context for every operation to track them in the logs easily.

        //In a tx if the lock level is set to write, does
        //it mean that all the operations after seting the lock will use write lcok?
        //Light weight tx handler is sometimes called inside a bigger tx.
        //If in the outer tx lock level was set to some thing other than read-commited
        //then we will end up taking un necessary locks.
        //To make sure that we done have this problem I explicitly set the locks to read-commited.
        connector.readCommitted();
        totalTime = System.currentTimeMillis();
        Object ret = performTask();
        totalTime = System.currentTimeMillis() - totalTime;
        if(LOG.isInfoEnabled()) {
          LOG.info(opType+" TX Finished. Total time taken. Time " + totalTime + " ms");
        }
        return ret;
      } catch (TransientStorageException e) {
        rollback = true;
        if (tryCount <= RETRY_COUNT) {
          totalTime = System.currentTimeMillis() - totalTime;
          if(LOG.isDebugEnabled()) {
            LOG.error("Tx Failed. total tx time " + " TotalRetryCount(" + RETRY_COUNT +
                            ") RemainingRetries(" + (RETRY_COUNT - tryCount) +
                            ") TX Stats: ms, Total Time: " + totalTime + "ms", e);
          }
        } else {
          if(LOG.isDebugEnabled()) {
            LOG.debug("Transaction failed after " + RETRY_COUNT + " retries.", e);
          }
          throw e;
        }
      } catch (IOException e) {
        rollback = true;
        if(LOG.isDebugEnabled()) {
          LOG.debug("Transaction failed.", e);
        }
        throw e;
      } catch (RuntimeException e) {
        rollback = true;
        if(LOG.isDebugEnabled()) {
          LOG.debug("Transaction failed.", e);
        }
        throw e;
      } catch (Error e) {
        rollback = true;
        if(LOG.isDebugEnabled()) {
          LOG.debug("Transaction failed.", e);
        }
        throw e;
      } finally {
        if (rollback && connector.isTransactionActive()) {
          if(LOG.isDebugEnabled()) {
            LOG.debug("Transaction rollback. retries:" + RETRY_COUNT);
          }
          connector.rollback();
        }
        NDCWrapper.pop();
        NDCWrapper.remove();
        if (rollback) {
          try {
            LOG.error("Rollback the TX");
            connector.rollback();
          } catch (Exception e) {
            LOG.error("Could not rollback transaction", e);
          }
        }
      }
    }

    throw new RuntimeException("LightWeightRequestHandler did not execute");
  }
}

@@ -1939,12 +1939,13 @@
 G.is
-Info
+Debug
 Enab
@@ -1967,20 +1967,21 @@
     LOG.
-info
+debug
 (opType+
@@ -2017,24 +2017,38 @@
 en. Time %22 +
+%0A             
  totalTime +
@@ -2311,21 +2311,21 @@
     LOG.
-error
+debug
 (%22Tx Fai
@@ -2372,30 +2372,9 @@
 (%22 +
- RETRY_COUNT +%0A       
+%0A
     
@@ -2381,28 +2381,37 @@
             
-    
+RETRY_COUNT +
  %22) Remainin
@@ -2450,28 +2450,16 @@
 ount) +%0A
-            
         
@@ -2564,56 +2564,17 @@
     
-if(
 LOG.
-isDebugEnabled()) %7B%0A            LOG.debug
+error
 (%22Tr
@@ -2627,36 +2627,24 @@
 ries.%22, e);%0A
-          %7D%0A
           th
@@ -2719,32 +2719,33 @@
 true;%0A        if
+ 
 (LOG.isDebugEnab
@@ -2891,32 +2891,33 @@
 true;%0A        if
+ 
 (LOG.isDebugEnab
@@ -3052,32 +3052,33 @@
 true;%0A        if
+ 
 (LOG.isDebugEnab
@@ -3506,25 +3506,67 @@
         
-LOG.error
+if (LOG.isDebugEnabled()) %7B%0A              LOG.debug
 (%22Rollba
@@ -3574,24 +3574,38 @@
 k the TX%22);%0A
+            %7D%0A
             

Fix logging levels