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