Search in sources :

Example 1 with TransactionFailedException

use of com.palantir.atlasdb.transaction.api.TransactionFailedException in project atlasdb by palantir.

the class AbstractConditionAwareTransactionManager method runTaskWithConditionWithRetry.

@Override
public <T, C extends PreCommitCondition, E extends Exception> T runTaskWithConditionWithRetry(Supplier<C> conditionSupplier, ConditionAwareTransactionTask<T, C, E> task) throws E {
    int failureCount = 0;
    UUID runId = UUID.randomUUID();
    while (true) {
        checkOpen();
        try {
            C condition = conditionSupplier.get();
            T result = runTaskWithConditionThrowOnConflict(condition, task);
            if (failureCount > 0) {
                log.info("[{}] Successfully completed transaction after {} retries.", SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount));
            }
            return result;
        } catch (TransactionFailedException e) {
            if (!e.canTransactionBeRetried()) {
                log.warn("[{}] Non-retriable exception while processing transaction.", SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount));
                throw e;
            }
            failureCount++;
            if (shouldStopRetrying(failureCount)) {
                log.warn("[{}] Failing after {} tries.", SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount), e);
                throw Throwables.rewrap(String.format("Failing after %d tries.", failureCount), e);
            }
            log.info("[{}] Retrying transaction after {} failure(s).", SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount), e);
        } catch (NotInitializedException e) {
            log.info("TransactionManager is not initialized. Aborting transaction with runTaskWithRetry", e);
            throw e;
        } catch (RuntimeException e) {
            log.warn("[{}] RuntimeException while processing transaction.", SafeArg.of("runId", runId), e);
            throw e;
        }
        sleepForBackoff(failureCount);
    }
}
Also used : NotInitializedException(com.palantir.exception.NotInitializedException) UUID(java.util.UUID) TransactionFailedException(com.palantir.atlasdb.transaction.api.TransactionFailedException)

Aggregations

TransactionFailedException (com.palantir.atlasdb.transaction.api.TransactionFailedException)1 NotInitializedException (com.palantir.exception.NotInitializedException)1 UUID (java.util.UUID)1