Search in sources :

Example 21 with DatasetManagementException

use of co.cask.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.

the class InMemoryDatasetFramework method deleteAllInstances.

@Override
public void deleteAllInstances(NamespaceId namespaceId) throws DatasetManagementException, IOException {
    writeLock.lock();
    try {
        for (DatasetSpecification spec : instances.row(namespaceId).values()) {
            DatasetDefinition def = getDefinitionForType(namespaceId, spec.getType());
            if (def == null) {
                throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", spec.getType(), namespaceId));
            }
            def.getAdmin(DatasetContext.from(namespaceId.getEntityName()), spec, null).drop();
            publishAudit(namespaceId.dataset(spec.getName()), AuditType.DELETE);
        }
        instances.row(namespaceId).clear();
    } finally {
        writeLock.unlock();
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) AbstractDatasetDefinition(co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition) DatasetDefinition(co.cask.cdap.api.dataset.DatasetDefinition)

Example 22 with DatasetManagementException

use of co.cask.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.

the class InMemoryDatasetFrameworkTest method getFramework.

@Override
protected DatasetFramework getFramework() throws DatasetManagementException {
    InMemoryDatasetFramework framework = new InMemoryDatasetFramework(registryFactory, DEFAULT_MODULES);
    framework.setAuditPublisher(inMemoryAuditPublisher);
    try {
        namespacedLocationFactory.get(NAMESPACE_ID).mkdirs();
    } catch (IOException e) {
        throw new DatasetManagementException(e.getMessage());
    }
    return framework;
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 23 with DatasetManagementException

use of co.cask.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.

the class ExternalDatasets method makeTrackable.

/**
   * If the output is an external sink then an external dataset is created for tracking purpose and returned.
   * If the output is a regular dataset then it is already trackable, hence same output is returned.
   *
   * @param admin {@link Admin} used to create external dataset
   * @param output output to be tracked
   * @return an external dataset if output is an external sink, otherwise the same output is returned
   */
public static Output makeTrackable(Admin admin, Output output) {
    // If output is not an external sink, return the same output as it can be tracked by itself.
    if (!(output instanceof Output.OutputFormatProviderOutput)) {
        return output;
    }
    // Output is an external sink, create an external dataset so that it can be tracked.
    String outputName = output.getName();
    OutputFormatProvider outputFormatProvider = ((Output.OutputFormatProviderOutput) output).getOutputFormatProvider();
    Map<String, String> outputFormatConfiguration = outputFormatProvider.getOutputFormatConfiguration();
    // this can be tracked by itself without creating an external dataset
    if (outputFormatProvider instanceof Dataset) {
        return output;
    }
    // Output is an external sink, create an external dataset so that it can be tracked.
    try {
        // Create an external dataset for the output format for lineage tracking
        Map<String, String> arguments = new HashMap<>();
        arguments.put("output.format.class", outputFormatProvider.getOutputFormatClassName());
        arguments.putAll(outputFormatConfiguration);
        if (!admin.datasetExists(outputName)) {
            // Note: the dataset properties are the same as the arguments since we cannot identify them separately
            // since they are mixed up in a single configuration object (CDAP-5674)
            // Also, the properties of the external dataset created will contain runtime arguments for the same reason.
            admin.createDataset(outputName, EXTERNAL_DATASET_TYPE, DatasetProperties.of(arguments));
        } else {
            // Check if the external dataset name clashes with an existing CDAP Dataset
            String datasetType = admin.getDatasetType(outputName);
            if (!EXTERNAL_DATASET_TYPE.equals(datasetType)) {
                throw new IllegalArgumentException("An external sink cannot have the same name as an existing CDAP Dataset instance " + outputName);
            }
        }
        return Output.ofDataset(outputName, Collections.unmodifiableMap(arguments)).alias(output.getAlias());
    } catch (DatasetManagementException e) {
        throw Throwables.propagate(e);
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) HashMap(java.util.HashMap) Dataset(co.cask.cdap.api.dataset.Dataset) OutputFormatProvider(co.cask.cdap.api.data.batch.OutputFormatProvider)

Example 24 with DatasetManagementException

use of co.cask.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.

the class DatasetServiceClient method addUserIdHeader.

private HttpRequest.Builder addUserIdHeader(HttpRequest.Builder builder) throws DatasetManagementException {
    if (!securityEnabled || !authorizationEnabled) {
        return builder;
    }
    String currUserShortName;
    try {
        currUserShortName = UserGroupInformation.getCurrentUser().getShortUserName();
    } catch (IOException e) {
        throw new DatasetManagementException("Unable to get the current user", e);
    }
    // If the request originated from the router and was forwarded to any service other than dataset service, before
    // going to dataset service via dataset service client, the userId could be set in the SecurityRequestContext.
    // e.g. deploying an app that contains a dataset.
    // For user datasets, if a dataset call originates from a program runtime, then find the userId from
    // UserGroupInformation#getCurrentUser()
    String userId = authenticationContext.getPrincipal().getName();
    if (NamespaceId.SYSTEM.equals(namespaceId)) {
        // configured principal
        if (!kerberosEnabled || currUserShortName.equals(masterShortUserName)) {
            LOG.trace("Accessing dataset in system namespace using the system principal because the current user " + "{} is the same as the CDAP master user {}.", currUserShortName, masterShortUserName);
            userId = currUserShortName;
        }
    }
    return builder.addHeader(Constants.Security.Headers.USER_ID, userId);
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException)

Example 25 with DatasetManagementException

use of co.cask.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.

the class DatasetServiceClient method getInstance.

@Nullable
public DatasetMeta getInstance(String instanceName, @Nullable Iterable<? extends EntityId> owners) throws DatasetManagementException {
    String query = "";
    if (owners != null) {
        Set<String> ownerParams = Sets.newHashSet();
        for (EntityId owner : owners) {
            ownerParams.add("owner=" + owner.toString());
        }
        query = ownerParams.isEmpty() ? "" : "?" + Joiner.on("&").join(ownerParams);
    }
    HttpResponse response = doGet("datasets/" + instanceName + query);
    if (HttpResponseStatus.NOT_FOUND.getCode() == response.getResponseCode()) {
        return null;
    }
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Cannot retrieve dataset instance %s info, details: %s", instanceName, response));
    }
    return GSON.fromJson(response.getResponseBodyAsString(), DatasetMeta.class);
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) HttpResponse(co.cask.common.http.HttpResponse) Nullable(javax.annotation.Nullable)

Aggregations

DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)28 IOException (java.io.IOException)14 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)8 DatasetId (co.cask.cdap.proto.id.DatasetId)6 DatasetDefinition (co.cask.cdap.api.dataset.DatasetDefinition)5 AbstractDatasetDefinition (co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition)5 Dataset (co.cask.cdap.api.dataset.Dataset)4 InstanceConflictException (co.cask.cdap.api.dataset.InstanceConflictException)4 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)4 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)3 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)3 Table (co.cask.cdap.api.dataset.table.Table)3 Nullable (javax.annotation.Nullable)3 TransactionFailureException (org.apache.tephra.TransactionFailureException)3 Test (org.junit.Test)3 FileSet (co.cask.cdap.api.dataset.lib.FileSet)2 SystemDatasetInstantiator (co.cask.cdap.data.dataset.SystemDatasetInstantiator)2 Principal (co.cask.cdap.proto.security.Principal)2 AuthorizationEnforcer (co.cask.cdap.security.spi.authorization.AuthorizationEnforcer)2 HttpResponse (co.cask.common.http.HttpResponse)2