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

The following examples show how to use org.apache.zookeeper.data.Stat#getEphemeralOwner() . 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: ZooKeeperMigrator.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private DataStatAclNode retrieveNode(ZooKeeper zooKeeper, String path) {
    Preconditions.checkNotNull(zooKeeper, "ZooKeeper client must not be null");
    Preconditions.checkNotNull(path, "path must not be null");
    final Stat stat = new Stat();
    final byte[] data;
    final List<ACL> acls;
    final long ephemeralOwner;
    try {
        data = zooKeeper.getData(path, false, stat);
        acls = zooKeeper.getACL(path, stat);
        ephemeralOwner = stat.getEphemeralOwner();
    } catch (InterruptedException | KeeperException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException(String.format("unable to get data, ACLs, and stats from %s for node at path %s", zooKeeper, path), e);
    }
    return new DataStatAclNode(path, data, stat, acls, ephemeralOwner);
}
 
Example 2
Source File: ZooKeeperMigrator.java    From nifi with Apache License 2.0 6 votes vote down vote up
private DataStatAclNode retrieveNode(ZooKeeper zooKeeper, String path) {
    Preconditions.checkNotNull(zooKeeper, "ZooKeeper client must not be null");
    Preconditions.checkNotNull(path, "path must not be null");
    final Stat stat = new Stat();
    final byte[] data;
    final List<ACL> acls;
    final long ephemeralOwner;
    try {
        data = zooKeeper.getData(path, false, stat);
        acls = zooKeeper.getACL(path, stat);
        ephemeralOwner = stat.getEphemeralOwner();
    } catch (InterruptedException | KeeperException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException(String.format("unable to get data, ACLs, and stats from %s for node at path %s", zooKeeper, path), e);
    }
    return new DataStatAclNode(path, data, stat, acls, ephemeralOwner);
}
 
Example 3
Source File: MockAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public List<HelixProperty.Stat> getPropertyStats(List<PropertyKey> keys) {
  if (keys == null || keys.size() == 0) {
    return Collections.emptyList();
  }

  List<HelixProperty.Stat> propertyStats = new ArrayList<>(keys.size());
  List<String> paths = new ArrayList<>(keys.size());
  for (PropertyKey key : keys) {
    paths.add(key.getPath());
  }
  Stat[] zkStats = _baseDataAccessor.getStats(paths, 0);

  for (int i = 0; i < keys.size(); i++) {
    Stat zkStat = zkStats[i];
    HelixProperty.Stat propertyStat = null;
    if (zkStat != null) {
      propertyStat =
          new HelixProperty.Stat(zkStat.getVersion(), zkStat.getCtime(), zkStat.getMtime(),
              zkStat.getEphemeralOwner());
    }
    propertyStats.add(propertyStat);
  }

  return propertyStats;
}
 
Example 4
Source File: MockAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  try {
    Stat stat = _baseDataAccessor.getStat(path, 0);
    if (stat != null) {
      return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime(),
          stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {

  }

  return null;
}
 
Example 5
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public List<HelixProperty.Stat> getPropertyStats(List<PropertyKey> keys) {
  if (keys == null || keys.size() == 0) {
    return Collections.emptyList();
  }

  List<HelixProperty.Stat> propertyStats = new ArrayList<>(keys.size());
  List<String> paths = new ArrayList<>(keys.size());
  for (PropertyKey key : keys) {
    paths.add(key.getPath());
  }
  Stat[] zkStats = _baseDataAccessor.getStats(paths, 0);

  for (int i = 0; i < keys.size(); i++) {
    Stat zkStat = zkStats[i];
    HelixProperty.Stat propertyStat = null;
    if (zkStat != null) {
      propertyStat =
          new HelixProperty.Stat(zkStat.getVersion(), zkStat.getCtime(), zkStat.getMtime(), zkStat.getEphemeralOwner());
    }
    propertyStats.add(propertyStat);
  }

  return propertyStats;
}
 
Example 6
Source File: ZKHelixDataAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
  PropertyType type = key.getType();
  String path = key.getPath();
  int options = constructOptions(type);
  try {
    Stat stat = _baseDataAccessor.getStat(path, options);
    if (stat != null) {
      return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime(), stat.getEphemeralOwner());
    }
  } catch (ZkNoNodeException e) {

  }

  return null;
}
 
Example 7
Source File: LocalZooKeeperConnectionService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static String createIfAbsent(ZooKeeper zk, String path, byte[] data, CreateMode createMode, boolean gc)
        throws KeeperException, InterruptedException {
    String pathCreated = null;
    try {
        pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
    } catch (NodeExistsException e) {
        // OK
        LOG.debug("Create skipped for existing znode: path={}", path);
    }
    // reset if what exists is the ephemeral garbage.
    if (gc && (pathCreated == null) && CreateMode.EPHEMERAL.equals(createMode)) {
        Stat stat = zk.exists(path, false);
        if (stat != null && zk.getSessionId() != stat.getEphemeralOwner()) {
            deleteIfExists(zk, path, -1);
            pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
        }
    }
    return pathCreated;
}
 
Example 8
Source File: SimpleLoadManagerImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private long getBrokerZnodeOwner() {
    try {
        Stat stat = new Stat();
        pulsar.getZkClient().getData(brokerZnodePath, false, stat);
        return stat.getEphemeralOwner();
    } catch (Exception e) {
        log.warn("Failed to get stat of {}", brokerZnodePath, e);
    }
    return 0;
}
 
Example 9
Source File: ZkUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the provided <i>path</i> exists or not and wait it expired if possible.
 *
 * @param zk the zookeeper client instance
 * @param path the zookeeper path
 * @param sessionTimeoutMs session timeout in milliseconds
 * @return true if path exists, otherwise return false
 * @throws KeeperException when failed to access zookeeper
 * @throws InterruptedException interrupted when waiting for znode to be expired
 */
public static boolean checkNodeAndWaitExpired(ZooKeeper zk,
                                              String path,
                                              long sessionTimeoutMs) throws KeeperException, InterruptedException {
    final CountDownLatch prevNodeLatch = new CountDownLatch(1);
    Watcher zkPrevNodeWatcher = watchedEvent -> {
        // check for prev node deletion.
        if (EventType.NodeDeleted == watchedEvent.getType()) {
            prevNodeLatch.countDown();
        }
    };
    Stat stat = zk.exists(path, zkPrevNodeWatcher);
    if (null != stat) {
        // if the ephemeral owner isn't current zookeeper client
        // wait for it to be expired
        if (stat.getEphemeralOwner() != zk.getSessionId()) {
            log.info("Previous znode : {} still exists, so waiting {} ms for znode deletion",
                path, sessionTimeoutMs);
            if (!prevNodeLatch.await(sessionTimeoutMs, TimeUnit.MILLISECONDS)) {
                throw new NodeExistsException(path);
            } else {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}
 
Example 10
Source File: NodeReader.java    From zkcopy with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        if (failed.get()) {
            return;
        }
        ReaderThread thread = (ReaderThread) Thread.currentThread();
        ZooKeeper zk = thread.getZooKeeper();
        Stat stat = new Stat();
        String path = znode.getAbsolutePath();
        LOGGER.debug("Reading node " + path);
        byte[] data = zk.getData(path, false, stat);
        if (stat.getEphemeralOwner() != 0) {
            znode.setEphemeral(true);
        }
        znode.setData(data);
        znode.setMtime(stat.getMtime());
        List<String> children = zk.getChildren(path, false);
        for (String child : children) {
            if ("zookeeper".equals(child)) {
                // reserved
                continue;
            }
            Node zchild = new Node(znode, child);
            znode.appendChild(zchild);
            pool.execute(new NodeReader(pool, zchild, totalCounter, processedCounter, failed));
        }
    } catch (KeeperException | InterruptedException e) {
        LOGGER.error("Could not read from remote server", e);
        failed.set(true);
    } finally {
        processedCounter.incrementAndGet();
    }
}
 
Example 11
Source File: MiniZKFCCluster.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Expire the ZK session of the given service. This requires
 * (and asserts) that the given service be the current active.
 * @throws NoNodeException if no service holds the lock
 */
public void expireActiveLockHolder(int idx)
    throws NoNodeException {
  Stat stat = new Stat();
  byte[] data = zks.getZKDatabase().getData(
      DummyZKFC.LOCK_ZNODE, stat, null);
  
  assertArrayEquals(Ints.toByteArray(svcs[idx].index), data);
  long session = stat.getEphemeralOwner();
  LOG.info("Expiring svc " + idx + "'s zookeeper session " + session);
  zks.closeSession(session);
}
 
Example 12
Source File: MiniZKFCCluster.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Expire the ZK session of the given service. This requires
 * (and asserts) that the given service be the current active.
 * @throws NoNodeException if no service holds the lock
 */
public void expireActiveLockHolder(int idx)
    throws NoNodeException {
  Stat stat = new Stat();
  byte[] data = zks.getZKDatabase().getData(
      DummyZKFC.LOCK_ZNODE, stat, null);
  
  assertArrayEquals(Ints.toByteArray(svcs[idx].index), data);
  long session = stat.getEphemeralOwner();
  LOG.info("Expiring svc " + idx + "'s zookeeper session " + session);
  zks.closeSession(session);
}
 
Example 13
Source File: ZKUtils.java    From Decision with Apache License 2.0 5 votes vote down vote up
private int removeOldChildZnodes(String path) throws Exception {

            int counter = 0;
            Iterator<String> children = client.getChildren().forPath(path).iterator();

            while (children.hasNext()) {

                String childrenPath = children.next();
                if (!STREAMING.ZK_HIGH_AVAILABILITY_NODE.equals('/'+childrenPath) && !STREAMING.ZK_PERSISTENCE_NODE.equals('/'+childrenPath)) {
                    if (client.getChildren().forPath(path + "/" + childrenPath).size() > 0) {
                        counter += removeOldChildZnodes(path + "/" + childrenPath);
                    } else {

                        Stat znode = client.checkExists().forPath(path + "/" + childrenPath);
                        Boolean deleteNode = true;
                        // avoid nulls and ephemeral znodes
                        if (znode != null && znode.getEphemeralOwner() == 0) {

                            String parentPath = path.substring(path.lastIndexOf("/") +1);

                            if (STREAM_OPERATIONS.SyncOperations.getAckOperations().contains(parentPath)) {

                                if ( new Date().getTime() - znode.getMtime() < MAX_LIVE_FOR_OPERATION_NODE) {
                                    deleteNode = false;
                                }
                            }

                            if (deleteNode) {
                                client.delete().deletingChildrenIfNeeded().forPath(path + "/" + childrenPath);
                                counter++;
                            }
                        }

                    }
                }
            }

            return counter;
        }
 
Example 14
Source File: ZkService.java    From binlake with Apache License 2.0 5 votes vote down vote up
/**
 * 检查是否还有临时节点
 *
 * @param host
 * @param port
 * @return
 * @throws Exception
 */
public boolean checkChildSENodeExist(String host, int port) throws Exception {
    String key = ApiCenter.makeZNodePath(host, port + "");
    List<String> childList = client.getChildren().forPath(zkPath + key);
    for (String child : childList) {
        Stat stat = client.checkExists().forPath(zkPath + key + "/" + child);
        if (stat != null && stat.getEphemeralOwner() > 0) {
            return true;
        }
    }

    return false;
}
 
Example 15
Source File: Backup.java    From zoocreeper with Apache License 2.0 5 votes vote down vote up
private void doBackup(ZooKeeper zk, JsonGenerator jgen, String path)
        throws KeeperException, InterruptedException, IOException {
    try {
        final Stat stat = new Stat();
        List<ACL> acls = nullToEmpty(zk.getACL(path, stat));
        if (stat.getEphemeralOwner() != 0 && !options.backupEphemeral) {
            LOGGER.debug("Skipping ephemeral node: {}", path);
            return;
        }

        final Stat dataStat = new Stat();
        byte[] data = zk.getData(path, false, dataStat);
        for (int i = 0; stat.compareTo(dataStat) != 0 && i < options.numRetries; i++) {
            LOGGER.warn("Retrying getACL / getData to read consistent state");
            acls = zk.getACL(path, stat);
            data = zk.getData(path, false, dataStat);
        }
        if (stat.compareTo(dataStat) != 0) {
            throw new IllegalStateException("Unable to read consistent data for znode: " + path);
        }
        LOGGER.debug("Backing up node: {}", path);
        dumpNode(jgen, path, stat, acls, data);
        final List<String> childPaths = nullToEmpty(zk.getChildren(path, false, null));
        Collections.sort(childPaths);
        for (String childPath : childPaths) {
            final String fullChildPath = createFullPath(path, childPath);
            if (!this.options.isPathExcluded(LOGGER, fullChildPath)) {
                if (this.options.isPathIncluded(LOGGER, fullChildPath)) {
                    doBackup(zk, jgen, fullChildPath);
                }
            }
        }
    } catch (NoNodeException e) {
        LOGGER.warn("Node disappeared during backup: {}", path);
    }
}
 
Example 16
Source File: SolrCLIZkUtilsTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static boolean isEphemeral(String zkPath) throws KeeperException, InterruptedException {
  Stat znodeStat = zkClient.exists(zkPath, null, true);
  return znodeStat.getEphemeralOwner() != 0;
}
 
Example 17
Source File: ZkMaintenanceUtils.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static boolean isEphemeral(SolrZkClient zkClient, String zkPath) throws KeeperException, InterruptedException {
  Stat znodeStat = zkClient.exists(zkPath, null, true);
  return znodeStat.getEphemeralOwner() != 0;
}
 
Example 18
Source File: ZooKeeperCache.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public boolean checkRegNodeAndWaitExpired(String regPath) throws IOException {
    final CountDownLatch prevNodeLatch = new CountDownLatch(1);
    Watcher zkPrevRegNodewatcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            // Check for prev znode deletion. Connection expiration is
            // not handling, since bookie has logic to shutdown.
            if (EventType.NodeDeleted == event.getType()) {
                prevNodeLatch.countDown();
            }
        }
    };
    try {
        Stat stat = getZooKeeper().exists(regPath, zkPrevRegNodewatcher);
        if (null != stat) {
            // if the ephemeral owner isn't current zookeeper client
            // wait for it to be expired.
            if (stat.getEphemeralOwner() != getZooKeeper().getSessionId()) {
                log.info("Previous bookie registration znode: {} exists, so waiting zk sessiontimeout:"
                    + " {} ms for znode deletion", regPath, getZooKeeper().getSessionTimeout());
                // waiting for the previous bookie reg znode deletion
                if (!prevNodeLatch.await(getZooKeeper().getSessionTimeout(), TimeUnit.MILLISECONDS)) {
                    throw new NodeExistsException(regPath);
                } else {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    } catch (KeeperException ke) {
        log.error("ZK exception checking and wait ephemeral znode {} expired : ", regPath, ke);
        throw new IOException("ZK exception checking and wait ephemeral znode "
            + regPath + " expired", ke);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        log.error("Interrupted checking and wait ephemeral znode {} expired : ", regPath, ie);
        throw new IOException("Interrupted checking and wait ephemeral znode "
            + regPath + " expired", ie);
    }
}
 
Example 19
Source File: ZKPathDumper.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively expand the path into the supplied string builder, increasing
 * the indentation by {@link #INDENT} as it proceeds (depth first) down
 * the tree
 * @param builder string build to append to
 * @param path path to examine
 * @param indent current indentation
 */
private void expand(StringBuilder builder,
    String path,
    int indent) {
  try {
    GetChildrenBuilder childrenBuilder = curator.getChildren();
    List<String> children = childrenBuilder.forPath(path);
    for (String child : children) {
      String childPath = path + "/" + child;
      String body;
      Stat stat = curator.checkExists().forPath(childPath);
      StringBuilder bodyBuilder = new StringBuilder(256);
      bodyBuilder.append("  [")
                        .append(stat.getDataLength())
                        .append("]");
      if (stat.getEphemeralOwner() > 0) {
        bodyBuilder.append("*");
      }
      if (verbose) {
        // verbose: extract ACLs
        builder.append(" -- ");
        List<ACL> acls =
            curator.getACL().forPath(childPath);
        for (ACL acl : acls) {
          builder.append(RegistrySecurity.aclToString(acl));
          builder.append(" ");
        }
      }
      body = bodyBuilder.toString();
      // print each child
      append(builder, indent, ' ');
      builder.append('/').append(child);
      builder.append(body);
      builder.append('\n');
      // recurse
      expand(builder, childPath, indent + INDENT);
    }
  } catch (Exception e) {
    builder.append(e.toString()).append("\n");
  }
}
 
Example 20
Source File: ZKPathDumper.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively expand the path into the supplied string builder, increasing
 * the indentation by {@link #INDENT} as it proceeds (depth first) down
 * the tree
 * @param builder string build to append to
 * @param path path to examine
 * @param indent current indentation
 */
private void expand(StringBuilder builder,
    String path,
    int indent) {
  try {
    GetChildrenBuilder childrenBuilder = curator.getChildren();
    List<String> children = childrenBuilder.forPath(path);
    for (String child : children) {
      String childPath = path + "/" + child;
      String body;
      Stat stat = curator.checkExists().forPath(childPath);
      StringBuilder bodyBuilder = new StringBuilder(256);
      bodyBuilder.append("  [")
                        .append(stat.getDataLength())
                        .append("]");
      if (stat.getEphemeralOwner() > 0) {
        bodyBuilder.append("*");
      }
      if (verbose) {
        // verbose: extract ACLs
        builder.append(" -- ");
        List<ACL> acls =
            curator.getACL().forPath(childPath);
        for (ACL acl : acls) {
          builder.append(RegistrySecurity.aclToString(acl));
          builder.append(" ");
        }
      }
      body = bodyBuilder.toString();
      // print each child
      append(builder, indent, ' ');
      builder.append('/').append(child);
      builder.append(body);
      builder.append('\n');
      // recurse
      expand(builder, childPath, indent + INDENT);
    }
  } catch (Exception e) {
    builder.append(e.toString()).append("\n");
  }
}