Search in sources :

Example 1 with ReconnectionSchedule

use of com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule in project presto by prestodb.

the class NativeCassandraSession method executeWithSession.

@Override
public <T> T executeWithSession(SessionCallable<T> sessionCallable) {
    ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy();
    ReconnectionSchedule schedule = reconnectionPolicy.newSchedule();
    long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis();
    while (true) {
        try {
            return sessionCallable.executeWithSession(session.get());
        } catch (NoHostAvailableException e) {
            long timeLeft = deadline - System.currentTimeMillis();
            if (timeLeft <= 0) {
                throw e;
            } else {
                long delay = Math.min(schedule.nextDelayMs(), timeLeft);
                log.warn(e.getCustomMessage(10, true, true));
                log.warn("Reconnecting in %dms", delay);
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException interrupted) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException("interrupted", interrupted);
                }
            }
        }
    }
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) ReconnectionSchedule(com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule)

Aggregations

NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)1 ReconnectionSchedule (com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule)1