简体   繁体   English

Cassandra Datastax驱动程序 - 连接池

[英]Cassandra Datastax Driver - Connection Pool

I'm trying to understand the connection pooling in Datastax Cassandra Driver, so I can better use it in my web service. 我正在尝试理解Datastax Cassandra Driver中的连接池,所以我可以在我的Web服务中更好地使用它。

I have version 1.0 of the documentation. 我有1.0版本的文档。 It says: 它说:

The Java driver uses connections asynchronously, so multiple requests can be submitted on the same connection at the same time. Java驱动程序异步使用连接,因此可以在同一连接上同时提交多个请求。

What do they understand by connection? 他们通过连接了解什么? When connecting to a cluster, we have: a Builder, a Cluster and a Session. 连接到群集时,我们有:构建器,群集和会话。 Which one of them is the connection? 其中哪一个是连接?

For example, there is this parameter: 例如,有这个参数:

maxSimultaneousRequestsPerConnection - number of simultaneous requests on all connections to a host after which more connections are created. maxSimultaneousRequestsPerConnection - 在主机的所有连接上同时发出的请求数,然后创建更多连接。

So, these connections are automatically created, in the case of connection pooling (which is what I would expect). 因此,在连接池的情况下,这些连接会自动创建(这是我所期望的)。 But what exactly are the connections? 但究竟是什么联系? Cluster objects? 集群对象? Sessions? 会议?

I'm trying to decide what to keep 'static' in my web service. 我正在尝试决定在我的网络服务中保持“静态”的内容。 For the moment, I decided to keep the Builder static, so for every call I create a new Cluster and a new Session. 目前,我决定保持Builder静态,因此对于每次调用,我都会创建一个新的Cluster和一个新的Session。 Is this ok? 这个可以吗? If the Cluster is the Connection, then it should be ok. 如果群集是连接,那么它应该没问题。 But is it? 但是吗? Now, the logger says, for every call: 现在,记录器说,每次通话:

2013:12:06 12:05:50 DEBUG Cluster:742 - Starting new cluster with contact points 2013:12:06 12:05:50调试群集:742 - 使用联系点启动新群集

2013:12:06 12:05:50 DEBUG ControlConnection:216 - [Control connection] Refreshing node list and token map 2013:12:06 12:05:50 DEBUG ControlConnection:216 - [控制连接]刷新节点列表和令牌映射

2013:12:06 12:05:50 DEBUG ControlConnection:219 - [Control connection] Refreshing schema 2013:12:06 12:05:50 DEBUG ControlConnection:219 - [控制连接]刷新模式

2013:12:06 12:05:50 DEBUG ControlConnection:147 - [Control connection] Successfully connected to... 2013:12:06 12:05:50 DEBUG ControlConnection:147 - [控制连接]成功连接到...

So, it connects to the Cluster every time? 那么,它每次都连接到集群? It's not what I want, I want to reuse connections. 这不是我想要的,我想重用连接。

So, the connection is actually the Session? 那么,连接实际上是Session? If this is the case, I should keep the Cluster static, not the Builder. 如果是这种情况,我应该保持群集静态,而不是构建器。

What method should I call, to be sure I reuse connections, whenever possible? 我应该调用什么方法,以确保尽可能重用连接?

The accepted answer (at the time of this writing) is giving the correct advice: 接受的答案 (在撰写本文时)给出了正确的建议:

As long as you use the same Session object, you [will] be reusing connections. 只要您使用相同的Session对象,您[将]重用连接。

However, some parts were originally oversimplified. 但是,有些部分最初过于简单。 I hope the following provides insight into the scope of each object type and their respective purposes. 我希望以下内容能够深入了解每种对象类型的范围及其各自的用途。

Builder ≠ Cluster ≠ Session ≠ Connection ≠ Statement Builder≠Cluster≠Session≠Connection≠Statement

A Cluster.Builder is used to configure and create a Cluster Cluster.Builder用于配置和创建群集

A Cluster represents the entire Cassandra ring Cluster代表整个Cassandra环

A ring consists of multiple nodes (hosts), and the ring can support one or more keyspaces . 环由多个节点(主机)组成,环可以支持一个或多个键空间 You can query a Cluster object about cluster- (ring)-level properties. 您可以在Cluster对象中查询cluster-(ring)-level属性。

I also think of it as the object that represents the calling application to the ring. 我也把它看作代表响铃的调用应用程序的对象。 You communicated your application's needs (eg encryption, compression, etc.) to the builder, but it is this object that first implements/communicates with the actual C* ring. 您将应用程序的需求(例如加密,压缩等)传达给构建器,但是此对象 首先实现/与实际C *环通信。 If your application uses more than one authentication credential for different users/purposes, you likely have different Cluster objects even if they connect to the same ring. 如果您的应用程序为不同的用户/目的使用多个身份验证凭据 ,即使它们连接到同一个环,您也可能拥有不同的Cluster对象。

A Session itself is not a connection, but it manages them Session 本身不是连接,但它管理它们

A session may need to talk to all nodes in the ring, which cannot be done with a single TCP connection except in the special case of rings that contain exactly one(1) node. 会话可能需要与环中的所有节点通信,除了在仅包含一(1)个节点的环的特殊情况之外,这不能通过单个TCP连接来完成。 The Session manages a connection pool , and that pool will generally have at least one connection for each node in the ring. 会话管理连接池 ,该池通常至少有一个连接用于环中的每个节点 This is why you should re-use Session objects as much as possible. 这就是您应该尽可能多地重用Session对象的原因。 An application does not directly manage or access connections. 应用程序不直接管理或访问连接。

A Session is accessed from the Cluster object; Cluster对象访问会话 ; it is usually "bound" to a single keyspace at a time, which becomes the default keyspace for the statements executed from that session. 它通常一次“绑定”到单个键空间 ,这成为从该会话执行的语句的默认键空间。 A statement can use a fully-qualified table name (eg keyspacename.tablename ) to access tables in other keyspaces, so it's not required to use multiple sessions to access data across keyspaces. 语句可以使用完全限定的表名(例如keyspacename.tablename )来访问其他键空间中的表,因此不需要使用多个会话来跨键空间访问数据。 Using multiple sessions to talk to the same ring increases the total number of TCP connections required. 使用多个会话同一个环进行通信会增加所需的TCP连接总数。

A Statement executes within a Session Statement会话中执行

Statements can be prepared or not, and each one either mutates data or queries it (and in some cases, both). 可以准备或不准备语句,每个语句都可以改变数据或查询数据(在某些情况下,两者都有)。 The fastest, most efficient statements need to communicate with at most one node, and a Session from a topology-aware Cluster should contact only that node (or one of its peers) on a single TCP connection. 最快,最有效的语句需要与最多一个节点通信,并且来自拓扑感知群集会话应该仅在单个TCP连接上联系该节点(或其一个对等节点)。 The least efficient statements must touch all replicas (a majority of nodes), but that will be handled by the coordinator node on the ring itself, so even for these statements the Session will only use a single connection from the application. 效率最低的语句必须触及所有副本(大多数节点),但这将由环本身上的协调器节点处理,因此即使对于这些语句, Session也只使用来自应用程序的单个连接。

Also, versions 2 and 3 of the Cassandra binary protocol used by the driver use multiplexing on the connections. 此外,驱动程序使用的Cassandra 二进制协议的版本2和3使用连接上的多路复用 So while a single statement requires at least one TCP connection, that single connection can potentially service up to 128 or 32k+ asynchronous requests simultaneously, depending on the protocol version (respectively). 因此,虽然单个语句至少需要一个TCP连接,但该单个连接可能同时为多达128或32k +异步请求提供服务,具体取决于协议版本(分别)。

You are right, the connection is actually in the Session, and the Session is the object you should give to your DAOs to write into Cassandra. 你是对的,连接实际上在Session中,Session是你应该给DAO写入Cassandra的对象。

As long as you use the same Session object, you should be reusing connections (you can see the Session as being your connection pool). 只要您使用相同的Session对象,就应该重用连接(您可以将Session视为连接池)。

Edit (2017/4/10) : I precised this answer following @William Price one. 编辑(2017/4/10):我在@William Price之后对这个答案进行了预测。 Please be aware that this answer is 4 years old, and Cassandra have changed a fair bit in the meantime ! 请注意,这个答案是4岁,而Cassandra在此期间已经改变了一点!

Just an update for the community. 只是社区的更新。 You can set connection pool in the following way 您可以通过以下方式设置连接池

private static Cluster cluster;

cluster.getConfiguration().getPoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL,100);

暂无
暂无

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

相关问题 告诉Datastax Java Cassandra驱动程序超时集群连接 - Tell Datastax Java Cassandra driver to timeout cluster connection datastax驱动程序连接= apache ignite和cassandra(您可能希望增加每个主机连接的驱动程序数) - datastax driver connection = apache ignite and cassandra(you may want to increase the driver number of per-host connections) Cassandra-com.datastax.driver.core.exceptions.DriverException:尝试获取可用连接时超时 - Cassandra - com.datastax.driver.core.exceptions.DriverException: Timeout while trying to acquire available connection Datastax Java驱动程序可以自动发现其连接池中特定数据中心的所有节点? - Datastax Java driver to autodiscover all the nodes for specific datacenter in its connection pool? Datastax Java驱动程序关闭前一个主机后,Cassandra重新连接尝试下一个主机 - Cassandra Re-Connection attempt to next host after previous host is down by Datastax Java Driver 如何使用Datastax Java驱动程序在属性文件中提供Cassandra的连接详细信息 - How to give connection details for cassandra in properties file using datastax java driver 从Datastax Cassandra Java驱动程序获取“对断开的连接进行写入尝试”错误 - Getting “Write attempt on defunct connection” Error From Datastax Cassandra Java Driver java cassandra connection java.lang.ClassNotFoundException:com.datastax.driver.core.Cluster - java cassandra connection java.lang.ClassNotFoundException: com.datastax.driver.core.Cluster NoHostAvailableException使用Cassandra和DataStax Java驱动程序如果是大ResultSet - NoHostAvailableException With Cassandra & DataStax Java Driver If Large ResultSet 带分页的Datastax Cassandra Java驱动程序RetryPolicy - Datastax Cassandra java driver RetryPolicy for Statement with paging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM