Search in sources :

Example 11 with ServiceUnavailableException

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

the class RuntimeServiceRoutingHandler method openConnection.

/**
 * Opens a {@link HttpURLConnection} to the given service for the given program run.
 *
 * @throws BadRequestException if the request for service routing is not valid
 */
private HttpURLConnection openConnection(HttpRequest request, String namespace, String app, String version, String programType, String program, String run, String service) throws BadRequestException {
    ApplicationId appId = new NamespaceId(namespace).app(app, version);
    ProgramRunId programRunId = new ProgramRunId(appId, ProgramType.valueOfCategoryName(programType, BadRequestException::new), program, run);
    requestValidator.getProgramRunStatus(programRunId, request);
    Discoverable discoverable = endpointStrategyLoadingCache.getUnchecked(service).pick(2, TimeUnit.SECONDS);
    if (discoverable == null) {
        throw new ServiceUnavailableException(service);
    }
    String prefix = String.format("%s/runtime/namespaces/%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s", Constants.Gateway.INTERNAL_API_VERSION_3, namespace, app, version, programType, program, run, service);
    URI uri = URIScheme.createURI(discoverable, request.uri().substring(prefix.length()));
    try {
        URL url = uri.toURL();
        HttpURLConnection urlConn;
        try {
            urlConn = (HttpURLConnection) url.openConnection();
        } catch (IOException e) {
            // If fail to open the connection, treat it as service unavailable so that the client can retry
            throw new ServiceUnavailableException(service);
        }
        if (urlConn instanceof HttpsURLConnection) {
            new HttpsEnabler().setTrustAll(true).enable((HttpsURLConnection) urlConn);
        }
        for (Map.Entry<String, String> header : request.headers().entries()) {
            urlConn.setRequestProperty(header.getKey(), header.getValue());
        }
        urlConn.setRequestMethod(request.method().name());
        urlConn.setDoInput(true);
        return urlConn;
    } catch (MalformedURLException | ProtocolException e) {
        // This can only happen if the incoming request is bad
        throw new BadRequestException("Invalid request due to " + e.getMessage(), e);
    }
}
Also used : ProtocolException(java.net.ProtocolException) Discoverable(org.apache.twill.discovery.Discoverable) MalformedURLException(java.net.MalformedURLException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BadRequestException(io.cdap.cdap.common.BadRequestException) HttpsEnabler(io.cdap.cdap.common.security.HttpsEnabler) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 12 with ServiceUnavailableException

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

the class RemoteMetadataReader method getMetadata.

@Override
public Map<MetadataScope, Metadata> getMetadata(MetadataEntity metadataEntity) throws MetadataException {
    Map<MetadataScope, Metadata> scopeMetadata = new HashMap<>();
    Set<MetadataRecord> metadata;
    try {
        metadata = metadataClient.getMetadata(metadataEntity);
    } catch (ServiceUnavailableException e) {
        throw e;
    } catch (Exception e) {
        throw new MetadataException(e);
    }
    metadata.forEach(record -> scopeMetadata.put(record.getScope(), new Metadata(record.getProperties(), record.getTags())));
    LOG.trace("Returning metadata record {} for {}", scopeMetadata, metadataEntity);
    return scopeMetadata;
}
Also used : HashMap(java.util.HashMap) Metadata(io.cdap.cdap.api.metadata.Metadata) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) MetadataRecord(io.cdap.cdap.common.metadata.MetadataRecord) MetadataException(io.cdap.cdap.api.metadata.MetadataException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) MetadataException(io.cdap.cdap.api.metadata.MetadataException) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope)

Example 13 with ServiceUnavailableException

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

the class LeaderElectionMessagingServiceTest method testFencing.

@Test
public void testFencing() throws IOException, InterruptedException, ExecutionException, TimeoutException, UnauthorizedException {
    final TopicId topicId = NamespaceId.SYSTEM.topic("topic");
    // Change the fencing time
    long oldFencingDelay = cConf.getLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS);
    cConf.setLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS, 3L);
    try {
        Injector injector = createInjector(0);
        ZKClientService zkClient = injector.getInstance(ZKClientService.class);
        zkClient.startAndWait();
        final MessagingService messagingService = injector.getInstance(MessagingService.class);
        if (messagingService instanceof Service) {
            ((Service) messagingService).startAndWait();
        }
        // Shouldn't be serving request yet.
        try {
            messagingService.listTopics(NamespaceId.SYSTEM);
            Assert.fail("Expected service unavailable exception");
        } catch (ServiceUnavailableException e) {
        // expected
        }
        // Retry until pass the fencing delay (with some buffer)
        Tasks.waitFor(topicId, new Callable<TopicId>() {

            @Override
            public TopicId call() throws Exception {
                try {
                    return messagingService.getTopic(topicId).getTopicId();
                } catch (ServiceUnavailableException e) {
                    return null;
                }
            }
        }, 10L, TimeUnit.SECONDS, 200, TimeUnit.MILLISECONDS);
        if (messagingService instanceof Service) {
            ((Service) messagingService).stopAndWait();
        }
        zkClient.stopAndWait();
    } finally {
        cConf.setLong(Constants.MessagingSystem.HA_FENCING_DELAY_SECONDS, oldFencingDelay);
    }
}
Also used : ZKClientService(org.apache.twill.zookeeper.ZKClientService) Injector(com.google.inject.Injector) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) MessagingService(io.cdap.cdap.messaging.MessagingService) ZKClientService(org.apache.twill.zookeeper.ZKClientService) Service(com.google.common.util.concurrent.Service) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) TopicId(io.cdap.cdap.proto.id.TopicId) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) TimeoutException(java.util.concurrent.TimeoutException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MessagingService(io.cdap.cdap.messaging.MessagingService) Test(org.junit.Test)

Example 14 with ServiceUnavailableException

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

the class RemoteSparkManager method checkAvailability.

/**
 * Checks if a user service is available by hitting the availability endpoint.
 */
private void checkAvailability() throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = clientConfig.resolveNamespacedURLV3(programId.getNamespaceId(), String.format("apps/%s/versions/%s/%s/%s/available", programId.getApplication(), programId.getVersion(), programId.getType().getCategoryName(), programId.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, clientConfig.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_UNAVAILABLE);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(programId);
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
        throw new ServiceUnavailableException(programId.toString());
    }
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) URL(java.net.URL)

Example 15 with ServiceUnavailableException

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

the class ExploreDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!acceptsURL(url)) {
        return null;
    }
    ExploreConnectionParams params = ExploreConnectionParams.parseConnectionUrl(url);
    String authToken = getString(params, ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN, null);
    String namespace = getString(params, ExploreConnectionParams.Info.NAMESPACE, NamespaceId.DEFAULT.getNamespace());
    boolean sslEnabled = getBoolean(params, ExploreConnectionParams.Info.SSL_ENABLED, false);
    boolean verifySSLCert = getBoolean(params, ExploreConnectionParams.Info.VERIFY_SSL_CERT, true);
    ExploreClient exploreClient = new FixedAddressExploreClient(params.getHost(), params.getPort(), authToken, sslEnabled, verifySSLCert);
    try {
        exploreClient.ping();
    } catch (UnauthenticatedException e) {
        throw new SQLException("Cannot connect to " + url + ", not authenticated.", e);
    } catch (ServiceUnavailableException | ExploreException e) {
        throw new SQLException("Cannot connect to " + url + ", service not available.", e);
    }
    return new ExploreConnection(exploreClient, namespace, params);
}
Also used : ExploreClient(io.cdap.cdap.explore.client.ExploreClient) FixedAddressExploreClient(io.cdap.cdap.explore.client.FixedAddressExploreClient) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) SQLException(java.sql.SQLException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) FixedAddressExploreClient(io.cdap.cdap.explore.client.FixedAddressExploreClient) ExploreException(io.cdap.cdap.explore.service.ExploreException)

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