Search in sources :

Example 1 with InvalidRecordException

use of org.apache.hadoop.registry.client.exceptions.InvalidRecordException in project hadoop by apache.

the class JsonSerDeser method fromBytes.

/**
   * Deserialize from a byte array, optionally checking for a marker string.
   * <p>
   * If the marker parameter is supplied (and not empty), then its presence
   * will be verified before the JSON parsing takes place; it is a fast-fail
   * check. If not found, an {@link InvalidRecordException} exception will be
   * raised
   * @param path path the data came from
   * @param bytes byte array
   * @param marker an optional string which, if set, MUST be present in the
   * UTF-8 parsed payload.
   * @return The parsed record
   * @throws IOException all problems
   * @throws EOFException not enough data
   * @throws InvalidRecordException if the JSON parsing failed.
   * @throws NoRecordException if the data is not considered a record: either
   * it is too short or it did not contain the marker string.
   */
public T fromBytes(String path, byte[] bytes, String marker) throws IOException, NoRecordException, InvalidRecordException {
    int len = bytes.length;
    if (len == 0) {
        throw new NoRecordException(path, E_NO_DATA);
    }
    if (StringUtils.isNotEmpty(marker) && len < marker.length()) {
        throw new NoRecordException(path, E_DATA_TOO_SHORT);
    }
    String json = new String(bytes, 0, len, UTF_8);
    if (StringUtils.isNotEmpty(marker) && !json.contains(marker)) {
        throw new NoRecordException(path, E_MISSING_MARKER_STRING + marker);
    }
    try {
        return fromJson(json);
    } catch (JsonProcessingException e) {
        throw new InvalidRecordException(path, e.toString(), e);
    }
}
Also used : NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException)

Example 2 with InvalidRecordException

use of org.apache.hadoop.registry.client.exceptions.InvalidRecordException in project hadoop by apache.

the class RegistryTypeUtils method retrieveAddressURLs.

/**
   * Get the address URLs. Guranteed to return at least one address.
   * @param epr endpoint
   * @return the address as a URL
   * @throws InvalidRecordException if the type is wrong, there are no addresses
   * or the payload ill-formatted
   * @throws MalformedURLException address can't be turned into a URL
   */
public static List<URL> retrieveAddressURLs(Endpoint epr) throws InvalidRecordException, MalformedURLException {
    if (epr == null) {
        throw new InvalidRecordException("", "Null endpoint");
    }
    List<String> addresses = retrieveAddressesUriType(epr);
    List<URL> results = new ArrayList<URL>(addresses.size());
    for (String address : addresses) {
        results.add(new URL(address));
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException) URL(java.net.URL)

Example 3 with InvalidRecordException

use of org.apache.hadoop.registry.client.exceptions.InvalidRecordException 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 4 with InvalidRecordException

use of org.apache.hadoop.registry.client.exceptions.InvalidRecordException 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)

Aggregations

InvalidRecordException (org.apache.hadoop.registry.client.exceptions.InvalidRecordException)4 NoRecordException (org.apache.hadoop.registry.client.exceptions.NoRecordException)3 EOFException (java.io.EOFException)2 RegistryPathStatus (org.apache.hadoop.registry.client.types.RegistryPathStatus)2 ServiceRecord (org.apache.hadoop.registry.client.types.ServiceRecord)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PathIsNotEmptyDirectoryException (org.apache.hadoop.fs.PathIsNotEmptyDirectoryException)1 PathNotFoundException (org.apache.hadoop.fs.PathNotFoundException)1