Search in sources :

Example 41 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

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 = new NamespaceName(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);
        // 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(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(com.yahoo.pulsar.broker.namespace.OwnershipCache) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 42 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

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);
    DestinationName topicName = DestinationName.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(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(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(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 destination list in the cache
    pulsar.getLocalZkCacheService().managedLedgerListCache().clearTree();
    // setup ownership to localhost
    doReturn(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(com.yahoo.pulsar.common.naming.NamespaceName) RestException(com.yahoo.pulsar.broker.web.RestException) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 43 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

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(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 44 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

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(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 45 with RestException

use of com.yahoo.pulsar.broker.web.RestException in project pulsar by yahoo.

the class NamespacesTest method testUnloadNamespaces.

@Test
public void testUnloadNamespaces() throws Exception {
    final NamespaceName testNs = this.testLocalNamespaces.get(1);
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceName) {
                NamespaceName ns = (NamespaceName) item;
                return ns.equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceName) {
                NamespaceName ns = (NamespaceName) item;
                return ns.equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }));
    doNothing().when(nsSvc).unloadNamespace(testNs);
    NamespaceBundle bundle = nsSvc.getNamespaceBundleFactory().getFullBundle(testNs);
    doNothing().when(namespaces).validateBundleOwnership(bundle, false, true);
    try {
        namespaces.unloadNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
        fail("should have failed as bydefault bundle is activated and can't be unloaded");
    } catch (RestException re) {
    // ok
    }
    verify(nsSvc, times(0)).unloadNamespace(testNs);
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

RestException (com.yahoo.pulsar.broker.web.RestException)79 KeeperException (org.apache.zookeeper.KeeperException)55 ApiOperation (io.swagger.annotations.ApiOperation)54 ApiResponses (io.swagger.annotations.ApiResponses)54 Path (javax.ws.rs.Path)54 WebApplicationException (javax.ws.rs.WebApplicationException)45 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)40 IOException (java.io.IOException)26 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)25 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)23 Policies (com.yahoo.pulsar.common.policies.data.Policies)23 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)22 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)21 NotAllowedException (com.yahoo.pulsar.broker.service.BrokerServiceException.NotAllowedException)20 TopicBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException)20 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)20 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)20 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)20 POST (javax.ws.rs.POST)19 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)18