Search in sources :

Example 1 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.

the class V1_ProducerConsumerTest method testInvalidSequence.

@Test
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    PulsarClient client1 = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build();
    client1.close();
    try {
        client1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    try {
        client1.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
    try {
        TypedMessageBuilder<byte[]> builder = producer.newMessage().value("InvalidMessage".getBytes());
        Message<byte[]> msg = ((TypedMessageBuilderImpl<byte[]>) builder).getMessage();
        consumer.acknowledge(msg);
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 2 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.

the class PartitionedProducerConsumerTest method testInvalidSequence.

@Test(timeOut = 30000)
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int numPartitions = 4;
    TopicName topicName = TopicName.get("persistent://my-property/my-ns/my-partitionedtopic4-" + System.currentTimeMillis());
    admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString()).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    try {
        TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("InvalidMessage".getBytes());
        consumer.acknowledge(mb.getMessage());
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    admin.topics().deletePartitionedTopic(topicName.toString());
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 3 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project kop by streamnative.

the class TransactionLogTest method shouldReadWriteMessages.

@Test
public void shouldReadWriteMessages() {
    Map<String, Long> pidMappings = Maps.newHashMap();
    pidMappings.put("zero", 0L);
    pidMappings.put("one", 1L);
    pidMappings.put("two", 2L);
    pidMappings.put("three", 3L);
    pidMappings.put("four", 4L);
    pidMappings.put("five", 5L);
    Map<Long, TransactionState> transactionStates = Maps.newHashMap();
    transactionStates.put(0L, TransactionState.EMPTY);
    transactionStates.put(1L, TransactionState.ONGOING);
    transactionStates.put(2L, TransactionState.PREPARE_COMMIT);
    transactionStates.put(3L, TransactionState.COMPLETE_COMMIT);
    transactionStates.put(4L, TransactionState.PREPARE_ABORT);
    transactionStates.put(5L, TransactionState.COMPLETE_ABORT);
    TypedMessageBuilderImpl<ByteBuffer> typedMessageBuilder = new TypedMessageBuilderImpl<>(null, Schema.BYTEBUFFER);
    // generate transaction log messages
    List<Message<ByteBuffer>> txnMessages = new ArrayList<>();
    pidMappings.forEach((transactionalId, producerId) -> {
        TransactionMetadata txnMetadata = TransactionMetadata.builder().transactionalId(transactionalId).producerId(producerId).lastProducerId(RecordBatch.NO_PRODUCER_ID).producerEpoch(producerEpoch).lastProducerEpoch(RecordBatch.NO_PRODUCER_EPOCH).txnTimeoutMs(transactionTimeoutMs).state(transactionStates.get(producerId)).topicPartitions(Sets.newSet()).txnStartTimestamp(0).txnLastUpdateTimestamp(0).build();
        if (!txnMetadata.getState().equals(TransactionState.EMPTY)) {
            txnMetadata.addPartitions(topicPartitions);
        }
        byte[] keyBytes = new TransactionLogKey(transactionalId).toBytes();
        ByteBuffer valueBytes = new TransactionLogValue(txnMetadata.prepareNoTransit()).toByteBuffer();
        typedMessageBuilder.keyBytes(keyBytes);
        typedMessageBuilder.value(valueBytes);
        txnMessages.add(typedMessageBuilder.getMessage());
    });
    int count = 0;
    for (Message<ByteBuffer> message : txnMessages) {
        TransactionLogKey logKey = TransactionLogKey.decode(ByteBuffer.wrap(message.getKeyBytes()), TransactionLogKey.HIGHEST_SUPPORTED_VERSION);
        String transactionalId = logKey.getTransactionId();
        TransactionMetadata txnMetadata = TransactionLogValue.readTxnRecordValue(transactionalId, message.getValue());
        assertEquals(pidMappings.get(transactionalId), new Long(txnMetadata.getProducerId()));
        assertEquals(producerEpoch, new Short(txnMetadata.getProducerEpoch()));
        assertEquals(transactionTimeoutMs, new Integer(txnMetadata.getTxnTimeoutMs()));
        assertEquals(transactionStates.get(txnMetadata.getProducerId()), txnMetadata.getState());
        if (txnMetadata.getState().equals(TransactionState.EMPTY)) {
            assertEquals(Collections.EMPTY_SET, txnMetadata.getTopicPartitions());
        } else {
            assertEquals(topicPartitions, txnMetadata.getTopicPartitions());
        }
        count += 1;
    }
    assertEquals(pidMappings.size(), count);
}
Also used : Message(org.apache.pulsar.client.api.Message) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) Test(org.testng.annotations.Test)

Example 5 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar-adapters by apache.

the class PulsarKafkaProducer method getRecordMetadata.

private RecordMetadata getRecordMetadata(String topic, TypedMessageBuilder<byte[]> msgBuilder, MessageId messageId, int size) {
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    // Combine ledger id and entry id to form offset
    long offset = MessageIdUtils.getOffset(msgId);
    int partition = msgId.getPartitionIndex();
    TopicPartition tp = new TopicPartition(topic, partition);
    TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) msgBuilder;
    long publishTime = 0L;
    try {
        // there is no hasPublishTime() currently
        publishTime = mb.getPublishTime();
    } catch (IllegalStateException ise) {
        logger.debug("could not get publish time");
    }
    return new RecordMetadata(tp, offset, 0L, publishTime, 0L, mb.hasKey() ? mb.getKey().length() : 0, size);
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) TopicPartition(org.apache.kafka.common.TopicPartition) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl)

Aggregations

TypedMessageBuilderImpl (org.apache.pulsar.client.impl.TypedMessageBuilderImpl)15 Test (org.testng.annotations.Test)9 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)3 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)3 ProducerBuilderImpl (org.apache.pulsar.client.impl.ProducerBuilderImpl)3 PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)3 ProducerConfigurationData (org.apache.pulsar.client.impl.conf.ProducerConfigurationData)3 TopicName (org.apache.pulsar.common.naming.TopicName)3 InstanceStateManager (org.apache.pulsar.functions.instance.state.InstanceStateManager)3 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)3 EnvironmentBasedSecretsProvider (org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Logger (org.slf4j.Logger)3 BeforeMethod (org.testng.annotations.BeforeMethod)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Message (org.apache.pulsar.client.api.Message)2