Java Code Examples for org.apache.curator.framework.api.CuratorEvent#getData()

The following examples show how to use org.apache.curator.framework.api.CuratorEvent#getData() . 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: ZkParallelFetcher.java    From Baragon with Apache License 2.0 6 votes vote down vote up
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
  try {
    KeeperException.Code code = KeeperException.Code.get(event.getResultCode());

    switch (code) {
      case OK:
        T data = event.getData() == null ? null : transformFunction.apply(event.getData());
        dataMap.put(ZKPaths.getNodeFromPath(event.getPath()), data);
        break;
      case NONODE:
        // In this case there was a race condition in which the child node was deleted before we asked for data.
        break;
      default:
        exceptions.add(KeeperException.create(code, event.getPath()));
    }
  } finally {
    countDownLatch.countDown();
  }
}
 
Example 2
Source File: NodeCache.java    From xian with Apache License 2.0 5 votes vote down vote up
private void processBackgroundResult(CuratorEvent event) throws Exception
{
    switch ( event.getType() )
    {
        case GET_DATA:
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                ChildData childData = new ChildData(path, event.getStat(), event.getData());
                setNewData(childData);
            }
            break;
        }

        case EXISTS:
        {
            if ( event.getResultCode() == KeeperException.Code.NONODE.intValue() )
            {
                setNewData(null);
            }
            else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                if ( dataIsCompressed )
                {
                    client.getData().decompressed().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
                else
                {
                    client.getData().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
            }
            break;
        }
    }
}
 
Example 3
Source File: CuratorStateManager.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
@Override
protected <M extends Message> ListenableFuture<M> getNodeData(
    WatchCallback watcher,
    String path,
    final Message.Builder builder) {
  final SettableFuture<M> future = SettableFuture.create();

  Watcher wc = ZkWatcherCallback.makeZkWatcher(watcher);

  BackgroundCallback cb = new BackgroundCallback() {
    @Override
    @SuppressWarnings("unchecked") // we don't know what M is until runtime
    public void processResult(CuratorFramework aClient, CuratorEvent event) throws Exception {
      byte[] data;
      if (event != null & (data = event.getData()) != null) {
        builder.mergeFrom(data);
        safeSetFuture(future, (M) builder.build());
      } else {
        safeSetException(future, new RuntimeException("Failed to fetch data from path: "
            + event.getPath()));
      }
    }
  };

  try {
    client.getData().usingWatcher(wc).inBackground(cb).forPath(path);

    // Suppress it since forPath() throws Exception
    // SUPPRESS CHECKSTYLE IllegalCatch
  } catch (Exception e) {
    safeSetException(future, new RuntimeException(
        "Could not getNodeData using watcher for path: " + path, e));
  }

  return future;
}
 
Example 4
Source File: NodeCache.java    From curator with Apache License 2.0 5 votes vote down vote up
private void processBackgroundResult(CuratorEvent event) throws Exception
{
    switch ( event.getType() )
    {
        case GET_DATA:
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                ChildData childData = new ChildData(path, event.getStat(), event.getData());
                setNewData(childData);
            }
            break;
        }

        case EXISTS:
        {
            if ( event.getResultCode() == KeeperException.Code.NONODE.intValue() )
            {
                setNewData(null);
            }
            else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                if ( dataIsCompressed )
                {
                    client.getData().decompressed().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
                else
                {
                    client.getData().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
            }
            break;
        }
    }
}
 
Example 5
Source File: CuratorAsyncManager.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private <T> Map<String, T> getAsyncThrows(
  final String pathNameForLogs,
  final Collection<String> paths,
  final Transcoder<T> transcoder,
  final Optional<ZkCache<T>> cache
)
  throws Exception {
  final Map<String, T> objects = new HashMap<>(paths.size());

  if (cache.isPresent()) {
    for (Iterator<String> itr = paths.iterator(); itr.hasNext();) {
      final String path = itr.next();
      final Optional<T> fromCache = cache.get().get(path);
      if (fromCache.isPresent()) {
        objects.put(path, fromCache.get());
        itr.remove();
      }
    }
  }

  if (paths.isEmpty()) {
    return objects;
  }

  final Map<String, T> synchronizedObjects = Collections.synchronizedMap(objects);

  final CountDownLatch latch = new CountDownLatch(paths.size());
  final AtomicInteger bytes = new AtomicInteger();

  final BackgroundCallback callback = new BackgroundCallback() {

    @Override
    public void processResult(CuratorFramework client, CuratorEvent event)
      throws Exception {
      try {
        if (event.getData() == null || event.getData().length == 0) {
          LOG.trace("Expected active node {} but it wasn't there", event.getPath());
          return;
        }

        bytes.getAndAdd(event.getData().length);
        final T object = transcoder.fromBytes(event.getData());
        synchronizedObjects.put(event.getPath(), object);

        if (cache.isPresent()) {
          cache.get().set(event.getPath(), object);
        }
      } finally {
        latch.countDown();
      }
    }
  };

  return queryAndReturnResultsThrows(
    objects,
    paths,
    callback,
    latch,
    pathNameForLogs,
    bytes,
    CuratorQueryMethod.GET_DATA
  );
}
 
Example 6
Source File: CuratorAsyncManager.java    From Singularity with Apache License 2.0 4 votes vote down vote up
protected <T> List<T> getAsyncNestedChildrenAsListThrows(
  final String pathNameForLogs,
  final List<String> parentPaths,
  final Transcoder<T> transcoder
)
  throws Exception {
  final List<String> allPaths = new ArrayList<>();
  for (String parent : parentPaths) {
    for (String child : getChildren(parent)) {
      allPaths.add(ZKPaths.makePath(parent, child));
    }
  }

  final List<T> results = new ArrayList<>();
  final CountDownLatch latch = new CountDownLatch(allPaths.size());
  final AtomicInteger bytes = new AtomicInteger();
  final BackgroundCallback callback = new BackgroundCallback() {

    @Override
    public void processResult(CuratorFramework client, CuratorEvent event)
      throws Exception {
      try {
        if (event.getData() == null || event.getData().length == 0) {
          LOG.trace("Expected active node {} but it wasn't there", event.getPath());
          return;
        }
        bytes.getAndAdd(event.getData().length);

        final T object = transcoder.fromBytes(event.getData());

        results.add(object);
      } finally {
        latch.countDown();
      }
    }
  };

  return queryAndReturnResultsThrows(
    results,
    allPaths,
    callback,
    latch,
    pathNameForLogs,
    bytes,
    CuratorQueryMethod.GET_DATA
  );
}
 
Example 7
Source File: CuratorAsyncManager.java    From Singularity with Apache License 2.0 4 votes vote down vote up
protected <T, Q> Map<T, List<Q>> getAsyncNestedChildDataAsMapThrows(
  final String pathNameForLogs,
  final Map<String, T> parentPathsMap,
  final String subpath,
  final Transcoder<Q> transcoder
)
  throws Exception {
  final Map<String, T> allPathsMap = Maps.newHashMap();
  for (Map.Entry<String, T> entry : parentPathsMap.entrySet()) {
    for (String child : getChildren(ZKPaths.makePath(entry.getKey(), subpath))) {
      allPathsMap.put(
        ZKPaths.makePath(entry.getKey(), subpath, child),
        entry.getValue()
      );
    }
  }

  final ConcurrentHashMap<T, List<Q>> resultsMap = new ConcurrentHashMap<>();
  final CountDownLatch latch = new CountDownLatch(allPathsMap.size());
  final AtomicInteger bytes = new AtomicInteger();
  final BackgroundCallback callback = new BackgroundCallback() {

    @Override
    public void processResult(CuratorFramework client, CuratorEvent event)
      throws Exception {
      try {
        if (event.getData() == null || event.getData().length == 0) {
          LOG.trace("Expected active node {} but it wasn't there", event.getPath());
          return;
        }
        bytes.getAndAdd(event.getData().length);

        final Q object = transcoder.fromBytes(event.getData());

        if (allPathsMap.get(event.getPath()) != null) {
          resultsMap.putIfAbsent(allPathsMap.get(event.getPath()), new ArrayList<Q>());
          resultsMap.get(allPathsMap.get(event.getPath())).add(object);
        }
      } finally {
        latch.countDown();
      }
    }
  };

  return queryAndReturnResultsThrows(
    resultsMap,
    allPathsMap.keySet(),
    callback,
    latch,
    pathNameForLogs,
    bytes,
    CuratorQueryMethod.GET_DATA
  );
}