/**
 * Copyright 2016 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * 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 com.palantir.atlasdb.timelock.atomix;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.UncheckedExecutionException;

import io.atomix.copycat.session.ClosedSessionException;

public final class AtomixRetryer {
    public static final int RETRY_ATTEMPTS = 3;

    private static final Retryer<Object> RETRYER = RetryerBuilder.newBuilder()
            .retryIfException(e -> e instanceof UncheckedExecutionException
                    && e.getCause() instanceof ClosedSessionException)
            .withStopStrategy(StopStrategies.stopAfterAttempt(RETRY_ATTEMPTS))
            .build();

    private AtomixRetryer() {
        // utility
    }

    @SuppressWarnings("unchecked") // We only want to create 1 retryer; this is safe given the type of supplier.
    public static <T> T getWithRetry(Supplier<CompletableFuture<T>> supplier) {
        try {
            return (T) RETRYER.call(() -> Futures.getUnchecked(supplier.get()));
        } catch (RetryException e) {
            throw Throwables.propagate(e);
        } catch (ExecutionException e) {
            throw Throwables.propagate(e.getCause());
        }
    }
}

@@ -772,47 +772,193 @@
 ort 
-com.github.rholder.retry.RetryException
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.github.rholder.retry.Attempt;%0Aimport com.github.rholder.retry.RetryException;%0Aimport com.github.rholder.retry.RetryListener
 ;%0Aim
@@ -1426,207 +1426,834 @@
 nal 
-Retryer%3CObject%3E RETRYER = RetryerBuilder.newBuilder()%0A            .retryIfException(e -%3E e instanceof UncheckedExecutionException%0A                    && e.getCause() instanceof ClosedSessionException
+Logger log = LoggerFactory.getLogger(AtomixRetryer.class);%0A%0A    private static final Retryer%3CObject%3E RETRYER = RetryerBuilder.newBuilder()%0A            .retryIfException(AtomixRetryer::canBeRetried)%0A            .withRetryListener(new RetryListener() %7B%0A                @Override%0A                public %3CV%3E void onRetry(Attempt%3CV%3E attempt) %7B%0A                    if (attempt.hasException() && canBeRetried(attempt.getExceptionCause())) %7B%0A                        log.warn(%22Encountered a retriable exception %5B%7B%7D%5D in an Atomix operation (attempt %7B%7D/%7B%7D). %22%0A                                        + %22Retrying%22,%0A                                attempt.getExceptionCause(),%0A                                attempt.getAttemptNumber(),%0A                                RETRY_ATTEMPTS);%0A                    %7D%0A                %7D%0A            %7D
 )%0A  
@@ -2886,11 +2886,220 @@
  %7D%0A    %7D
+%0A%0A    private static boolean canBeRetried(Throwable throwable) %7B%0A        return throwable instanceof UncheckedExecutionException%0A                && throwable.getCause() instanceof ClosedSessionException;%0A    %7D
 %0A%7D%0A

Add logging to AtomixRetryer.