【问题标题】:Slow connection to Cassandra与 Cassandra 的连接缓慢
【发布时间】:2015-10-09 06:41:24
【问题描述】:

我无法通过 Datastax PHP-和 Java-Driver 连接到我的 3 节点 cassandra 集群。

特别是对于 PHP 驱动程序而言,我可以快速连接以缩短网站的加载时间,这一点至关重要。

我该如何调试或者是什么原因?

Java 输出显示如下:

09:59:40,284 [main] DEBUG - com.datastax.driver.NEW_NODE_DELAY_SECONDS is undefined, using default value 1
09:59:40,284 [main] DEBUG - com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE is undefined, using default value 4
09:59:40,297 [main] DEBUG - com.datastax.driver.NOTIF_LOCK_TIMEOUT_SECONDS is undefined, using default value 60
09:59:40,357 [main] DEBUG - Starting new cluster with contact points [/XXX.XXX.XXX.XXX:9042, /XXX.XXX.XXX.YYY:9042, /XXX.XXX.XXX.ZZZ:9042]
09:59:40,402 [main] DEBUG - Using SLF4J as the default logging framework
09:59:40,489 [main] DEBUG - java.nio.Buffer.address: available
09:59:40,490 [main] DEBUG - sun.misc.Unsafe.theUnsafe: available
09:59:40,490 [main] DEBUG - sun.misc.Unsafe.copyMemory: available
09:59:40,490 [main] DEBUG - java.nio.Bits.unaligned: true
09:59:40,492 [main] DEBUG - Java version: 8
09:59:40,492 [main] DEBUG - -Dio.netty.noUnsafe: false
09:59:40,492 [main] DEBUG - sun.misc.Unsafe: available
09:59:40,492 [main] DEBUG - -Dio.netty.noJavassist: false
09:59:40,665 [main] DEBUG - Javassist: available
09:59:40,665 [main] DEBUG - -Dio.netty.tmpdir: /var/folders/4y/t4b47lbn1zjbjpb6x09l30wm0000gn/T (java.io.tmpdir)
09:59:40,666 [main] DEBUG - -Dio.netty.bitMode: 64 (sun.arch.data.model)
09:59:40,666 [main] DEBUG - -Dio.netty.noPreferDirect: false
09:59:40,708 [main] DEBUG - com.datastax.driver.FORCE_NIO is undefined, using default value false
09:59:40,710 [main] INFO  - Did not find Netty's native epoll transport in the classpath, defaulting to NIO.
09:59:40,714 [main] DEBUG - -Dio.netty.eventLoopThreads: 8
09:59:40,723 [main] DEBUG - -Dio.netty.noKeySetOptimization: false
09:59:40,723 [main] DEBUG - -Dio.netty.selectorAutoRebuildThreshold: 512
09:59:40,747 [main] DEBUG - -Dio.netty.leakDetectionLevel: simple
09:59:41,035 [main] DEBUG - com.datastax.driver.DISABLE_COALESCING is undefined, using default value false
09:59:41,046 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.Message$ResponseMatcher
09:59:41,066 [main] DEBUG - -Dio.netty.allocator.numHeapArenas: 4
09:59:41,066 [main] DEBUG - -Dio.netty.allocator.numDirectArenas: 4
09:59:41,066 [main] DEBUG - -Dio.netty.allocator.pageSize: 8192
09:59:41,066 [main] DEBUG - -Dio.netty.allocator.maxOrder: 11
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.chunkSize: 16777216
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.tinyCacheSize: 512
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.smallCacheSize: 256
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.normalCacheSize: 64
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
09:59:41,067 [main] DEBUG - -Dio.netty.allocator.cacheTrimInterval: 8192
09:59:41,078 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.FrameMatcher
09:59:41,082 [main] DEBUG - Generated: io.netty.util.internal.__matchers__.com.datastax.driver.core.Message$RequestMatcher
09:59:41,104 [main] DEBUG - -Dio.netty.initialSeedUniquifier: 0x24d6f22f78c5a924 (took 8 ms)
09:59:41,130 [main] DEBUG - -Dio.netty.allocator.type: unpooled
09:59:41,130 [main] DEBUG - -Dio.netty.threadLocalDirectBufferSize: 65536
09:59:41,197 [cluster1-nio-worker-0] DEBUG - Connection[/XXX.XXX.XXX.YYY:9042-1, inFlight=0, closed=false] Connection opened successfully
09:59:41,218 [cluster1-nio-worker-0] DEBUG - -Dio.netty.recycler.maxCapacity.default: 262144
09:59:41,432 [main] DEBUG - [Control connection] Refreshing node list and token map
09:59:41,518 [main] DEBUG - [Control connection] Refreshing schema
09:59:42,137 [main] DEBUG - [Control connection] Refreshing node list and token map
09:59:42,315 [main] DEBUG - [Control connection] Successfully connected to /XXX.XXX.XXX.YYY:9042
09:59:42,315 [main] INFO  - Using data-center name '168' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.XXX:9042 added
09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.YYY:9042 added
09:59:42,315 [main] INFO  - New Cassandra host /XXX.XXX.XXX.ZZZ:9042 added
09:59:42,342 [cluster1-nio-worker-1] DEBUG - Connection[/XXX.XXX.XXX.XXX:9042-2, inFlight=0, closed=false] Connection opened successfully
09:59:42,345 [cluster1-nio-worker-2] DEBUG - Connection[/XXX.XXX.XXX.YYY:9042-1, inFlight=0, closed=false] Connection opened successfully
09:59:42,348 [cluster1-nio-worker-3] DEBUG - Connection[/XXX.XXX.XXX.ZZZ:9042-1, inFlight=0, closed=false] Connection opened successfully
09:59:42,580 [cluster1-nio-worker-2] DEBUG - Added connection pool for /XXX.XXX.XXX.XXX:9042
09:59:42,591 [cluster1-nio-worker-3] DEBUG - Added connection pool for /XXX.XXX.XXX.YYY:9042
09:59:42,609 [cluster1-nio-worker-1] DEBUG - Added connection pool for /XXX.XXX.XXX.ZZZ:9042

如您所见,大约需要 2.5 秒,这对于我的用例来说太慢了。

PHP 驱动程序也是如此,但我没有日志。

连接驱动程序后查询速度非常快。唯一的问题是连接时间慢。我已经将三个节点都设置为接触点。

编辑

澄清一下:我的 PHP 驱动程序是问题所在。我想知道为什么它不使用池/持久连接。当我连续两次调用脚本时,每次调用需要 2-5 秒。我认为第二个调用应该使用持久池。 phpinfo() 显示持久集群和会话 = 0。这是我正在使用的代码:

$cluster = Cassandra::cluster()
        ->withContactPoints('XXX.XXX.XXX.XXX', 'XXX.XXX.XXX.YYY, 'XXX.XXX.XXX.ZZZ')
        ->withCredentials('USERNAME', 'PASSWORD')
        ->build();
$keyspace = 'myKeyspace';
$session = $cluster->connect($keyspace);

更新

问题出在我的网络上。带宽太少。

【问题讨论】:

  • 请在您投反对票时告诉我我的问题有什么问题。

标签: java php cassandra datastax


【解决方案1】:

DataStax 驱动程序是全功能驱动程序。他们知道您的集群拓扑和集群状态,这需要在集群对象构建阶段进行一些昂贵的操作。集群对象的创建通常需要几秒钟的时间(取决于集群的大小/节点数)。

最佳做法是不要为每个请求都创建集群对象(这将是非常低效的)。相反,您希望一次性构建集群对象并保持连接打开。然后,当您收到来自前端的请求时,使用现有的集群对象处理它。

如果使用得当,Cassandra 将为您提供非常快的响应时间。

有关其他 c* 客户端最佳实践,请查看 Brian's Cassandra Loader。这是一个很好的参考应用程序,也是一个非常高效的批量加载程序。

一些关键的最佳实践包括:如果您使用的是执行异步,请限制异步请求的数量;如果您使用的是批处理,请确保批处理是特定于令牌的以避免过度协调;除非您需要原子性,否则不要使用记录批处理,并且不要从您的应用程序中动态操作您的架构以避免架构不匹配。

【讨论】:

  • 嗨,这是真的。但是,问题是,为什么我的 PHP 实例必须在每次驱动程序保持这些连接时都重新连接?刚刚更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 2017-08-22
  • 2014-08-04
  • 1970-01-01
相关资源
最近更新 更多