Search in sources :

Example 1 with ServiceUnavailableException

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);
    }
}
Also used : RecordWritable(co.cask.cdap.api.data.batch.RecordWritable) Dataset(co.cask.cdap.api.dataset.Dataset) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) RecordScannable(co.cask.cdap.api.data.batch.RecordScannable) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) Type(java.lang.reflect.Type) SystemDatasetInstantiator(co.cask.cdap.data.dataset.SystemDatasetInstantiator) ContextManager(co.cask.cdap.hive.context.ContextManager) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 2 with ServiceUnavailableException

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);
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) MalformedURLException(java.net.MalformedURLException) InetSocketAddress(java.net.InetSocketAddress) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) URL(java.net.URL)

Example 3 with ServiceUnavailableException

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);
        }
    }
}
Also used : ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with ServiceUnavailableException

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");
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 5 with ServiceUnavailableException

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);
        }
    }
}
Also used : ForbiddenException(co.cask.cdap.common.ForbiddenException) MasterServiceManager(co.cask.cdap.common.twill.MasterServiceManager) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ForbiddenException(co.cask.cdap.common.ForbiddenException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException)

Aggregations

ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)14 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)4 NotFoundException (co.cask.cdap.common.NotFoundException)4 BadRequestException (co.cask.cdap.common.BadRequestException)3 ForbiddenException (co.cask.cdap.common.ForbiddenException)3 MasterServiceManager (co.cask.cdap.common.twill.MasterServiceManager)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 IOException (java.io.IOException)3 Dataset (co.cask.cdap.api.dataset.Dataset)2 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 Status (ch.qos.logback.core.status.Status)1 StatusListener (ch.qos.logback.core.status.StatusListener)1 DatasetInstantiationException (co.cask.cdap.api.data.DatasetInstantiationException)1 RecordScannable (co.cask.cdap.api.data.batch.RecordScannable)1 RecordWritable (co.cask.cdap.api.data.batch.RecordWritable)1 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)1 MeteredDataset (co.cask.cdap.api.dataset.metrics.MeteredDataset)1