卡桑德拉表现出突然的行为

Cassandra showing abrupt behaviour

我有一个单元测试,我在其中多次写入和读取 cassandra。

future = function(Cassandra update with value x) - async //write and read updated value
value = future.get(); //reading
print value; 
assert value == x;

//doing the above operation multiple times with different values of x; 

运行 相同的代码多次显示不同的结果,即为 'value' 属性打印不同的结果。

我在本地主机上使用 cassandra

replication = {
 'class': 'SimpleStrategy',
  'replication_factor': '1'
};

值得注意的是,我在table中的同一行写入和读取(在所有读取和写入中,主键相同)。 尽管我多次修改同一个对象,但它们应该按顺序 运行,因为我在每个更新语句后 运行 宁阻塞函数 future.get()

我将 Cassandra 2.0.14datastax 驱动程序和 jdk 1.8 一起使用。

知道为什么我必须面对这种行为吗?

找到原因了。 在我的代码(不是测试代码)中,我实际上并没有按顺序写入和读取。读取未等待写入完成。

我在做什么:

`CompletionStage<Void> Function() {
   someOperation
    .thenAccept(variable -> AsyncWriteInDb(variable));
}
// AsyncWriteInDb returns CompletionStage<Void> when write is completed.
// I was reading just after execution of this function. 
`

我应该做什么:

` CompletionStage<Void> Function() {
   someOperation
    .thenCompose(variable -> AsyncWriteInDb(variable));
}
//AsyncWriteInDb returns CompletionStage<Void> when write is completed.
`

如果我写得更早(错误)的代码部分如下,这样更容易理解:

`CompletionStage<Void> Function() {
   someOperation
    .thenAccept(variable -> {
       AsyncWriteInDb(variable);
       return;
    });
}
// thenAccept's lamda was returning after initiating an asyncDbWrite.
// Reading just after this doesnt ensure sequential reading after writing.
`