Search in sources :

Example 6 with ServiceUnavailableException

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

the class DatasetServiceClient method doRequest.

private HttpResponse doRequest(HttpRequest.Builder requestBuilder) throws DatasetManagementException, UnauthorizedException {
    HttpRequest request = addUserIdHeader(requestBuilder).build();
    try {
        LOG.trace("Executing {} {}", request.getMethod(), request.getURL().getPath());
        HttpResponse response = remoteClient.execute(request);
        LOG.trace("Executed {} {}", request.getMethod(), request.getURL().getPath());
        return response;
    } catch (ServiceUnavailableException e) {
        // thrown by RemoteClient in case of ConnectException
        logThreadDump();
        LOG.trace("Caught exception for {} {}", request.getMethod(), request.getURL().getPath(), e);
        throw e;
    } catch (SocketTimeoutException e) {
        // passed through by RemoteClient
        logThreadDump();
        LOG.trace("Caught exception for {} {}", request.getMethod(), request.getURL().getPath(), e);
        throw new DatasetManagementException(remoteClient.createErrorMessage(request, null), e);
    } catch (IOException e) {
        // other network exceptions
        LOG.trace("Caught exception for {} {}", request.getMethod(), request.getURL().getPath(), e);
        throw new DatasetManagementException(remoteClient.createErrorMessage(request, null), e);
    } catch (Throwable e) {
        // anything unexpected
        LOG.trace("Caught exception for {} {}", request.getMethod(), request.getURL().getPath(), e);
        throw e;
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponse(io.cdap.common.http.HttpResponse) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException)

Example 7 with ServiceUnavailableException

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

the class RemotePluginFinder method getPlugins.

/**
 * Gets a list of {@link PluginInfo} from the artifact extension endpoint.
 *
 * @param namespaceId      namespace of the call happening in
 * @param parentArtifactId the parent artifact id
 * @param pluginType       the plugin type to look for
 * @param pluginName       the plugin name to look for
 * @return a list of {@link PluginInfo}
 * @throws IOException              if it failed to get the information
 * @throws PluginNotExistsException if the given plugin type and name doesn't exist
 */
private List<PluginInfo> getPlugins(NamespaceId namespaceId, ArtifactId parentArtifactId, String pluginType, String pluginName) throws IOException, PluginNotExistsException, UnauthorizedException {
    // replace the space in the name
    // TODO: CDAP-18375 improve url encoding the our remote call
    pluginName = pluginName.replace(" ", "%20");
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.GET, String.format("namespaces/%s/artifacts/%s/versions/%s/extensions/%s/plugins/%s?scope=%s&pluginScope=%s", namespaceId.getNamespace(), parentArtifactId.getArtifact(), parentArtifactId.getVersion(), pluginType, pluginName, NamespaceId.SYSTEM.equals(parentArtifactId.getNamespaceId()) ? ArtifactScope.SYSTEM : ArtifactScope.USER, NamespaceId.SYSTEM.equals(namespaceId.getNamespaceId()) ? ArtifactScope.SYSTEM : ArtifactScope.USER));
    HttpResponse response = remoteClient.execute(requestBuilder.build());
    int responseCode = response.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        switch(responseCode) {
            // throw retryable error if app fabric is not available, might be due to restarting
            case HttpURLConnection.HTTP_BAD_GATEWAY:
            case HttpURLConnection.HTTP_UNAVAILABLE:
            case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                throw new ServiceUnavailableException(Constants.Service.APP_FABRIC_HTTP, Constants.Service.APP_FABRIC_HTTP + " service is not available with status " + responseCode);
            case HttpURLConnection.HTTP_NOT_FOUND:
                throw new PluginNotExistsException(namespaceId, pluginType, pluginName);
        }
        throw new IllegalArgumentException("Failure in getting plugin information with type " + pluginType + " and name " + pluginName + " that extends " + parentArtifactId + ". Reason is " + responseCode + ": " + response.getResponseBodyAsString());
    }
    return GSON.fromJson(response.getResponseBodyAsString(), PLUGIN_INFO_LIST_TYPE);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) HttpResponse(io.cdap.common.http.HttpResponse) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException)

Example 8 with ServiceUnavailableException

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

the class ProgramLifecycleHttpHandler method getServiceAvailability.

/**
 * Return the availability (i.e. discoverable registration) status of a service.
 */
@GET
@Path("/apps/{app-name}/versions/{app-version}/{service-type}/{program-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-type") String serviceType, @PathParam("program-name") String programName) throws Exception {
    // Currently we only support services and sparks as the service-type
    ProgramType programType = getProgramType(serviceType);
    if (!ServiceDiscoverable.getUserServiceTypes().contains(programType)) {
        throw new BadRequestException("Only service or spark is support for service availability check");
    }
    ProgramId programId = new ProgramId(new ApplicationId(namespaceId, appName, appVersion), programType, programName);
    ProgramStatus status = lifecycleService.getProgramStatus(programId);
    if (status == ProgramStatus.STOPPED) {
        throw new ServiceUnavailableException(programId.toString(), "Service is stopped. Please start it.");
    }
    // Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
    String discoverableName = ServiceDiscoverable.getName(programId);
    // TODO: CDAP-12959 - Should use the UserServiceEndpointStrategy and discover based on the version
    // and have appVersion nullable for the non versioned endpoint
    EndpointStrategy strategy = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(discoverableName));
    if (strategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
        LOG.trace("Discoverable endpoint {} not found", discoverableName);
        throw new ServiceUnavailableException(programId.toString(), "Service is running but not accepting requests at this time.");
    }
    responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
Also used : RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) BadRequestException(io.cdap.cdap.common.BadRequestException) ProgramType(io.cdap.cdap.proto.ProgramType) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with ServiceUnavailableException

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

the class HBaseDatasetMetricsReporter method report.

private void report(Map<TableId, HBaseTableUtil.TableStats> tableStats) throws IOException, UnauthorizedException {
    Map<String, String> reverseNamespaceMap = hBaseTableUtil.getHBaseToCDAPNamespaceMap();
    for (Map.Entry<TableId, HBaseTableUtil.TableStats> statEntry : tableStats.entrySet()) {
        String hbaseNamespace = statEntry.getKey().getNamespace();
        String cdapNamespace = reverseNamespaceMap.get(hbaseNamespace);
        // tableNames that doesn't start with user are ignored
        if (NamespaceId.SYSTEM.getNamespace().equals(cdapNamespace)) {
            continue;
        }
        String tableName = statEntry.getKey().getTableName();
        try {
            Collection<DatasetSpecificationSummary> instances = dsFramework.getInstances(new NamespaceId(cdapNamespace));
            for (DatasetSpecificationSummary spec : instances) {
                DatasetSpecification specification = dsFramework.getDatasetSpec(new DatasetId(cdapNamespace, spec.getName()));
                if (specification.isParent(tableName)) {
                    MetricsContext collector = metricsService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, cdapNamespace, Constants.Metrics.Tag.DATASET, spec.getName()));
                    collector.gauge("dataset.size.mb", statEntry.getValue().getTotalSizeMB());
                    break;
                }
            }
        } catch (DatasetManagementException | ServiceUnavailableException e) {
        // No op
        }
    }
}
Also used : TableId(io.cdap.cdap.data2.util.TableId) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) DatasetSpecificationSummary(io.cdap.cdap.proto.DatasetSpecificationSummary) DatasetId(io.cdap.cdap.proto.id.DatasetId) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with ServiceUnavailableException

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

the class RuntimeServiceRoutingHandler method routeServiceWithBody.

/**
 * Handles PUT and POST calls from program runtime to access CDAP services.
 * It simply verify the request and forward the call to internal CDAP services.
 */
@Path("/services/{service}/**")
@PUT
@POST
public BodyConsumer routeServiceWithBody(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("app") String app, @PathParam("version") String version, @PathParam("program-type") String programType, @PathParam("program") String program, @PathParam("run") String run, @PathParam("service") String service) throws Exception {
    HttpURLConnection urlConn = openConnection(request, namespace, app, version, programType, program, run, service);
    urlConn.setDoOutput(true);
    OutputStream output;
    try {
        output = urlConn.getOutputStream();
    } catch (UnknownServiceException e) {
        throw new BadRequestException(e.getMessage(), e);
    } catch (IOException e) {
        // If fails to get output stream, treat it as service unavailable so that the client can retry
        throw new ServiceUnavailableException(service, e);
    }
    return new BodyConsumer() {

        @Override
        public void chunk(ByteBuf byteBuf, HttpResponder httpResponder) {
            try {
                byteBuf.readBytes(output, byteBuf.readableBytes());
            } catch (IOException e) {
                throw new ServiceUnavailableException(service, e);
            }
        }

        @Override
        public void finished(HttpResponder httpResponder) {
            try {
                output.close();
            } catch (IOException e) {
                throw new ServiceUnavailableException(service, e);
            }
            try {
                ResponseInfo responseInfo = new ResponseInfo(service, urlConn);
                responder.sendContent(HttpResponseStatus.valueOf(responseInfo.getResponseCode()), new RelayBodyProducer(urlConn.getURL(), responseInfo), responseInfo.getHeaders());
            } catch (BadRequestException e) {
                throw new ServiceException(e, HttpResponseStatus.BAD_REQUEST);
            }
        }

        @Override
        public void handleError(Throwable throwable) {
            LOG.warn("Exception raised for call to {}", urlConn.getURL(), throwable);
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) UnknownServiceException(java.net.UnknownServiceException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ByteBuf(io.netty.buffer.ByteBuf) HttpURLConnection(java.net.HttpURLConnection) UnknownServiceException(java.net.UnknownServiceException) ServiceException(io.cdap.cdap.common.ServiceException) BadRequestException(io.cdap.cdap.common.BadRequestException) BodyConsumer(io.cdap.http.BodyConsumer) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) PUT(javax.ws.rs.PUT)

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