我目前有一个带有单个 Zookeeper 节点和 Curator 的设置来访问数据。读取数据是通过 Curator TreeCache 完成的。
我有以下测试:
public void test_callback_successive_changes_success_global_new_version() throws InterruptedException {
ZookeeperTestsHelper.createNewNodeInZookeeperSingleCommand("/my/path/new_node", "some string4", curator);
ZookeeperTestsHelper.createNewNodeInZookeeperSingleCommand("/my/path/new_node", "some string5", curator);
Thread.sleep(1000);
assertThat(<check what events the listener heard>);
}
请注意,在执行测试之前“new_node”不存在。
public static void createNewNodeInZookeeperSingleCommand(final String fullPathOfValueInZookeeper, final String valueToSet, final CuratorFramework curator) {
try {
if (curator.checkExists().forPath(fullPathOfValueInZookeeper) != null) {
System.out.println("E: " + valueToSet);
//Node already exists just set it
curator.setData().forPath(fullPathOfValueInZookeeper, valueToSet.getBytes());
} else {
System.out.println("N: " + valueToSet);
//Node needs to be created
curator.create().creatingParentsIfNeeded().forPath(fullPathOfValueInZookeeper, valueToSet.getBytes());
}
} catch (Exception e) {
throw new IllegalStateException("Error", e);
}
}
我期望缓存监听器将首先听到添加了“some string 4”的节点的事件,然后听到添加了“some string 5”的节点的另一个事件。
相反,我只接收添加了值“some string 5”的节点的事件
查看日志,两个命令都正在执行。即“N:某个字符串 4”和“E:某个字符串 5”都被记录。 Zookeeper 中的最终值是正确的(“某个字符串 5”),但我不明白为什么 Curator 缓存只看到一个事件?
最佳答案
ZooKeeper(或 Curator 的 TreeCache)不保证您不会错过连续更改的事件。保证您不会看到与其他客户端所看到的顺序不同的连续更改。
关于java - Zookeeper 错过了连续更改的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38806413/