Search in sources :

Example 6 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class DefaultNamespaceAdmin method list.

/**
   * Lists all namespaces
   *
   * @return a list of {@link NamespaceMeta} for all namespaces
   */
@Override
public List<NamespaceMeta> list() throws Exception {
    List<NamespaceMeta> namespaces = nsStore.list();
    Principal principal = authenticationContext.getPrincipal();
    final Predicate<EntityId> filter = authorizationEnforcer.createFilter(principal);
    return Lists.newArrayList(Iterables.filter(namespaces, new com.google.common.base.Predicate<NamespaceMeta>() {

        @Override
        public boolean apply(NamespaceMeta namespaceMeta) {
            return filter.apply(namespaceMeta.getNamespaceId());
        }
    }));
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) Principal(co.cask.cdap.proto.security.Principal) Predicate(co.cask.cdap.api.Predicate)

Example 7 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class DefaultNamespaceAdmin method delete.

/**
   * Deletes the specified namespace
   *
   * @param namespaceId the {@link Id.Namespace} of the specified namespace
   * @throws NamespaceCannotBeDeletedException if the specified namespace cannot be deleted
   * @throws NamespaceNotFoundException if the specified namespace does not exist
   */
@Override
@AuthEnforce(entities = "namespaceId", enforceOn = NamespaceId.class, actions = Action.ADMIN)
public synchronized void delete(@Name("namespaceId") final NamespaceId namespaceId) throws Exception {
    // TODO: CDAP-870, CDAP-1427: Delete should be in a single transaction.
    NamespaceMeta namespaceMeta = get(namespaceId);
    if (checkProgramsRunning(namespaceId)) {
        throw new NamespaceCannotBeDeletedException(namespaceId, String.format("Some programs are currently running in namespace " + "'%s', please stop them before deleting namespace", namespaceId));
    }
    LOG.info("Deleting namespace '{}'.", namespaceId);
    try {
        resourceDeleter.get().deleteResources(namespaceMeta);
        // namespace in the storage provider (Hive, HBase, etc), since we re-use their default namespace.
        if (!NamespaceId.DEFAULT.equals(namespaceId)) {
            // Finally delete namespace from MDS and remove from cache
            deleteNamespaceMeta(namespaceId);
            // revoke privileges as the final step. This is done in the end, because if it is done before actual deletion,
            // and deletion fails, we may have a valid (or invalid) namespace in the system, that no one has privileges on,
            // so no one can clean up. This may result in orphaned privileges, which will be cleaned up by the create API
            // if the same namespace is successfully re-created.
            privilegesManager.revoke(namespaceId);
            LOG.info("Namespace '{}' deleted", namespaceId);
        } else {
            LOG.info("Keeping the '{}' namespace after removing all data.", NamespaceId.DEFAULT);
        }
    } catch (Exception e) {
        LOG.warn("Error while deleting namespace {}", namespaceId, e);
        throw new NamespaceCannotBeDeletedException(namespaceId, e);
    }
}
Also used : NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) NamespaceCannotBeCreatedException(co.cask.cdap.common.NamespaceCannotBeCreatedException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NamespaceAlreadyExistsException(co.cask.cdap.common.NamespaceAlreadyExistsException) AuthEnforce(co.cask.cdap.common.security.AuthEnforce)

Example 8 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class ListNamespacesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    Table table = Table.builder().setHeader("name", "description", "config").setRows(namespaceClient.list(), new RowMaker<NamespaceMeta>() {

        @Override
        public List<?> makeRow(NamespaceMeta object) {
            return Lists.newArrayList(object.getName(), object.getDescription(), NamespaceCommandUtils.prettyPrintNamespaceConfigCLI(object.getConfig()));
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta)

Example 9 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class StreamFileJanitor method cleanAll.

/**
   * Performs file cleanup for all streams.
   */
public void cleanAll() throws Exception {
    List<NamespaceMeta> namespaces = namespaceQueryAdmin.list();
    for (final NamespaceMeta namespace : namespaces) {
        final NamespaceId namespaceId = namespace.getNamespaceId();
        final Location streamBaseLocation = impersonator.doAs(namespaceId, new Callable<Location>() {

            @Override
            public Location call() throws Exception {
                return namespacedLocationFactory.get(namespaceId).append(streamBaseDirPath);
            }
        });
        boolean exists = streamBaseLocation.exists();
        if (exists) {
            // Remove everything under the deleted directory
            Location deletedLocation = StreamUtils.getDeletedLocation(streamBaseLocation);
            if (deletedLocation.exists()) {
                Locations.deleteContent(deletedLocation);
            }
        }
        if (!exists) {
            continue;
        }
        Iterable<Location> streamLocations = StreamUtils.listAllStreams(streamBaseLocation);
        for (final Location streamLocation : streamLocations) {
            final StreamId streamId = namespaceId.stream(StreamUtils.getStreamNameFromLocation(streamLocation));
            final AtomicLong ttl = new AtomicLong(0);
            if (isStreamExists(streamId)) {
                ttl.set(streamAdmin.getConfig(streamId).getTTL());
            }
            clean(streamLocation, ttl.get(), System.currentTimeMillis());
        }
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) AtomicLong(java.util.concurrent.atomic.AtomicLong) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Example 10 with NamespaceMeta

use of co.cask.cdap.proto.NamespaceMeta in project cdap by caskdata.

the class SchedulerQueueResolver method getQueue.

/**
   * Get queue at namespace level if it is empty returns the default queue.
   *
   * @param namespaceId NamespaceId
   * @return schedule queue at namespace level or default queue.
   */
@Nullable
public String getQueue(Id.Namespace namespaceId) throws IOException, NamespaceNotFoundException {
    NamespaceMeta meta;
    try {
        meta = namespaceQueryAdmin.get(namespaceId.toEntityId());
    } catch (NamespaceNotFoundException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
    if (meta != null) {
        NamespaceConfig config = meta.getConfig();
        String namespaceQueue = config.getSchedulerQueueName();
        return Strings.isNullOrEmpty(namespaceQueue) ? getDefaultQueue() : namespaceQueue;
    } else {
        return getDefaultQueue();
    }
}
Also used : NamespaceConfig(co.cask.cdap.proto.NamespaceConfig) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) IOException(java.io.IOException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)58 NamespaceId (co.cask.cdap.proto.id.NamespaceId)26 Test (org.junit.Test)22 IOException (java.io.IOException)12 Location (org.apache.twill.filesystem.Location)6 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)5 BadRequestException (co.cask.cdap.common.BadRequestException)5 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)5 ApplicationManager (co.cask.cdap.test.ApplicationManager)5 ExecutionException (java.util.concurrent.ExecutionException)5 NamespaceConfig (co.cask.cdap.proto.NamespaceConfig)4 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)4 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 RowMaker (co.cask.cdap.cli.util.RowMaker)3 Table (co.cask.cdap.cli.util.table.Table)3 NotFoundException (co.cask.cdap.common.NotFoundException)3 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 NamespaceAdmin (co.cask.cdap.common.namespace.NamespaceAdmin)3 Id (co.cask.cdap.proto.Id)3 ApplicationId (co.cask.cdap.proto.id.ApplicationId)3