Search in sources :

Example 21 with ServiceUnavailableException

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

the class MonitorHandler method resetServiceLogLevels.

/**
 * Reset the log levels of the service.
 * All loggers will be reset to the level when the service started.
 */
@Path("system/services/{service-name}/resetloglevels")
@POST
public void resetServiceLogLevels(FullHttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.UPDATE);
    MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
    if (!masterServiceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Failed to reset log levels for service %s " + "because the service is not enabled", serviceName));
    }
    try {
        Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
        masterServiceManager.resetServiceLogLevels(loggerNames == null ? Collections.emptySet() : loggerNames);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IllegalStateException ise) {
        throw new ServiceUnavailableException(String.format("Failed to reset log levels for service %s " + "because the service may not be ready yet", serviceName));
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid Json in the body");
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 22 with ServiceUnavailableException

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

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 23 with ServiceUnavailableException

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

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 24 with ServiceUnavailableException

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

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 25 with ServiceUnavailableException

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

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)58 IOException (java.io.IOException)24 BadRequestException (io.cdap.cdap.common.BadRequestException)14 NotFoundException (io.cdap.cdap.common.NotFoundException)14 HttpResponse (io.cdap.common.http.HttpResponse)12 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)10 HttpRequest (io.cdap.common.http.HttpRequest)10 Injector (com.google.inject.Injector)8 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)8 URL (java.net.URL)8 Path (javax.ws.rs.Path)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 ForbiddenException (io.cdap.cdap.common.ForbiddenException)6 MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)6 SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)6 HttpURLConnection (java.net.HttpURLConnection)6 Test (org.junit.Test)6 Service (com.google.common.util.concurrent.Service)4 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)4 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)4