Search in sources :

Example 46 with NamespaceName

use of org.apache.pulsar.common.naming.NamespaceName in project incubator-pulsar by apache.

the class AdminApiTest method namespaces.

@Test(invocationCount = 1)
public void namespaces() throws PulsarAdminException, PulsarServerException, Exception {
    admin.clusters().createCluster("usw", new ClusterData());
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use", "usw"));
    admin.properties().updateProperty("prop-xyz", propertyAdmin);
    assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1").bundles, Policies.defaultBundle());
    admin.namespaces().createNamespace("prop-xyz/use/ns2");
    admin.namespaces().createNamespace("prop-xyz/use/ns3", 4);
    assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns3").bundles.numBundles, 4);
    assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns3").bundles.boundaries.size(), 5);
    admin.namespaces().deleteNamespace("prop-xyz/use/ns3");
    try {
        admin.namespaces().createNamespace("non-existing/usw/ns1");
        fail("Should not have passed");
    } catch (NotFoundException e) {
    // Ok
    }
    assertEquals(admin.namespaces().getNamespaces("prop-xyz"), Lists.newArrayList("prop-xyz/use/ns1", "prop-xyz/use/ns2"));
    assertEquals(admin.namespaces().getNamespaces("prop-xyz", "use"), Lists.newArrayList("prop-xyz/use/ns1", "prop-xyz/use/ns2"));
    try {
        admin.namespaces().createNamespace("prop-xyz/usc/ns1");
        fail("Should not have passed");
    } catch (NotAuthorizedException e) {
    // Ok, got the non authorized exception since usc cluster is not in the allowed clusters list.
    }
    admin.namespaces().grantPermissionOnNamespace("prop-xyz/use/ns1", "my-role", EnumSet.allOf(AuthAction.class));
    Policies policies = new Policies();
    policies.auth_policies.namespace_auth.put("my-role", EnumSet.allOf(AuthAction.class));
    assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1"), policies);
    assertEquals(admin.namespaces().getPermissions("prop-xyz/use/ns1"), policies.auth_policies.namespace_auth);
    assertEquals(admin.namespaces().getTopics("prop-xyz/use/ns1"), Lists.newArrayList());
    admin.namespaces().revokePermissionsOnNamespace("prop-xyz/use/ns1", "my-role");
    policies.auth_policies.namespace_auth.remove("my-role");
    assertEquals(admin.namespaces().getPolicies("prop-xyz/use/ns1"), policies);
    assertEquals(admin.namespaces().getPersistence("prop-xyz/use/ns1"), new PersistencePolicies(1, 1, 1, 0.0));
    admin.namespaces().setPersistence("prop-xyz/use/ns1", new PersistencePolicies(3, 2, 1, 10.0));
    assertEquals(admin.namespaces().getPersistence("prop-xyz/use/ns1"), new PersistencePolicies(3, 2, 1, 10.0));
    // Force topic creation and namespace being loaded
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://prop-xyz/use/ns1/my-topic").create();
    producer.close();
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/my-topic");
    admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
    NamespaceName ns = NamespaceName.get("prop-xyz/use/ns1");
    // Now, w/ bundle policies, we will use default bundle
    NamespaceBundle defaultBundle = bundleFactory.getFullBundle(ns);
    int i = 0;
    for (; i < 10; i++) {
        Optional<NamespaceEphemeralData> data1 = pulsar.getNamespaceService().getOwnershipCache().getOwnerAsync(defaultBundle).get();
        if (!data1.isPresent()) {
            // Already unloaded
            break;
        }
        LOG.info("Waiting for unload namespace {} to complete. Current service unit isDisabled: {}", defaultBundle, data1.get().isDisabled());
        Thread.sleep(1000);
    }
    assertTrue(i < 10);
    admin.namespaces().deleteNamespace("prop-xyz/use/ns1");
    assertEquals(admin.namespaces().getNamespaces("prop-xyz", "use"), Lists.newArrayList("prop-xyz/use/ns2"));
    try {
        admin.namespaces().unload("prop-xyz/use/ns1");
        fail("should have raised exception");
    } catch (Exception e) {
    // OK excepted
    }
    // Force topic creation and namespace being loaded
    producer = pulsarClient.newProducer().topic("persistent://prop-xyz/use/ns2/my-topic").create();
    producer.close();
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns2/my-topic");
// both unload and delete should succeed for ns2 on other broker with a redirect
// otheradmin.namespaces().unload("prop-xyz/use/ns2");
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) AuthAction(org.apache.pulsar.common.policies.data.AuthAction) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) NamespaceEphemeralData(org.apache.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 47 with NamespaceName

use of org.apache.pulsar.common.naming.NamespaceName in project incubator-pulsar by apache.

the class DispatchRateLimiter method getPoliciesDispatchRate.

/**
 * Gets configured dispatch-rate from namespace policies. Returns null if dispatch-rate is not configured
 *
 * @return
 */
public DispatchRate getPoliciesDispatchRate() {
    final NamespaceName namespace = TopicName.get(this.topicName).getNamespaceObject();
    final String cluster = brokerService.pulsar().getConfiguration().getClusterName();
    final String path = path(POLICIES, namespace.toString());
    Optional<Policies> policies = Optional.empty();
    try {
        policies = brokerService.pulsar().getConfigurationCache().policiesCache().getAsync(path).get(cacheTimeOutInSec, SECONDS);
    } catch (Exception e) {
        log.warn("Failed to get message-rate for {}", this.topicName, e);
    }
    // return policy-dispatch rate only if it's enabled in policies
    return policies.map(p -> {
        DispatchRate dispatchRate = p.clusterDispatchRate.get(cluster);
        return isDispatchRateEnabled(dispatchRate) ? dispatchRate : null;
    }).orElse(null);
}
Also used : DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) TopicName(org.apache.pulsar.common.naming.TopicName) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PulsarWebResource.path(org.apache.pulsar.broker.web.PulsarWebResource.path) BrokerService(org.apache.pulsar.broker.service.BrokerService) RateLimiter(org.apache.pulsar.common.util.RateLimiter) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) POLICIES(org.apache.pulsar.broker.cache.ConfigurationCacheService.POLICIES) Optional(java.util.Optional) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ZooKeeperCache.cacheTimeOutInSec(org.apache.pulsar.zookeeper.ZooKeeperCache.cacheTimeOutInSec) SECONDS(java.util.concurrent.TimeUnit.SECONDS) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Policies(org.apache.pulsar.common.policies.data.Policies) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate)

Example 48 with NamespaceName

use of org.apache.pulsar.common.naming.NamespaceName 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 49 with NamespaceName

use of org.apache.pulsar.common.naming.NamespaceName 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)

Example 50 with NamespaceName

use of org.apache.pulsar.common.naming.NamespaceName in project incubator-pulsar by apache.

the class NamespacesTest method testNamespacesApiRedirects.

@Test
public void testNamespacesApiRedirects() throws Exception {
    // Redirect cases
    uriField.set(namespaces, uriInfo);
    doReturn(false).when(namespaces).isLeaderBroker();
    URI uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString());
    doReturn(uri).when(uriInfo).getRequestUri();
    // Trick to force redirection
    conf.setAuthorizationEnabled(true);
    try {
        namespaces.deleteNamespace(this.testProperty, this.testOtherCluster, this.testLocalNamespaces.get(2).getLocalName(), false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString() + "/unload");
    doReturn(uri).when(uriInfo).getRequestUri();
    try {
        namespaces.unloadNamespaceBundle(this.testProperty, this.testOtherCluster, this.testLocalNamespaces.get(2).getLocalName(), "0x00000000_0xffffffff", false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testGlobalNamespaces.get(0).toString() + "/configversion");
    doReturn(uri).when(uriInfo).getRequestUri();
    // setup to redirect to another broker in the same cluster
    doReturn(Optional.of(new URL("http://otherhost" + ":" + BROKER_WEBSERVICE_PORT))).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceName>() {

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

        @Override
        public boolean matches(Object item) {
            NamespaceName nsname = (NamespaceName) item;
            return nsname.equals(NamespacesTest.this.testGlobalNamespaces.get(0));
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    admin.namespaces().setNamespaceReplicationClusters(testGlobalNamespaces.get(0).toString(), Lists.newArrayList("usw"));
    uri = URI.create("http://localhost" + ":" + BROKER_WEBSERVICE_PORT + "/admin/namespace/" + this.testLocalNamespaces.get(2).toString() + "?authoritative=false");
    doReturn(uri).when(uriInfo).getRequestUri();
    doReturn(true).when(namespaces).isLeaderBroker();
    try {
        namespaces.deleteNamespace(this.testLocalNamespaces.get(2).getProperty(), this.testLocalNamespaces.get(2).getCluster(), this.testLocalNamespaces.get(2).getLocalName(), false);
        fail("Should have raised exception to redirect request");
    } catch (WebApplicationException wae) {
        // OK
        assertEquals(wae.getResponse().getStatus(), Status.TEMPORARY_REDIRECT.getStatusCode());
        assertEquals(wae.getResponse().getLocation().toString(), UriBuilder.fromUri(uri).host("broker-usc.com").port(BROKER_WEBSERVICE_PORT).toString());
    }
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) WebApplicationException(javax.ws.rs.WebApplicationException) Matcher(org.hamcrest.Matcher) URI(java.net.URI) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

NamespaceName (org.apache.pulsar.common.naming.NamespaceName)99 WebTarget (javax.ws.rs.client.WebTarget)52 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)52 Test (org.testng.annotations.Test)27 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)26 List (java.util.List)15 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)13 RestException (org.apache.pulsar.broker.web.RestException)13 TopicName (org.apache.pulsar.common.naming.TopicName)13 Field (java.lang.reflect.Field)12 URL (java.net.URL)11 NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)11 Policies (org.apache.pulsar.common.policies.data.Policies)10 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)8 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)8 KeeperException (org.apache.zookeeper.KeeperException)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6