Search in sources :

Example 71 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class RouteConfigHttpHandler method storeRouteConfig.

@PUT
@Path("/routeconfig")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void storeRouteConfig(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    ProgramId programId = namespace.app(appId).service(serviceId);
    Map<String, Integer> routes = parseBody(request, ROUTE_CONFIG_TYPE);
    if (routes == null || routes.isEmpty()) {
        throw new BadRequestException("Route config contains invalid format or empty content.");
    }
    List<ProgramId> nonExistingServices = new ArrayList<>();
    for (String version : routes.keySet()) {
        ProgramId routeProgram = namespace.app(appId, version).service(serviceId);
        if (lifecycleService.getProgramSpecification(routeProgram) == null) {
            nonExistingServices.add(routeProgram);
        }
    }
    if (nonExistingServices.size() > 0) {
        throw new BadRequestException("The following versions of the application/service could not be found : " + nonExistingServices);
    }
    RouteConfig routeConfig = new RouteConfig(routes);
    if (!routeConfig.isValid()) {
        throw new BadRequestException("Route Percentage needs to add up to 100.");
    }
    routeStore.store(programId, routeConfig);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(co.cask.cdap.common.BadRequestException) RouteConfig(co.cask.cdap.route.store.RouteConfig) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 72 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class NamespaceHttpHandler method deleteDatasets.

@DELETE
@Path("/unrecoverable/namespaces/{namespace-id}/datasets")
public void deleteDatasets(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace) throws Exception {
    if (!cConf.getBoolean(Constants.Dangerous.UNRECOVERABLE_RESET, Constants.Dangerous.DEFAULT_UNRECOVERABLE_RESET)) {
        responder.sendString(HttpResponseStatus.FORBIDDEN, String.format("All datasets in namespace %s cannot be deleted because '%s' is not enabled." + " Please enable it and restart CDAP Master.", namespace, Constants.Dangerous.UNRECOVERABLE_RESET));
        return;
    }
    NamespaceId namespaceId = new NamespaceId(namespace);
    namespaceAdmin.deleteDatasets(namespaceId);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 73 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class NamespaceHttpHandler method delete.

@DELETE
@Path("/unrecoverable/namespaces/{namespace-id}")
public void delete(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace) throws Exception {
    if (!cConf.getBoolean(Constants.Dangerous.UNRECOVERABLE_RESET, Constants.Dangerous.DEFAULT_UNRECOVERABLE_RESET)) {
        responder.sendString(HttpResponseStatus.FORBIDDEN, String.format("Namespace '%s' cannot be deleted because '%s' is not enabled. " + "Please enable it and restart CDAP Master.", namespace, Constants.Dangerous.UNRECOVERABLE_RESET));
        return;
    }
    NamespaceId namespaceId = new NamespaceId(namespace);
    namespaceAdmin.delete(namespaceId);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 74 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class AbstractNamespaceResourceDeleter method deleteResources.

@Override
public void deleteResources(NamespaceMeta namespaceMeta) throws Exception {
    final NamespaceId namespaceId = namespaceMeta.getNamespaceId();
    // Delete Preferences associated with this namespace
    preferencesStore.deleteProperties(namespaceId.getNamespace());
    // Delete all dashboards associated with this namespace
    dashboardStore.delete(namespaceId.getNamespace());
    // Delete all applications
    applicationLifecycleService.removeAll(namespaceId);
    // Delete datasets and modules
    dsFramework.deleteAllInstances(namespaceId);
    dsFramework.deleteAllModules(namespaceId);
    // Delete queues and streams data
    queueAdmin.dropAllInNamespace(namespaceId);
    // Delete all the streams in namespace
    deleteStreams(namespaceId);
    // Delete all meta data
    store.removeAll(namespaceId);
    deleteMetrics(namespaceId);
    // delete all artifacts in the namespace
    artifactRepository.clear(namespaceId);
    // delete all messaging topics in the namespace
    for (TopicId topicId : messagingService.listTopics(namespaceId)) {
        messagingService.deleteTopic(topicId);
    }
    LOG.info("All data for namespace '{}' deleted.", namespaceId);
    // namespace in the storage provider (Hive, HBase, etc), since we re-use their default namespace.
    if (!NamespaceId.DEFAULT.equals(namespaceId)) {
        impersonator.doAs(namespaceId, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                // Delete namespace in storage providers
                storageProviderNamespaceAdmin.delete(namespaceId);
                return null;
            }
        });
    }
}
Also used : TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId)

Example 75 with NamespaceId

use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.

the class AbstractStorageProviderNamespaceAdmin method createLocation.

private void createLocation(NamespaceMeta namespaceMeta) throws IOException {
    NamespaceId namespaceId = namespaceMeta.getNamespaceId();
    boolean createdHome = false;
    Location namespaceHome;
    if (hasCustomLocation(namespaceMeta)) {
        namespaceHome = validateCustomLocation(namespaceMeta);
    } else {
        // no namespace custom location was provided one must be created by cdap
        namespaceHome = namespacedLocationFactory.get(namespaceMeta);
        if (namespaceHome.exists()) {
            throw new FileAlreadyExistsException(null, null, String.format("HDFS directory '%s' for '%s' already exists.", namespaceHome, namespaceId));
        }
        createdHome = createNamespaceDir(namespaceHome, "home", namespaceId);
    }
    // data/
    Location dataLoc = namespaceHome.append(Constants.Dataset.DEFAULT_DATA_DIR);
    // tmp/
    Location tempLoc = namespaceHome.append(cConf.get(Constants.AppFabric.TEMP_DIR));
    // streams/
    Location streamsLoc = namespaceHome.append(cConf.get(Constants.Stream.BASE_DIR));
    // streams/.deleted/
    Location deletedLoc = streamsLoc.append(StreamUtils.DELETED);
    String configuredGroupName = namespaceMeta.getConfig().getGroupName();
    boolean createdData = false;
    boolean createdTemp = false;
    boolean createdStreams = false;
    try {
        if (createdHome && SecurityUtil.isKerberosEnabled(cConf)) {
            // set the group id of the namespace home if configured, or the current user's primary group
            String groupToSet = configuredGroupName;
            if (groupToSet == null) {
                // attempt to determine the current user's primary group. Note that we cannot use ugi.getPrimaryGroup()
                // because that is not implemented at least in Hadoop 2.0 and 2.2, possibly other versions. Also note
                // that there is no guarantee that getGroupNames() returns anything.
                String[] groups = UserGroupInformation.getCurrentUser().getGroupNames();
                if (groups != null && groups.length > 0) {
                    groupToSet = groups[0];
                }
            }
            // if this is still null at this point, then the directory will have whatever HDFS assigned at creation
            if (groupToSet != null) {
                namespaceHome.setGroup(groupToSet);
            }
        }
        // create all the directories with default permissions
        createdData = createNamespaceDir(dataLoc, "data", namespaceId);
        createdTemp = createNamespaceDir(tempLoc, "temp", namespaceId);
        createdStreams = createNamespaceDir(streamsLoc, "streams", namespaceId);
        createNamespaceDir(deletedLoc, "deleted streams", namespaceId);
        // if a group name is configured, then that group; otherwise the same group as the namespace home dir
        if (SecurityUtil.isKerberosEnabled(cConf)) {
            String groupToSet = configuredGroupName != null ? configuredGroupName : namespaceHome.getGroup();
            for (Location loc : new Location[] { dataLoc, tempLoc, streamsLoc, deletedLoc }) {
                loc.setGroup(groupToSet);
                // set the permissions to rwx for group, if a group name was configured for the namespace
                if (configuredGroupName != null) {
                    String permissions = loc.getPermissions();
                    loc.setPermissions(permissions.substring(0, 3) + "rwx" + permissions.substring(6));
                }
            }
        }
    } catch (Throwable t) {
        if (createdHome) {
            deleteDirSilently(namespaceHome, t, "home", namespaceMeta.getNamespaceId());
        } else {
            if (createdData) {
                deleteDirSilently(dataLoc, t, "data", namespaceMeta.getNamespaceId());
            }
            if (createdTemp) {
                deleteDirSilently(tempLoc, t, "temp", namespaceMeta.getNamespaceId());
            }
            if (createdStreams) {
                deleteDirSilently(streamsLoc, t, "streams", namespaceMeta.getNamespaceId());
            }
        }
        throw t;
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Location(org.apache.twill.filesystem.Location)

Aggregations

NamespaceId (co.cask.cdap.proto.id.NamespaceId)234 Test (org.junit.Test)99 Path (javax.ws.rs.Path)47 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)43 ApplicationId (co.cask.cdap.proto.id.ApplicationId)35 IOException (java.io.IOException)34 StreamId (co.cask.cdap.proto.id.StreamId)30 DatasetId (co.cask.cdap.proto.id.DatasetId)27 TableId (co.cask.cdap.data2.util.TableId)26 Id (co.cask.cdap.proto.Id)24 ProgramId (co.cask.cdap.proto.id.ProgramId)24 NotFoundException (co.cask.cdap.common.NotFoundException)22 ArtifactId (co.cask.cdap.proto.id.ArtifactId)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 TopicId (co.cask.cdap.proto.id.TopicId)19 GET (javax.ws.rs.GET)18 Location (org.apache.twill.filesystem.Location)18 ArrayList (java.util.ArrayList)15 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)13 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)12