Search in sources :

Example 16 with DatasetModuleConflictException

use of io.cdap.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException in project cdap by caskdata.

the class DefaultDatasetTypeService method deployDefaultModules.

private void deployDefaultModules() throws Exception {
    // adding default modules to be available in dataset manager service
    for (Map.Entry<String, DatasetModule> module : defaultModules.entrySet()) {
        try {
            // NOTE: we assume default modules are always in classpath, hence passing null for jar location
            // NOTE: we add default modules in the system namespace
            DatasetModuleId defaultModule = NamespaceId.SYSTEM.datasetModule(module.getKey());
            typeManager.addModule(defaultModule, module.getValue().getClass().getName(), null, false);
        } catch (DatasetModuleConflictException e) {
            // perfectly fine: we need to add default modules only the very first time service is started
            LOG.debug("Not adding {} module: it already exists", module.getKey());
        } catch (Throwable th) {
            LOG.error("Failed to add {} module. Aborting.", module.getKey(), th);
            throw Throwables.propagate(th);
        }
    }
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) DatasetModuleConflictException(io.cdap.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DatasetModule(io.cdap.cdap.api.dataset.module.DatasetModule)

Example 17 with DatasetModuleConflictException

use of io.cdap.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException in project cdap by caskdata.

the class DatasetTypeManager method deleteModules.

/**
 * Deletes all modules in a namespace, other than system.
 * Presumes that the namespace has already been checked to be non-system.
 *
 * @param namespaceId the {@link NamespaceId} to delete modules from.
 */
public void deleteModules(final NamespaceId namespaceId) throws DatasetModuleConflictException {
    Preconditions.checkArgument(namespaceId != null && !NamespaceId.SYSTEM.equals(namespaceId), "Cannot delete modules from system namespace");
    LOG.info("Deleting all modules from namespace {}", namespaceId);
    try {
        TransactionRunners.run(transactionRunner, context -> {
            final DatasetTypeTable datasetTypeTable = DatasetTypeTable.create(context);
            final DatasetInstanceTable datasetInstanceTable = new DatasetInstanceTable(context);
            final Set<String> typesToDelete = new HashSet<String>();
            final List<Location> moduleLocations = new ArrayList<>();
            final Collection<DatasetModuleMeta> modules = datasetTypeTable.getModules(namespaceId);
            try {
                impersonator.doAs(namespaceId, new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        for (DatasetModuleMeta module : modules) {
                            typesToDelete.addAll(module.getTypes());
                            moduleLocations.add(Locations.getLocationFromAbsolutePath(locationFactory, module.getJarLocationPath()));
                        }
                        return null;
                    }
                });
            } catch (Exception e) {
                // the callable throws no checked exceptions
                throw Throwables.propagate(e);
            }
            // check if there are any instances that use types of these modules?
            Collection<DatasetSpecification> instances = datasetInstanceTable.getByTypes(namespaceId, typesToDelete);
            // cannot delete when there's instance that uses it
            if (!instances.isEmpty()) {
                throw new DatasetModuleConflictException("Cannot delete all modules: existing dataset instances depend on it. Delete them first");
            }
            datasetTypeTable.deleteModules(namespaceId);
            // Delete module locations
            for (Location moduleLocation : moduleLocations) {
                if (!moduleLocation.delete()) {
                    LOG.debug("Could not delete dataset module archive - {}", moduleLocation);
                }
            }
        });
    } catch (RuntimeException e) {
        for (Throwable cause : Throwables.getCausalChain(e)) {
            if (cause instanceof DatasetModuleConflictException) {
                throw (DatasetModuleConflictException) cause;
            }
        }
        LOG.error("Failed to delete all modules from namespace {}", namespaceId);
        throw Throwables.propagate(e);
    } catch (Exception e) {
        LOG.error("Operation failed", e);
        throw Throwables.propagate(e);
    }
}
Also used : DatasetTypeTable(io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetTypeTable) ArrayList(java.util.ArrayList) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) IOException(java.io.IOException) TypeConflictException(io.cdap.cdap.data2.dataset2.TypeConflictException) DatasetInstanceTable(io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetInstanceTable) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Location(org.apache.twill.filesystem.Location)

Aggregations

DatasetModuleConflictException (co.cask.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException)10 IOException (java.io.IOException)7 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 Location (org.apache.twill.filesystem.Location)6 DatasetModuleCannotBeDeletedException (co.cask.cdap.common.DatasetModuleCannotBeDeletedException)5 DatasetModuleNotFoundException (co.cask.cdap.common.DatasetModuleNotFoundException)5 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 DatasetModule (co.cask.cdap.api.dataset.module.DatasetModule)4 ConflictException (co.cask.cdap.common.ConflictException)4 DatasetModuleId (co.cask.cdap.proto.id.DatasetModuleId)4 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 DatasetModuleConflictException (io.cdap.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException)4 DatasetModuleMeta (io.cdap.cdap.proto.DatasetModuleMeta)4 DatasetModuleId (io.cdap.cdap.proto.id.DatasetModuleId)4 DatasetTypeNotFoundException (co.cask.cdap.common.DatasetTypeNotFoundException)3 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)3 NotFoundException (co.cask.cdap.common.NotFoundException)3 DatasetModuleMeta (co.cask.cdap.proto.DatasetModuleMeta)3