Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -872,12 +873,16 @@ protected String getHostEndpoint(final String nodeName) {
private static class NodeMonitoringWorker implements Runnable {

private static final Logger LOGGER = Logger.getLogger(NodeMonitoringWorker.class.getName());
private static final long INITIAL_BACKOFF_MS = 100;
private static final long MAX_BACKOFF_MS = 5000;
private static final Random random = new Random();

protected final FullServicesContainer servicesContainer;
protected final ClusterTopologyMonitorImpl monitor;
protected final HostSpec hostSpec;
protected final @Nullable HostSpec writerHostSpec;
protected boolean writerChanged = false;
protected int connectionAttempts = 0;

public NodeMonitoringWorker(
final FullServicesContainer servicesContainer,
Expand Down Expand Up @@ -905,9 +910,10 @@ public void run() {
try {
connection = this.servicesContainer.getPluginService().forceConnect(
hostSpec, this.monitor.monitoringProperties);
this.connectionAttempts = 0;
} catch (SQLException ex) {
// A problem occurred while connecting. We will try again on the next iteration.
TimeUnit.MILLISECONDS.sleep(100);
TimeUnit.MILLISECONDS.sleep(calculateBackoffWithJitter(this.connectionAttempts++));
continue;
}
}
Expand Down Expand Up @@ -1037,5 +1043,11 @@ private void readerThreadFetchTopology(final Connection connection, final @Nulla
LOGGER.fine(Utils.logTopology(hosts));
}
}

private static long calculateBackoffWithJitter(int attempt) {
long backoff = INITIAL_BACKOFF_MS * Math.round(Math.pow(2, Math.min(attempt, 6)));
backoff = Math.min(backoff, MAX_BACKOFF_MS);
return Math.round(backoff * (0.5 + random.nextDouble() * 0.5));
}
}
}