Search in sources :

Example 1 with Updatable

use of co.cask.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(co.cask.cdap.api.dataset.Updatable) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) CompositeDatasetAdmin(co.cask.cdap.api.dataset.lib.CompositeDatasetAdmin) Map(java.util.Map)

Example 2 with Updatable

use of co.cask.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(co.cask.cdap.api.dataset.DatasetManagementException) InstanceConflictException(co.cask.cdap.api.dataset.InstanceConflictException) InstanceNotFoundException(co.cask.cdap.api.dataset.InstanceNotFoundException) Updatable(co.cask.cdap.api.dataset.Updatable) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) AbstractDatasetDefinition(co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition) DatasetDefinition(co.cask.cdap.api.dataset.DatasetDefinition) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException)

Example 3 with Updatable

use of co.cask.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 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 4 with Updatable

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

the class InMemoryDatasetOpExecutor method update.

@Override
public DatasetSpecification 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 spec;
    } catch (IncompatibleUpdateException e) {
        throw new ConflictException(e.getMessage());
    }
}
Also used : ConflictException(co.cask.cdap.common.ConflictException) Updatable(co.cask.cdap.api.dataset.Updatable) ConstantClassLoaderProvider(co.cask.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider) 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)

Aggregations

DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)4 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)4 Updatable (co.cask.cdap.api.dataset.Updatable)4 IncompatibleUpdateException (co.cask.cdap.api.dataset.IncompatibleUpdateException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)2 DatasetType (co.cask.cdap.data2.datafabric.dataset.DatasetType)2 DatasetContext (co.cask.cdap.api.dataset.DatasetContext)1 DatasetDefinition (co.cask.cdap.api.dataset.DatasetDefinition)1 InstanceConflictException (co.cask.cdap.api.dataset.InstanceConflictException)1 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)1 AbstractDatasetDefinition (co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition)1 CompositeDatasetAdmin (co.cask.cdap.api.dataset.lib.CompositeDatasetAdmin)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 ConflictException (co.cask.cdap.common.ConflictException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ConstantClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider)1 DatasetClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.DatasetClassLoaderProvider)1 DirectoryClassLoaderProvider (co.cask.cdap.data2.datafabric.dataset.type.DirectoryClassLoaderProvider)1 IOException (java.io.IOException)1 Map (java.util.Map)1