简体   繁体   English

Astyanax客户端中的setMaxConns和setMaxConnsPerHost

[英]setMaxConns and setMaxConnsPerHost in Astyanax client

I am using Astyanax client to read the data from Cassandra database . 我正在使用Astyanax clientCassandra database读取数据。 I have a single cluster with four nodes . 我有一个包含four nodes single cluster I am having replication factor of 2 . 我的replication factor of 2 I am trying to understand what is the difference between 我试图了解两者之间的区别

setMaxConns and setMaxConnsPerHost 

methods in Astyanax client? 客户端中的方法? I cannot find proper documentation on this. 我找不到关于此的适当文档。

I have a Multithreaded code which which spawn multiple threads and then create the connection to Cassandra database only once (as it is a Singleton) and then keep on reusing for other request. 我有一个多线程代码,该代码产生多个线程,然后仅一次创建与Cassandra数据库的连接(因为它是一个Singleton),然后继续为其他请求重用。

Now I am trying to understand how the above two methods will play a role in read performance? 现在,我试图了解以上两种方法将如何在读取性能中发挥作用? And How those values should be set up? 以及如何设置这些值?

And If I am setting those above two methods as- 如果我将以上两种方法设置为-

setMaxConns(-1) and setMaxConnsPerHost(20) 

then what does it mean? 那是什么意思 Any explanation will be of great help. 任何解释都会有很大帮助。

Updated Code:- 更新的代码:-

Below is the code, I am using to make the connection- 下面是代码,我用来建立连接-

private CassandraAstyanaxConnection() {

    context = new AstyanaxContext.Builder()
    .forCluster(ModelConstants.CLUSTER)
    .forKeyspace(ModelConstants.KEYSPACE)
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      
        .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
    )
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool")
        .setPort(9160)
        .setMaxConnsPerHost(20)
        .setMaxConns(-1)
        .setSeeds("host1:9160,host2:9160,host3:9160,host4:9160")
    )
    .withAstyanaxConfiguration(new AstyanaxConfigurationImpl()      
        .setCqlVersion("3.0.0")
        .setTargetCassandraVersion("1.2"))
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    keyspace = context.getEntity();

    emp_cf = ColumnFamily.newColumnFamily(
        ModelConstants.COLUMN_FAMILY, 
        StringSerializer.get(), 
        StringSerializer.get());
}

If I am debugging this code, it is not even hitting the BagOfConnectionsConnectionPoolImpl class. 如果我正在调试此代码,那么它甚至都不会到达BagOfConnectionsConnectionPoolImpl类。 I put a lot of breakpoint in the same class to see how it is using the conenctions and other default parameters. 我在同一个类中放置了很多断点,以了解它如何使用连接和其他默认参数。 But don't know why it is not hitting that class. 但是不知道为什么它没有达到那个水平。

The behavior regarding these configuration properties might be dependent on implementation. 有关这些配置属性的行为可能取决于实现。

BagOfConnectionsConnectionPoolImpl BagOfConnectionsConnectionPoolImpl

BagOfConnectionsConnectionPoolImpl is the only implementation at the moment that honors both these properties. BagOfConnectionsConnectionPoolImpl是目前唯一实现这两个属性的实现。 It behaves as follows: 它的行为如下:

Connection is borrowed from the pool on every cassandra operation (query or mutation) and returned to pool upon completion of operation. 在每个cassandra操作(查询或突变)中,连接都是从池中借用的,并在操作完成后返回到池中。

maxConnsPerHost - maximum number of connections per single cassandra host. maxConnsPerHost每个单个cassandra主机的最大连接数。

maxConns - maximum number of connections in the pool. maxConns池中的最大连接数。

Both these numbers must be positive, so setMaxConns(-1) just won't work. 这两个数字都必须为正数,因此setMaxConns(-1)无效。

On the attempt to borrow a connection from pool, the pool checks active connection number against maxConns . 尝试从池借用连接时,池会根据maxConns检查活动的连接号。 If the limit is exceeded, it waits until some connection is released. 如果超出限制,它将等待直到释放某些连接。 If no connection is available in specified timeout, the pool throws PoolTimeoutException . 如果在指定的超时时间内没有可用的连接,则池将引发PoolTimeoutException

If maxConns limit is not exceeded, the pool attempts to find a cassandra host it's aware of (specified as seed or found during discovery) that has the number of active connections below maxConnsPerHost and connect to it. 如果未超过maxConns限制,则池将尝试查找其知道的(指定为种子或在发现过程中找到的)cassandra主机,该主机的活动连接数低于maxConnsPerHost并连接到该主机。 If all hosts reached connection limit, the pool throws NoAvailableHostsException . 如果所有主机都达到连接限制,则池将引发NoAvailableHostsException

For example, let's take a client that connects to cluster of 4 nodes: 例如,让我们以一个连接到4个节点的集群的客户端为例:

setMaxConns(100); setMaxConnsPerHost(10) setMaxConns(100); setMaxConnsPerHost(10) : Effective maximum number of connections is 40 (10 connections per node, no further connection attempts will be made). setMaxConns(100); setMaxConnsPerHost(10) :有效的最大连接数为40(每个节点10个连接,将不再尝试连接)。 NoAvailableHostsException will be thrown. 将引发NoAvailableHostsException

setMaxConns(20); setMaxConnsPerHost(10) setMaxConns(20); setMaxConnsPerHost(10) : Effective maximum number of connections is 20. The connections to different hosts will be distributed uniformly, but not necessary equally. setMaxConns(20); setMaxConnsPerHost(10) :有效的最大连接数为20。连接到不同主机的连接将均匀分布,但不必相等。 PoolTimeoutException will be thrown. PoolTimeoutException将被抛出。

Things get more complicated if nodes join or leave cluster, but general idea is the same. 如果节点加入或离开群集,事情会变得更加复杂,但是总体思路是相同的。

TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl TokenAwareConnectionPoolImpl和RoundRobinConnectionPoolImpl

Both TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl ignore maxConns configuration property. TokenAwareConnectionPoolImplRoundRobinConnectionPoolImpl忽略maxConns配置属性。 They just select a host (depending on row token or randomly) and attempt to connect to it. 他们只是选择一个主机(取决于行令牌或随机选择)并尝试连接到它。

If the number of active connections to that host exceeds maxConnsPerHost , the pool waits until some connection is released. 如果与该主机的活动连接数超过maxConnsPerHost ,则池将等待直到释放某些连接。 If no connection is available during specified timeout, another connection attempt to (potentially) another host is executed as a part of failover. 如果在指定的超时时间内没有可用的连接,则作为故障转移的一部分,将执行(可能)与另一台主机的另一连接尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM