use of co.cask.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 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 co.cask.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 = endpointStrategySupplier.get().pick(1L, TimeUnit.SECONDS);
if (discoverable == null) {
throw new ServiceUnavailableException(discoverableServiceName);
}
InetSocketAddress address = discoverable.getSocketAddress();
String scheme = Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload()) ? Constants.Security.SSL_URI_SCHEME : Constants.Security.URI_SCHEME;
String urlStr = String.format("%s%s:%d%s%s", scheme, address.getHostName(), address.getPort(), basePath, resource);
try {
return new URL(urlStr);
} 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, urlStr), e);
}
}
use of co.cask.cdap.common.ServiceUnavailableException in project cdap by caskdata.
the class RemoteDatasetOpExecutor method startUp.
@Override
protected void startUp() throws Exception {
// wait for dataset executor to be discoverable
LOG.info("Starting DatasetOpExecutor.");
int timeout = cConf.getInt(Constants.Startup.STARTUP_SERVICE_TIMEOUT);
if (timeout > 0) {
try {
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() {
try {
remoteClient.resolve("");
} catch (ServiceUnavailableException e) {
return false;
}
return true;
}
}, timeout, TimeUnit.SECONDS, Math.min(timeout, Math.max(10, timeout / 10)), TimeUnit.SECONDS);
LOG.info("DatasetOpExecutor started.");
} catch (TimeoutException e) {
// its not a nice message... throw one with a better message
throw new TimeoutException(String.format("Timed out waiting to discover the %s service. " + "Check the container logs then try restarting the service.", Constants.Service.DATASET_EXECUTOR));
} catch (InterruptedException e) {
throw new RuntimeException(String.format("Interrupted while waiting to discover the %s service.", Constants.Service.DATASET_EXECUTOR));
} catch (ExecutionException e) {
throw new RuntimeException(String.format("Error while waiting to discover the %s service.", Constants.Service.DATASET_EXECUTOR), e);
}
}
}
use of co.cask.cdap.common.ServiceUnavailableException in project cdap by caskdata.
the class MonitorHandler method updateServiceLogLevels.
/**
* Update log levels for this service.
*/
@Path("system/services/{service-name}/loglevels")
@PUT
public void updateServiceLogLevels(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
if (!serviceManagementMap.containsKey(serviceName)) {
throw new NotFoundException(String.format("Invalid service name %s", serviceName));
}
MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
if (!masterServiceManager.isServiceEnabled()) {
throw new ForbiddenException(String.format("Failed to update log levels for service %s " + "because the service is not enabled", serviceName));
}
try {
// we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
// serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
masterServiceManager.updateServiceLogLevels(transformLogLevelsMap(decodeArguments(request)));
responder.sendStatus(HttpResponseStatus.OK);
} catch (IllegalStateException ise) {
throw new ServiceUnavailableException(String.format("Failed to update log levels for service %s " + "because the service may not be ready yet", serviceName));
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
} catch (JsonSyntaxException e) {
throw new BadRequestException("Invalid Json in the body");
}
}
use of co.cask.cdap.common.ServiceUnavailableException in project cdap by caskdata.
the class MonitorHandler method restartInstances.
private void restartInstances(HttpResponder responder, String serviceName, int instanceId, boolean restartAll) throws Exception {
long startTimeMs = System.currentTimeMillis();
boolean isSuccess = true;
if (!serviceManagementMap.containsKey(serviceName)) {
throw new NotFoundException(String.format("Invalid service name %s", serviceName));
}
MasterServiceManager masterServiceManager = serviceManagementMap.get(serviceName);
try {
if (!masterServiceManager.isServiceEnabled()) {
String message = String.format("Failed to restart instance for %s because the service is not enabled.", serviceName);
LOG.debug(message);
isSuccess = false;
throw new ForbiddenException(message);
}
if (restartAll) {
masterServiceManager.restartAllInstances();
} else {
if (instanceId < 0 || instanceId >= masterServiceManager.getInstances()) {
throw new IllegalArgumentException();
}
masterServiceManager.restartInstances(instanceId);
}
responder.sendStatus(HttpResponseStatus.OK);
} catch (IllegalStateException ise) {
String message = String.format("Failed to restart instance for %s because the service may not be ready yet", serviceName);
LOG.debug(message, ise);
isSuccess = false;
throw new ServiceUnavailableException(message);
} catch (IllegalArgumentException iex) {
String message = String.format("Failed to restart instance %d for service: %s because invalid instance id", instanceId, serviceName);
LOG.debug(message, iex);
isSuccess = false;
throw new BadRequestException(message);
} catch (Exception ex) {
LOG.warn(String.format("Exception when trying to restart instances for service %s", serviceName), ex);
isSuccess = false;
throw new Exception(String.format("Error restarting instance %d for service: %s", instanceId, serviceName));
} finally {
long endTimeMs = System.currentTimeMillis();
if (restartAll) {
serviceStore.setRestartAllInstancesRequest(serviceName, startTimeMs, endTimeMs, isSuccess);
} else {
serviceStore.setRestartInstanceRequest(serviceName, startTimeMs, endTimeMs, isSuccess, instanceId);
}
}
}
Aggregations