Cassandra - 有没有办法限制异步查询的数量? [英] Cassandra - Is there a way to limit number of async queries?

查看:40
本文介绍了Cassandra - 有没有办法限制异步查询的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法限制 cassandra java 驱动程序同时执行的查询数量?

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
}

不幸的是,这可能会导致 NoHostAvailableException.

Unfortunately, this may lead to NoHostAvailableException.

谢谢.

推荐答案

您可以使用信号量来限制并发查询的数量:

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();
}

但在一天结束时它并没有那么不同:当您超出容量时,信号量将阻塞(或者如果您使用定时版本的获取,则不会得到 NoHostAvailableException ).因此,您可能还想对触发这些查询的组件应用背压.

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.

您可能还想调整连接池以调整容量,请参阅我们的文档(适用于 2.1,如果您使用的是 2.0,请使用页面顶部的下拉菜单).

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).

这篇关于Cassandra - 有没有办法限制异步查询的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆