Search in sources :

Example 1 with MetacatException

use of com.netflix.metacat.common.exception.MetacatException in project metacat by Netflix.

the class DruidConnectorTableService method get.

/**
 * {@inheritDoc}
 */
@Override
public TableInfo get(@Nonnull final ConnectorRequestContext context, @Nonnull final QualifiedName name) {
    log.debug("Get table metadata for qualified name {} for request {}", name, context);
    try {
        final ObjectNode node = this.druidClient.getLatestDataByName(name.getTableName());
        final DataSource dataSource = DruidConverterUtil.getDatasourceFromLatestSegmentJsonObject(node);
        return this.druidConnectorInfoConverter.getTableInfoFromDatasource(dataSource);
    } catch (MetacatException e) {
        log.error(String.format("Table %s not found.", name), e);
        throw new TableNotFoundException(name);
    } catch (HttpClientErrorException e) {
        log.error(String.format("Failed getting table %s.", name), e);
        if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
            throw new TableNotFoundException(name);
        } else {
            throw new InvalidMetadataException(String.format("Invalid table %s. %s", name, e.getMessage()));
        }
    }
}
Also used : TableNotFoundException(com.netflix.metacat.common.server.connectors.exception.TableNotFoundException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MetacatException(com.netflix.metacat.common.exception.MetacatException) InvalidMetadataException(com.netflix.metacat.common.server.connectors.exception.InvalidMetadataException) DataSource(com.netflix.metacat.connector.druid.converter.DataSource)

Example 2 with MetacatException

use of com.netflix.metacat.common.exception.MetacatException in project metacat by Netflix.

the class RequestWrapper method processRequest.

/**
 * Simple request wrapper to process request.
 *
 * @param resourceRequestName request name
 * @param supplier            supplier
 * @param <R>                 response
 * @return response of the supplier
 */
public <R> R processRequest(final String resourceRequestName, final Supplier<R> supplier) {
    final long start = registry.clock().wallTime();
    final Map<String, String> tags = Maps.newHashMap();
    tags.put("request", resourceRequestName);
    registry.counter(requestCounterId.withTags(tags)).increment();
    try {
        log.info("### Calling method: {}", resourceRequestName);
        return supplier.get();
    } catch (UnsupportedOperationException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatNotSupportedException("Catalog does not support the operation. " + e.getMessage());
    } catch (IllegalArgumentException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatBadRequestException(String.format("%s.%s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage()));
    } catch (Exception e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s failed.", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName);
        log.error(message, e);
        if (e instanceof MetacatException) {
            throw e;
        } else {
            throw new MetacatException(message, e);
        }
    } finally {
        final long duration = registry.clock().wallTime() - start;
        log.info("### Time taken to complete {} is {} ms", resourceRequestName, duration);
        this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
    }
}
Also used : MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) MetacatException(com.netflix.metacat.common.exception.MetacatException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatException(com.netflix.metacat.common.exception.MetacatException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) MetacatUserMetadataException(com.netflix.metacat.common.exception.MetacatUserMetadataException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) PartitionAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) MetacatTooManyRequestsException(com.netflix.metacat.common.exception.MetacatTooManyRequestsException)

Example 3 with MetacatException

use of com.netflix.metacat.common.exception.MetacatException in project metacat by Netflix.

the class RequestWrapper method processRequest.

/**
 * Request wrapper to to process request.
 *
 * @param name                name
 * @param resourceRequestName request name
 * @param requestTags         tags that needs to be added to the registry
 * @param supplier            supplier
 * @param <R>                 response
 * @return response of supplier
 */
public <R> R processRequest(final QualifiedName name, final String resourceRequestName, final Map<String, String> requestTags, final Supplier<R> supplier) {
    final long start = registry.clock().wallTime();
    final Map<String, String> tags = new HashMap<>(name.parts());
    if (requestTags != null) {
        tags.putAll(requestTags);
    }
    tags.put("request", resourceRequestName);
    tags.put("scheme", MetacatContextManager.getContext().getScheme());
    registry.counter(requestCounterId.withTags(tags)).increment();
    try {
        // check rate limit in try-catch block in case ratelimiter throws exception.
        // those exceptions can be tracked correctly, by the existing finally block that logs metrics.
        checkRequestRateLimit(name, resourceRequestName, tags);
        log.info("### Calling method: {} for {}", resourceRequestName, name);
        return supplier.get();
    } catch (UnsupportedOperationException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatNotSupportedException("Catalog does not support the operation. " + e.getMessage());
    } catch (DatabaseAlreadyExistsException | TableAlreadyExistsException | PartitionAlreadyExistsException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatAlreadyExistsException(e.getMessage());
    } catch (NotFoundException | MetacatNotFoundException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatNotFoundException(String.format("Unable to locate for %s. Details: %s", name, e.getMessage()));
    } catch (InvalidMetaException | IllegalArgumentException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatBadRequestException(String.format("%s.%s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage()));
    } catch (TablePreconditionFailedException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatPreconditionFailedException(String.format("%s.%s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage()));
    } catch (ConnectorException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        log.error(message, e);
        for (Throwable ex : Throwables.getCausalChain(e)) {
            if (ex.getMessage().contains("too many connections") || ex.getMessage().contains("Timeout: Pool empty")) {
                throw new MetacatTooManyRequestsException(ex.getMessage());
            }
        }
        throw new MetacatException(message, e);
    } catch (UserMetadataServiceException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s usermetadata operation failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        throw new MetacatUserMetadataException(message);
    } catch (Exception e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        log.error(message, e);
        if (e instanceof MetacatException) {
            throw e;
        } else {
            throw new MetacatException(message, e);
        }
    } finally {
        final long duration = registry.clock().wallTime() - start;
        log.info("### Time taken to complete {} for {} is {} ms", resourceRequestName, name, duration);
        tryAddTableTypeTag(tags, name);
        this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) HashMap(java.util.HashMap) MetacatException(com.netflix.metacat.common.exception.MetacatException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) PartitionAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException) MetacatUserMetadataException(com.netflix.metacat.common.exception.MetacatUserMetadataException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) MetacatTooManyRequestsException(com.netflix.metacat.common.exception.MetacatTooManyRequestsException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) MetacatException(com.netflix.metacat.common.exception.MetacatException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) MetacatUserMetadataException(com.netflix.metacat.common.exception.MetacatUserMetadataException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) PartitionAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) MetacatTooManyRequestsException(com.netflix.metacat.common.exception.MetacatTooManyRequestsException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException)

Example 4 with MetacatException

use of com.netflix.metacat.common.exception.MetacatException in project metacat by Netflix.

the class RequestWrapper method processRequest.

/**
 * Request wrapper to to process request.
 *
 * @param name                name
 * @param resourceRequestName request name
 * @param supplier            supplier
 * @param <R>                 response
 * @return response of supplier
 */
public <R> R processRequest(final QualifiedName name, final String resourceRequestName, final Supplier<R> supplier) {
    final long start = registry.clock().wallTime();
    final Map<String, String> tags = new HashMap<>(name.parts());
    tags.put("request", resourceRequestName);
    registry.counter(requestCounterId.withTags(tags)).increment();
    try {
        log.info("### Calling method: {} for {}", resourceRequestName, name);
        return supplier.get();
    } catch (UnsupportedOperationException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatNotSupportedException("Catalog does not support the operation");
    } catch (DatabaseAlreadyExistsException | TableAlreadyExistsException | PartitionAlreadyExistsException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatAlreadyExistsException(e.getMessage());
    } catch (NotFoundException | MetacatNotFoundException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatNotFoundException(String.format("Unable to locate for %s. Details: %s", name, e.getMessage()));
    } catch (InvalidMetaException | IllegalArgumentException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatBadRequestException(String.format("%s.%s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage()));
    } catch (TablePreconditionFailedException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        log.error(e.getMessage(), e);
        throw new MetacatPreconditionFailedException(String.format("%s.%s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage()));
    } catch (ConnectorException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        log.error(message, e);
        throw new MetacatException(message, e);
    } catch (UserMetadataServiceException e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s usermetadata operation failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        throw new MetacatUserMetadataException(message);
    } catch (Exception e) {
        collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
        final String message = String.format("%s.%s -- %s failed for %s", e.getMessage(), e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
        log.error(message, e);
        throw new MetacatException(message, e);
    } finally {
        final long duration = registry.clock().wallTime() - start;
        log.info("### Time taken to complete {} for {} is {} ms", resourceRequestName, name, duration);
        this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) HashMap(java.util.HashMap) MetacatException(com.netflix.metacat.common.exception.MetacatException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) PartitionAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException) MetacatUserMetadataException(com.netflix.metacat.common.exception.MetacatUserMetadataException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) MetacatException(com.netflix.metacat.common.exception.MetacatException) DatabaseAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException) TablePreconditionFailedException(com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException) InvalidMetaException(com.netflix.metacat.common.server.connectors.exception.InvalidMetaException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) MetacatUserMetadataException(com.netflix.metacat.common.exception.MetacatUserMetadataException) NotFoundException(com.netflix.metacat.common.server.connectors.exception.NotFoundException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) PartitionAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException) TableAlreadyExistsException(com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException)

Example 5 with MetacatException

use of com.netflix.metacat.common.exception.MetacatException in project metacat by Netflix.

the class MetacatErrorDecoder method decode.

/**
 * {@inheritDoc}
 */
@Override
public Exception decode(final String methodKey, final Response response) {
    try {
        String message = "";
        if (response.body() != null) {
            message = Util.toString(response.body().asReader());
            try {
                final ObjectNode body = metacatJson.parseJsonObject(message);
                message = body.path("error").asText();
                if (Strings.isNullOrEmpty(message)) {
                    message = body.path("message").asText("No error message supplied.");
                }
            } catch (final MetacatJsonException ignored) {
            }
        }
        switch(response.status()) {
            // NOT IMPLEMENTED
            case 501:
            case // UNSUPPORTED_MEDIA_TYPE
            415:
                return new MetacatNotSupportedException(message);
            case // BAD_REQUEST
            400:
                return new MetacatBadRequestException(message);
            case // Forbidden
            403:
                return new MetacatUnAuthorizedException(message);
            case // NOT_FOUND
            404:
                return new MetacatNotFoundException(message);
            case // CONFLICT
            409:
                return new MetacatAlreadyExistsException(message);
            case // PRECONDITION_FAILED
            412:
                return new MetacatPreconditionFailedException(message);
            case 429:
                return new RetryableException(response.status(), message, response.request() == null ? null : response.request().httpMethod(), new MetacatTooManyRequestsException(message), Date.from(Instant.now().plus(1, ChronoUnit.MINUTES)), response.request());
            // INTERNAL_SERVER_ERROR
            case 500:
            case // SERVICE_UNAVAILABLE
            503:
                return new RetryableException(response.status(), message, response.request() == null ? null : response.request().httpMethod(), new MetacatException(message), null, response.request());
            default:
                return new MetacatException(message);
        }
    } catch (final IOException e) {
        return super.decode(methodKey, response);
    }
}
Also used : MetacatTooManyRequestsException(com.netflix.metacat.common.exception.MetacatTooManyRequestsException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MetacatNotSupportedException(com.netflix.metacat.common.exception.MetacatNotSupportedException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) MetacatException(com.netflix.metacat.common.exception.MetacatException) IOException(java.io.IOException) MetacatNotFoundException(com.netflix.metacat.common.exception.MetacatNotFoundException) RetryableException(feign.RetryableException) MetacatPreconditionFailedException(com.netflix.metacat.common.exception.MetacatPreconditionFailedException) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) MetacatUnAuthorizedException(com.netflix.metacat.common.exception.MetacatUnAuthorizedException) MetacatAlreadyExistsException(com.netflix.metacat.common.exception.MetacatAlreadyExistsException)

Aggregations

MetacatException (com.netflix.metacat.common.exception.MetacatException)5 MetacatAlreadyExistsException (com.netflix.metacat.common.exception.MetacatAlreadyExistsException)4 MetacatBadRequestException (com.netflix.metacat.common.exception.MetacatBadRequestException)4 MetacatNotFoundException (com.netflix.metacat.common.exception.MetacatNotFoundException)4 MetacatNotSupportedException (com.netflix.metacat.common.exception.MetacatNotSupportedException)4 MetacatPreconditionFailedException (com.netflix.metacat.common.exception.MetacatPreconditionFailedException)4 MetacatTooManyRequestsException (com.netflix.metacat.common.exception.MetacatTooManyRequestsException)3 MetacatUserMetadataException (com.netflix.metacat.common.exception.MetacatUserMetadataException)3 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)3 DatabaseAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException)3 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)3 NotFoundException (com.netflix.metacat.common.server.connectors.exception.NotFoundException)3 PartitionAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.PartitionAlreadyExistsException)3 TableAlreadyExistsException (com.netflix.metacat.common.server.connectors.exception.TableAlreadyExistsException)3 TablePreconditionFailedException (com.netflix.metacat.common.server.connectors.exception.TablePreconditionFailedException)3 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HashMap (java.util.HashMap)2 MetacatUnAuthorizedException (com.netflix.metacat.common.exception.MetacatUnAuthorizedException)1 MetacatJsonException (com.netflix.metacat.common.json.MetacatJsonException)1