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