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());
}
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);
}
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);
}
}
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);
}
}
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);
}
Aggregations