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);
}
}
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;
}
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);
}
}
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());
}
}
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);
}
Aggregations