Search in sources :

Example 1 with Updatable

use of io.cdap.cdap.api.dataset.Updatable in project cdap by caskdata.

the class InMemoryDatasetOpExecutor method update.

@Override
public DatasetCreationResponse update(DatasetId datasetInstanceId, DatasetTypeMeta typeMeta, DatasetProperties props, DatasetSpecification existing) throws Exception {
    DatasetType type = client.getDatasetType(typeMeta, null, new ConstantClassLoaderProvider());
    if (type == null) {
        throw new IllegalArgumentException("Dataset type cannot be instantiated for provided type meta: " + typeMeta);
    }
    try {
        DatasetSpecification spec = type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
        DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
        if (admin instanceof Updatable) {
            ((Updatable) admin).update(existing);
        } else {
            admin.create();
        }
        if (spec.getDescription() == null && existing.getDescription() != null) {
            spec.setDescription(existing.getDescription());
        }
        return new DatasetCreationResponse(spec, null);
    } catch (IncompatibleUpdateException e) {
        throw new ConflictException(e.getMessage());
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) Updatable(io.cdap.cdap.api.dataset.Updatable) ConstantClassLoaderProvider(io.cdap.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetType(io.cdap.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException)

Example 2 with Updatable

use of io.cdap.cdap.api.dataset.Updatable 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 DatasetCreationResponse 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, () -> {
            LOG.trace("Getting dataset type {}", typeMeta.getName());
            DatasetType type1 = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
            if (type1 == null) {
                throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
            }
            LOG.trace("Got dataset type {}", typeMeta.getName());
            return type1;
        });
        DatasetSpecification spec = ImpersonationUtils.doAs(ugi, () -> {
            LOG.trace("Configuring dataset {} of type {}", datasetInstanceId.getDataset(), typeMeta.getName());
            DatasetSpecification spec1 = existing == null ? type.configure(datasetInstanceId.getEntityName(), props) : type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
            LOG.trace("Configured dataset {} of type {}", datasetInstanceId.getDataset(), typeMeta.getName());
            DatasetAdmin admin = type.getAdmin(context, spec1);
            try {
                if (existing != null) {
                    if (admin instanceof Updatable) {
                        ((Updatable) admin).update(existing);
                    } else {
                        admin.upgrade();
                    }
                } else {
                    LOG.trace("Creating dataset {} of type {}", datasetInstanceId.getDataset(), typeMeta.getName());
                    admin.create();
                    LOG.trace("Created dataset {} of type {}", datasetInstanceId.getDataset(), typeMeta.getName());
                }
            } finally {
                Closeables.closeQuietly(admin);
            }
            return spec1;
        });
        // Writing system metadata should be done without impersonation since user may not have access to system tables.
        LOG.trace("Computing metadata for dataset {}", datasetInstanceId.getDataset());
        SystemMetadata metadata = computeSystemMetadata(datasetInstanceId, spec, props, typeMeta, type, context, existing != null, ugi);
        LOG.trace("Computed metadata for dataset {}", datasetInstanceId.getDataset());
        return new DatasetCreationResponse(spec, metadata);
    } 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(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetType(io.cdap.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException) AccessException(io.cdap.cdap.api.security.AccessException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException) DirectoryClassLoaderProvider(io.cdap.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider) Updatable(io.cdap.cdap.api.dataset.Updatable) SystemMetadata(io.cdap.cdap.data2.metadata.system.SystemMetadata) BadRequestException(io.cdap.cdap.common.BadRequestException) DatasetClassLoaderProvider(io.cdap.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider) DatasetContext(io.cdap.cdap.api.dataset.DatasetContext) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException)

Example 3 with Updatable

use of io.cdap.cdap.api.dataset.Updatable in project cdap by caskdata.

the class InMemoryDatasetFramework method updateInstance.

@Override
public void updateInstance(DatasetId datasetInstanceId, DatasetProperties props) throws DatasetManagementException, IOException {
    writeLock.lock();
    try {
        DatasetSpecification oldSpec = instances.get(datasetInstanceId.getParent(), datasetInstanceId);
        if (oldSpec == null) {
            throw new InstanceNotFoundException(datasetInstanceId.getEntityName());
        }
        DatasetDefinition def = getDefinitionForType(datasetInstanceId.getParent(), oldSpec.getType());
        if (def == null) {
            throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", oldSpec.getType(), datasetInstanceId.getParent()));
        }
        DatasetSpecification spec = AbstractDatasetDefinition.reconfigure(def, datasetInstanceId.getEntityName(), props, oldSpec).setOriginalProperties(props);
        if (props.getDescription() != null) {
            spec = spec.setDescription(props.getDescription());
        }
        instances.put(datasetInstanceId.getParent(), datasetInstanceId, spec);
        DatasetAdmin admin = def.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, null);
        if (admin instanceof Updatable) {
            ((Updatable) admin).update(oldSpec);
        } else {
            admin.upgrade();
        }
        publishAudit(datasetInstanceId, AuditType.UPDATE);
    } catch (IncompatibleUpdateException e) {
        throw new InstanceConflictException("Update failed for dataset instance " + datasetInstanceId, e);
    } finally {
        writeLock.unlock();
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) InstanceConflictException(io.cdap.cdap.api.dataset.InstanceConflictException) InstanceNotFoundException(io.cdap.cdap.api.dataset.InstanceNotFoundException) Updatable(io.cdap.cdap.api.dataset.Updatable) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetDefinition(io.cdap.cdap.api.dataset.DatasetDefinition) AbstractDatasetDefinition(io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException)

Example 4 with Updatable

use of io.cdap.cdap.api.dataset.Updatable in project cdap by caskdata.

the class CubeDatasetAdmin method update.

@Override
public void update(DatasetSpecification oldSpec) throws IOException {
    // update all existing resolution tables, create all new resolutions
    for (Map.Entry<String, DatasetSpecification> entry : spec.getSpecifications().entrySet()) {
        DatasetSpecification oldSubSpec = spec.getSpecification(entry.getKey());
        DatasetAdmin subAdmin = delegates.get(entry.getKey());
        if (oldSubSpec != null && subAdmin instanceof Updatable) {
            ((Updatable) subAdmin).update(oldSubSpec);
        } else if (oldSubSpec == null) {
            subAdmin.create();
        }
    }
// TODO (CDAP-6342) delete all resolutions that were removed as part of the update
}
Also used : Updatable(io.cdap.cdap.api.dataset.Updatable) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) CompositeDatasetAdmin(io.cdap.cdap.api.dataset.lib.CompositeDatasetAdmin) Map(java.util.Map)

Aggregations

DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)4 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)4 Updatable (io.cdap.cdap.api.dataset.Updatable)4 IncompatibleUpdateException (io.cdap.cdap.api.dataset.IncompatibleUpdateException)3 DatasetType (io.cdap.cdap.data2.datafabric.dataset.DatasetType)2 DatasetContext (io.cdap.cdap.api.dataset.DatasetContext)1 DatasetDefinition (io.cdap.cdap.api.dataset.DatasetDefinition)1 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 InstanceConflictException (io.cdap.cdap.api.dataset.InstanceConflictException)1 InstanceNotFoundException (io.cdap.cdap.api.dataset.InstanceNotFoundException)1 AbstractDatasetDefinition (io.cdap.cdap.api.dataset.lib.AbstractDatasetDefinition)1 CompositeDatasetAdmin (io.cdap.cdap.api.dataset.lib.CompositeDatasetAdmin)1 AccessException (io.cdap.cdap.api.security.AccessException)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ConflictException (io.cdap.cdap.common.ConflictException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 ConstantClassLoaderProvider (io.cdap.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider)1 DatasetClassLoaderProvider (io.cdap.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider)1 DirectoryClassLoaderProvider (io.cdap.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider)1 SystemMetadata (io.cdap.cdap.data2.metadata.system.SystemMetadata)1