Search in sources :

Example 11 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method deleteQueues.

@DELETE
@Path("/queues")
public synchronized void deleteQueues(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws NamespaceNotFoundException {
    // synchronized to avoid a potential race condition here:
    // 1. the check for state returns that all flows are STOPPED
    // 2. The API deletes queues because
    // Between 1. and 2., a flow is started using the /namespaces/{namespace-id}/apps/{app-id}/flows/{flow-id}/start API
    // Averting this race condition by synchronizing this method. The resource that needs to be locked here is
    // runtimeService. This should work because the method that is used to start a flow - startStopProgram - is also
    // synchronized on this.
    // This synchronization works in HA mode because even in HA mode there is only one leader at a time.
    NamespaceId namespace = validateAndGetNamespace(namespaceId);
    try {
        List<ProgramRecord> flows = lifecycleService.list(validateAndGetNamespace(namespaceId), ProgramType.FLOW);
        for (ProgramRecord flow : flows) {
            String appId = flow.getApp();
            String flowId = flow.getName();
            ProgramId programId = new ProgramId(namespaceId, appId, ProgramType.FLOW, flowId);
            ProgramStatus status = lifecycleService.getProgramStatus(programId);
            if (ProgramStatus.STOPPED != status) {
                responder.sendString(HttpResponseStatus.FORBIDDEN, String.format("Flow '%s' from application '%s' in namespace '%s' is running, " + "please stop it first.", flowId, appId, namespaceId));
                return;
            }
        }
        queueAdmin.dropAllInNamespace(namespace);
        // delete process metrics that are used to calculate the queue size (system.queue.pending metric)
        FlowUtils.deleteFlowPendingMetrics(metricStore, namespaceId, null, null);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (Exception e) {
        LOG.error("Error while deleting queues in namespace " + namespace, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : ProgramRecord(co.cask.cdap.proto.ProgramRecord) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId) ConflictException(co.cask.cdap.common.ConflictException) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) MethodNotAllowedException(co.cask.cdap.common.MethodNotAllowedException) NotImplementedException(co.cask.cdap.common.NotImplementedException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) SchedulerException(co.cask.cdap.internal.app.runtime.schedule.SchedulerException) JsonSyntaxException(com.google.gson.JsonSyntaxException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramStatus(co.cask.cdap.proto.ProgramStatus) BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 12 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class DefaultNamespaceAdmin method updateProperties.

@Override
public synchronized void updateProperties(NamespaceId namespaceId, NamespaceMeta namespaceMeta) throws Exception {
    if (!exists(namespaceId)) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    authorizationEnforcer.enforce(namespaceId, authenticationContext.getPrincipal(), Action.ADMIN);
    NamespaceMeta existingMeta = nsStore.get(namespaceId);
    // Already ensured that namespace exists, so namespace meta should not be null
    Preconditions.checkNotNull(existingMeta);
    NamespaceMeta.Builder builder = new NamespaceMeta.Builder(existingMeta);
    if (namespaceMeta.getDescription() != null) {
        builder.setDescription(namespaceMeta.getDescription());
    }
    NamespaceConfig config = namespaceMeta.getConfig();
    if (config != null && !Strings.isNullOrEmpty(config.getSchedulerQueueName())) {
        builder.setSchedulerQueueName(config.getSchedulerQueueName());
    }
    if (config != null) {
        builder.setExploreAsPrincipal(config.isExploreAsPrincipal());
    }
    Set<String> difference = existingMeta.getConfig().getDifference(config);
    if (!difference.isEmpty()) {
        throw new BadRequestException(String.format("Mappings %s for namespace %s cannot be updated once the namespace " + "is created.", difference, namespaceId));
    }
    NamespaceMeta updatedMeta = builder.build();
    nsStore.update(updatedMeta);
    // refresh the cache with new meta
    namespaceMetaCache.refresh(namespaceId);
    LOG.info("Namespace {} updated with meta {}", namespaceId, updatedMeta);
}
Also used : NamespaceConfig(co.cask.cdap.proto.NamespaceConfig) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) CacheBuilder(com.google.common.cache.CacheBuilder) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 13 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class DefaultNamespaceAdmin method get.

/**
   * Gets details of a namespace
   *
   * @param namespaceId the {@link Id.Namespace} of the requested namespace
   * @return the {@link NamespaceMeta} of the requested namespace
   * @throws NamespaceNotFoundException if the requested namespace is not found
   * @throws UnauthorizedException if the namespace is not authorized to the logged-user
   */
@Override
public NamespaceMeta get(NamespaceId namespaceId) throws Exception {
    NamespaceMeta namespaceMeta;
    try {
        namespaceMeta = namespaceMetaCache.get(namespaceId);
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof NamespaceNotFoundException || cause instanceof IOException || cause instanceof UnauthorizedException) {
            throw (Exception) cause;
        }
        throw e;
    }
    Principal principal = authenticationContext.getPrincipal();
    // meta. See: CDAP-7387
    if (masterShortUserName != null && masterShortUserName.equals(principal.getName())) {
        return namespaceMeta;
    }
    Predicate<EntityId> filter = authorizationEnforcer.createFilter(principal);
    if (!filter.apply(namespaceId)) {
        throw new UnauthorizedException(principal, namespaceId);
    }
    return namespaceMeta;
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) Principal(co.cask.cdap.proto.security.Principal)

Example 14 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class StreamHandlerTest method testListStreams.

@Test
public void testListStreams() throws Exception {
    List<StreamDetail> specs = listStreams(NamespaceId.DEFAULT);
    Assert.assertTrue(specs.isEmpty());
    StreamId s1 = NamespaceId.DEFAULT.stream("stream1");
    StreamId s2 = NamespaceId.DEFAULT.stream("stream2");
    createStream(s1);
    specs = listStreams(NamespaceId.DEFAULT);
    Assert.assertEquals(1, specs.size());
    StreamDetail specification = specs.get(0);
    Assert.assertEquals(s1.getStream(), specification.getName());
    createStream(s2);
    specs = listStreams(NamespaceId.DEFAULT);
    Assert.assertEquals(2, specs.size());
    try {
        listStreams(new NamespaceId("notfound"));
        Assert.fail("Should have thrown NamespaceNotFoundException");
    } catch (NamespaceNotFoundException ex) {
    // expected
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) StreamDetail(co.cask.cdap.proto.StreamDetail) NamespaceId(co.cask.cdap.proto.id.NamespaceId) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) Test(org.junit.Test)

Example 15 with NamespaceNotFoundException

use of co.cask.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class AppLifecycleHttpHandlerTest method testDeployNonExistingNamespace.

/**
   * Tests deploying an application in a non-existing non-default namespace.
   */
@Test
public void testDeployNonExistingNamespace() throws Exception {
    HttpResponse response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, "random");
    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
    NotFoundException nfe = new NamespaceNotFoundException(new NamespaceId("random"));
    Assert.assertEquals(nfe.getMessage(), readResponse(response));
}
Also used : HttpResponse(org.apache.http.HttpResponse) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) Test(org.junit.Test)

Aggregations

NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)20 IOException (java.io.IOException)11 NotFoundException (co.cask.cdap.common.NotFoundException)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)7 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)5 HttpResponse (co.cask.common.http.HttpResponse)5 URL (java.net.URL)5 BadRequestException (co.cask.cdap.common.BadRequestException)4 NamespaceCannotBeDeletedException (co.cask.cdap.common.NamespaceCannotBeDeletedException)3 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)3 Test (org.junit.Test)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)2 NamespaceAlreadyExistsException (co.cask.cdap.common.NamespaceAlreadyExistsException)2 SecureKeyAlreadyExistsException (co.cask.cdap.common.SecureKeyAlreadyExistsException)2 NamespaceConfig (co.cask.cdap.proto.NamespaceConfig)2 StreamDetail (co.cask.cdap.proto.StreamDetail)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 TypeToken (com.google.common.reflect.TypeToken)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2