Search in sources :

Example 26 with ServiceUnavailableException

use of io.cdap.cdap.common.ServiceUnavailableException in project cdap by caskdata.

the class RemoteClient method resolve.

/**
 * Discover the service address, then append the base path and specified resource to get the URL.
 *
 * @param resource the resource to use
 * @return the resolved URL
 * @throws ServiceUnavailableException if the service could not be discovered
 */
public URL resolve(String resource) {
    Discoverable discoverable = endpointStrategy.pick(1L, TimeUnit.SECONDS);
    if (discoverable == null) {
        throw new ServiceUnavailableException(discoverableServiceName);
    }
    URI uri = URIScheme.createURI(discoverable, "%s%s", basePath, resource);
    try {
        return rewriteURL(uri.toURL());
    } catch (MalformedURLException e) {
        // shouldn't happen. If it does, it means there is some bug in the service announcer
        throw new IllegalStateException(String.format("Discovered service %s, but it announced malformed URL %s", discoverableServiceName, uri), e);
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) MalformedURLException(java.net.MalformedURLException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) URI(java.net.URI)

Example 27 with ServiceUnavailableException

use of io.cdap.cdap.common.ServiceUnavailableException in project cdap by caskdata.

the class RemoteClient method execute.

/**
 * Perform the request, returning the response. If there was a ConnectException while making the request,
 * a ServiceUnavailableException is thrown.
 *
 * @param request the request to perform
 * @return the response
 * @throws IOException if there was an IOException while performing the request
 * @throws ServiceUnavailableException if there was a ConnectException while making the request, or if the response
 *                                     was a 503
 */
public HttpResponse execute(HttpRequest request) throws IOException, UnauthorizedException {
    HttpRequest httpRequest = request;
    URL rewrittenURL = rewriteURL(request.getURL());
    Multimap<String, String> headers = setHeader(request);
    httpRequest = new HttpRequest(request.getMethod(), rewrittenURL, headers, request.getBody(), request.getBodyLength());
    try {
        HttpResponse response = HttpRequests.execute(httpRequest, httpRequestConfig);
        switch(response.getResponseCode()) {
            case HttpURLConnection.HTTP_BAD_GATEWAY:
            case HttpURLConnection.HTTP_UNAVAILABLE:
            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                throw new ServiceUnavailableException(discoverableServiceName, response.getResponseBodyAsString());
            case HttpURLConnection.HTTP_FORBIDDEN:
                throw new UnauthorizedException(response.getResponseBodyAsString());
            default:
                return response;
        }
    } catch (ConnectException e) {
        throw new ServiceUnavailableException(discoverableServiceName, e);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(io.cdap.common.http.HttpResponse) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) URL(java.net.URL) ConnectException(java.net.ConnectException)

Example 28 with ServiceUnavailableException

use of io.cdap.cdap.common.ServiceUnavailableException in project cdap by caskdata.

the class DatasetSerDe method getDatasetSchema.

private void getDatasetSchema(Configuration conf, DatasetId datasetId) throws SerDeException {
    try (ContextManager.Context hiveContext = ContextManager.getContext(conf)) {
        // Because it calls initialize just to get the object inspector
        if (hiveContext == null) {
            LOG.info("Hive provided a null conf, will not be able to get dataset schema.");
            return;
        }
        // some datasets like Table and ObjectMappedTable have schema in the dataset properties
        try {
            DatasetSpecification datasetSpec = hiveContext.getDatasetSpec(datasetId);
            String schemaStr = datasetSpec.getProperty("schema");
            if (schemaStr != null) {
                schema = Schema.parseJson(schemaStr);
                return;
            }
        } catch (DatasetManagementException | ServiceUnavailableException | UnauthorizedException e) {
            throw new SerDeException("Could not instantiate dataset " + datasetId, e);
        } catch (IOException e) {
            throw new SerDeException("Exception getting schema for dataset " + datasetId, e);
        }
        // other datasets must be instantiated to get their schema
        // conf is null if this is a query that writes to a dataset
        ClassLoader parentClassLoader = conf == null ? null : conf.getClassLoader();
        try (SystemDatasetInstantiator datasetInstantiator = hiveContext.createDatasetInstantiator(parentClassLoader)) {
            Dataset dataset = datasetInstantiator.getDataset(datasetId);
            if (dataset == null) {
                throw new SerDeException("Could not find dataset " + datasetId);
            }
            Type recordType;
            if (dataset instanceof RecordScannable) {
                recordType = ((RecordScannable) dataset).getRecordType();
            } else if (dataset instanceof RecordWritable) {
                recordType = ((RecordWritable) dataset).getRecordType();
            } else {
                throw new SerDeException("Dataset " + datasetId + " is not explorable.");
            }
            schema = schemaGenerator.generate(recordType);
        } catch (UnsupportedTypeException e) {
            throw new SerDeException("Dataset " + datasetId + " has an unsupported schema.", e);
        } catch (IOException e) {
            throw new SerDeException("Exception while trying to instantiate dataset " + datasetId, e);
        }
    } catch (IOException e) {
        throw new SerDeException("Could not get hive context from configuration.", e);
    }
}
Also used : RecordWritable(io.cdap.cdap.api.data.batch.RecordWritable) Dataset(io.cdap.cdap.api.dataset.Dataset) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) RecordScannable(io.cdap.cdap.api.data.batch.RecordScannable) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) Type(java.lang.reflect.Type) SystemDatasetInstantiator(io.cdap.cdap.data.dataset.SystemDatasetInstantiator) ContextManager(io.cdap.cdap.hive.context.ContextManager) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 29 with ServiceUnavailableException

use of io.cdap.cdap.common.ServiceUnavailableException in project cdap by caskdata.

the class RemoteDatasetFrameworkRetryTest method createFramework.

@Override
protected RemoteDatasetFramework createFramework(AuthenticationContext authenticationContext, RemoteClientFactory remoteClientFactory) {
    cConf.set("system.dataset.remote.retry.policy.base.delay.ms", "0");
    cConf.set("system.dataset.remote.retry.policy.max.retries", "1");
    RemoteClientFactory mockedFactory = Mockito.spy(remoteClientFactory);
    Set<URL> failedURIs = new HashSet<>();
    Mockito.doAnswer(i -> {
        RemoteClient realClient = (RemoteClient) i.callRealMethod();
        RemoteClient mocked = Mockito.spy(realClient);
        Mockito.doAnswer(i2 -> {
            HttpRequest request = i2.getArgumentAt(0, HttpRequest.class);
            // Fail the first GET, allow the second
            if (request.getMethod() == HttpMethod.GET && failedURIs.add(request.getURL())) {
                throw new ServiceUnavailableException("service");
            }
            return i2.callRealMethod();
        }).when(mocked).execute(Mockito.any());
        return mocked;
    }).when(mockedFactory).createRemoteClient(Mockito.any(), Mockito.any(), Mockito.any());
    return super.createFramework(authenticationContext, mockedFactory);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) URL(java.net.URL) HashSet(java.util.HashSet)

Aggregations

ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)29 IOException (java.io.IOException)12 BadRequestException (io.cdap.cdap.common.BadRequestException)7 NotFoundException (io.cdap.cdap.common.NotFoundException)7 HttpResponse (io.cdap.common.http.HttpResponse)6 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)5 HttpRequest (io.cdap.common.http.HttpRequest)5 URL (java.net.URL)5 Injector (com.google.inject.Injector)4 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)4 Path (javax.ws.rs.Path)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ForbiddenException (io.cdap.cdap.common.ForbiddenException)3 MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)3 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)3 Service (com.google.common.util.concurrent.Service)2 Dataset (io.cdap.cdap.api.dataset.Dataset)2 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)2 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2