java - Zookeeper 错过了连续更改的事件

标签 java apache-zookeeper apache-curator

我目前有一个带有单个 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/

相关文章:

java - 为什么在SpringMVC中rest-servlet,xml是必须的?

apache - 在 Curator 重试尝试时处理来自 ZooKeeper 的 EndOfStreamException?

java - Apache Curator 双重锁定问题与多个服务

java - 如何在post请求中设置身份验证?

java - 我必须在 blueJ 中创建 3 个对象并调用 display() 但我什至无法创建对象。请看代码结果

java - Java中最小值始终显示为0

java - Kafka Log4j 附加程序中的延迟

apache-kafka - 卡夫卡 Storm 喷口 : Got fetch request with offset out of range

hadoop - HBase如何使用ZooKeeper?

java - 如何使用 Curator for Zookeeper 有效地使用 LeaderElection 配方?