Search in sources :

Example 1 with DatasetClassLoaderProvider

use of co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider in project cdap by caskdata.

the class DatasetAdminService method createOrUpdate.

/**
   * Configures and creates a Dataset
   *
   * @param datasetInstanceId dataset instance to be created
   * @param typeMeta type meta for the dataset
   * @param props dataset instance properties
   * @param existing if dataset already exists (in case of update), the existing properties
   * @return dataset specification
   */
public DatasetSpecification createOrUpdate(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetProperties props, @Nullable final DatasetSpecification existing) throws Exception {
    if (existing == null) {
        LOG.info("Creating dataset instance {}, type meta: {}", datasetInstanceId, typeMeta);
    } else {
        LOG.info("Updating dataset instance {}, type meta: {}, existing: {}", datasetInstanceId, typeMeta, existing);
    }
    try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
        final DatasetContext context = DatasetContext.from(datasetInstanceId.getNamespace());
        UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
        final DatasetType type = ImpersonationUtils.doAs(ugi, new Callable<DatasetType>() {

            @Override
            public DatasetType call() throws Exception {
                DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
                if (type == null) {
                    throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
                }
                return type;
            }
        });
        DatasetSpecification spec = ImpersonationUtils.doAs(ugi, new Callable<DatasetSpecification>() {

            @Override
            public DatasetSpecification call() throws Exception {
                DatasetSpecification spec = existing == null ? type.configure(datasetInstanceId.getEntityName(), props) : type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
                DatasetAdmin admin = type.getAdmin(context, spec);
                if (existing != null) {
                    if (admin instanceof Updatable) {
                        ((Updatable) admin).update(existing);
                    } else {
                        admin.upgrade();
                    }
                } else {
                    admin.create();
                }
                return spec;
            }
        });
        // Writing system metadata should be done without impersonation since user may not have access to system tables.
        writeSystemMetadata(datasetInstanceId, spec, props, typeMeta, type, context, existing != null, ugi);
        return spec;
    } catch (Exception e) {
        if (e instanceof IncompatibleUpdateException) {
            // this is expected to happen if user provides bad update properties, so we log this as debug
            LOG.debug("Incompatible update for dataset '{}'", datasetInstanceId, e);
        } else {
            LOG.error("Error {} dataset '{}': {}", existing == null ? "creating" : "updating", datasetInstanceId, e.getMessage(), e);
        }
        throw e;
    }
}
Also used : DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) DatasetType(co.cask.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DirectoryClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider) Updatable(co.cask.cdap.api.dataset.Updatable) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider) DatasetContext(co.cask.cdap.api.dataset.DatasetContext) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException)

Example 2 with DatasetClassLoaderProvider

use of co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider in project cdap by caskdata.

the class DatasetAdminService method drop.

public void drop(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetSpecification spec) throws Exception {
    LOG.info("Dropping dataset with spec: {}, type meta: {}", spec, typeMeta);
    try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
        UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
        ImpersonationUtils.doAs(ugi, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
                if (type == null) {
                    throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
                }
                DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
                admin.drop();
                return null;
            }
        });
    }
    // Remove metadata for the dataset
    metadataStore.removeMetadata(datasetInstanceId);
}
Also used : DirectoryClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) DatasetClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider) DatasetType(co.cask.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 3 with DatasetClassLoaderProvider

use of co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider in project cdap by caskdata.

the class LineageWriterDatasetFramework method getDataset.

@Nullable
@Override
public <T extends Dataset> T getDataset(final DatasetId datasetInstanceId, final Map<String, String> arguments, @Nullable final ClassLoader classLoader, final DatasetClassLoaderProvider classLoaderProvider, @Nullable final Iterable<? extends EntityId> owners, final AccessType accessType) throws DatasetManagementException, IOException {
    Principal principal = authenticationContext.getPrincipal();
    try {
        // For system, skip authorization and lineage (user program shouldn't allow to access system dataset CDAP-6649)
        // For non-system dataset, always perform authorization and lineage.
        AuthorizationEnforcer enforcer;
        DefaultDatasetRuntimeContext.DatasetAccessRecorder accessRecorder;
        if (!DatasetsUtil.isUserDataset(datasetInstanceId)) {
            enforcer = SYSTEM_NAMESPACE_ENFORCER;
            accessRecorder = SYSTEM_NAMESPACE_ACCESS_RECORDER;
        } else {
            enforcer = authorizationEnforcer;
            accessRecorder = new BasicDatasetAccessRecorder(datasetInstanceId, accessType, owners);
        }
        return DefaultDatasetRuntimeContext.execute(enforcer, accessRecorder, principal, datasetInstanceId, getConstructorDefaultAnnotation(accessType), new Callable<T>() {

            @Override
            public T call() throws Exception {
                return LineageWriterDatasetFramework.super.getDataset(datasetInstanceId, arguments, classLoader, classLoaderProvider, owners, accessType);
            }
        });
    } catch (IOException | DatasetManagementException | ServiceUnavailableException e) {
        throw e;
    } catch (Exception e) {
        throw new DatasetManagementException("Failed to create dataset instance: " + datasetInstanceId, e);
    }
}
Also used : AuthorizationEnforcer(co.cask.cdap.security.spi.authorization.AuthorizationEnforcer) IOException(java.io.IOException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DefaultDatasetRuntimeContext(co.cask.cdap.data2.dataset2.DefaultDatasetRuntimeContext) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) Principal(co.cask.cdap.proto.security.Principal) Nullable(javax.annotation.Nullable)

Aggregations

DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 IOException (java.io.IOException)3 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)2 IncompatibleUpdateException (co.cask.cdap.api.dataset.IncompatibleUpdateException)2 BadRequestException (co.cask.cdap.common.BadRequestException)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 DatasetType (co.cask.cdap.data2.datafabric.dataset.DatasetType)2 DatasetClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider)2 DirectoryClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 DatasetContext (co.cask.cdap.api.dataset.DatasetContext)1 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)1 Updatable (co.cask.cdap.api.dataset.Updatable)1 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 DefaultDatasetRuntimeContext (co.cask.cdap.data2.dataset2.DefaultDatasetRuntimeContext)1 Principal (co.cask.cdap.proto.security.Principal)1 AuthorizationEnforcer (co.cask.cdap.security.spi.authorization.AuthorizationEnforcer)1 Nullable (javax.annotation.Nullable)1