Search in sources :

Example 6 with RegistryPathStatus

use of org.apache.hadoop.registry.client.types.RegistryPathStatus in project hadoop by apache.

the class RegistryOperationsService method stat.

@Override
public RegistryPathStatus stat(String path) throws IOException {
    validatePath(path);
    Stat stat = zkStat(path);
    String name = RegistryPathUtils.lastPathEntry(path);
    RegistryPathStatus status = new RegistryPathStatus(name, stat.getCtime(), stat.getDataLength(), stat.getNumChildren());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Stat {} => {}", path, status);
    }
    return status;
}
Also used : Stat(org.apache.zookeeper.data.Stat) RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus)

Example 7 with RegistryPathStatus

use of org.apache.hadoop.registry.client.types.RegistryPathStatus in project hadoop by apache.

the class RegistryAdminService method purge.

/**
   * Recursive operation to purge all matching records under a base path.
   * <ol>
   *   <li>Uses a depth first search</li>
   *   <li>A match is on ID and persistence policy, or, if policy==-1, any match</li>
   *   <li>If a record matches then it is deleted without any child searches</li>
   *   <li>Deletions will be asynchronous if a callback is provided</li>
   * </ol>
   *
   * The code is designed to be robust against parallel deletions taking place;
   * in such a case it will stop attempting that part of the tree. This
   * avoid the situation of more than 1 purge happening in parallel and
   * one of the purge operations deleteing the node tree above the other.
   * @param path base path
   * @param selector selector for the purge policy
   * @param purgePolicy what to do if there is a matching record with children
   * @param callback optional curator callback
   * @return the number of delete operations perfomed. As deletes may be for
   * everything under a path, this may be less than the number of records
   * actually deleted
   * @throws IOException problems
   * @throws PathIsNotEmptyDirectoryException if an entry cannot be deleted
   * as it has children and the purge policy is FailOnChildren
   */
@VisibleForTesting
public int purge(String path, NodeSelector selector, PurgePolicy purgePolicy, BackgroundCallback callback) throws IOException {
    boolean toDelete = false;
    // look at self to see if it has a service record
    Map<String, RegistryPathStatus> childEntries;
    Collection<RegistryPathStatus> entries;
    try {
        // list this path's children
        childEntries = RegistryUtils.statChildren(this, path);
        entries = childEntries.values();
    } catch (PathNotFoundException e) {
        // exit
        return 0;
    }
    try {
        RegistryPathStatus registryPathStatus = stat(path);
        ServiceRecord serviceRecord = resolve(path);
        // there is now an entry here.
        toDelete = selector.shouldSelect(path, registryPathStatus, serviceRecord);
    } catch (EOFException ignored) {
    // ignore
    } catch (InvalidRecordException ignored) {
    // ignore
    } catch (NoRecordException ignored) {
    // ignore
    } catch (PathNotFoundException e) {
        // exit
        return 0;
    }
    if (toDelete && !entries.isEmpty()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Match on record @ {} with children ", path);
        }
        // there's children
        switch(purgePolicy) {
            case SkipOnChildren:
                // don't do the deletion... continue to next record
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping deletion");
                }
                toDelete = false;
                break;
            case PurgeAll:
                // mark for deletion
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Scheduling for deletion with children");
                }
                toDelete = true;
                entries = new ArrayList<RegistryPathStatus>(0);
                break;
            case FailOnChildren:
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Failing deletion operation");
                }
                throw new PathIsNotEmptyDirectoryException(path);
        }
    }
    int deleteOps = 0;
    if (toDelete) {
        try {
            zkDelete(path, true, callback);
        } catch (PathNotFoundException e) {
            // this is a no-op, and all children can be skipped
            return deleteOps;
        }
        deleteOps++;
    }
    // now go through the children
    for (RegistryPathStatus status : entries) {
        String childname = status.path;
        String childpath = RegistryPathUtils.join(path, childname);
        deleteOps += purge(childpath, selector, purgePolicy, callback);
    }
    return deleteOps;
}
Also used : PathIsNotEmptyDirectoryException(org.apache.hadoop.fs.PathIsNotEmptyDirectoryException) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord) RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) EOFException(java.io.EOFException) NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with RegistryPathStatus

use of org.apache.hadoop.registry.client.types.RegistryPathStatus in project hadoop by apache.

the class TestRegistryOperations method testMkdirNoParent.

@Test
public void testMkdirNoParent() throws Throwable {
    String path = ENTRY_PATH + "/missing";
    try {
        operations.mknode(path, false);
        RegistryPathStatus stat = operations.stat(path);
        fail("Got a status " + stat);
    } catch (PathNotFoundException expected) {
    // expected
    }
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) Test(org.junit.Test) AbstractRegistryTest(org.apache.hadoop.registry.AbstractRegistryTest)

Example 9 with RegistryPathStatus

use of org.apache.hadoop.registry.client.types.RegistryPathStatus in project hadoop by apache.

the class TestRegistryOperations method testListListFully.

@Test
public void testListListFully() throws Throwable {
    ServiceRecord r1 = new ServiceRecord();
    ServiceRecord r2 = createRecord("i", PersistencePolicies.PERMANENT, "r2");
    String path = USERPATH + SC_HADOOP + "/listing";
    operations.mknode(path, true);
    String r1path = path + "/r1";
    operations.bind(r1path, r1, 0);
    String r2path = path + "/r2";
    operations.bind(r2path, r2, 0);
    RegistryPathStatus r1stat = operations.stat(r1path);
    assertEquals("r1", r1stat.path);
    RegistryPathStatus r2stat = operations.stat(r2path);
    assertEquals("r2", r2stat.path);
    assertNotEquals(r1stat, r2stat);
    // listings now
    List<String> list = operations.list(path);
    assertEquals("Wrong no. of children", 2, list.size());
    // there's no order here, so create one
    Map<String, String> names = new HashMap<String, String>();
    String entries = "";
    for (String child : list) {
        names.put(child, child);
        entries += child + " ";
    }
    assertTrue("No 'r1' in " + entries, names.containsKey("r1"));
    assertTrue("No 'r2' in " + entries, names.containsKey("r2"));
    Map<String, RegistryPathStatus> stats = RegistryUtils.statChildren(operations, path);
    assertEquals("Wrong no. of children", 2, stats.size());
    assertEquals(r1stat, stats.get("r1"));
    assertEquals(r2stat, stats.get("r2"));
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) HashMap(java.util.HashMap) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord) Test(org.junit.Test) AbstractRegistryTest(org.apache.hadoop.registry.AbstractRegistryTest)

Example 10 with RegistryPathStatus

use of org.apache.hadoop.registry.client.types.RegistryPathStatus in project hadoop by apache.

the class TestRegistryOperations method testPutNoParent.

@Test
public void testPutNoParent() throws Throwable {
    ServiceRecord record = new ServiceRecord();
    record.set(YarnRegistryAttributes.YARN_ID, "testPutNoParent");
    String path = "/path/without/parent";
    try {
        operations.bind(path, record, 0);
        // didn't get a failure
        // trouble
        RegistryPathStatus stat = operations.stat(path);
        fail("Got a status " + stat);
    } catch (PathNotFoundException expected) {
    // expected
    }
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord) Test(org.junit.Test) AbstractRegistryTest(org.apache.hadoop.registry.AbstractRegistryTest)

Aggregations

RegistryPathStatus (org.apache.hadoop.registry.client.types.RegistryPathStatus)10 AbstractRegistryTest (org.apache.hadoop.registry.AbstractRegistryTest)6 ServiceRecord (org.apache.hadoop.registry.client.types.ServiceRecord)6 Test (org.junit.Test)6 PathNotFoundException (org.apache.hadoop.fs.PathNotFoundException)4 HashMap (java.util.HashMap)3 EOFException (java.io.EOFException)2 PathIsNotEmptyDirectoryException (org.apache.hadoop.fs.PathIsNotEmptyDirectoryException)2 InvalidRecordException (org.apache.hadoop.registry.client.exceptions.InvalidRecordException)2 NoRecordException (org.apache.hadoop.registry.client.exceptions.NoRecordException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 URI (java.net.URI)1 RegistryTypeUtils.inetAddrEndpoint (org.apache.hadoop.registry.client.binding.RegistryTypeUtils.inetAddrEndpoint)1 RegistryTypeUtils.restEndpoint (org.apache.hadoop.registry.client.binding.RegistryTypeUtils.restEndpoint)1 ZKPathDumper (org.apache.hadoop.registry.client.impl.zk.ZKPathDumper)1 Stat (org.apache.zookeeper.data.Stat)1