代码之家  ›  专栏  ›  技术社区  ›  TrongBang

cassandra在没有thread.sleep的情况下引发writetimeout异常

  •  1
  • TrongBang  · 技术社区  · 6 年前

    我有一个批量工作,写大约30万行到卡桑德拉。我把它们分成小批,每批50行。

    下面是伪代码。

    @Override
    public void executeQuery(List<BatchStatement> batches) {
        List<ResultSetFuture> futures = List.of();
        for (BatchStatement batch: batches) {
            futures.add(session.executeAsync(batch));
        }
    
        for(ResultSetFuture rsf: futures) {
            rsf.getUninterruptibly();
            /* I have to add the following code to avoid WriteTimeoutException
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                logger.error("Thread.sleep", e);
            }
            */
    
        }
    }
    

    我不知道没有thread.sleep为什么会出现writetimeout异常。如何避免这种情况?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Alex Ott    6 年前

    通过对数据使用批处理语句(这很可能属于不同的分区),您的系统实际上超载了,因为协调节点需要向其他节点发送请求并等待应答。您只需要为特定的用例使用批处理,而不需要像在关系数据库中那样使用批处理——以加速执行。这个 documentation 描述批处理的错误使用。

    为每一行发送单独的异步请求将改善情况,但您需要注意不要同时发送太多的请求(通过使用信号量),并通过增加每个连接的空中请求数 pooling options .