【问题标题】:Import data from File to Cassandra Cluster with 5 nodes causes BusyConnectionException将数据从文件导入具有 5 个节点的 Cassandra 集群导致 BusyConnectionException
【发布时间】:2020-09-09 18:25:58
【问题描述】:

对于我的论文,我需要将数据从文件上传到 Cassandra 集群。 session.execute() 太慢了。所以我决定使用 session.executeAsyn()。但它会导致 BusyConnectionException。

这是我的 Java 代码:

    final PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, 32768)
            .setMaxRequestsPerConnection(HostDistance.REMOTE, 32768);
    final Cluster cluster = Cluster.builder()
            .withPoolingOptions(poolingOptions)
            .addContactPoint("x.x.x.x")
            .withPort(9042)
            .build();
    final Session session = cluster.connect();
    System.out.println("session object---" + session.getState());
    final String path = "&PathToFile%";
    final File dir = new File(path);

    session.execute("use products;");
    for (final File file : dir.listFiles()) {
        final BufferedReader br = new BufferedReader(new FileReader(file));
        String str;
        final String insert = br.readLine();
        while ((str = br.readLine()) != null) {
            final String query = insert + str.substring(0, str.length() - 1) + "IF NOT EXISTS ;";
            session.executeAsync(query);
        }
    }
    session.close();
    cluster.close();
}

以下是我在执行代码时遇到的异常:

查询 /x.x.x.1:9042 时出错:com.datastax.driver.core.exceptions.BusyPoolException: [/x.x.x.1] 池忙(没有可用连接,队列已达到最大大小 256) 查询 /x.x.x.2:9042 时出错:com.datastax.driver.core.exceptions.BusyPoolException: [/x.x.x.2] 池忙(没有可用连接,队列已达到最大大小 256) 查询 /x.x.x.3:9042 时出错:com.datastax.driver.core.exceptions.BusyPoolException: [/x.x.x.3] 池忙(没有可用连接,队列已达到其最大大小 256) 查询 /x.x.x.4:9042 时出错:com.datastax.driver.core.exceptions.BusyPoolException: [/x.x.x.4] 池忙(没有可用的连接,队列已达到其最大大小 256) 查询 /x.x.x.5:9042 时出错:com.datastax.driver.core.exceptions.BusyPoolException: [/x.x.x.5] 池忙(没有可用的连接,队列已达到其最大大小 256)

【问题讨论】:

  • 本系统 CLI 上的ulimit -a 返回什么?还要检查this

标签: java cassandra connection-pooling datastax-java-driver


【解决方案1】:

当您在一个连接上放置太多请求时会发生繁忙异常。您需要控制发送多少请求。最简单的方法是使用信号量或类似的东西。我 have a class 包装了 Session 并允许控制飞行请求的数量,因此它的行为类似于异步,直到达到限制,并且会阻塞直到飞行请求的数量低于限制。你可以使用我的代码,或者实现类似的东西。

更新:您正在使用light-weight transactions (LWT)IF NOT EXISTS 子句),这会严重影响集群的性能,因为每个插入都需要与其他节点协调......

【讨论】:

  • 感谢您的回答。如果我使用你的课程,我应该用 poolingOptions 删除这部分吗?所以我使用了你的课程:final Session session = cluster.connect(); final SessionLimiter sl = new SessionLimiter(session, 20); ,我用 session.execute() 替换了 sesseion.executeAsyn。它的赖特?
  • 不,你只需要将 sessioin 包装到 SessionLimiter 中,如您在此处看到的:github.com/alexott/datastax-bootcamp-project/blob/master/java/…,然后调用 sl.executeAsync 而不是 session.executeAsync... 你不需要指定请求的数量 - 它将从集群的配置中获取
  • 我做到了,但它太慢了。 ://
  • 30/s 。调试:Connection[/x.x.x.1:9042-1, inFlight=0, closed=false] was inactive for 30 seconds, sending heartbeatConnection[/x.x.x.1:9042-1, inFlight=0, closed=false] heartbeat query succeeded....我认为它只适用于这个节点(x.x.x.1),因为没有关于其他节点的信息。
  • 切换到使用准备好的语句而不是为每一行生成查询
猜你喜欢
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
相关资源
最近更新 更多