Cassandra丢失了对同一分区的多次异步写入的数据

时间:2016-06-08 10:05:22

标签: java asynchronous cassandra datastax

我正在使用Cassandra 2.1.14和Datastax Java驱动程序(2.1.10.2/3.0.2)。 Cassandra目前正在一台测试服务器上运行。除了异步写入同一分区外,一切正常。这是我的代码片段:

PreparedStatement insertId = session.prepare("INSERT INTO " + KEYSPACE + "." + ID_TABLE +
            " (" +
            PARTITION + ", " + INDEX + ", " + ID +
            ") " +
            "VALUES (?, ?, ?) USING TTL ?").setConsistencyLevel(ConsistencyLevel.ONE);

BoundStatement boundStatement = new BoundStatement(insertId);

for (int i = 0; i < list.size(); i++) {
    if (ASYNC_WRITE) {
        ResultSetFuture resultSetFuture = session.executeAsync(boundStatement.bind(partition, i, list.get(i), TTL_SECONDS));

        Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {

            @Override
            public void onFailure(Throwable e) {
                Logger.Log.error("Exception: " + Logger.getStackTraceString(e));
            }

            @Override
            public void onSuccess(ResultSet results) {
                Logger.Log.debug("OK");
            }
        });
    } else {
        session.execute(boundStatement.bind(partition, i, list.get(i), TTL_SECONDS);
    }
}

对于包含3个元素和异步写入的列表,只写入两个元素,例如0和2或1和2.不抛出任何异常,并为所有写入调用onSuccess()。因此,服务器上绝对没有负载,如下所述:Async writes seem to be broken in Cassandra。当我切换到同步写入时,所有元素都被写入。

更新
我找到了解决方案。看起来每次调用executeAsync之前都必须创建一个新的BoundStatement:

for (int i = 0; i < list.size(); i++) {
    BoundStatement boundStatement = new BoundStatement(insertId);
    if (ASYNC_WRITE) {
        ResultSetFuture resultSetFuture = session.executeAsync(boundStatement.bind(partition, i, list.get(i), TTL_SECONDS));

        Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {

            @Override
            public void onFailure(Throwable e) {
                Logger.Log.error("Exception: " + Logger.getStackTraceString(e));
            }

            @Override
            public void onSuccess(ResultSet results) {
                Logger.Log.debug("OK");
            }
        });
    } else {
        session.execute(boundStatement.bind(partition, i, list.get(i), TTL_SECONDS);
    }
}

0 个答案:

没有答案