org.apache.helix.manager.zk.zookeeper.ZkClient类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(258)

本文整理了Java中org.apache.helix.manager.zk.zookeeper.ZkClient类的一些代码示例,展示了ZkClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZkClient类的具体详情如下:
包路径:org.apache.helix.manager.zk.zookeeper.ZkClient
类名称:ZkClient

ZkClient介绍

[英]Abstracts the interaction with zookeeper and allows permanent (not just one time) watches on nodes in ZooKeeper. WARN: Do not use this class directly, use org.apache.helix.manager.zk.ZkClient instead.
[中]抽象与zookeeper的交互,并允许在zookeeper中的节点上进行永久(而不仅仅是一次)监视。警告:不要直接使用此类,请使用org。阿帕奇。螺旋。经理zk。而不是我的客户。

代码示例

代码示例来源:origin: apache/helix

  1. public Stat writeDataReturnStat(final String path, Object datat, final int expectedVersion) {
  2. long startT = System.currentTimeMillis();
  3. try {
  4. final byte[] data = serialize(datat, path);
  5. checkDataSizeLimit(data);
  6. final Stat stat = (Stat) retryUntilConnected(new Callable<Object>() {
  7. @Override public Object call() throws Exception {
  8. return getConnection().writeDataReturnStat(path, data, expectedVersion);
  9. }
  10. });
  11. record(path, data, startT, ZkClientMonitor.AccessType.WRITE);
  12. return stat;
  13. } catch (Exception e) {
  14. recordFailure(path, ZkClientMonitor.AccessType.WRITE);
  15. throw e;
  16. } finally {
  17. long endT = System.currentTimeMillis();
  18. if (LOG.isTraceEnabled()) {
  19. LOG.trace("setData, path: " + path + ", time: " + (endT - startT) + " ms");
  20. }
  21. }
  22. }

代码示例来源:origin: apache/helix

  1. /**
  2. * Create an ephemeral node.
  3. *
  4. * @param path
  5. * @throws ZkInterruptedException
  6. * if operation was interrupted, or a required reconnection got interrupted
  7. * @throws IllegalArgumentException
  8. * if called from anything except the ZooKeeper event thread
  9. * @throws ZkException
  10. * if any ZooKeeper exception occurred
  11. * @throws RuntimeException
  12. * if any other exception occurs
  13. */
  14. public void createEphemeral(final String path)
  15. throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
  16. create(path, null, CreateMode.EPHEMERAL);
  17. }

代码示例来源:origin: org.apache.helix/helix-core

  1. /**
  2. * Create a persistent node.
  3. *
  4. * @param path
  5. * @throws ZkInterruptedException
  6. * if operation was interrupted, or a required reconnection got interrupted
  7. * @throws IllegalArgumentException
  8. * if called from anything except the ZooKeeper event thread
  9. * @throws ZkException
  10. * if any ZooKeeper exception occurred
  11. * @throws RuntimeException
  12. * if any other exception occurs
  13. */
  14. public void createPersistent(String path)
  15. throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
  16. createPersistent(path, false);
  17. }

代码示例来源:origin: apache/helix

  1. if (isClosed()) {
  2. throw new IllegalStateException("ZkClient already closed!");
  3. acquireEventLock();
  4. try {
  5. setShutdownTrigger(false);
  6. IZkConnection zkConnection = getConnection();
  7. _eventThread = new ZkEventThread(zkConnection.getServers());
  8. _eventThread.start();
  9. if (isManagingZkConnection()) {
  10. zkConnection.connect(watcher);
  11. LOG.debug("Awaiting connection to Zookeeper server");
  12. if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
  13. throw new ZkTimeoutException(
  14. "Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
  15. if (isConnectionClosed()) {
  16. throw new HelixException(
  17. "Unable to connect to zookeeper server with the specified ZkConnection");
  18. setCurrentState(KeeperState.SyncConnected);
  19. getEventLock().unlock();
  20. close();

代码示例来源:origin: apache/helix

  1. getEventLock().lock();
  2. try {
  3. if (getShutdownTrigger()) {
  4. if (LOG.isDebugEnabled()) {
  5. LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath()
  6. processStateChanged(event);
  7. processDataOrChildChange(event);
  8. getEventLock().getStateChangedCondition().signalAll();
  9. getEventLock().getZNodeEventCondition().signalAll();
  10. getEventLock().getDataChangedCondition().signalAll();
  11. fireAllEvents();
  12. getEventLock().getZNodeEventCondition().signalAll();
  13. getEventLock().getDataChangedCondition().signalAll();
  14. getEventLock().unlock();
  15. recordStateChange(stateChanged, dataChanged);

代码示例来源:origin: apache/helix

  1. LOG.trace("closing a zkclient. callStack: " + Arrays.asList(calls));
  2. getEventLock().lock();
  3. IZkConnection connection = getConnection();
  4. try {
  5. if (connection == null || _closed) {
  6. return;
  7. setShutdownTrigger(true);
  8. _eventThread.interrupt();
  9. _eventThread.join(2000);
  10. if (isManagingZkConnection()) {
  11. LOG.info("Closing zkclient: " + ((ZkConnection) connection).getZookeeper());
  12. connection.close();
  13. setCurrentState(null);
  14. getEventLock().getStateChangedCondition().signalAll();
  15. if (isManagingZkConnection()) {
  16. connection.close();
  17. getEventLock().unlock();
  18. if (_monitor != null) {
  19. _monitor.unregister();

代码示例来源:origin: apache/helix

  1. @Override public Object call() throws Exception {
  2. getConnection().exists(path, true);
  3. return null;
  4. }
  5. });

代码示例来源:origin: org.apache.helix/helix-core

  1. acquireEventLock();
  2. try {
  3. setShutdownTrigger(false);
  4. _eventThread = new ZkEventThread(_connection.getServers());
  5. _eventThread.start();
  6. if (!waitUntilConnected(maxMsToWaitUntilConnected, TimeUnit.MILLISECONDS)) {
  7. throw new ZkTimeoutException(
  8. "Unable to connect to zookeeper server within timeout: " + maxMsToWaitUntilConnected);
  9. getEventLock().unlock();
  10. close();

代码示例来源:origin: apache/helix

  1. public long getCreationTime(String path) {
  2. acquireEventLock();
  3. try {
  4. return getConnection().getCreateTime(path);
  5. } catch (KeeperException e) {
  6. throw ZkException.create(e);
  7. } catch (InterruptedException e) {
  8. throw new ZkInterruptedException(e);
  9. } finally {
  10. getEventLock().unlock();
  11. }
  12. }

代码示例来源:origin: org.apache.helix/helix-core

  1. public boolean waitUntilExists(String path, TimeUnit timeUnit, long time)
  2. throws ZkInterruptedException {
  3. Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time));
  4. if (LOG.isDebugEnabled()) {
  5. LOG.debug("Waiting until znode '" + path + "' becomes available.");
  6. }
  7. if (exists(path)) {
  8. return true;
  9. }
  10. acquireEventLock();
  11. try {
  12. while (!exists(path, true)) {
  13. boolean gotSignal = getEventLock().getZNodeEventCondition().awaitUntil(timeout);
  14. if (!gotSignal) {
  15. return false;
  16. }
  17. }
  18. return true;
  19. } catch (InterruptedException e) {
  20. throw new ZkInterruptedException(e);
  21. } finally {
  22. getEventLock().unlock();
  23. }
  24. }

代码示例来源:origin: apache/helix

  1. private void reconnect() {
  2. getEventLock().lock();
  3. try {
  4. ZkConnection connection = ((ZkConnection) getConnection());
  5. connection.reconnect(this);
  6. } catch (InterruptedException e) {
  7. throw new ZkInterruptedException(e);
  8. } finally {
  9. getEventLock().unlock();
  10. }
  11. }

代码示例来源:origin: apache/helix

  1. public void setCurrentState(KeeperState currentState) {
  2. getEventLock().lock();
  3. try {
  4. _currentState = currentState;
  5. } finally {
  6. getEventLock().unlock();
  7. }
  8. }

代码示例来源:origin: org.apache.helix/helix-core

  1. public boolean waitForKeeperState(KeeperState keeperState, long time, TimeUnit timeUnit)
  2. throws ZkInterruptedException {
  3. if (_zookeeperEventThread != null && Thread.currentThread() == _zookeeperEventThread) {
  4. throw new IllegalArgumentException("Must not be done in the zookeeper event thread.");
  5. }
  6. Date timeout = new Date(System.currentTimeMillis() + timeUnit.toMillis(time));
  7. LOG.debug("Waiting for keeper state " + keeperState);
  8. acquireEventLock();
  9. try {
  10. boolean stillWaiting = true;
  11. while (_currentState != keeperState) {
  12. if (!stillWaiting) {
  13. return false;
  14. }
  15. stillWaiting = getEventLock().getStateChangedCondition().awaitUntil(timeout);
  16. }
  17. LOG.debug("State is " + (_currentState == null ? "CLOSED" : _currentState));
  18. return true;
  19. } catch (InterruptedException e) {
  20. throw new ZkInterruptedException(e);
  21. } finally {
  22. getEventLock().unlock();
  23. }
  24. }

代码示例来源:origin: apache/helix

  1. if (isClosed()) {
  2. throw new IllegalStateException("ZkClient already closed!");
  3. final ZkConnection zkConnection = (ZkConnection) getConnection();
  4. waitForRetry();
  5. } catch (SessionExpiredException e) {
  6. waitForRetry();
  7. } catch (KeeperException e) {
  8. throw ZkException.create(e);

代码示例来源:origin: org.apache.helix/helix-core

  1. LOG.trace("closing a zkclient. callStack: " + Arrays.asList(calls));
  2. getEventLock().lock();
  3. try {
  4. if (_connection == null || _closed) {
  5. setShutdownTrigger(true);
  6. _eventThread.interrupt();
  7. _eventThread.join(2000);
  8. setCurrentState(null);
  9. getEventLock().getStateChangedCondition().signalAll();
  10. getEventLock().unlock();
  11. if (_monitor != null) {
  12. _monitor.unregister();

代码示例来源:origin: org.apache.helix/helix-core

  1. /**
  2. * Delete the path as well as all its children.
  3. * @param path
  4. * @throws HelixException
  5. */
  6. public void deleteRecursively(String path) throws HelixException {
  7. List<String> children;
  8. try {
  9. children = getChildren(path, false);
  10. } catch (ZkNoNodeException e) {
  11. // if the node to be deleted does not exist, treat it as success.
  12. return;
  13. }
  14. for (String subPath : children) {
  15. deleteRecursively(path + "/" + subPath);
  16. }
  17. // delete() function call will return true if successful, false if the path does not
  18. // exist (in this context, it should be treated as successful), and throw exception
  19. // if there is any other failure case.
  20. try {
  21. delete(path);
  22. } catch (Exception e) {
  23. LOG.error("Failed to delete " + path, e);
  24. throw new HelixException("Failed to delete " + path, e);
  25. }
  26. }

代码示例来源:origin: org.apache.helix/helix-core

  1. final ZkConnection zkConnection = (ZkConnection) getConnection();
  2. waitForRetry();
  3. } catch (SessionExpiredException e) {
  4. waitForRetry();
  5. } catch (KeeperException e) {
  6. throw ZkException.create(e);

代码示例来源:origin: apache/helix

  1. throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
  2. try {
  3. create(path, null, acl, CreateMode.PERSISTENT);
  4. } catch (ZkNodeExistsException e) {
  5. if (!createParents) {
  6. createPersistent(parentDir, createParents, acl);
  7. createPersistent(path, createParents, acl);

代码示例来源:origin: apache/helix

  1. /**
  2. * Delete the path as well as all its children.
  3. * This method is deprecated, please use {@link #deleteRecursively(String)}} instead
  4. * @param path ZK path
  5. * @return true if successfully deleted all children, and the given path, else false
  6. */
  7. @Deprecated
  8. public boolean deleteRecursive(String path) {
  9. try {
  10. deleteRecursively(path);
  11. return true;
  12. } catch (HelixException e) {
  13. LOG.error("Failed to recursively delete path " + path, e);
  14. return false;
  15. }
  16. }

代码示例来源:origin: org.apache.helix/helix-core

  1. protected ZkClient(IZkConnection zkConnection, int connectionTimeout, long operationRetryTimeout,
  2. PathBasedZkSerializer zkSerializer, String monitorType, String monitorKey,
  3. String monitorInstanceName, boolean monitorRootPathOnly) {
  4. if (zkConnection == null) {
  5. throw new NullPointerException("Zookeeper connection is null!");
  6. }
  7. _connection = zkConnection;
  8. _pathBasedZkSerializer = zkSerializer;
  9. _operationRetryTimeoutInMillis = operationRetryTimeout;
  10. connect(connectionTimeout, this);
  11. // initiate monitor
  12. try {
  13. if (monitorKey != null && !monitorKey.isEmpty() && monitorType != null && !monitorType
  14. .isEmpty()) {
  15. _monitor =
  16. new ZkClientMonitor(monitorType, monitorKey, monitorInstanceName, monitorRootPathOnly);
  17. _monitor.setZkEventThread(_eventThread);
  18. } else {
  19. LOG.info("ZkClient monitor key or type is not provided. Skip monitoring.");
  20. }
  21. } catch (JMException e) {
  22. LOG.error("Error in creating ZkClientMonitor", e);
  23. }
  24. }

相关文章