卡珊德拉 - 有没有办法来限制异步的查询号码是多少? [英] Cassandra - Is there a way to limit number of async queries?

查看:174
本文介绍了卡珊德拉 - 有没有办法来限制异步的查询号码是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法来限制由卡桑德拉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 当你超出它的容量,信号将阻止(或者,如果您使用的是扔收购定时版本)。所以,你可能会想pressure申请回触发这些查询的组件。

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

这篇关于卡珊德拉 - 有没有办法来限制异步的查询号码是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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