Search in sources :

Example 11 with DatasetModuleMeta

use of io.cdap.cdap.proto.DatasetModuleMeta in project cdap by caskdata.

the class GenerateClientUsageExample method datasetModuleClient.

public void datasetModuleClient() throws Exception {
    // Construct the client used to interact with CDAP
    DatasetModuleClient datasetModuleClient = new DatasetModuleClient(clientConfig);
    // Add a dataset module
    File moduleJarFile = createAppJarFile(SomeDatasetModule.class);
    DatasetModuleId datasetModuleId = NamespaceId.DEFAULT.datasetModule("someDatasetModule");
    datasetModuleClient.add(datasetModuleId, SomeDatasetModule.class.getName(), moduleJarFile);
    // Fetch the dataset module information
    DatasetModuleMeta datasetModuleMeta = datasetModuleClient.get(datasetModuleId);
    // Delete all dataset modules
    datasetModuleClient.deleteAll(NamespaceId.DEFAULT);
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) File(java.io.File) DatasetModuleClient(io.cdap.cdap.client.DatasetModuleClient)

Example 12 with DatasetModuleMeta

use of io.cdap.cdap.proto.DatasetModuleMeta in project cdap by caskdata.

the class DefaultDatasetTypeService method getModule.

/**
 * Returns the {@link DatasetModuleMeta metadata} of the specified {@link DatasetModuleId}.
 */
@Override
public DatasetModuleMeta getModule(DatasetModuleId datasetModuleId) throws Exception {
    ensureNamespaceExists(datasetModuleId.getParent());
    DatasetModuleMeta moduleMeta = typeManager.getModule(datasetModuleId);
    if (moduleMeta == null) {
        throw new DatasetModuleNotFoundException(datasetModuleId);
    }
    return moduleMeta;
}
Also used : DatasetModuleNotFoundException(io.cdap.cdap.common.DatasetModuleNotFoundException) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta)

Example 13 with DatasetModuleMeta

use of io.cdap.cdap.proto.DatasetModuleMeta in project cdap by caskdata.

the class DatasetTypeTable method getTypeMeta.

private DatasetTypeMeta getTypeMeta(NamespaceId namespaceId, String typeName, DatasetModuleMeta moduleMeta) throws IOException {
    List<DatasetModuleMeta> modulesToLoad = Lists.newArrayList();
    // adding first all modules we depend on, then myself
    for (String usedModule : moduleMeta.getUsesModules()) {
        // Try to find module in the specified namespace first, then the system namespace
        DatasetModuleMeta usedModuleMeta = getModuleWithFallback(namespaceId.datasetModule(usedModule));
        // Module could not be found in either user or system namespace, bail out
        Preconditions.checkState(usedModuleMeta != null, String.format("Unable to find metadata about module %s that module %s uses.", usedModule, moduleMeta.getName()));
        modulesToLoad.add(usedModuleMeta);
    }
    modulesToLoad.add(moduleMeta);
    return new DatasetTypeMeta(typeName, modulesToLoad);
}
Also used : DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) DatasetTypeMeta(io.cdap.cdap.proto.DatasetTypeMeta)

Example 14 with DatasetModuleMeta

use of io.cdap.cdap.proto.DatasetModuleMeta 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)

Example 15 with DatasetModuleMeta

use of io.cdap.cdap.proto.DatasetModuleMeta in project cdap by caskdata.

the class AuthorizationDatasetTypeService method deleteAll.

@Override
public void deleteAll(NamespaceId namespaceId) throws Exception {
    Principal principal = authenticationContext.getPrincipal();
    for (DatasetModuleMeta meta : delegate.listModules(namespaceId)) {
        DatasetModuleId datasetModuleId = namespaceId.datasetModule(meta.getName());
        accessEnforcer.enforce(datasetModuleId, principal, StandardPermission.DELETE);
    }
    delegate.deleteAll(namespaceId);
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) Principal(io.cdap.cdap.proto.security.Principal)

Aggregations

DatasetModuleMeta (io.cdap.cdap.proto.DatasetModuleMeta)20 DatasetModuleId (io.cdap.cdap.proto.id.DatasetModuleId)9 DatasetTypeMeta (io.cdap.cdap.proto.DatasetTypeMeta)6 Location (org.apache.twill.filesystem.Location)5 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)4 DatasetModuleNotFoundException (io.cdap.cdap.common.DatasetModuleNotFoundException)4 DatasetTypeTable (io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetTypeTable)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 RowMaker (io.cdap.cdap.cli.util.RowMaker)3 Table (io.cdap.cdap.cli.util.table.Table)3 DatasetInstanceTable (io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetInstanceTable)3 TypeConflictException (io.cdap.cdap.data2.dataset2.TypeConflictException)3 DatasetTypeId (io.cdap.cdap.proto.id.DatasetTypeId)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 HttpResponse (io.cdap.common.http.HttpResponse)3 File (java.io.File)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 DatasetDefinitionRegistry (io.cdap.cdap.api.dataset.module.DatasetDefinitionRegistry)2