Last active
May 17, 2019 17:39
-
-
Save jsanda/eed761963eae9976a61b64467a61f55a to your computer and use it in GitHub Desktop.
java driver (v3.x) throttling for async execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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