Search in sources :

Example 46 with NamespaceBundle

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

the class NamespaceBundlesTest method testsplitBundles.

@Test
public void testsplitBundles() throws Exception {
    NamespaceName nsname = NamespaceName.get("pulsar/global/ns1");
    TopicName topicName = TopicName.get("persistent://pulsar/global/ns1/topic-1");
    NamespaceBundles bundles = factory.getBundles(nsname);
    NamespaceBundle bundle = bundles.findBundle(topicName);
    final int numberSplitBundles = 4;
    // (1) split in 4
    Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles = factory.splitBundles(bundle, numberSplitBundles);
    // existing_no_bundles(1) +
    // additional_new_split_bundle(4) -
    // parent_target_bundle(1)
    int totalExpectedSplitBundles = bundles.getBundles().size() + numberSplitBundles - 1;
    validateSplitBundlesRange(bundles.getFullBundle(), splitBundles.getRight());
    assertEquals(totalExpectedSplitBundles, splitBundles.getLeft().getBundles().size());
    // (2) split in 4: first bundle from above split bundles
    NamespaceBundleFactory utilityFactory = getNamespaceBundleFactory();
    NamespaceBundles bundles2 = splitBundles.getLeft();
    NamespaceBundle testChildBundle = bundles2.getBundles().get(0);
    Pair<NamespaceBundles, List<NamespaceBundle>> splitChildBundles = splitBundlesUtilFactory(utilityFactory, nsname, bundles2, testChildBundle, numberSplitBundles);
    // existing_no_bundles(4) +
    // additional_new_split_bundle(4) -
    // parent_target_bundle(1)
    totalExpectedSplitBundles = bundles2.getBundles().size() + numberSplitBundles - 1;
    validateSplitBundlesRange(testChildBundle, splitChildBundles.getRight());
    assertEquals(totalExpectedSplitBundles, splitChildBundles.getLeft().getBundles().size());
    // (3) split in 3: second bundle from above split bundles
    NamespaceBundle testChildBundl2 = bundles2.getBundles().get(1);
    Pair<NamespaceBundles, List<NamespaceBundle>> splitChildBundles2 = splitBundlesUtilFactory(utilityFactory, nsname, bundles2, testChildBundl2, 3);
    // existing_no_bundles(4) +
    // additional_new_split_bundle(3) -
    // parent_target_bundle(1)
    totalExpectedSplitBundles = bundles2.getBundles().size() + 3 - 1;
    validateSplitBundlesRange(testChildBundl2, splitChildBundles2.getRight());
    assertEquals(totalExpectedSplitBundles, splitChildBundles2.getLeft().getBundles().size());
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) ArrayList(java.util.ArrayList) List(java.util.List) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 47 with NamespaceBundle

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

the class NamespaceBundlesTest method assertBundles.

private void assertBundles(NamespaceBundleFactory utilityFactory, NamespaceName nsname, NamespaceBundle bundle, Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles, int numBundles) throws Exception {
    NamespaceBundle bundle1 = splitBundles.getRight().get(0);
    NamespaceBundle bundle2 = splitBundles.getRight().get(1);
    NamespaceBundles nspaceBundles = splitBundles.getLeft();
    Pair<NamespaceBundles, List<NamespaceBundle>> bundle1Split = splitBundlesUtilFactory(utilityFactory, nsname, nspaceBundles, bundle1, numBundles);
    assertBundleDivideInTwo(bundle1, bundle1Split.getRight(), numBundles);
    Pair<NamespaceBundles, List<NamespaceBundle>> bundle2Split = splitBundlesUtilFactory(utilityFactory, nsname, nspaceBundles, bundle2, numBundles);
    assertBundleDivideInTwo(bundle2, bundle2Split.getRight(), numBundles);
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) ArrayList(java.util.ArrayList) List(java.util.List)

Example 48 with NamespaceBundle

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

the class NonPersistentTopicTest method testLoadManagerAssignmentForNonPersistentTestAssignment.

/**
 * verifies load manager assigns topic only if broker started in non-persistent mode
 *
 * <pre>
 * 1. Start broker with disable non-persistent topic mode
 * 2. Create namespace with non-persistency set
 * 3. Create non-persistent topic
 * 4. Load-manager should not be able to find broker
 * 5. Create producer on that topic should fail
 * </pre>
 */
@Test(dataProvider = "loadManager")
public void testLoadManagerAssignmentForNonPersistentTestAssignment(String loadManagerName) throws Exception {
    final String namespace = "my-property/use/my-ns";
    final String topicName = "non-persistent://" + namespace + "/loadManager";
    final String defaultLoadManagerName = conf.getLoadManagerClassName();
    final boolean defaultENableNonPersistentTopic = conf.isEnableNonPersistentTopics();
    try {
        // start broker to not own non-persistent namespace and create non-persistent namespace
        stopBroker();
        conf.setEnableNonPersistentTopics(false);
        conf.setLoadManagerClassName(loadManagerName);
        startBroker();
        Field field = PulsarService.class.getDeclaredField("loadManager");
        field.setAccessible(true);
        @SuppressWarnings("unchecked") AtomicReference<LoadManager> loadManagerRef = (AtomicReference<LoadManager>) field.get(pulsar);
        LoadManager manager = LoadManager.create(pulsar);
        manager.start();
        loadManagerRef.set(manager);
        NamespaceBundle fdqn = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
        LoadManager loadManager = pulsar.getLoadManager().get();
        ResourceUnit broker = null;
        try {
            broker = loadManager.getLeastLoaded(fdqn).get();
        } catch (Exception e) {
        // Ok. (ModulearLoadManagerImpl throws RuntimeException incase don't find broker)
        }
        assertNull(broker);
        try {
            Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).createAsync().get(1, TimeUnit.SECONDS);
            producer.close();
            fail("topic loading should have failed");
        } catch (Exception e) {
        // Ok
        }
        NonPersistentTopic topicRef = (NonPersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
        assertNull(topicRef);
    } finally {
        conf.setEnableNonPersistentTopics(defaultENableNonPersistentTopic);
        conf.setLoadManagerClassName(defaultLoadManagerName);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ResourceUnit(org.apache.pulsar.broker.loadbalance.ResourceUnit) Field(java.lang.reflect.Field) LoadManager(org.apache.pulsar.broker.loadbalance.LoadManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Example 49 with NamespaceBundle

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

the class NonPersistentTopicTest method testNonPersistentBrokerModeRejectPersistentTopic.

/**
 * verifies that broker started with onlyNonPersistent mode doesn't own persistent-topic
 *
 * @param loadManagerName
 * @throws Exception
 */
@Test(dataProvider = "loadManager")
public void testNonPersistentBrokerModeRejectPersistentTopic(String loadManagerName) throws Exception {
    final String namespace = "my-property/use/my-ns";
    final String topicName = "persistent://" + namespace + "/loadManager";
    final String defaultLoadManagerName = conf.getLoadManagerClassName();
    final boolean defaultEnablePersistentTopic = conf.isEnablePersistentTopics();
    final boolean defaultEnableNonPersistentTopic = conf.isEnableNonPersistentTopics();
    try {
        // start broker to not own non-persistent namespace and create non-persistent namespace
        stopBroker();
        conf.setEnableNonPersistentTopics(true);
        conf.setEnablePersistentTopics(false);
        conf.setLoadManagerClassName(loadManagerName);
        startBroker();
        Field field = PulsarService.class.getDeclaredField("loadManager");
        field.setAccessible(true);
        @SuppressWarnings("unchecked") AtomicReference<LoadManager> loadManagerRef = (AtomicReference<LoadManager>) field.get(pulsar);
        LoadManager manager = LoadManager.create(pulsar);
        manager.start();
        loadManagerRef.set(manager);
        NamespaceBundle fdqn = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
        LoadManager loadManager = pulsar.getLoadManager().get();
        ResourceUnit broker = null;
        try {
            broker = loadManager.getLeastLoaded(fdqn).get();
        } catch (Exception e) {
        // Ok. (ModulearLoadManagerImpl throws RuntimeException incase don't find broker)
        }
        assertNull(broker);
        try {
            Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).createAsync().get(1, TimeUnit.SECONDS);
            producer.close();
            fail("topic loading should have failed");
        } catch (Exception e) {
        // Ok
        }
        NonPersistentTopic topicRef = (NonPersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
        assertNull(topicRef);
    } finally {
        conf.setEnablePersistentTopics(defaultEnablePersistentTopic);
        conf.setEnableNonPersistentTopics(defaultEnableNonPersistentTopic);
        conf.setLoadManagerClassName(defaultLoadManagerName);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ResourceUnit(org.apache.pulsar.broker.loadbalance.ResourceUnit) Field(java.lang.reflect.Field) LoadManager(org.apache.pulsar.broker.loadbalance.LoadManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) Test(org.testng.annotations.Test) ZookeeperServerTest(org.apache.pulsar.zookeeper.ZookeeperServerTest)

Example 50 with NamespaceBundle

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

the class BrokerService method removeTopicFromCache.

public void removeTopicFromCache(String topic) {
    try {
        TopicName topicName = TopicName.get(topic);
        NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(topicName);
        checkArgument(namespaceBundle instanceof NamespaceBundle);
        String bundleName = namespaceBundle.toString();
        String namespaceName = topicName.getNamespaceObject().toString();
        synchronized (multiLayerTopicsMap) {
            ConcurrentOpenHashMap<String, ConcurrentOpenHashMap<String, Topic>> namespaceMap = multiLayerTopicsMap.get(namespaceName);
            ConcurrentOpenHashMap<String, Topic> bundleMap = namespaceMap.get(bundleName);
            bundleMap.remove(topic);
            if (bundleMap.isEmpty()) {
                namespaceMap.remove(bundleName);
            }
            if (namespaceMap.isEmpty()) {
                multiLayerTopicsMap.remove(namespaceName);
                final ClusterReplicationMetrics clusterReplicationMetrics = pulsarStats.getClusterReplicationMetrics();
                replicationClients.forEach((cluster, client) -> {
                    clusterReplicationMetrics.remove(clusterReplicationMetrics.getKeyName(namespaceName, cluster));
                });
            }
        }
    } catch (Exception e) {
        log.warn("Got exception when retrieving bundle name during removeTopicFromCache", e);
    }
    topics.remove(topic);
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) ClusterReplicationMetrics(org.apache.pulsar.broker.stats.ClusterReplicationMetrics) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) NonPersistentTopic(org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) IOException(java.io.IOException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) KeeperException(org.apache.zookeeper.KeeperException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) TopicName(org.apache.pulsar.common.naming.TopicName)

Aggregations

NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)66 Test (org.testng.annotations.Test)42 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)23 NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)18 TopicName (org.apache.pulsar.common.naming.TopicName)18 KeeperException (org.apache.zookeeper.KeeperException)17 Field (java.lang.reflect.Field)14 RestException (org.apache.pulsar.broker.web.RestException)14 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)13 Policies (org.apache.pulsar.common.policies.data.Policies)13 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)11 ExecutionException (java.util.concurrent.ExecutionException)10 URL (java.net.URL)9 List (java.util.List)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 URI (java.net.URI)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 LoadManager (org.apache.pulsar.broker.loadbalance.LoadManager)7 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)7