Search in sources :

Example 1 with MessageRoutingMode

use of org.apache.pulsar.client.api.MessageRoutingMode in project pulsar by apache.

the class PersistentTopicE2ETest method testProducerReturnedMessageId.

@Test
public void testProducerReturnedMessageId() throws Exception {
    final String topicName = "persistent://prop/ns-abc/topic-xyz";
    // 1. producer connect
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get();
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) topicRef.getManagedLedger();
    long ledgerId = managedLedger.getLedgersInfoAsList().get(0).getLedgerId();
    // 2. producer publish messages
    final int SyncMessages = 10;
    for (int i = 0; i < SyncMessages; i++) {
        String message = "my-message-" + i;
        MessageId receivedMessageId = producer.send(message.getBytes());
        assertEquals(receivedMessageId, new MessageIdImpl(ledgerId, i, -1));
    }
    // 3. producer publish messages async
    final int AsyncMessages = 10;
    final CountDownLatch counter = new CountDownLatch(AsyncMessages);
    for (int i = SyncMessages; i < (SyncMessages + AsyncMessages); i++) {
        String content = "my-message-" + i;
        final int index = i;
        producer.sendAsync(content.getBytes()).thenAccept((msgId) -> {
            assertEquals(msgId, new MessageIdImpl(ledgerId, index, -1));
            counter.countDown();
        }).exceptionally((ex) -> {
            return null;
        });
    }
    counter.await();
    // 4. producer disconnect
    producer.close();
}
Also used : Metrics(org.apache.pulsar.common.stats.Metrics) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) LookupService(org.apache.pulsar.client.impl.LookupService) Producer(org.apache.pulsar.client.api.Producer) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) ByteBuffer(java.nio.ByteBuffer) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) Map(java.util.Map) ToString(lombok.ToString) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ConsumerImpl(org.apache.pulsar.client.impl.ConsumerImpl) CompressionType(org.apache.pulsar.client.api.CompressionType) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) EqualsAndHashCode(lombok.EqualsAndHashCode) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) CountDownLatch(java.util.concurrent.CountDownLatch) Consumer(org.apache.pulsar.client.api.Consumer) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Optional(java.util.Optional) JSONSchema(org.apache.pulsar.client.impl.schema.JSONSchema) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) Awaitility(org.awaitility.Awaitility) ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Assert.assertNull(org.testng.Assert.assertNull) DataProvider(org.testng.annotations.DataProvider) Assert.assertEquals(org.testng.Assert.assertEquals) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) SchemaType(org.apache.pulsar.common.schema.SchemaType) ArrayList(java.util.ArrayList) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) MessageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) Assert(org.testng.Assert) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ProducerBusyException(org.apache.pulsar.client.api.PulsarClientException.ProducerBusyException) SchemaDefinition(org.apache.pulsar.client.api.schema.SchemaDefinition) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentDispatcherMultipleConsumers(org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers) ExecutorService(java.util.concurrent.ExecutorService) SchemaRegistry(org.apache.pulsar.broker.service.schema.SchemaRegistry) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) DEFAULT_CONSUMER_EPOCH(org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) MessageRedeliveryController(org.apache.pulsar.broker.service.persistent.MessageRedeliveryController) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Assert.fail(org.testng.Assert.fail) Field(java.lang.reflect.Field) EntryCacheImpl(org.apache.bookkeeper.mledger.impl.EntryCacheImpl) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Schema(org.apache.pulsar.client.api.Schema) TimeUnit(java.util.concurrent.TimeUnit) MessageId(org.apache.pulsar.client.api.MessageId) Data(lombok.Data) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) Schemas(org.apache.pulsar.schema.Schemas) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ToString(lombok.ToString) CountDownLatch(java.util.concurrent.CountDownLatch) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 2 with MessageRoutingMode

use of org.apache.pulsar.client.api.MessageRoutingMode in project pulsar by apache.

the class TopicsConsumerImplTest method testAsyncConsumer.

@Test(timeOut = testTimeout)
public void testAsyncConsumer() throws Exception {
    String key = "TopicsConsumerAsyncTest";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 30;
    final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key;
    List<String> topicNames = Lists.newArrayList(topicName1, topicName2, topicName3);
    TenantInfoImpl tenantInfo = createDefaultTenantInfo();
    admin.tenants().createTenant("prop", tenantInfo);
    admin.topics().createPartitionedTopic(topicName2, 2);
    admin.topics().createPartitionedTopic(topicName3, 3);
    // 1. producer connect
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).enableBatching(false).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).enableBatching(false).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    // 2. Create consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topics(topicNames).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    assertTrue(consumer instanceof MultiTopicsConsumerImpl);
    // Asynchronously produce messages
    List<Future<MessageId>> futures = Lists.newArrayList();
    for (int i = 0; i < totalMessages / 3; i++) {
        futures.add(producer1.sendAsync((messagePredicate + "producer1-" + i).getBytes()));
        futures.add(producer2.sendAsync((messagePredicate + "producer2-" + i).getBytes()));
        futures.add(producer3.sendAsync((messagePredicate + "producer3-" + i).getBytes()));
    }
    log.info("Waiting for async publish to complete : {}", futures.size());
    for (Future<MessageId> future : futures) {
        future.get();
    }
    log.info("start async consume");
    CountDownLatch latch = new CountDownLatch(totalMessages);
    @Cleanup("shutdownNow") ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(() -> IntStream.range(0, totalMessages).forEach(index -> consumer.receiveAsync().thenAccept(msg -> {
        assertTrue(msg instanceof TopicMessageImpl);
        try {
            consumer.acknowledge(msg);
        } catch (PulsarClientException e1) {
            fail("message acknowledge failed", e1);
        }
        latch.countDown();
        log.info("receive index: {}, latch countDown: {}", index, latch.getCount());
    }).exceptionally(ex -> {
        log.warn("receive index: {}, failed receive message {}", index, ex.getMessage());
        ex.printStackTrace();
        return null;
    })));
    latch.await();
    log.info("success latch wait");
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : SubscriptionStats(org.apache.pulsar.common.policies.data.SubscriptionStats) Topic(org.apache.pulsar.broker.service.Topic) Producer(org.apache.pulsar.client.api.Producer) LoggerFactory(org.slf4j.LoggerFactory) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) AfterMethod(org.testng.annotations.AfterMethod) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertNotNull(org.testng.Assert.assertNotNull) Sets(com.google.common.collect.Sets) Executors(java.util.concurrent.Executors) TopicMetadata(org.apache.pulsar.client.api.TopicMetadata) Consumer(org.apache.pulsar.client.api.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Optional(java.util.Optional) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Awaitility(org.awaitility.Awaitility) ProducerConsumerBase(org.apache.pulsar.client.api.ProducerConsumerBase) IntStream(java.util.stream.IntStream) TopicStats(org.apache.pulsar.common.policies.data.TopicStats) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) SubscriptionInitialPosition(org.apache.pulsar.client.api.SubscriptionInitialPosition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MessageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ExecutorService(java.util.concurrent.ExecutorService) Timeout(io.netty.util.Timeout) Logger(org.slf4j.Logger) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) MessageRouter(org.apache.pulsar.client.api.MessageRouter) Assert.fail(org.testng.Assert.fail) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Schema(org.apache.pulsar.client.api.Schema) TimeUnit(java.util.concurrent.TimeUnit) PartitionedTopicStats(org.apache.pulsar.common.policies.data.PartitionedTopicStats) MessageId(org.apache.pulsar.client.api.MessageId) Assert.assertTrue(org.testng.Assert.assertTrue) CountDownLatch(java.util.concurrent.CountDownLatch) Cleanup(lombok.Cleanup) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) ExecutorService(java.util.concurrent.ExecutorService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 3 with MessageRoutingMode

use of org.apache.pulsar.client.api.MessageRoutingMode in project pulsar by apache.

the class ProducerHandler method getProducerBuilder.

protected ProducerBuilder<byte[]> getProducerBuilder(PulsarClient client) {
    ProducerBuilder<byte[]> builder = client.newProducer().enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition);
    // Set to false to prevent the server thread from being blocked if a lot of messages are pending.
    builder.blockIfQueueFull(false);
    if (queryParams.containsKey("producerName")) {
        builder.producerName(queryParams.get("producerName"));
    }
    if (queryParams.containsKey("initialSequenceId")) {
        builder.initialSequenceId(Long.parseLong(queryParams.get("initialSequenceId")));
    }
    if (queryParams.containsKey("hashingScheme")) {
        builder.hashingScheme(HashingScheme.valueOf(queryParams.get("hashingScheme")));
    }
    if (queryParams.containsKey("sendTimeoutMillis")) {
        builder.sendTimeout(Integer.parseInt(queryParams.get("sendTimeoutMillis")), TimeUnit.MILLISECONDS);
    }
    if (queryParams.containsKey("batchingEnabled")) {
        builder.enableBatching(Boolean.parseBoolean(queryParams.get("batchingEnabled")));
    }
    if (queryParams.containsKey("batchingMaxMessages")) {
        builder.batchingMaxMessages(Integer.parseInt(queryParams.get("batchingMaxMessages")));
    }
    if (queryParams.containsKey("maxPendingMessages")) {
        builder.maxPendingMessages(Integer.parseInt(queryParams.get("maxPendingMessages")));
    }
    if (queryParams.containsKey("batchingMaxPublishDelay")) {
        builder.batchingMaxPublishDelay(Integer.parseInt(queryParams.get("batchingMaxPublishDelay")), TimeUnit.MILLISECONDS);
    }
    if (queryParams.containsKey("messageRoutingMode")) {
        checkArgument(Enums.getIfPresent(MessageRoutingMode.class, queryParams.get("messageRoutingMode")).isPresent(), "Invalid messageRoutingMode %s", queryParams.get("messageRoutingMode"));
        MessageRoutingMode routingMode = MessageRoutingMode.valueOf(queryParams.get("messageRoutingMode"));
        if (!MessageRoutingMode.CustomPartition.equals(routingMode)) {
            builder.messageRoutingMode(routingMode);
        }
    }
    if (queryParams.containsKey("compressionType")) {
        checkArgument(Enums.getIfPresent(CompressionType.class, queryParams.get("compressionType")).isPresent(), "Invalid compressionType %s", queryParams.get("compressionType"));
        builder.compressionType(CompressionType.valueOf(queryParams.get("compressionType")));
    }
    return builder;
}
Also used : MessageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode)

Example 4 with MessageRoutingMode

use of org.apache.pulsar.client.api.MessageRoutingMode in project pulsar by apache.

the class AbstractWebSocketHandlerTest method producerBuilderTest.

@Test
public void producerBuilderTest() throws IOException {
    String producerV2 = "/ws/v2/producer/persistent/my-property/my-ns/my-topic";
    // the params are all different with the default value
    Map<String, String[]> queryParams = new HashMap<String, String>() {

        {
            put("producerName", "my-producer");
            put("initialSequenceId", "1");
            put("hashingScheme", "Murmur3_32Hash");
            put("sendTimeoutMillis", "30001");
            put("batchingEnabled", "false");
            put("batchingMaxMessages", "1001");
            put("maxPendingMessages", "1001");
            put("batchingMaxPublishDelay", "2");
            put("messageRoutingMode", "RoundRobinPartition");
            put("compressionType", "LZ4");
        }
    }.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> new String[] { entry.getValue() }));
    httpServletRequest = mock(HttpServletRequest.class);
    when(httpServletRequest.getRequestURI()).thenReturn(producerV2);
    when(httpServletRequest.getParameterMap()).thenReturn(queryParams);
    WebSocketService service = mock(WebSocketService.class);
    when(service.isAuthenticationEnabled()).thenReturn(false);
    when(service.isAuthorizationEnabled()).thenReturn(false);
    when(service.getPulsarClient()).thenReturn(newPulsarClient());
    MockedServletUpgradeResponse response = new MockedServletUpgradeResponse(null);
    MockedProducerHandler producerHandler = new MockedProducerHandler(service, httpServletRequest, response);
    assertEquals(response.getStatusCode(), 500);
    assertTrue(response.getMessage().contains("Connection refused"));
    ProducerConfigurationData conf = producerHandler.getConf();
    assertEquals(conf.getProducerName(), "my-producer");
    assertEquals(conf.getInitialSequenceId().longValue(), 1L);
    assertEquals(conf.getHashingScheme(), HashingScheme.Murmur3_32Hash);
    assertEquals(conf.getSendTimeoutMs(), 30001);
    assertFalse(conf.isBatchingEnabled());
    assertEquals(conf.getBatchingMaxMessages(), 1001);
    assertEquals(conf.getMaxPendingMessages(), 1001);
    assertEquals(conf.getMessageRoutingMode(), MessageRoutingMode.RoundRobinPartition);
    assertEquals(conf.getCompressionType(), CompressionType.LZ4);
    producerHandler.clearQueryParams();
    conf = producerHandler.getConf();
    // The default message routing mode is SinglePartition, which is different with ProducerBuilder
    assertEquals(conf.getMessageRoutingMode(), MessageRoutingMode.SinglePartition);
    producerHandler.putQueryParam("messageRoutingMode", "CustomPartition");
    conf = producerHandler.getConf();
    // ProducerHandler doesn't support CustomPartition
    assertEquals(conf.getMessageRoutingMode(), MessageRoutingMode.SinglePartition);
}
Also used : TopicName(org.apache.pulsar.common.naming.TopicName) Getter(lombok.Getter) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) Mock(org.mockito.Mock) SubscriptionMode(org.apache.pulsar.client.api.SubscriptionMode) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) ArrayList(java.util.ArrayList) MessageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ConsumerBuilderImpl(org.apache.pulsar.client.impl.ConsumerBuilderImpl) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) AfterClass(org.testng.annotations.AfterClass) CompressionType(org.apache.pulsar.client.api.CompressionType) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) AuthenticationDataSource(org.apache.pulsar.broker.authentication.AuthenticationDataSource) HashingScheme(org.apache.pulsar.client.api.HashingScheme) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) TimeUnit(java.util.concurrent.TimeUnit) URLEncoder(java.net.URLEncoder) List(java.util.List) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) HttpServletRequest(javax.servlet.http.HttpServletRequest) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 5 with MessageRoutingMode

use of org.apache.pulsar.client.api.MessageRoutingMode in project incubator-inlong by apache.

the class PulsarUtils method createProducer.

public static Producer<byte[]> createProducer(PulsarClient client, String topic, Configuration configuration) throws PulsarClientException {
    MessageRoutingMode routeMode = Enum.valueOf(MessageRoutingMode.class, PRODUCER_ROUTE_MODE.defaultValue());
    Duration sendTimeout = parseDuration(configuration.getString(PRODUCER_SEND_TIMEOUT));
    int pendingQueueSize = configuration.getInteger(PRODUCER_PENDING_QUEUE_SIZE);
    int pendingSize = configuration.getInteger(PRODUCER_PENDING_SIZE);
    boolean blockIfQueueFull = configuration.getBoolean(PRODUCER_BLOCK_QUEUE_FULL);
    return client.newProducer().topic(topic).messageRoutingMode(routeMode).sendTimeout((int) sendTimeout.toMillis(), TimeUnit.MILLISECONDS).maxPendingMessages(pendingQueueSize).maxPendingMessagesAcrossPartitions(pendingSize).blockIfQueueFull(blockIfQueueFull).create();
}
Also used : MessageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode) TimeUtils.parseDuration(org.apache.flink.util.TimeUtils.parseDuration) Duration(java.time.Duration)

Aggregations

MessageRoutingMode (org.apache.pulsar.client.api.MessageRoutingMode)32 TimeUnit (java.util.concurrent.TimeUnit)24 Test (org.testng.annotations.Test)24 Sets (com.google.common.collect.Sets)21 Producer (org.apache.pulsar.client.api.Producer)21 PulsarClient (org.apache.pulsar.client.api.PulsarClient)21 Awaitility (org.awaitility.Awaitility)21 Assert (org.testng.Assert)21 Assert.assertEquals (org.testng.Assert.assertEquals)21 Assert.assertTrue (org.testng.Assert.assertTrue)21 BeforeMethod (org.testng.annotations.BeforeMethod)21 ArrayList (java.util.ArrayList)18 List (java.util.List)18 Map (java.util.Map)18 Cleanup (lombok.Cleanup)18 Consumer (org.apache.pulsar.client.api.Consumer)18 HashMap (java.util.HashMap)15 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)15 MessageId (org.apache.pulsar.client.api.MessageId)15 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)15