org.nohope.cassandra.factory.CassandraSessionCircuitBreaker Maven / Gradle / Ivy
The newest version!
package org.nohope.cassandra.factory;
import com.datastax.driver.core.*;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Counts number of successful and failed requests to cassandra.
* Later it will shutdown application in case of very high failure rate.
*/
public class CassandraSessionCircuitBreaker implements Session {
private static final Logger LOG = LoggerFactory.getLogger(CassandraSessionCircuitBreaker.class);
private final Session decorated;
private final AtomicInteger numSuccess = new AtomicInteger(0);
private final AtomicInteger numFailures = new AtomicInteger(0);
private final Thread circuitBreakerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
runCircuitBreaker();
} catch (final InterruptedException e) {
LOG.warn("circuit breaker interrupted");
} catch (final Exception e) {
LOG.error("Unexpected exception, circuit breaker won't print messages any more", e);
}
}
}, "cassandra-circuit-breaker");
private FutureCallback