Search in sources :

Example 6 with NamespaceService

use of org.apache.pulsar.broker.namespace.NamespaceService in project incubator-pulsar by apache.

the class ServerCnxTest method setup.

@BeforeMethod
public void setup() throws Exception {
    svcConfig = spy(new ServiceConfiguration());
    pulsar = spy(new PulsarService(svcConfig));
    doReturn(new DefaultSchemaRegistryService()).when(pulsar).getSchemaRegistryService();
    svcConfig.setKeepAliveIntervalSeconds(inSec(1, TimeUnit.SECONDS));
    svcConfig.setBacklogQuotaCheckEnabled(false);
    doReturn(svcConfig).when(pulsar).getConfiguration();
    doReturn("use").when(svcConfig).getClusterName();
    mlFactoryMock = mock(ManagedLedgerFactory.class);
    doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
    ZooKeeper mockZk = createMockZooKeeper();
    doReturn(mockZk).when(pulsar).getZkClient();
    doReturn(createMockBookKeeper(mockZk)).when(pulsar).getBookKeeperClient();
    configCacheService = mock(ConfigurationCacheService.class);
    ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
    doReturn(Optional.empty()).when(zkDataCache).get(anyObject());
    doReturn(zkDataCache).when(configCacheService).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    LocalZooKeeperCacheService zkCache = mock(LocalZooKeeperCacheService.class);
    doReturn(CompletableFuture.completedFuture(Optional.empty())).when(zkDataCache).getAsync(any());
    doReturn(zkDataCache).when(zkCache).policiesCache();
    doReturn(configCacheService).when(pulsar).getConfigurationCache();
    doReturn(zkCache).when(pulsar).getLocalZkCacheService();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    namespaceService = mock(NamespaceService.class);
    doReturn(namespaceService).when(pulsar).getNamespaceService();
    doReturn(true).when(namespaceService).isServiceUnitOwned(any(NamespaceBundle.class));
    doReturn(true).when(namespaceService).isServiceUnitActive(any(TopicName.class));
    setupMLAsyncCallbackMocks();
    clientChannelHelper = new ClientChannelHelper();
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Policies(org.apache.pulsar.common.policies.data.Policies) DefaultSchemaRegistryService(org.apache.pulsar.broker.service.schema.DefaultSchemaRegistryService) ClientChannelHelper(org.apache.pulsar.broker.service.utils.ClientChannelHelper) TopicName(org.apache.pulsar.common.naming.TopicName) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockedPulsarServiceBaseTest.createMockZooKeeper(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ConfigurationCacheService(org.apache.pulsar.broker.cache.ConfigurationCacheService) LocalZooKeeperCacheService(org.apache.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 7 with NamespaceService

use of org.apache.pulsar.broker.namespace.NamespaceService in project incubator-pulsar by apache.

the class PersistentTopicConcurrentTest method setup.

@BeforeMethod
public void setup(Method m) throws Exception {
    super.setUp(m);
    ServiceConfiguration svcConfig = spy(new ServiceConfiguration());
    PulsarService pulsar = spy(new PulsarService(svcConfig));
    doReturn(svcConfig).when(pulsar).getConfiguration();
    mlFactoryMock = mock(ManagedLedgerFactory.class);
    ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
    ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(2));
    final ManagedCursor cursor = ledger.openCursor("c1");
    cursorMock = cursor;
    ledgerMock = ledger;
    mlFactoryMock = factory;
    doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory();
    ZooKeeper mockZk = createMockZooKeeper();
    doReturn(mockZk).when(pulsar).getZkClient();
    brokerService = spy(new BrokerService(pulsar));
    doReturn(brokerService).when(pulsar).getBrokerService();
    serverCnx = spy(new ServerCnx(brokerService));
    doReturn(true).when(serverCnx).isActive();
    NamespaceService nsSvc = mock(NamespaceService.class);
    doReturn(nsSvc).when(pulsar).getNamespaceService();
    doReturn(true).when(nsSvc).isServiceUnitOwned(any(NamespaceBundle.class));
    doReturn(true).when(nsSvc).isServiceUnitActive(any(TopicName.class));
    final List<Position> addedEntries = Lists.newArrayList();
    for (int i = 0; i < 100; i++) {
        Position pos = ledger.addEntry("entry".getBytes());
        addedEntries.add(pos);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) ServerCnx(org.apache.pulsar.broker.service.ServerCnx) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) TopicName(org.apache.pulsar.common.naming.TopicName) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockedPulsarServiceBaseTest.createMockZooKeeper(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) ManagedLedgerConfig(org.apache.bookkeeper.mledger.ManagedLedgerConfig) BrokerService(org.apache.pulsar.broker.service.BrokerService) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with NamespaceService

use of org.apache.pulsar.broker.namespace.NamespaceService in project incubator-pulsar by apache.

the class PatternTopicsConsumerImplTest method testAutoUnbubscribePatternConsumer.

@Test(timeOut = testTimeout)
public void testAutoUnbubscribePatternConsumer() throws Exception {
    String key = "AutoUnsubscribePatternConsumer";
    String subscriptionName = "my-ex-subscription-" + key;
    String topicName1 = "persistent://prop/use/ns-abc/pattern-topic-1-" + key;
    String topicName2 = "persistent://prop/use/ns-abc/pattern-topic-2-" + key;
    String topicName3 = "persistent://prop/use/ns-abc/pattern-topic-3-" + key;
    Pattern pattern = Pattern.compile("persistent://prop/use/ns-abc/pattern-topic.*");
    // 1. create partition
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 2. create producer
    String messagePredicate = "my-message-" + key + "-";
    int totalMessages = 30;
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topicsPattern(pattern).patternAutoDiscoveryPeriod(2).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    assertTrue(consumer instanceof PatternTopicsConsumerImpl);
    // 4. verify consumer get methods, to get 0 number of partitions and topics: 6=1+2+3
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 3);
    // 5. produce data to topic 1,2,3; verify should receive all the message
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    // 6. remove producer 1,3; verify only consumer 2 left
    // seems no direct way to verify auto-unsubscribe, because this patternConsumer also referenced the topic.
    List<String> topicNames = Lists.newArrayList(topicName2);
    NamespaceService nss = pulsar.getNamespaceService();
    doReturn(topicNames).when(nss).getListOfTopics(NamespaceName.get("prop", "use", "ns-abc"));
    // 7. call recheckTopics to unsubscribe topic 1,3 , verify topics number: 2=6-1-3
    log.debug("recheck topics change");
    PatternTopicsConsumerImpl<byte[]> consumer1 = ((PatternTopicsConsumerImpl<byte[]>) consumer);
    consumer1.run(consumer1.getRecheckPatternTimeout());
    Thread.sleep(100);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getPartitionedTopics().size(), 2);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getConsumers().size(), 2);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getTopics().size(), 1);
    // 8. produce data to topic2, verify should receive all the message
    for (int i = 0; i < totalMessages; i++) {
        producer2.send((messagePredicate + "round2-producer2-" + i).getBytes());
    }
    messageSet = 0;
    message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) Test(org.testng.annotations.Test)

Example 9 with NamespaceService

use of org.apache.pulsar.broker.namespace.NamespaceService in project incubator-pulsar by apache.

the class PulsarWebResource method validateTopicOwnership.

/**
 * Checks whether the broker is the owner of the namespace. Otherwise it will raise an exception to redirect the
 * client to the appropriate broker. If no broker owns the namespace yet, this function will try to acquire the
 * ownership by default.
 *
 * @param authoritative
 *
 * @param property
 * @param cluster
 * @param namespace
 */
protected void validateTopicOwnership(TopicName topicName, boolean authoritative) {
    NamespaceService nsService = pulsar().getNamespaceService();
    try {
        // per function name, this is trying to acquire the whole namespace ownership
        Optional<URL> webUrl = nsService.getWebServiceUrl(topicName, authoritative, isRequestHttps(), false);
        // Ensure we get a url
        if (webUrl == null || !webUrl.isPresent()) {
            log.info("Unable to get web service url");
            throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for topic:" + topicName);
        }
        if (!nsService.isServiceUnitOwned(topicName)) {
            boolean newAuthoritative = isLeaderBroker(pulsar());
            // Replace the host and port of the current request and redirect
            URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.get().getHost()).port(webUrl.get().getPort()).replaceQueryParam("authoritative", newAuthoritative).build();
            // Redirect
            log.debug("Redirecting the rest call to {}", redirect);
            throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
        }
    } catch (IllegalArgumentException iae) {
        // namespace format is not valid
        log.debug(String.format("Failed to find owner for topic :%s", topicName), iae);
        throw new RestException(Status.PRECONDITION_FAILED, "Can't find owner for topic " + topicName);
    } catch (IllegalStateException ise) {
        log.debug(String.format("Failed to find owner for topic:%s", topicName), ise);
        throw new RestException(Status.PRECONDITION_FAILED, "Can't find owner for topic " + topicName);
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception oe) {
        log.debug(String.format("Failed to find owner for topic:%s", topicName), oe);
        throw new RestException(oe);
    }
}
Also used : NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 10 with NamespaceService

use of org.apache.pulsar.broker.namespace.NamespaceService in project incubator-pulsar by apache.

the class PulsarWebResource method validateBundleOwnership.

public void validateBundleOwnership(NamespaceBundle bundle, boolean authoritative, boolean readOnly) throws Exception {
    NamespaceService nsService = pulsar().getNamespaceService();
    try {
        // Call getWebServiceUrl() to acquire or redirect the request
        // Get web service URL of owning broker.
        // 1: If namespace is assigned to this broker, continue
        // 2: If namespace is assigned to another broker, redirect to the webservice URL of another broker
        // authoritative flag is ignored
        // 3: If namespace is unassigned and readOnly is true, return 412
        // 4: If namespace is unassigned and readOnly is false:
        // - If authoritative is false and this broker is not leader, forward to leader
        // - If authoritative is false and this broker is leader, determine owner and forward w/ authoritative=true
        // - If authoritative is true, own the namespace and continue
        Optional<URL> webUrl = nsService.getWebServiceUrl(bundle, authoritative, isRequestHttps(), readOnly);
        // Ensure we get a url
        if (webUrl == null || !webUrl.isPresent()) {
            log.warn("Unable to get web service url");
            throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle.toString());
        }
        if (!nsService.isServiceUnitOwned(bundle)) {
            boolean newAuthoritative = this.isLeaderBroker();
            // Replace the host and port of the current request and redirect
            URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(webUrl.get().getHost()).port(webUrl.get().getPort()).replaceQueryParam("authoritative", newAuthoritative).build();
            log.debug("{} is not a service unit owned", bundle);
            // Redirect
            log.debug("Redirecting the rest call to {}", redirect);
            throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
        }
    } catch (IllegalArgumentException iae) {
        // namespace format is not valid
        log.debug(String.format("Failed to find owner for ServiceUnit %s", bundle), iae);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit format is not expected. ServiceUnit " + bundle);
    } catch (IllegalStateException ise) {
        log.debug(String.format("Failed to find owner for ServiceUnit %s", bundle), ise);
        throw new RestException(Status.PRECONDITION_FAILED, "ServiceUnit bundle is actived. ServiceUnit " + bundle);
    } catch (NullPointerException e) {
        log.warn("Unable to get web service url");
        throw new RestException(Status.PRECONDITION_FAILED, "Failed to find ownership for ServiceUnit:" + bundle);
    } catch (WebApplicationException wae) {
        throw wae;
    }
}
Also used : NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI) URL(java.net.URL)

Aggregations

NamespaceService (org.apache.pulsar.broker.namespace.NamespaceService)10 BeforeMethod (org.testng.annotations.BeforeMethod)5 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)4 PulsarService (org.apache.pulsar.broker.PulsarService)4 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)4 MockedPulsarServiceBaseTest.createMockZooKeeper (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper)4 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)4 TopicName (org.apache.pulsar.common.naming.TopicName)4 ZooKeeper (org.apache.zookeeper.ZooKeeper)4 WebApplicationException (javax.ws.rs.WebApplicationException)3 ConfigurationCacheService (org.apache.pulsar.broker.cache.ConfigurationCacheService)3 LocalZooKeeperCacheService (org.apache.pulsar.broker.cache.LocalZooKeeperCacheService)3 Policies (org.apache.pulsar.common.policies.data.Policies)3 InetSocketAddress (java.net.InetSocketAddress)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 URL (java.net.URL)2 BrokerService (org.apache.pulsar.broker.service.BrokerService)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Maps (com.google.common.collect.Maps)1