Zookeeper missed events on successive changes

2019-07-11 19:27发布

问题:

I currently have a setup with a single zookeeper node and Curator to access the data. Reading data is done through a Curator TreeCache.

I have the following test:

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>);
}

Note that "new_node" does not exists before the test is executed.

    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);
    }
}

I'm expecting that the cache listener will first heard the event of a node added with "some string 4" and then heard another event of node updated with "some string 5".

Instead I am only receiving the event for a node added with value "some string 5"

Looking at the logs both commands are being executed. i.e. "N: some string 4" and "E: some string 5" are both logged. And the final value in Zookeeper is correct ("some string 5") but I do not understand why the Curator cache is only seeing a single event?

回答1:

ZooKeeper (or Curator's TreeCache) don't guarantee that you will not miss events on successive changes. The guarantee is that you won't see successive changes in a different order from what other clients would see.