Search in sources :

Example 1 with NamespaceCannotBeDeletedException

use of io.cdap.cdap.common.NamespaceCannotBeDeletedException 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, permissions = StandardPermission.DELETE)
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 {
        // if needed, run master environment specific logic if it is a non-default namespace (see below for more info)
        MasterEnvironment masterEnv = MasterEnvironments.getMasterEnvironment();
        if (masterEnv != null && !NamespaceId.DEFAULT.equals(namespaceId)) {
            masterEnv.onNamespaceDeletion(namespaceId.getNamespace(), namespaceMeta.getConfig().getConfigs());
        }
        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);
            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);
    }
    emitNamespaceCountMetric();
}
Also used : MasterEnvironment(io.cdap.cdap.master.spi.environment.MasterEnvironment) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceCannotBeDeletedException(io.cdap.cdap.common.NamespaceCannotBeDeletedException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) NamespaceCannotBeDeletedException(io.cdap.cdap.common.NamespaceCannotBeDeletedException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NamespaceCannotBeCreatedException(io.cdap.cdap.common.NamespaceCannotBeCreatedException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) AuthEnforce(io.cdap.cdap.common.security.AuthEnforce)

Example 2 with NamespaceCannotBeDeletedException

use of io.cdap.cdap.common.NamespaceCannotBeDeletedException in project cdap by caskdata.

the class AbstractNamespaceClient method deleteDatasets.

@Override
public void deleteDatasets(NamespaceId namespaceId) throws Exception {
    URL url = resolve(String.format("unrecoverable/namespaces/%s/datasets", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.delete(url).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    } else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
        String msg = String.format("Datasets in the namespace '%s' cannot be deleted. Reason: '%s'.", namespaceId, response.getResponseBodyAsString());
        throw new NamespaceCannotBeDeletedException(namespaceId, msg);
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    throw new IOException(String.format("Cannot delete datasets in namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(io.cdap.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Example 3 with NamespaceCannotBeDeletedException

use of io.cdap.cdap.common.NamespaceCannotBeDeletedException in project cdap by caskdata.

the class AbstractNamespaceClient method delete.

@Override
public void delete(NamespaceId namespaceId) throws Exception {
    URL url = resolve(String.format("unrecoverable/namespaces/%s", namespaceId.getNamespace()));
    HttpResponse response = execute(HttpRequest.delete(url).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    } else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
        throw new NamespaceCannotBeDeletedException(namespaceId, response.getResponseBodyAsString());
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return;
    }
    throw new IOException(String.format("Cannot delete namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(io.cdap.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Aggregations

NamespaceCannotBeDeletedException (io.cdap.cdap.common.NamespaceCannotBeDeletedException)3 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)3 IOException (java.io.IOException)3 HttpResponse (io.cdap.common.http.HttpResponse)2 URL (java.net.URL)2 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)1 NamespaceCannotBeCreatedException (io.cdap.cdap.common.NamespaceCannotBeCreatedException)1 AuthEnforce (io.cdap.cdap.common.security.AuthEnforce)1 MasterEnvironment (io.cdap.cdap.master.spi.environment.MasterEnvironment)1 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)1 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)1