Java Code Examples for org.apache.zookeeper.data.Stat#getCversion()

The following examples show how to use org.apache.zookeeper.data.Stat#getCversion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZkChildrenCache.java    From Singularity with Apache License 2.0 6 votes vote down vote up
private int getCurrentChildVersion(String path) {
  Stat stat = null;

  try {
    stat = curator.checkExists().forPath(path);
  } catch (Exception e) {
    LOG.error("While checking stat for {}", path, e);
    return -1;
  }

  if (stat != null) {
    return stat.getCversion();
  }

  return -1;
}
 
Example 2
Source File: ZKLogSegmentMetadataStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
    CompletableFuture<Versioned<List<String>>> result = ((CompletableFuture<Versioned<List<String>>>) ctx);
    if (KeeperException.Code.OK.intValue() == rc) {
        /** cversion: the number of changes to the children of this znode **/
        LongVersion zkVersion = new LongVersion(stat.getCversion());
        result.complete(new Versioned(children, zkVersion));
    } else if (KeeperException.Code.NONODE.intValue() == rc) {
        result.completeExceptionally(new LogNotFoundException("Log " + path + " not found"));
    } else {
        result.completeExceptionally(new ZKException("Failed to get log segments from " + path,
                KeeperException.Code.get(rc)));
    }
}
 
Example 3
Source File: ZKReplicationQueueStorage.java    From hbase with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
protected int getHFileRefsZNodeCversion() throws ReplicationException {
  Stat stat = new Stat();
  try {
    ZKUtil.getDataNoWatch(zookeeper, hfileRefsZNode, stat);
  } catch (KeeperException e) {
    throw new ReplicationException("Failed to get stat of replication hfile references node.", e);
  }
  return stat.getCversion();
}
 
Example 4
Source File: CuratorCacheImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
private void checkChildrenChanged(String fromPath, Stat oldStat, Stat newStat)
{
    if ( (state.get() != State.STARTED) || !recursive )
    {
        return;
    }

    if ( (oldStat != null) && (oldStat.getCversion() == newStat.getCversion()) )
    {
        return; // children haven't changed
    }

    try
    {
        BackgroundCallback callback = (__, event) -> {
            if ( event.getResultCode() == OK.intValue() )
            {
                event.getChildren().forEach(child -> nodeChanged(ZKPaths.makePath(fromPath, child)));
            }
            else if ( event.getResultCode() == NONODE.intValue() )
            {
                removeStorage(event.getPath());
            }
            else
            {
                handleException(event);
            }
            outstandingOps.decrement();
        };

        outstandingOps.increment();
        client.getChildren().inBackground(callback).forPath(fromPath);
    }
    catch ( Exception e )
    {
        handleException(e);
    }
}
 
Example 5
Source File: ZooKeeperStateHandleStore.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Gets all available state handles from ZooKeeper and locks the respective state nodes.
 *
 * <p>If there is a concurrent modification, the operation is retried until it succeeds.
 *
 * @return All state handles from ZooKeeper.
 * @throws Exception If a ZooKeeper or state handle operation fails
 */
@SuppressWarnings("unchecked")
public List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock() throws Exception {
	final List<Tuple2<RetrievableStateHandle<T>, String>> stateHandles = new ArrayList<>();

	boolean success = false;

	retry:
	while (!success) {
		stateHandles.clear();

		Stat stat = client.checkExists().forPath("/");
		if (stat == null) {
			break; // Node does not exist, done.
		} else {
			// Initial cVersion (number of changes to the children of this node)
			int initialCVersion = stat.getCversion();

			List<String> children = client.getChildren().forPath("/");

			for (String path : children) {
				path = "/" + path;

				try {
					final RetrievableStateHandle<T> stateHandle = getAndLock(path);
					stateHandles.add(new Tuple2<>(stateHandle, path));
				} catch (KeeperException.NoNodeException ignored) {
					// Concurrent deletion, retry
					continue retry;
				} catch (IOException ioException) {
					LOG.warn("Could not get all ZooKeeper children. Node {} contained " +
						"corrupted data. Ignoring this node.", path, ioException);
				}
			}

			int finalCVersion = client.checkExists().forPath("/").getCversion();

			// Check for concurrent modifications
			success = initialCVersion == finalCVersion;
		}
	}

	return stateHandles;
}
 
Example 6
Source File: ZooKeeperStateHandleStore.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Gets all available state handles from ZooKeeper and locks the respective state nodes.
 *
 * <p>If there is a concurrent modification, the operation is retried until it succeeds.
 *
 * @return All state handles from ZooKeeper.
 * @throws Exception If a ZooKeeper or state handle operation fails
 */
@SuppressWarnings("unchecked")
public List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock() throws Exception {
	final List<Tuple2<RetrievableStateHandle<T>, String>> stateHandles = new ArrayList<>();

	boolean success = false;

	retry:
	while (!success) {
		stateHandles.clear();

		Stat stat = client.checkExists().forPath("/");
		if (stat == null) {
			break; // Node does not exist, done.
		} else {
			// Initial cVersion (number of changes to the children of this node)
			int initialCVersion = stat.getCversion();

			List<String> children = client.getChildren().forPath("/");

			for (String path : children) {
				path = "/" + path;

				try {
					final RetrievableStateHandle<T> stateHandle = getAndLock(path);
					stateHandles.add(new Tuple2<>(stateHandle, path));
				} catch (KeeperException.NoNodeException ignored) {
					// Concurrent deletion, retry
					continue retry;
				} catch (IOException ioException) {
					LOG.warn("Could not get all ZooKeeper children. Node {} contained " +
						"corrupted data. Ignoring this node.", path, ioException);
				}
			}

			int finalCVersion = client.checkExists().forPath("/").getCversion();

			// Check for concurrent modifications
			success = initialCVersion == finalCVersion;
		}
	}

	return stateHandles;
}
 
Example 7
Source File: ZKReplicationQueueStorage.java    From hbase with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
protected int getQueuesZNodeCversion() throws KeeperException {
  Stat stat = new Stat();
  ZKUtil.getDataNoWatch(this.zookeeper, this.queuesZNode, stat);
  return stat.getCversion();
}