Search in sources :

Example 1 with RegistryPathStatus

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

the class RegistryUtils method extractServiceRecords.

/**
   * Extract all service records under a list of stat operations...this
   * skips entries that are too short or simply not matching
   * @param operations operation support for fetches
   * @param parentpath path of the parent of all the entries
   * @param stats Collection of stat results
   * @return a possibly empty map of fullpath:record.
   * @throws IOException for any IO Operation that wasn't ignored.
   */
public static Map<String, ServiceRecord> extractServiceRecords(RegistryOperations operations, String parentpath, Collection<RegistryPathStatus> stats) throws IOException {
    Map<String, ServiceRecord> results = new HashMap<String, ServiceRecord>(stats.size());
    for (RegistryPathStatus stat : stats) {
        if (stat.size > ServiceRecord.RECORD_TYPE.length()) {
            // maybe has data
            String path = join(parentpath, stat.path);
            try {
                ServiceRecord serviceRecord = operations.resolve(path);
                results.put(path, serviceRecord);
            } catch (EOFException ignored) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("data too short for {}", path);
                }
            } catch (InvalidRecordException record) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Invalid record at {}", path);
                }
            } catch (NoRecordException record) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No record at {}", path);
                }
            }
        }
    }
    return results;
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) HashMap(java.util.HashMap) EOFException(java.io.EOFException) NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord)

Example 2 with RegistryPathStatus

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

the class RegistryUtils method statChildren.

/**
   * List children of a directory and retrieve their
   * {@link RegistryPathStatus} values.
   * <p>
   * This is not an atomic operation; A child may be deleted
   * during the iteration through the child entries. If this happens,
   * the <code>PathNotFoundException</code> is caught and that child
   * entry ommitted.
   *
   * @param path path
   * @return a possibly empty map of child entries listed by
   * their short name.
   * @throws PathNotFoundException path is not in the registry.
   * @throws InvalidPathnameException the path is invalid.
   * @throws IOException Any other IO Exception
   */
public static Map<String, RegistryPathStatus> statChildren(RegistryOperations registryOperations, String path) throws PathNotFoundException, InvalidPathnameException, IOException {
    List<String> childNames = registryOperations.list(path);
    Map<String, RegistryPathStatus> results = new HashMap<String, RegistryPathStatus>();
    for (String childName : childNames) {
        String child = join(path, childName);
        try {
            RegistryPathStatus stat = registryOperations.stat(child);
            results.put(childName, stat);
        } catch (PathNotFoundException pnfe) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("stat failed on {}: moved? {}", child, pnfe, pnfe);
            }
        // and continue
        }
    }
    return results;
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) HashMap(java.util.HashMap) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException)

Example 3 with RegistryPathStatus

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

the class TestRegistryOperations method testStat.

@Test
public void testStat() throws Throwable {
    putExampleServiceEntry(ENTRY_PATH, 0);
    RegistryPathStatus stat = operations.stat(ENTRY_PATH);
    assertTrue(stat.size > 0);
    assertTrue(stat.time > 0);
    assertEquals(NAME, stat.path);
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) Test(org.junit.Test) AbstractRegistryTest(org.apache.hadoop.registry.AbstractRegistryTest)

Example 4 with RegistryPathStatus

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

the class TestRegistryOperations method testLsParent.

@Test
public void testLsParent() throws Throwable {
    ServiceRecord written = putExampleServiceEntry(ENTRY_PATH, 0);
    RegistryPathStatus stat = operations.stat(ENTRY_PATH);
    List<String> children = operations.list(PARENT_PATH);
    assertEquals(1, children.size());
    assertEquals(NAME, children.get(0));
    Map<String, RegistryPathStatus> childStats = RegistryUtils.statChildren(operations, PARENT_PATH);
    assertEquals(1, childStats.size());
    assertEquals(stat, childStats.get(NAME));
    Map<String, ServiceRecord> records = RegistryUtils.extractServiceRecords(operations, PARENT_PATH, childStats.values());
    assertEquals(1, records.size());
    ServiceRecord record = records.get(ENTRY_PATH);
    RegistryTypeUtils.validateServiceRecord(ENTRY_PATH, record);
    assertMatches(written, record);
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord) Test(org.junit.Test) AbstractRegistryTest(org.apache.hadoop.registry.AbstractRegistryTest)

Example 5 with RegistryPathStatus

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

the class TestRegistryRMOperations method testCreateComplexApplication.

/**
   * Create a complex example app
   * @throws Throwable
   */
@Test
public void testCreateComplexApplication() throws Throwable {
    String appId = "application_1408631738011_0001";
    String cid = "container_1408631738011_0001_01_";
    String cid1 = cid + "000001";
    String cid2 = cid + "000002";
    String appPath = USERPATH + "tomcat";
    ServiceRecord webapp = createRecord(appId, PersistencePolicies.APPLICATION, "tomcat-based web application", null);
    webapp.addExternalEndpoint(restEndpoint("www", new URI("http", "//loadbalancer/", null)));
    ServiceRecord comp1 = createRecord(cid1, PersistencePolicies.CONTAINER, null, null);
    comp1.addExternalEndpoint(restEndpoint("www", new URI("http", "//rack4server3:43572", null)));
    comp1.addInternalEndpoint(inetAddrEndpoint("jmx", "JMX", "rack4server3", 43573));
    // Component 2 has a container lifespan
    ServiceRecord comp2 = createRecord(cid2, PersistencePolicies.CONTAINER, null, null);
    comp2.addExternalEndpoint(restEndpoint("www", new URI("http", "//rack1server28:35881", null)));
    comp2.addInternalEndpoint(inetAddrEndpoint("jmx", "JMX", "rack1server28", 35882));
    operations.mknode(USERPATH, false);
    operations.bind(appPath, webapp, BindFlags.OVERWRITE);
    String componentsPath = appPath + RegistryConstants.SUBPATH_COMPONENTS;
    operations.mknode(componentsPath, false);
    String dns1 = RegistryPathUtils.encodeYarnID(cid1);
    String dns1path = componentsPath + dns1;
    operations.bind(dns1path, comp1, BindFlags.CREATE);
    String dns2 = RegistryPathUtils.encodeYarnID(cid2);
    String dns2path = componentsPath + dns2;
    operations.bind(dns2path, comp2, BindFlags.CREATE);
    ZKPathDumper pathDumper = registry.dumpPath(false);
    LOG.info(pathDumper.toString());
    logRecord("tomcat", webapp);
    logRecord(dns1, comp1);
    logRecord(dns2, comp2);
    ServiceRecord dns1resolved = operations.resolve(dns1path);
    assertEquals("Persistence policies on resolved entry", PersistencePolicies.CONTAINER, dns1resolved.get(YarnRegistryAttributes.YARN_PERSISTENCE, ""));
    Map<String, RegistryPathStatus> children = RegistryUtils.statChildren(operations, componentsPath);
    assertEquals(2, children.size());
    Collection<RegistryPathStatus> componentStats = children.values();
    Map<String, ServiceRecord> records = RegistryUtils.extractServiceRecords(operations, componentsPath, componentStats);
    assertEquals(2, records.size());
    ServiceRecord retrieved1 = records.get(dns1path);
    logRecord(retrieved1.get(YarnRegistryAttributes.YARN_ID, ""), retrieved1);
    assertMatches(dns1resolved, retrieved1);
    assertEquals(PersistencePolicies.CONTAINER, retrieved1.get(YarnRegistryAttributes.YARN_PERSISTENCE, ""));
    // create a listing under components/
    operations.mknode(componentsPath + "subdir", false);
    // this shows up in the listing of child entries
    Map<String, RegistryPathStatus> childrenUpdated = RegistryUtils.statChildren(operations, componentsPath);
    assertEquals(3, childrenUpdated.size());
    // the non-record child this is not picked up in the record listing
    Map<String, ServiceRecord> recordsUpdated = RegistryUtils.extractServiceRecords(operations, componentsPath, childrenUpdated);
    assertEquals(2, recordsUpdated.size());
    // now do some deletions.
    // synchronous delete container ID 2
    // fail if the app policy is chosen
    assertEquals(0, purge("/", cid2, PersistencePolicies.APPLICATION, RegistryAdminService.PurgePolicy.FailOnChildren));
    // succeed for container
    assertEquals(1, purge("/", cid2, PersistencePolicies.CONTAINER, RegistryAdminService.PurgePolicy.FailOnChildren));
    assertPathNotFound(dns2path);
    assertPathExists(dns1path);
    // expect a skip on children to skip
    assertEquals(0, purge("/", appId, PersistencePolicies.APPLICATION, RegistryAdminService.PurgePolicy.SkipOnChildren));
    assertPathExists(appPath);
    assertPathExists(dns1path);
    // attempt to delete app with policy of fail on children
    try {
        int p = purge("/", appId, PersistencePolicies.APPLICATION, RegistryAdminService.PurgePolicy.FailOnChildren);
        fail("expected a failure, got a purge count of " + p);
    } catch (PathIsNotEmptyDirectoryException expected) {
    // expected
    }
    assertPathExists(appPath);
    assertPathExists(dns1path);
    // now trigger recursive delete
    assertEquals(1, purge("/", appId, PersistencePolicies.APPLICATION, RegistryAdminService.PurgePolicy.PurgeAll));
    assertPathNotFound(appPath);
    assertPathNotFound(dns1path);
}
Also used : RegistryPathStatus(org.apache.hadoop.registry.client.types.RegistryPathStatus) ZKPathDumper(org.apache.hadoop.registry.client.impl.zk.ZKPathDumper) PathIsNotEmptyDirectoryException(org.apache.hadoop.fs.PathIsNotEmptyDirectoryException) URI(java.net.URI) RegistryTypeUtils.restEndpoint(org.apache.hadoop.registry.client.binding.RegistryTypeUtils.restEndpoint) RegistryTypeUtils.inetAddrEndpoint(org.apache.hadoop.registry.client.binding.RegistryTypeUtils.inetAddrEndpoint) 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