Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

java - Cassandra - Is there a way to limit number of async queries?

I would like to know if there is way to limit the number of queries executed simultaneously by the cassandra java driver ?

Currently, I execute a lot of queries as follows :

... 
PreparedStatement stmt = session.prepare("SELECT * FROM users WHERE id = ?");
BoundStatement boundStatement = new BoundStatement(stmt);
List<ResultSetFuture> futures = Lists.newArrayListWithExpectedSize(list.length);

for(String id : list ) {
     futures.add(session.executeAsync(boundStatement.bind(id)));
}

for (ListenableFuture<ResultSet> future : futures) {
ResultSet rs = future.get();
... // do some stuff
}

Unfortunately, this may lead to NoHostAvailableException.

Thanks you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use a semaphore to throttle the number of concurrent queries:

final Semaphore semaphore = new Semaphore(numberOfConcurrentQueries);
...
semaphore.acquire();
try {
    ResultSetFuture future = session.executeAsync("...");
    Futures.addCallback(future, new FutureCallback<ResultSet>() {
        @Override
        public void onSuccess(ResultSet result) {
            semaphore.release();
        }

        @Override
        public void onFailure(Throwable t) {
            semaphore.release();
        }
    });
} catch (Exception e) {
    semaphore.release();
}

But at the end of the day it's not so different: instead of getting a NoHostAvailableException when you exceed the capacity, the semaphore will block (or throw if you use a timed version of acquire). So you'll probably want to apply backpressure to the component that triggers these queries as well.

You might also want to tune your connection pools to adjust the capacity, see our docs (that's for 2.1, use the dropdown at the top of the page if you're on 2.0).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...