use of org.apache.pulsar.common.naming.NamespaceBundle in project incubator-pulsar by apache.
the class NamespaceBundlesTest method testConstructor.
@SuppressWarnings("unchecked")
@Test
public void testConstructor() throws Exception {
try {
new NamespaceBundles(null, (SortedSet<Long>) null, null);
fail("Should fail w/ null pointer exception");
} catch (NullPointerException npe) {
// OK, expected
}
try {
new NamespaceBundles(NamespaceName.get("pulsar/use/ns2"), (SortedSet<Long>) null, null);
fail("Should fail w/ null pointer exception");
} catch (NullPointerException npe) {
// OK, expected
}
try {
new NamespaceBundles(NamespaceName.get("pulsar.use.ns2"), (SortedSet<Long>) null, null);
fail("Should fail w/ illegal argument exception");
} catch (IllegalArgumentException iae) {
// OK, expected
}
try {
new NamespaceBundles(NamespaceName.get("pulsar/use/ns2"), (SortedSet<Long>) null, factory);
fail("Should fail w/ null pointer exception");
} catch (NullPointerException npe) {
// OK, expected
}
SortedSet<Long> partitions = Sets.newTreeSet();
try {
new NamespaceBundles(NamespaceName.get("pulsar/use/ns2"), partitions, factory);
fail("Should fail w/ illegal argument exception");
} catch (IllegalArgumentException iae) {
// OK, expected
}
partitions.add(0l);
partitions.add(0x10000000l);
partitions.add(0x40000000l);
partitions.add(0xffffffffl);
NamespaceBundles bundles = new NamespaceBundles(NamespaceName.get("pulsar/use/ns2"), partitions, factory);
Field partitionField = NamespaceBundles.class.getDeclaredField("partitions");
Field nsField = NamespaceBundles.class.getDeclaredField("nsname");
Field bundlesField = NamespaceBundles.class.getDeclaredField("bundles");
partitionField.setAccessible(true);
nsField.setAccessible(true);
bundlesField.setAccessible(true);
long[] partFld = (long[]) partitionField.get(bundles);
// the same instance
assertEquals(partitions.size(), partFld.length);
NamespaceName nsFld = (NamespaceName) nsField.get(bundles);
assertTrue(nsFld.toString().equals("pulsar/use/ns2"));
ArrayList<NamespaceBundle> bundleList = (ArrayList<NamespaceBundle>) bundlesField.get(bundles);
assertEquals(bundleList.size(), 3);
assertEquals(bundleList.get(0), factory.getBundle(nsFld, Range.range(0l, BoundType.CLOSED, 0x10000000l, BoundType.OPEN)));
assertEquals(bundleList.get(1), factory.getBundle(nsFld, Range.range(0x10000000l, BoundType.CLOSED, 0x40000000l, BoundType.OPEN)));
assertEquals(bundleList.get(2), factory.getBundle(nsFld, Range.range(0x40000000l, BoundType.CLOSED, 0xffffffffl, BoundType.CLOSED)));
}
use of org.apache.pulsar.common.naming.NamespaceBundle in project incubator-pulsar by apache.
the class NamespaceBundlesTest method testSplitBundleInTwo.
@Test
public void testSplitBundleInTwo() throws Exception {
final int NO_BUNDLES = 2;
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);
// (1) split : [0x00000000,0xffffffff] => [0x00000000_0x7fffffff,0x7fffffff_0xffffffff]
Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles = factory.splitBundles(bundle, NO_BUNDLES);
assertNotNull(splitBundles);
assertBundleDivideInTwo(bundle, splitBundles.getRight(), NO_BUNDLES);
// (2) split: [0x00000000,0x7fffffff] => [0x00000000_0x3fffffff,0x3fffffff_0x7fffffff],
// [0x7fffffff,0xffffffff] => [0x7fffffff_0xbfffffff,0xbfffffff_0xffffffff]
NamespaceBundleFactory utilityFactory = getNamespaceBundleFactory();
assertBundles(utilityFactory, nsname, bundle, splitBundles, NO_BUNDLES);
// (3) split: [0x00000000,0x3fffffff] => [0x00000000_0x1fffffff,0x1fffffff_0x3fffffff],
// [0x3fffffff,0x7fffffff] => [0x3fffffff_0x5fffffff,0x5fffffff_0x7fffffff]
Pair<NamespaceBundles, List<NamespaceBundle>> splitChildBundles = splitBundlesUtilFactory(utilityFactory, nsname, splitBundles.getLeft(), splitBundles.getRight().get(0), NO_BUNDLES);
assertBundles(utilityFactory, nsname, splitBundles.getRight().get(0), splitChildBundles, NO_BUNDLES);
// (4) split: [0x7fffffff,0xbfffffff] => [0x7fffffff_0x9fffffff,0x9fffffff_0xbfffffff],
// [0xbfffffff,0xffffffff] => [0xbfffffff_0xdfffffff,0xdfffffff_0xffffffff]
splitChildBundles = splitBundlesUtilFactory(utilityFactory, nsname, splitBundles.getLeft(), splitBundles.getRight().get(1), NO_BUNDLES);
assertBundles(utilityFactory, nsname, splitBundles.getRight().get(1), splitChildBundles, NO_BUNDLES);
}
use of org.apache.pulsar.common.naming.NamespaceBundle in project incubator-pulsar by apache.
the class BrokerService method addTopicToStatsMaps.
private void addTopicToStatsMaps(TopicName topicName, Topic topic) {
try {
NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(topicName);
if (namespaceBundle != null) {
synchronized (multiLayerTopicsMap) {
String serviceUnit = namespaceBundle.toString();
//
multiLayerTopicsMap.computeIfAbsent(topicName.getNamespace(), //
k -> new ConcurrentOpenHashMap<>()).computeIfAbsent(serviceUnit, //
k -> new ConcurrentOpenHashMap<>()).put(topicName.toString(), topic);
}
}
invalidateOfflineTopicStatCache(topicName);
} catch (Exception e) {
log.warn("Got exception when retrieving bundle name during create persistent topic", e);
}
}
use of org.apache.pulsar.common.naming.NamespaceBundle in project incubator-pulsar by apache.
the class NamespacesBase method internalUnsubscribeNamespace.
protected void internalUnsubscribeNamespace(String subscription, boolean authoritative) {
validateAdminAccessOnProperty(namespaceName.getProperty());
try {
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
Exception exception = null;
for (NamespaceBundle nsBundle : bundles.getBundles()) {
try {
// check if the bundle is owned by any broker, if not then there are no subscriptions
if (pulsar().getNamespaceService().getOwner(nsBundle).isPresent()) {
// TODO: make this admin call asynchronous
pulsar().getAdminClient().namespaces().unsubscribeNamespaceBundle(namespaceName.toString(), nsBundle.getBundleRange(), subscription);
}
} catch (Exception e) {
if (exception == null) {
exception = e;
}
}
}
if (exception != null) {
if (exception instanceof PulsarAdminException) {
throw new RestException((PulsarAdminException) exception);
} else {
throw new RestException(exception.getCause());
}
}
} catch (WebApplicationException wae) {
throw wae;
} catch (Exception e) {
throw new RestException(e);
}
log.info("[{}] Successfully unsubscribed {} on all the bundles for namespace {}", clientAppId(), subscription, namespaceName);
}
use of org.apache.pulsar.common.naming.NamespaceBundle in project incubator-pulsar by apache.
the class NamespaceService method unloadSLANamespace.
public void unloadSLANamespace() throws Exception {
PulsarAdmin adminClient = null;
String namespaceName = getSLAMonitorNamespace(host, config);
LOG.info("Checking owner for SLA namespace {}", namespaceName);
NamespaceBundle nsFullBundle = getFullBundle(NamespaceName.get(namespaceName));
if (!getOwner(nsFullBundle).isPresent()) {
// Next lookup will assign the bundle to this broker.
return;
}
LOG.info("Trying to unload SLA namespace {}", namespaceName);
adminClient = pulsar.getAdminClient();
adminClient.namespaces().unload(namespaceName);
LOG.info("Namespace {} unloaded successfully", namespaceName);
}
Aggregations