Search in sources :

Example 6 with NamespaceBundles

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

the class NamespaceServiceTest method testCreateNamespaceWithDefaultNumberOfBundles.

@Test
public void testCreateNamespaceWithDefaultNumberOfBundles() throws Exception {
    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    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);
    // Split bundle and take ownership of split bundles
    CompletableFuture<Void> result = namespaceService.splitAndOwnBundle(originalBundle, false);
    try {
        result.get();
    } catch (Exception e) {
        // make sure: no failure
        fail("split bundle faild", e);
    }
    NamespaceBundleFactory bundleFactory = this.pulsar.getNamespaceService().getNamespaceBundleFactory();
    NamespaceBundles updatedNsBundles = bundleFactory.getBundles(nsname);
    // new updated bundles shouldn't be null
    assertNotNull(updatedNsBundles);
    List<NamespaceBundle> bundleList = updatedNsBundles.getBundles();
    assertNotNull(bundles);
    NamespaceBundleFactory utilityFactory = NamespaceBundleFactory.createFactory(pulsar, Hashing.crc32());
    // (1) validate bundleFactory-cache has newly split bundles and removed old parent bundle
    Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles = splitBundles(utilityFactory, nsname, bundles, originalBundle);
    assertNotNull(splitBundles);
    Set<NamespaceBundle> splitBundleSet = new HashSet<>(splitBundles.getRight());
    splitBundleSet.removeAll(bundleList);
    assertTrue(splitBundleSet.isEmpty());
    // (2) validate LocalZookeeper policies updated with newly created split
    // bundles
    String path = joinPath(LOCAL_POLICIES_ROOT, nsname.toString());
    byte[] content = this.pulsar.getLocalZkCache().getZooKeeper().getData(path, null, new Stat());
    Policies policies = ObjectMapperFactory.getThreadLocal().readValue(content, Policies.class);
    NamespaceBundles localZkBundles = bundleFactory.getBundles(nsname, policies.bundles);
    assertTrue(updatedNsBundles.equals(localZkBundles));
    log.info("Policies: {}", policies);
    // (3) validate ownership of new split bundles by local owner
    bundleList.stream().forEach(b -> {
        try {
            byte[] data = this.pulsar.getLocalZkCache().getZooKeeper().getData(ServiceUnitZkUtils.path(b), null, new Stat());
            NamespaceEphemeralData node = ObjectMapperFactory.getThreadLocal().readValue(data, NamespaceEphemeralData.class);
            Assert.assertEquals(node.getNativeUrl(), this.pulsar.getBrokerServiceUrl());
        } catch (Exception e) {
            fail("failed to setup ownership", e);
        }
    });
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) TopicName(org.apache.pulsar.common.naming.TopicName) Field(java.lang.reflect.Field) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Stat(org.apache.zookeeper.data.Stat) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 7 with NamespaceBundles

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

the class NamespaceServiceTest method testSplitAndOwnBundles.

@Test
public void testSplitAndOwnBundles() throws Exception {
    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    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);
    // Split bundle and take ownership of split bundles
    CompletableFuture<Void> result = namespaceService.splitAndOwnBundle(originalBundle, false);
    try {
        result.get();
    } catch (Exception e) {
        // make sure: no failure
        fail("split bundle faild", e);
    }
    NamespaceBundleFactory bundleFactory = this.pulsar.getNamespaceService().getNamespaceBundleFactory();
    NamespaceBundles updatedNsBundles = bundleFactory.getBundles(nsname);
    // new updated bundles shouldn't be null
    assertNotNull(updatedNsBundles);
    List<NamespaceBundle> bundleList = updatedNsBundles.getBundles();
    assertNotNull(bundles);
    NamespaceBundleFactory utilityFactory = NamespaceBundleFactory.createFactory(pulsar, Hashing.crc32());
    // (1) validate bundleFactory-cache has newly split bundles and removed old parent bundle
    Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles = splitBundles(utilityFactory, nsname, bundles, originalBundle);
    assertNotNull(splitBundles);
    Set<NamespaceBundle> splitBundleSet = new HashSet<>(splitBundles.getRight());
    splitBundleSet.removeAll(bundleList);
    assertTrue(splitBundleSet.isEmpty());
    // (2) validate LocalZookeeper policies updated with newly created split
    // bundles
    String path = joinPath(LOCAL_POLICIES_ROOT, nsname.toString());
    byte[] content = this.pulsar.getLocalZkCache().getZooKeeper().getData(path, null, new Stat());
    Policies policies = ObjectMapperFactory.getThreadLocal().readValue(content, Policies.class);
    NamespaceBundles localZkBundles = bundleFactory.getBundles(nsname, policies.bundles);
    assertTrue(updatedNsBundles.equals(localZkBundles));
    log.info("Policies: {}", policies);
    // (3) validate ownership of new split bundles by local owner
    bundleList.stream().forEach(b -> {
        try {
            byte[] data = this.pulsar.getLocalZkCache().getZooKeeper().getData(ServiceUnitZkUtils.path(b), null, new Stat());
            NamespaceEphemeralData node = ObjectMapperFactory.getThreadLocal().readValue(data, NamespaceEphemeralData.class);
            Assert.assertEquals(node.getNativeUrl(), this.pulsar.getBrokerServiceUrl());
        } catch (Exception e) {
            fail("failed to setup ownership", e);
        }
    });
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) TopicName(org.apache.pulsar.common.naming.TopicName) Field(java.lang.reflect.Field) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Stat(org.apache.zookeeper.data.Stat) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with NamespaceBundles

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

the class NamespaceServiceTest method testremoveOwnershipNamespaceBundle.

@Test
public void testremoveOwnershipNamespaceBundle() throws Exception {
    OwnershipCache ownershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    ManagedLedger ledger = mock(ManagedLedger.class);
    when(ledger.getCursors()).thenReturn(Lists.newArrayList());
    doNothing().when(ownershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), ownershipCache);
    NamespaceService namespaceService = pulsar.getNamespaceService();
    NamespaceName nsname = NamespaceName.get("prop/use/ns1");
    NamespaceBundles bundles = namespaceService.getNamespaceBundleFactory().getBundles(nsname);
    NamespaceBundle bundle = bundles.getBundles().get(0);
    assertNotNull(ownershipCache.tryAcquiringOwnership(bundle));
    assertNotNull(ownershipCache.getOwnedBundle(bundle));
    ownershipCache.removeOwnership(bundles).get();
    assertNull(ownershipCache.getOwnedBundle(bundle));
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Field(java.lang.reflect.Field) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) Test(org.testng.annotations.Test)

Example 9 with NamespaceBundles

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

the class AdminApiTest method testNamespaceSplitBundle.

@Test
public void testNamespaceSplitBundle() throws Exception {
    // Force to create a topic
    final String namespace = "prop-xyz/use/ns1";
    final String topicName = (new StringBuilder("persistent://")).append(namespace).append("/ds2").toString();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    producer.send("message".getBytes());
    publishMessagesOnPersistentTopic(topicName, 0);
    assertEquals(admin.persistentTopics().getList(namespace), Lists.newArrayList(topicName));
    try {
        admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", true);
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    // bundle-factory cache must have updated split bundles
    NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
    }
    producer.close();
}
Also used : NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 10 with NamespaceBundles

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

the class AdminApiTest method testNamespaceSplitBundleConcurrent.

@Test
public void testNamespaceSplitBundleConcurrent() throws Exception {
    // Force to create a topic
    final String namespace = "prop-xyz/use/ns1";
    final String topicName = (new StringBuilder("persistent://")).append(namespace).append("/ds2").toString();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    producer.send("message".getBytes());
    publishMessagesOnPersistentTopic(topicName, 0);
    assertEquals(admin.persistentTopics().getList(namespace), Lists.newArrayList(topicName));
    try {
        admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0xffffffff", false);
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    // bundle-factory cache must have updated split bundles
    NamespaceBundles bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    String[] splitRange = { namespace + "/0x00000000_0x7fffffff", namespace + "/0x7fffffff_0xffffffff" };
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertEquals(bundles.getBundles().get(i).toString(), splitRange[i]);
    }
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        executorService.invokeAll(Arrays.asList(() -> {
            log.info("split 2 bundles at the same time. spilt: 0x00000000_0x7fffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0x7fffffff", false);
            return null;
        }, () -> {
            log.info("split 2 bundles at the same time. spilt: 0x7fffffff_0xffffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0x7fffffff_0xffffffff", false);
            return null;
        }));
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    String[] splitRange4 = { namespace + "/0x00000000_0x3fffffff", namespace + "/0x3fffffff_0x7fffffff", namespace + "/0x7fffffff_0xbfffffff", namespace + "/0xbfffffff_0xffffffff" };
    bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    assertEquals(bundles.getBundles().size(), 4);
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertEquals(bundles.getBundles().get(i).toString(), splitRange4[i]);
    }
    try {
        executorService.invokeAll(Arrays.asList(() -> {
            log.info("split 4 bundles at the same time. spilt: 0x00000000_0x3fffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0x00000000_0x3fffffff", false);
            return null;
        }, () -> {
            log.info("split 4 bundles at the same time. spilt: 0x3fffffff_0x7fffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0x3fffffff_0x7fffffff", false);
            return null;
        }, () -> {
            log.info("split 4 bundles at the same time. spilt: 0x7fffffff_0xbfffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0x7fffffff_0xbfffffff", false);
            return null;
        }, () -> {
            log.info("split 4 bundles at the same time. spilt: 0xbfffffff_0xffffffff ");
            admin.namespaces().splitNamespaceBundle(namespace, "0xbfffffff_0xffffffff", false);
            return null;
        }));
    } catch (Exception e) {
        fail("split bundle shouldn't have thrown exception");
    }
    String[] splitRange8 = { namespace + "/0x00000000_0x1fffffff", namespace + "/0x1fffffff_0x3fffffff", namespace + "/0x3fffffff_0x5fffffff", namespace + "/0x5fffffff_0x7fffffff", namespace + "/0x7fffffff_0x9fffffff", namespace + "/0x9fffffff_0xbfffffff", namespace + "/0xbfffffff_0xdfffffff", namespace + "/0xdfffffff_0xffffffff" };
    bundles = bundleFactory.getBundles(NamespaceName.get(namespace));
    assertEquals(bundles.getBundles().size(), 8);
    for (int i = 0; i < bundles.getBundles().size(); i++) {
        assertEquals(bundles.getBundles().get(i).toString(), splitRange8[i]);
    }
    producer.close();
}
Also used : NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) ExecutorService(java.util.concurrent.ExecutorService) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NotAuthorizedException(org.apache.pulsar.client.admin.PulsarAdminException.NotAuthorizedException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)21 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)17 Test (org.testng.annotations.Test)13 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)11 RestException (org.apache.pulsar.broker.web.RestException)8 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)7 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)7 TopicName (org.apache.pulsar.common.naming.TopicName)7 KeeperException (org.apache.zookeeper.KeeperException)7 Field (java.lang.reflect.Field)6 List (java.util.List)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 Policies (org.apache.pulsar.common.policies.data.Policies)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)4 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)4 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)4 NamespaceBundleFactory (org.apache.pulsar.common.naming.NamespaceBundleFactory)4 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)4 URL (java.net.URL)3