use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class AdminResource method getNamespacePolicies.
protected Policies getNamespacePolicies(String property, String cluster, String namespace) {
try {
Policies policies = policiesCache().get(AdminResource.path(POLICIES, property, cluster, namespace)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist"));
// fetch bundles from LocalZK-policies
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(NamespaceName.get(property, cluster, namespace));
BundlesData bundleData = NamespaceBundleFactory.getBundlesData(bundles);
policies.bundles = bundleData != null ? bundleData : policies.bundles;
return policies;
} catch (RestException re) {
throw re;
} catch (Exception e) {
log.error("[{}] Failed to get namespace policies {}/{}/{}", clientAppId(), property, cluster, namespace, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.naming.NamespaceBundles 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.NamespaceBundles 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.NamespaceBundles 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.NamespaceBundles in project incubator-pulsar by apache.
the class NamespaceServiceTest method testIsServiceUnitDisabled.
@Test
public void testIsServiceUnitDisabled() throws Exception {
OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
ManagedLedger ledger = mock(ManagedLedger.class);
when(ledger.getCursors()).thenReturn(Lists.newArrayList());
doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
ownership.setAccessible(true);
ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
NamespaceService namespaceService = pulsar.getNamespaceService();
NamespaceName nsname = NamespaceName.get("pulsar/global/ns1");
TopicName topicName = TopicName.get("persistent://pulsar/global/ns1/topic-1");
NamespaceBundles bundles = namespaceService.getNamespaceBundleFactory().getBundles(nsname);
NamespaceBundle originalBundle = bundles.findBundle(topicName);
assertFalse(namespaceService.isNamespaceBundleDisabled(originalBundle));
}
Aggregations