Skip to content

Instantly share code, notes, and snippets.

@jsanda
Last active May 17, 2019 17:39
Show Gist options
  • Save jsanda/eed761963eae9976a61b64467a61f55a to your computer and use it in GitHub Desktop.
Save jsanda/eed761963eae9976a61b64467a61f55a to your computer and use it in GitHub Desktop.
java driver (v3.x) throttling for async execution
private boolean availableInFlightSlots(Statement st) {
boolean available = false;
Iterator<Host> hostIterator = loadBalancingPolicy.newQueryPlan(session.getLoggedKeyspace(), st);
hostIter: while(hostIterator.hasNext()) {
Host host = hostIterator.next();
int inFlightQueries = session.getState().getInFlightQueries(host);
switch(loadBalancingPolicy.distance(host)) {
case LOCAL:
if (inFlightQueries < maxInFlightLocal) {
available = true;
break hostIter;
}
break;
case REMOTE:
if (inFlightQueries < maxInFlightRemote) {
available = true;
break hostIter;
}
break;
default:
// IGNORED is something we're not going to write to
break;
}
}
return available;
}
private ResultSetFuture scheduleStatement(Statement st, Scheduler scheduler) {
while(true) {
if(availableInFlightSlots(st)) {
return session.executeAsync(st);
} else {
try {
Thread.sleep(0, 1);
} catch (InterruptedException e) {
//
}
}
}
public ResultSetFuture execute(Statement query) {
return scheduleStatement(query));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment