Search in sources :

Example 51 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesTest method testSplitBundleWithUnDividedRange.

@Test
public void testSplitBundleWithUnDividedRange() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x08375b1a", "0x08375b1b", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
    mockWebUrl(localWebServiceUrl, testNs);
    // split bundles
    try {
        namespaces.splitNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x08375b1a_0x08375b1b", false, false);
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(org.apache.pulsar.broker.namespace.OwnershipCache) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 52 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesTest method testValidateTopicOwnership.

@Test
public void testValidateTopicOwnership() throws Exception {
    try {
        URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
        String bundledNsLocal = "test-bundled-namespace-1";
        BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
        createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
        final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
        OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
        doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
        Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
        ownership.setAccessible(true);
        ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
        TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
        PersistentTopics topics = spy(new PersistentTopics());
        topics.setServletContext(new MockServletContext());
        topics.setPulsar(pulsar);
        doReturn(false).when(topics).isRequestHttps();
        doReturn("test").when(topics).clientAppId();
        mockWebUrl(localWebServiceUrl, testNs);
        doReturn("persistent").when(topics).domain();
        try {
            topics.validateTopicName(topicName.getProperty(), topicName.getCluster(), topicName.getNamespacePortion(), topicName.getEncodedLocalName());
            topics.validateAdminOperationOnTopic(false);
        } catch (RestException e) {
            fail("validateAdminAccessOnProperty failed");
        }
    } catch (RestException e) {
        fail("validateAdminAccessOnProperty failed");
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(org.apache.pulsar.broker.namespace.OwnershipCache) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) PersistentTopics(org.apache.pulsar.broker.admin.v1.PersistentTopics) URL(java.net.URL) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 53 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method setPeerClusterNames.

@POST
@Path("/{cluster}/peers")
@ApiOperation(value = "Update peer-cluster-list for a cluster.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Cluster has been updated"), @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 412, message = "Peer cluster doesn't exist"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public void setPeerClusterNames(@PathParam("cluster") String cluster, LinkedHashSet<String> peerClusterNames) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    // validate if peer-cluster exist
    if (peerClusterNames != null && !peerClusterNames.isEmpty()) {
        for (String peerCluster : peerClusterNames) {
            try {
                if (cluster.equalsIgnoreCase(peerCluster)) {
                    throw new RestException(Status.PRECONDITION_FAILED, cluster + " itself can't be part of peer-list");
                }
                clustersCache().get(path("clusters", peerCluster)).orElseThrow(() -> new RestException(Status.PRECONDITION_FAILED, "Peer cluster " + peerCluster + " does not exist"));
            } catch (RestException e) {
                log.warn("[{}] Peer cluster doesn't exist from {}, {}", clientAppId(), peerClusterNames, e.getMessage());
                throw e;
            } catch (Exception e) {
                log.warn("[{}] Failed to validate peer-cluster list {}, {}", clientAppId(), peerClusterNames, e.getMessage());
                throw new RestException(e);
            }
        }
    }
    try {
        String clusterPath = path("clusters", cluster);
        Stat nodeStat = new Stat();
        byte[] content = globalZk().getData(clusterPath, null, nodeStat);
        ClusterData currentClusterData = jsonMapper().readValue(content, ClusterData.class);
        currentClusterData.setPeerClusterNames(peerClusterNames);
        // Write back the new updated ClusterData into zookeeper
        globalZk().setData(clusterPath, jsonMapper().writeValueAsBytes(currentClusterData), nodeStat.getVersion());
        globalZkCache().invalidate(clusterPath);
        log.info("[{}] Successfully added peer-cluster {} for {}", clientAppId(), peerClusterNames, cluster);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update cluster {}: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 54 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method deleteFailureDomain.

private void deleteFailureDomain(String clusterPath) {
    try {
        String failureDomain = joinPath(clusterPath, ConfigurationCacheService.FAILURE_DOMAIN);
        if (globalZk().exists(failureDomain, false) == null) {
            return;
        }
        for (String domain : globalZk().getChildren(failureDomain, false)) {
            String domainPath = joinPath(failureDomain, domain);
            globalZk().delete(domainPath, -1);
        }
        globalZk().delete(failureDomain, -1);
        failureDomainCache().clear();
        failureDomainListCache().clear();
    } catch (Exception e) {
        log.warn("Failed to delete failure-domain under cluster {}", clusterPath);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 55 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class ClustersBase method deleteCluster.

@DELETE
@Path("/{cluster}")
@ApiOperation(value = "Delete an existing cluster")
@ApiResponses(value = { @ApiResponse(code = 204, message = "Cluster has been updated"), @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Cluster doesn't exist"), @ApiResponse(code = 412, message = "Cluster is not empty") })
public void deleteCluster(@PathParam("cluster") String cluster) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    // Check that the cluster is not used by any property (eg: no namespaces provisioned there)
    boolean isClusterUsed = false;
    try {
        for (String property : globalZk().getChildren(path(POLICIES), false)) {
            if (globalZk().exists(path(POLICIES, property, cluster), false) == null) {
                continue;
            }
            if (!globalZk().getChildren(path(POLICIES, property, cluster), false).isEmpty()) {
                // We found a property that has at least a namespace in this cluster
                isClusterUsed = true;
                break;
            }
        }
        // check the namespaceIsolationPolicies associated with the cluster
        String path = path("clusters", cluster, "namespaceIsolationPolicies");
        Optional<NamespaceIsolationPolicies> nsIsolationPolicies = namespaceIsolationPoliciesCache().get(path);
        // Need to delete the isolation policies if present
        if (nsIsolationPolicies.isPresent()) {
            if (nsIsolationPolicies.get().getPolicies().isEmpty()) {
                globalZk().delete(path, -1);
                namespaceIsolationPoliciesCache().invalidate(path);
            } else {
                isClusterUsed = true;
            }
        }
    } catch (Exception e) {
        log.error("[{}] Failed to get cluster usage {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
    if (isClusterUsed) {
        log.warn("[{}] Failed to delete cluster {} - Cluster not empty", clientAppId(), cluster);
        throw new RestException(Status.PRECONDITION_FAILED, "Cluster not empty");
    }
    try {
        String clusterPath = path("clusters", cluster);
        deleteFailureDomain(clusterPath);
        globalZk().delete(clusterPath, -1);
        globalZkCache().invalidate(clusterPath);
        log.info("[{}] Deleted cluster {}", clientAppId(), cluster);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to delete cluster {} - Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to delete cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

RestException (org.apache.pulsar.broker.web.RestException)112 KeeperException (org.apache.zookeeper.KeeperException)81 WebApplicationException (javax.ws.rs.WebApplicationException)55 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)55 ExecutionException (java.util.concurrent.ExecutionException)53 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)51 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)49 IOException (java.io.IOException)36 Policies (org.apache.pulsar.common.policies.data.Policies)33 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)29 ApiOperation (io.swagger.annotations.ApiOperation)27 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)25 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)24 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)24 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)24 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)24 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)24 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)24 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)22