在Astyanax客户端中的setMaxConns和setMaxConnsPerHost [英] setMaxConns and setMaxConnsPerHost in Astyanax client

查看:215
本文介绍了在Astyanax客户端中的setMaxConns和setMaxConnsPerHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Astyanax客户端 Cassandra数据库中读取数据。我有一个单个集群四个节点。我有复制因子2 。我试图理解



之间有什么区别。

  setMaxConns和setMaxConnsPerHost 
pre>

方法在Astyanax客户端?我找不到正确的文档。



我有一个多线程代码,它产生多个线程,然后创建一个连接到Cassandra数据库只有一次(因为它是一个Singleton)然后继续重用其他请求。



现在我想了解上述两种方法如何在读性能中发挥作用?



如果我将上述两个方法设置为 -

  setMaxConns(-1)和setMaxConnsPerHost(20)

这是什么意思?任何解释都会有很大的帮助。



更新代码: -



是代码,我正在使用连接 -

 私有CassandraAstyanaxConnection(){

= 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 $ b .setSeeds(host1:9160,host2:9160,host3:9160,host4:9160)

.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setCqlVersion )
.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());
}



如果我调试这个代码,甚至不会碰到 BagOfConnectionsConnectionPoolImpl 类。我把很多断点在同一个类看看它是如何使用conenctions和其他默认参数。但是不知道为什么不打这个类。

解决方案

这些配置属性的行为可能取决于实现。



BagOfConnectionsConnectionPoolImpl



BagOfConnectionsConnectionPoolImpl 尊重这两个属性的时刻。它的行为如下:



每个cassandra操作(查询或突变)从池中借用连接,并在操作完成后返回到池。



maxConnsPerHost - 每个Cassandra主机的最大连接数。



maxConns - 池中的最大连接数。



这两个数字必须为正数,因此 setMaxConns(-1)将不起作用。



在尝试从池借用连接时,池会根据 maxConns 检查活动连接数。如果超过限制,它将等待直到某些连接释放。如果在指定的超时中没有可用的连接,则池会引发 PoolTimeoutException



如果 maxConns不会超过限制,池会尝试查找其感知到的活动连接数低于 maxConnsPerHost 并连接到它。如果所有主机都达到连接限制,则该池会抛出 NoAvailableHostsException



例如, 4个节点的集群:



setMaxConns(100); setMaxConnsPerHost(10):有效最大连接数为40(每个节点10个连接,不进行进一步的连接尝试)。 将抛出NoAvailableHostsException



setMaxConns setMaxConnsPerHost(10):有效最大连接数为20.与不同主机的连接将均匀分布,但不是必须平等分配。 PoolTimeoutException 将被抛出。



如果节点加入或离开集群,事情变得更复杂, 。



TokenAwareConnectionPoolImpl& RoundRobinConnectionPoolImpl



两者 TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl 忽略 maxConns 配置属性。他们只是选择一个主机(取决于行令牌或随机),并尝试连接到它。



如果到该主机的活动连接数超过 maxConnsPerHost ,则池会等待,直到某些连接释放。如果在指定的超时期间没有可用的连接,则另一个连接尝试(可能)尝试另一个主机作为故障转移的一部分。


I am using Astyanax client to read the data from Cassandra database. I have a single cluster with four nodes. I am having 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.

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. 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 is the only implementation at the moment that honors both these properties. 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.

maxConnsPerHost - maximum number of connections per single cassandra host.

maxConns - maximum number of connections in the pool.

Both these numbers must be positive, so setMaxConns(-1) just won't work.

On the attempt to borrow a connection from pool, the pool checks active connection number against 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.

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. If all hosts reached connection limit, the pool throws NoAvailableHostsException.

For example, let's take a client that connects to cluster of 4 nodes:

setMaxConns(100); setMaxConnsPerHost(10): Effective maximum number of connections is 40 (10 connections per node, no further connection attempts will be made). NoAvailableHostsException will be thrown.

setMaxConns(20); setMaxConnsPerHost(10): Effective maximum number of connections is 20. The connections to different hosts will be distributed uniformly, but not necessary equally. PoolTimeoutException will be thrown.

Things get more complicated if nodes join or leave cluster, but general idea is the same.

TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl

Both TokenAwareConnectionPoolImpl & RoundRobinConnectionPoolImpl ignore maxConns configuration property. 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. If no connection is available during specified timeout, another connection attempt to (potentially) another host is executed as a part of failover.

这篇关于在Astyanax客户端中的setMaxConns和setMaxConnsPerHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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