Cassandra:将列设置为 null 后的写入会随机丢失.这是一个错误,还是我做错了什么


@Test
public void testWriteUpdateRead() throws Exception {
  Cluster cluster = Cluster.builder()
      .addContactPoint("127.0.0.1")
      .build();
  Session cs = cluster.connect();
  cs.execute("DROP KEYSPACE if exists readtest;");
  cs.execute("CREATE KEYSPACE readtest WITH replication " +
      "= {'class':'SimpleStrategy', 'replication_factor':1};");
  cs.execute("create table readtest.sessions(" +
      "id text primary key," +
      "passwordHash text," +
      ");");
  for (int i = 0; i < 1000; i++) {
    String sessionID = UUID.randomUUID().toString();
    cs.execute("insert into readtest.sessions (id, passwordHash) values('" + sessionID + "', null)");
    cs.execute("update readtest.sessions set passwordHash='" + sessionID + "' where id = '" + sessionID + "' ");
    ResultSet rs = cs.execute("select * from readtest.sessions where id = '" + sessionID + "'");
    Row row = rs.one();
    assertThat("failed ith time=" + i, row.getString("passwordHash"), equalTo(sessionID));
  }
  cs.close();
  cluster.close();
}

上面的测试总是失败,并且总是在一些随机迭代中。谁能解释为什么有时更新会丢失?

我在本地运行Cassandra,单节点。在 osx 上使用 brew 安装。


堆栈跟踪:

java.lang.AssertionError: failed ith time=4
Expected: "fd2fb850-b3ad-4bfb-8971-485c4df097dc"
     but: was null
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at in.folks.blaze.service.CassandraTest.testWriteUpdateRead(CassandraTest.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

其他信息:

Cassandra version: [cqlsh 5.0.1 | Cassandra 2.1.2 | CQL spec 3.2.0 | Native protocol v3]
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>2.1.4</version>
</dependency>
<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-mapping</artifactId>
  <version>2.1.4</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.10.17</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>

这似乎是在创建行后立即更新的问题。 如果你在第二次更新后等待一小段时间,你会得到正确的值,这似乎确实是一个问题,但也许只有在本地运行 cassandra 时才会注意到。

我怀疑这是一个 cassandra 问题,尽管它目前似乎在 trunk 和 cassandra-2.1 分支上得到了解决。 我可以针对 1.2.19、2.0.11 和 2.1.2 进行复制,但无法针对 trunk 或 cassandra-2.1 进行复制。 我似乎无法根据更改日志确定确切的更改。

编辑2014年1月5日:用户列表上的评论对可能发生的情况有更多解释:https://groups.google.com/a/lists.datastax.com/forum/#!topic/java-driver-user/0IDCz96lAeA。 显然,当写入的时间戳完全相同时,就会发生这种情况。

相关内容

最新更新