Search in sources :

Example 71 with NamespaceName

use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.

the class NamespaceIsolationPoliciesTest method testBrokerAssignment.

@Test
public void testBrokerAssignment() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceName ns = new NamespaceName("pulsar/use/testns-1");
    SortedSet<BrokerStatus> primaryCandidates = new TreeSet<>();
    BrokerStatus primary = new BrokerStatus("prod1-broker1.messaging.use.example.com", true, 0);
    BrokerStatus secondary = new BrokerStatus("prod1-broker4.use.example.com", true, 0);
    BrokerStatus shared = new BrokerStatus("use.example.com", true, 0);
    SortedSet<BrokerStatus> secondaryCandidates = new TreeSet<>();
    SortedSet<BrokerStatus> sharedCandidates = new TreeSet<>();
    policies.assignBroker(ns, primary, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 0);
    assertEquals(sharedCandidates.size(), 0);
    assertTrue(primaryCandidates.first().equals(primary));
    policies.assignBroker(ns, secondary, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 1);
    assertEquals(sharedCandidates.size(), 0);
    assertTrue(secondaryCandidates.first().equals(secondary));
    policies.assignBroker(new NamespaceName("pulsar/use1/testns-1"), shared, primaryCandidates, secondaryCandidates, sharedCandidates);
    assertEquals(primaryCandidates.size(), 1);
    assertEquals(secondaryCandidates.size(), 1);
    assertEquals(sharedCandidates.size(), 1);
    assertTrue(sharedCandidates.first().equals(shared));
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) TreeSet(java.util.TreeSet) BrokerStatus(com.yahoo.pulsar.common.policies.data.BrokerStatus) Test(org.testng.annotations.Test)

Example 72 with NamespaceName

use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.

the class NamespaceIsolationPoliciesTest method testGetNamespaceIsolationPolicyByNamespace.

@Test
public void testGetNamespaceIsolationPolicyByNamespace() throws Exception {
    NamespaceIsolationPolicies policies = this.getDefaultTestPolicies();
    NamespaceIsolationPolicy nsPolicy = policies.getPolicyByNamespace(new NamespaceName("no/such/namespace"));
    assertTrue(nsPolicy == null);
    nsPolicy = policies.getPolicyByNamespace(new NamespaceName("pulsar/use/testns-1"));
    assertNotNull(nsPolicy);
    assertEquals(new NamespaceIsolationPolicyImpl(policies.getPolicies().get("policy1")), nsPolicy);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy) Test(org.testng.annotations.Test)

Example 73 with NamespaceName

use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.

the class NamespaceIsolationPolicyImplTest method testFindBrokers.

@Test
public void testFindBrokers() throws Exception {
    NamespaceIsolationPolicyImpl defaultPolicy = this.getDefaultPolicy();
    List<URL> brokers = new ArrayList<URL>();
    for (int i = 0; i < 10; i++) {
        String broker = String.format("prod1-broker%d.messaging.use.example.com", i);
        brokers.add(new URL(String.format("http://%s:8080", broker)));
    }
    List<URL> otherBrokers = new ArrayList<URL>();
    for (int i = 0; i < 10; i++) {
        String broker = String.format("prod1-broker%d.messaging.usw.example.com", i);
        brokers.add(new URL(String.format("http://%s:8080", broker)));
    }
    List<URL> primaryBrokers = defaultPolicy.findPrimaryBrokers(brokers, new NamespaceName("pulsar/use/testns-1"));
    assertEquals(primaryBrokers.size(), 3);
    for (URL primaryBroker : primaryBrokers) {
        assertTrue(primaryBroker.getHost().matches("prod1-broker[1-3].messaging.use.example.com"));
    }
    primaryBrokers = defaultPolicy.findPrimaryBrokers(otherBrokers, new NamespaceName("pulsar/use/testns-1"));
    assertTrue(primaryBrokers.isEmpty());
    try {
        primaryBrokers = defaultPolicy.findPrimaryBrokers(brokers, new NamespaceName("no/such/namespace"));
    } catch (IllegalArgumentException iae) {
    // OK
    }
    List<URL> secondaryBrokers = defaultPolicy.findSecondaryBrokers(brokers, new NamespaceName("pulsar/use/testns-1"));
    assertEquals(secondaryBrokers.size(), 10);
    for (URL secondaryBroker : secondaryBrokers) {
        assertTrue(secondaryBroker.getHost().matches("prod1-broker.*.messaging.use.example.com"));
    }
    secondaryBrokers = defaultPolicy.findSecondaryBrokers(otherBrokers, new NamespaceName("pulsar/use/testns-1"));
    assertTrue(secondaryBrokers.isEmpty());
    try {
        secondaryBrokers = defaultPolicy.findSecondaryBrokers(brokers, new NamespaceName("no/such/namespace"));
    } catch (IllegalArgumentException iae) {
    // OK
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) ArrayList(java.util.ArrayList) URL(java.net.URL) Test(org.testng.annotations.Test)

Example 74 with NamespaceName

use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.

the class AdminApiTest method testNamespaceUnloadBundle.

@Test
public void testNamespaceUnloadBundle() throws Exception {
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
    // Force to create a destination
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/ds2", 0);
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/ds2"));
    // create consumer and subscription
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe("persistent://prop-xyz/use/ns1/ds2", "my-sub", conf);
    assertEquals(admin.persistentTopics().getSubscriptions("persistent://prop-xyz/use/ns1/ds2"), Lists.newArrayList("my-sub"));
    // Create producer
    Producer producer = pulsarClient.createProducer("persistent://prop-xyz/use/ns1/ds2");
    for (int i = 0; i < 10; i++) {
        String message = "message-" + i;
        producer.send(message.getBytes());
    }
    consumer.close();
    producer.close();
    try {
        admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
    } catch (Exception e) {
        fail("Unload shouldn't have throw exception");
    }
    // check that no one owns the namespace
    NamespaceBundle bundle = bundleFactory.getBundle(new NamespaceName("prop-xyz/use/ns1"), Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED));
    assertFalse(pulsar.getNamespaceService().isServiceUnitOwned(bundle));
    assertFalse(otherPulsar.getNamespaceService().isServiceUnitOwned(bundle));
    pulsarClient.shutdown();
    LOG.info("--- RELOAD ---");
    // Force reload of namespace and wait for topic to be ready
    for (int i = 0; i < 30; i++) {
        try {
            admin.persistentTopics().getStats("persistent://prop-xyz/use/ns1/ds2");
            break;
        } catch (PulsarAdminException e) {
            LOG.warn("Failed to get topic stats.. {}", e.getMessage());
            Thread.sleep(1000);
        }
    }
    admin.persistentTopics().deleteSubscription("persistent://prop-xyz/use/ns1/ds2", "my-sub");
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/ds2");
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) ConflictException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 75 with NamespaceName

use of com.yahoo.pulsar.common.naming.NamespaceName in project pulsar by yahoo.

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().getDestinations("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 producer = pulsarClient.createProducer("persistent://prop-xyz/use/ns1/my-topic");
    producer.close();
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/my-topic");
    admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
    NamespaceName ns = new NamespaceName("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.createProducer("persistent://prop-xyz/use/ns2/my-topic");
    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(com.yahoo.pulsar.common.naming.NamespaceBundle) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) NotAuthorizedException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) NotAuthorizedException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PreconditionFailedException(com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) NotFoundException(com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) ConflictException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) AuthAction(com.yahoo.pulsar.common.policies.data.AuthAction) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) Producer(com.yahoo.pulsar.client.api.Producer) NamespaceEphemeralData(com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)93 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)41 Test (org.testng.annotations.Test)40 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)38 RestException (com.yahoo.pulsar.broker.web.RestException)25 Policies (com.yahoo.pulsar.common.policies.data.Policies)18 ApiOperation (io.swagger.annotations.ApiOperation)17 ApiResponses (io.swagger.annotations.ApiResponses)17 Path (javax.ws.rs.Path)17 KeeperException (org.apache.zookeeper.KeeperException)17 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)16 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)14 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)14 NamespaceBundles (com.yahoo.pulsar.common.naming.NamespaceBundles)13 Field (java.lang.reflect.Field)13 URL (java.net.URL)13 WebApplicationException (javax.ws.rs.WebApplicationException)13 PersistencePolicies (com.yahoo.pulsar.common.policies.data.PersistencePolicies)12 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)10 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)9