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());
}
}
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);
}
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;
}
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
}
}
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));
}
Aggregations