Search in sources :

Example 46 with RestException

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

the class AdminTest method brokerStats.

@Test
void brokerStats() throws Exception {
    doReturn("client-id").when(brokerStats).clientAppId();
    Collection<Metrics> metrics = brokerStats.getMetrics();
    assertNotNull(metrics);
    LocalBrokerData loadReport = (LocalBrokerData) brokerStats.getLoadReport();
    assertNotNull(loadReport);
    assertNotNull(loadReport.getCpu());
    Collection<Metrics> mBeans = brokerStats.getMBeans();
    assertTrue(!mBeans.isEmpty());
    AllocatorStats allocatorStats = brokerStats.getAllocatorStats("default");
    assertNotNull(allocatorStats);
    Map<String, Map<String, PendingBookieOpsStats>> bookieOpsStats = brokerStats.getPendingBookieOpsStats();
    assertTrue(bookieOpsStats.isEmpty());
    StreamingOutput topic = brokerStats.getTopics2();
    assertNotNull(topic);
    try {
        brokerStats.getBrokerResourceAvailability("prop", "use", "ns2");
        fail("should have failed as ModularLoadManager doesn't support it");
    } catch (RestException re) {
    // Ok
    }
}
Also used : Metrics(org.apache.pulsar.common.stats.Metrics) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) AllocatorStats(org.apache.pulsar.common.stats.AllocatorStats) RestException(org.apache.pulsar.broker.web.RestException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 47 with RestException

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

the class NamespacesTest method testGlobalNamespaceReplicationConfiguration.

@Test
public void testGlobalNamespaceReplicationConfiguration() throws Exception {
    assertEquals(namespaces.getNamespaceReplicationClusters(this.testGlobalNamespaces.get(0).getProperty(), this.testGlobalNamespaces.get(0).getCluster(), this.testGlobalNamespaces.get(0).getLocalName()), Lists.newArrayList());
    namespaces.setNamespaceReplicationClusters(this.testGlobalNamespaces.get(0).getProperty(), this.testGlobalNamespaces.get(0).getCluster(), this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use", "usw"));
    assertEquals(namespaces.getNamespaceReplicationClusters(this.testGlobalNamespaces.get(0).getProperty(), this.testGlobalNamespaces.get(0).getCluster(), this.testGlobalNamespaces.get(0).getLocalName()), Lists.newArrayList("use", "usw"));
    try {
        namespaces.setNamespaceReplicationClusters(this.testGlobalNamespaces.get(0).getProperty(), this.testGlobalNamespaces.get(0).getCluster(), this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use", "invalid-cluster"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.FORBIDDEN.getStatusCode());
    }
    try {
        namespaces.setNamespaceReplicationClusters(this.testGlobalNamespaces.get(0).getProperty(), this.testGlobalNamespaces.get(0).getCluster(), this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use", "global"));
        fail("should have failed");
    } catch (RestException e) {
        // Ok, global should not be allowed in the list of replication clusters
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use", "invalid-cluster"));
        fail("should have failed");
    } catch (RestException e) {
        // Ok, invalid-cluster is an invalid cluster id
        assertEquals(e.getResponse().getStatus(), Status.FORBIDDEN.getStatusCode());
    }
    admin.properties().updateProperty(testProperty, new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use", "usc")));
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use", "usw"));
        fail("should have failed");
    } catch (RestException e) {
        // Ok, usw was not configured in the list of allowed clusters
        assertEquals(e.getResponse().getStatus(), Status.FORBIDDEN.getStatusCode());
    }
    // Sometimes watcher event consumes scheduled exception, so set to always fail to ensure exception is
    // thrown for api call.
    mockZookKeeper.setAlwaysFail(Code.SESSIONEXPIRED);
    pulsar.getConfigurationCache().policiesCache().invalidate(AdminResource.path(POLICIES, this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName()));
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    } finally {
        mockZookKeeper.unsetAlwaysFail();
    }
    mockZookKeeper.failNow(Code.BADVERSION);
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName(), Lists.newArrayList("use"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    try {
        namespaces.getNamespaceReplicationClusters(this.testProperty, "global", "non-existing-ns");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, "global", "non-existing-ns", Lists.newArrayList("use"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    pulsar.getConfigurationCache().policiesCache().clear();
    // ensure the ZooKeeper read happens, bypassing the cache
    try {
        namespaces.getNamespaceReplicationClusters(this.testProperty, "global", this.testGlobalNamespaces.get(0).getLocalName());
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), 500);
    }
    try {
        namespaces.getNamespaceReplicationClusters(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName());
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.setNamespaceReplicationClusters(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), Lists.newArrayList("use"));
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 48 with RestException

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

the class NamespacesTest method testValidateAdminAccessOnProperty.

@Test
public void testValidateAdminAccessOnProperty() throws Exception {
    try {
        final String property = "prop";
        pulsar.getConfiguration().setAuthenticationEnabled(true);
        pulsar.getConfiguration().setAuthorizationEnabled(true);
        final String path = PulsarWebResource.path(POLICIES, property);
        final String data = ObjectMapperFactory.getThreadLocal().writeValueAsString(new PropertyAdmin(Lists.newArrayList(namespaces.clientAppId()), Sets.newHashSet("use")));
        ZkUtils.createFullPathOptimistic(pulsar.getConfigurationCache().getZooKeeper(), path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        namespaces.validateAdminAccessOnProperty(property);
    } catch (RestException e) {
        fail("validateAdminAccessOnProperty failed");
    } finally {
        pulsar.getConfiguration().setAuthenticationEnabled(false);
        pulsar.getConfiguration().setAuthorizationEnabled(false);
    }
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 49 with RestException

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

the class NamespacesTest method testSplitBundles.

@Test
public void testSplitBundles() throws Exception {
    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);
    mockWebUrl(localWebServiceUrl, testNs);
    // split bundles
    try {
        namespaces.splitNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0xffffffff", false, true);
        // verify split bundles
        BundlesData bundlesData = namespaces.getBundlesData(testProperty, testLocalCluster, bundledNsLocal);
        assertNotNull(bundlesData);
        assertTrue(bundlesData.boundaries.size() == 3);
        assertTrue(bundlesData.boundaries.get(0).equals("0x00000000"));
        assertTrue(bundlesData.boundaries.get(1).equals("0x7fffffff"));
        assertTrue(bundlesData.boundaries.get(2).equals("0xffffffff"));
    } 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 50 with RestException

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

the class NamespacesTest method testDeleteNamespaces.

@Test
public void testDeleteNamespaces() throws Exception {
    try {
        namespaces.deleteNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", false);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    NamespaceName testNs = this.testLocalNamespaces.get(1);
    TopicName topicName = TopicName.get(testNs.getPersistentTopicName("my-topic"));
    ZkUtils.createFullPathOptimistic(mockZookKeeper, "/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), new byte[0], null, null);
    // setup ownership to localhost
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    try {
        namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
        fail("should have failed");
    } catch (RestException e) {
        // Ok, namespace not empty
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    testNs = this.testGlobalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
    testNs = this.testLocalNamespaces.get(0);
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
    List<String> nsList = Lists.newArrayList(this.testLocalNamespaces.get(1).toString(), this.testLocalNamespaces.get(2).toString());
    nsList.sort(null);
    assertEquals(namespaces.getPropertyNamespaces(this.testProperty), nsList);
    testNs = this.testLocalNamespaces.get(1);
    try {
        namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    // delete the topic from ZK
    mockZookKeeper.delete("/managed-ledgers/" + topicName.getPersistenceNamingEncoding(), -1);
    // ensure refreshed topics list in the cache
    pulsar.getLocalZkCacheService().managedLedgerListCache().clearTree();
    // setup ownership to localhost
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testNs, false, false, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testNs);
    namespaces.deleteNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), false);
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

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