Search in sources :

Example 56 with NamespaceBundle

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

the class AdminApiTest method testNamespaceUnloadBundle.

@Test
public void testNamespaceUnloadBundle() throws Exception {
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
    // Force to create a topic
    publishMessagesOnPersistentTopic("persistent://prop-xyz/use/ns1/ds2", 0);
    assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList("persistent://prop-xyz/use/ns1/ds2"));
    // create consumer and subscription
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://prop-xyz/use/ns1/ds2").subscriptionName("my-sub").subscribe();
    assertEquals(admin.persistentTopics().getSubscriptions("persistent://prop-xyz/use/ns1/ds2"), Lists.newArrayList("my-sub"));
    // Create producer
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://prop-xyz/use/ns1/ds2").create();
    for (int i = 0; i < 10; i++) {
        String message = "message-" + i;
        producer.send(message.getBytes());
    }
    consumer.close();
    producer.close();
    try {
        admin.namespaces().unloadNamespaceBundle("prop-xyz/use/ns1", "0x00000000_0xffffffff");
    } catch (Exception e) {
        fail("Unload shouldn't have throw exception");
    }
    // check that no one owns the namespace
    NamespaceBundle bundle = bundleFactory.getBundle(NamespaceName.get("prop-xyz/use/ns1"), Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED));
    assertFalse(pulsar.getNamespaceService().isServiceUnitOwned(bundle));
    assertFalse(otherPulsar.getNamespaceService().isServiceUnitOwned(bundle));
    pulsarClient.shutdown();
    LOG.info("--- RELOAD ---");
    // Force reload of namespace and wait for topic to be ready
    for (int i = 0; i < 30; i++) {
        try {
            admin.persistentTopics().getStats("persistent://prop-xyz/use/ns1/ds2");
            break;
        } catch (PulsarAdminException e) {
            LOG.warn("Failed to get topic stats.. {}", e.getMessage());
            Thread.sleep(1000);
        }
    }
    admin.persistentTopics().deleteSubscription("persistent://prop-xyz/use/ns1/ds2", "my-sub");
    admin.persistentTopics().delete("persistent://prop-xyz/use/ns1/ds2");
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) 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 57 with NamespaceBundle

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

the class NamespaceServiceTest method testSplitMapWithRefreshedStatMap.

@Test
public void testSplitMapWithRefreshedStatMap() 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);
    PersistentTopic topic = new PersistentTopic(topicName.toString(), ledger, pulsar.getBrokerService());
    Method method = pulsar.getBrokerService().getClass().getDeclaredMethod("addTopicToStatsMaps", TopicName.class, Topic.class);
    method.setAccessible(true);
    method.invoke(pulsar.getBrokerService(), topicName, topic);
    String nspace = originalBundle.getNamespaceObject().toString();
    List<Topic> list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace, originalBundle.toString());
    assertNotNull(list);
    // 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);
    }
    // old bundle should be removed from status-map
    list = this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace, originalBundle.toString());
    assertTrue(list.isEmpty());
    // status-map should be updated with new split bundles
    NamespaceBundle splitBundle = pulsar.getNamespaceService().getBundle(topicName);
    assertTrue(!CollectionUtils.isEmpty(this.pulsar.getBrokerService().getAllTopicsFromNamespaceBundle(nspace, splitBundle.toString())));
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) TopicName(org.apache.pulsar.common.naming.TopicName) Field(java.lang.reflect.Field) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 58 with NamespaceBundle

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

the class NamespaceServiceTest method testUnloadNamespaceBundleWithStuckTopic.

/**
 * It verifies that unloading bundle will timeout and will not hung even if one of the topic-unloading stuck.
 *
 * @throws Exception
 */
@Test(timeOut = 6000)
public void testUnloadNamespaceBundleWithStuckTopic() throws Exception {
    final String topicName = "persistent://my-property/use/my-ns/my-topic1";
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name").subscribe();
    ConcurrentOpenHashMap<String, CompletableFuture<Topic>> topics = pulsar.getBrokerService().getTopics();
    Topic spyTopic = spy(topics.get(topicName).get());
    topics.clear();
    CompletableFuture<Topic> topicFuture = CompletableFuture.completedFuture(spyTopic);
    // add mock topic
    topics.put(topicName, topicFuture);
    // return uncompleted future as close-topic result.
    doAnswer(new Answer<CompletableFuture<Void>>() {

        @Override
        public CompletableFuture<Void> answer(InvocationOnMock invocation) throws Throwable {
            return new CompletableFuture<Void>();
        }
    }).when(spyTopic).close();
    NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
    // try to unload bundle whose topic will be stuck
    pulsar.getNamespaceService().unloadNamespaceBundle(bundle, 1, TimeUnit.SECONDS);
    try {
        pulsar.getLocalZkCache().getZooKeeper().getData(ServiceUnitZkUtils.path(bundle), null, null);
        fail("it should fail as node is not present");
    } catch (org.apache.zookeeper.KeeperException.NoNodeException e) {
    // ok
    }
    consumer.close();
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) CompletableFuture(java.util.concurrent.CompletableFuture) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 59 with NamespaceBundle

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

the class OwnershipCacheTest method testGetOwnedServiceUnits.

@Test
public void testGetOwnedServiceUnits() throws Exception {
    OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
    NamespaceName testNs = NamespaceName.get("pulsar/test/ns-6");
    NamespaceBundle testBundle = bundleFactory.getFullBundle(testNs);
    // case 1: no one owns the namespace
    assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
    assertTrue(cache.getOwnedBundles().isEmpty());
    // case 2: someone else owns the namespace
    ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle), new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884", "http://otherhost:8080", "https://otherhost:4443", false));
    assertTrue(cache.getOwnedBundles().isEmpty());
    Thread.sleep(500);
    // try to acquire, which will load the read-only cache
    NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();
    assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
    assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
    assertTrue(!data1.isDisabled());
    assertTrue(cache.getOwnedBundles().isEmpty());
    // case 3: this broker owns the namespace
    // delete the ephemeral node by others
    zkCache.getZooKeeper().delete(ServiceUnitZkUtils.path(testBundle), -1);
    // force to read directly from ZK
    localCache.ownerInfoCache().invalidate(ServiceUnitZkUtils.path(testBundle));
    data1 = cache.tryAcquiringOwnership(testBundle).get();
    assertEquals(data1.getNativeUrl(), selfBrokerUrl);
    assertTrue(!data1.isDisabled());
    assertTrue(cache.getOwnedBundles().size() == 1);
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Test(org.testng.annotations.Test)

Example 60 with NamespaceBundle

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

the class OwnershipCacheTest method testGetOwner.

@Test
public void testGetOwner() throws Exception {
    OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
    NamespaceBundle testBundle = bundleFactory.getFullBundle(NamespaceName.get("pulsar/test/ns-3"));
    // case 1: no one owns the namespace
    assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
    // case 2: someone owns the namespace
    ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle), new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884", "http://otherhost:8080", "https://otherhost:4443", false));
    // try to acquire, which will load the read-only cache
    NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();
    assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
    assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
    assertTrue(!data1.isDisabled());
    // Now do getOwner and compare w/ the returned values
    NamespaceEphemeralData readOnlyData = cache.getOwnerAsync(testBundle).get().get();
    assertEquals(data1, readOnlyData);
    MockZooKeeper mockZk = (MockZooKeeper) zkCache.getZooKeeper();
    mockZk.failNow(KeeperException.Code.NONODE);
    Optional<NamespaceEphemeralData> res = cache.getOwnerAsync(bundleFactory.getFullBundle(NamespaceName.get("pulsar/test/ns-none"))).get();
    assertFalse(res.isPresent());
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) Test(org.testng.annotations.Test)

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