Search in sources :

Example 1 with SinkRecord

use of org.apache.pulsar.functions.instance.SinkRecord in project pulsar by yahoo.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Example 2 with SinkRecord

use of org.apache.pulsar.functions.instance.SinkRecord in project incubator-pulsar by apache.

the class PulsarSinkTest method testSinkAndMessageRouting.

@Test
public void testSinkAndMessageRouting() throws Exception {
    String[] topics = { "topic-1", "topic-2", "topic-3", null };
    String defaultTopic = "default";
    PulsarSinkConfig pulsarConfig = getPulsarConfigs();
    pulsarConfig.setTopic(defaultTopic);
    PulsarClient pulsarClient;
    /**
     * test At-least-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATLEAST_ONCE);
    PulsarSink pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }
        }, "out1");
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
        PulsarSink.PulsarSinkAtLeastOnceProcessor pulsarSinkAtLeastOnceProcessor = (PulsarSink.PulsarSinkAtLeastOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(topic));
        } else {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(defaultTopic));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(otherTopic -> {
            if (topic != null) {
                return topic.equals(otherTopic);
            } else {
                return defaultTopic.equals(otherTopic);
            }
        }));
    }
    /**
     * test At-most-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATMOST_ONCE);
    pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public Optional<String> getKey() {
                return Optional.empty();
            }

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }
        }, "out1");
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
        PulsarSink.PulsarSinkAtMostOnceProcessor pulsarSinkAtLeastOnceProcessor = (PulsarSink.PulsarSinkAtMostOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(topic));
        } else {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(defaultTopic));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(o -> {
            return getTopicEquals(o, topic, defaultTopic);
        }));
    }
    /**
     * test Effectively-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.EFFECTIVELY_ONCE);
    pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public Optional<String> getKey() {
                return Optional.empty();
            }

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }

            @Override
            public Optional<String> getPartitionId() {
                if (topic != null) {
                    return Optional.of(topic + "-id-1");
                } else {
                    return Optional.of(defaultTopic + "-id-1");
                }
            }

            @Override
            public Optional<Integer> getPartitionIndex() {
                return Optional.of(1);
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, "out1");
        assertEquals(1, record.getPartitionIndex().get().intValue());
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
        PulsarSink.PulsarSinkEffectivelyOnceProcessor pulsarSinkEffectivelyOnceProcessor = (PulsarSink.PulsarSinkEffectivelyOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkEffectivelyOnceProcessor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            Assert.assertTrue(pulsarSinkEffectivelyOnceProcessor.publishProducers.containsKey(String.format("%s-%s-id-1", defaultTopic, defaultTopic)));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(o -> {
            return getTopicEquals(o, topic, defaultTopic);
        }));
        verify(pulsarClient.newProducer(), times(1)).producerName(argThat(o -> {
            if (topic != null) {
                return String.format("%s-id-1", topic).equals(o);
            } else {
                return String.format("%s-id-1", defaultTopic).equals(o);
            }
        }));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Optional(java.util.Optional) HashMap(java.util.HashMap) SinkContext(org.apache.pulsar.io.core.SinkContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 3 with SinkRecord

use of org.apache.pulsar.functions.instance.SinkRecord in project incubator-pulsar by apache.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Example 4 with SinkRecord

use of org.apache.pulsar.functions.instance.SinkRecord in project pulsar by yahoo.

the class PulsarSinkTest method testSinkAndMessageRouting.

@Test
public void testSinkAndMessageRouting() throws Exception {
    String[] topics = { "topic-1", "topic-2", "topic-3", null };
    String defaultTopic = "default";
    PulsarSinkConfig pulsarConfig = getPulsarConfigs();
    pulsarConfig.setTopic(defaultTopic);
    PulsarClient pulsarClient;
    /**
     * test At-least-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATLEAST_ONCE);
    PulsarSink pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }
        }, "out1");
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
        PulsarSink.PulsarSinkAtLeastOnceProcessor pulsarSinkAtLeastOnceProcessor = (PulsarSink.PulsarSinkAtLeastOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(topic));
        } else {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(defaultTopic));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(otherTopic -> {
            if (topic != null) {
                return topic.equals(otherTopic);
            } else {
                return defaultTopic.equals(otherTopic);
            }
        }));
    }
    /**
     * test At-most-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATMOST_ONCE);
    pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public Optional<String> getKey() {
                return Optional.empty();
            }

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }
        }, "out1");
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
        PulsarSink.PulsarSinkAtMostOnceProcessor pulsarSinkAtLeastOnceProcessor = (PulsarSink.PulsarSinkAtMostOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(topic));
        } else {
            Assert.assertTrue(pulsarSinkAtLeastOnceProcessor.publishProducers.containsKey(defaultTopic));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(o -> {
            return getTopicEquals(o, topic, defaultTopic);
        }));
    }
    /**
     * test Effectively-once *
     */
    pulsarClient = getPulsarClient();
    pulsarConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.EFFECTIVELY_ONCE);
    pulsarSink = new PulsarSink(pulsarClient, pulsarConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    for (String topic : topics) {
        SinkRecord<String> record = new SinkRecord<>(new Record<String>() {

            @Override
            public Optional<String> getKey() {
                return Optional.empty();
            }

            @Override
            public String getValue() {
                return "in1";
            }

            @Override
            public Optional<String> getDestinationTopic() {
                return getTopicOptional(topic);
            }

            @Override
            public Optional<String> getPartitionId() {
                if (topic != null) {
                    return Optional.of(topic + "-id-1");
                } else {
                    return Optional.of(defaultTopic + "-id-1");
                }
            }

            @Override
            public Optional<Integer> getPartitionIndex() {
                return Optional.of(1);
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, "out1");
        assertEquals(1, record.getPartitionIndex().get().intValue());
        pulsarSink.write(record);
        Assert.assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
        PulsarSink.PulsarSinkEffectivelyOnceProcessor pulsarSinkEffectivelyOnceProcessor = (PulsarSink.PulsarSinkEffectivelyOnceProcessor) pulsarSink.pulsarSinkProcessor;
        if (topic != null) {
            Assert.assertTrue(pulsarSinkEffectivelyOnceProcessor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            Assert.assertTrue(pulsarSinkEffectivelyOnceProcessor.publishProducers.containsKey(String.format("%s-%s-id-1", defaultTopic, defaultTopic)));
        }
        verify(pulsarClient.newProducer(), times(1)).topic(argThat(o -> {
            return getTopicEquals(o, topic, defaultTopic);
        }));
        verify(pulsarClient.newProducer(), times(1)).producerName(argThat(o -> {
            if (topic != null) {
                return String.format("%s-id-1", topic).equals(o);
            } else {
                return String.format("%s-id-1", defaultTopic).equals(o);
            }
        }));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Optional(java.util.Optional) HashMap(java.util.HashMap) SinkContext(org.apache.pulsar.io.core.SinkContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 5 with SinkRecord

use of org.apache.pulsar.functions.instance.SinkRecord in project pulsar by apache.

the class PulsarSinkTest method testWriteGenericRecords.

private void testWriteGenericRecords(ProcessingGuarantees guarantees) throws Exception {
    String defaultTopic = "default";
    PulsarSinkConfig sinkConfig = getPulsarConfigs();
    sinkConfig.setTopic(defaultTopic);
    sinkConfig.setTypeClassName(GenericRecord.class.getName());
    sinkConfig.setProcessingGuarantees(guarantees);
    PulsarClient client = getPulsarClient();
    PulsarSink pulsarSink = new PulsarSink(client, sinkConfig, new HashMap<>(), mock(ComponentStatsManager.class), Thread.currentThread().getContextClassLoader());
    pulsarSink.open(new HashMap<>(), mock(SinkContext.class));
    if (ProcessingGuarantees.ATMOST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtMostOnceProcessor);
    } else if (ProcessingGuarantees.ATLEAST_ONCE == guarantees) {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkAtLeastOnceProcessor);
    } else {
        assertTrue(pulsarSink.pulsarSinkProcessor instanceof PulsarSink.PulsarSinkEffectivelyOnceProcessor);
    }
    PulsarSinkProcessorBase processor = (PulsarSinkProcessorBase) pulsarSink.pulsarSinkProcessor;
    assertFalse(processor.publishProducers.containsKey(defaultTopic));
    String[] topics = { "topic-1", "topic-2", "topic-3" };
    for (String topic : topics) {
        RecordSchemaBuilder builder = SchemaBuilder.record("MyRecord");
        builder.field("number").type(SchemaType.INT32);
        builder.field("text").type(SchemaType.STRING);
        GenericSchema<GenericRecord> schema = Schema.generic(builder.build(SchemaType.AVRO));
        GenericRecordBuilder recordBuilder = schema.newRecordBuilder();
        recordBuilder.set("number", 1);
        recordBuilder.set("text", topic);
        GenericRecord genericRecord = recordBuilder.build();
        SinkRecord<GenericRecord> record = new SinkRecord<>(new Record<GenericRecord>() {

            @Override
            public Optional<String> getDestinationTopic() {
                return Optional.of(topic);
            }

            @Override
            public Schema<GenericRecord> getSchema() {
                return schema;
            }

            @Override
            public GenericRecord getValue() {
                return genericRecord;
            }

            @Override
            public Optional<String> getPartitionId() {
                return Optional.of(topic + "-id-1");
            }

            @Override
            public Optional<Long> getRecordSequence() {
                return Optional.of(1L);
            }
        }, genericRecord);
        pulsarSink.write(record);
        if (ProcessingGuarantees.EFFECTIVELY_ONCE == guarantees) {
            assertTrue(processor.publishProducers.containsKey(String.format("%s-%s-id-1", topic, topic)));
        } else {
            assertTrue(processor.publishProducers.containsKey(topic));
        }
        verify(client.newProducer(), times(1)).topic(argThat(otherTopic -> topic != null ? topic.equals(otherTopic) : defaultTopic.equals(otherTopic)));
        verify(client, times(1)).newProducer(argThat(otherSchema -> Objects.equals(otherSchema, schema)));
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Producer(org.apache.pulsar.client.api.Producer) Test(org.testng.annotations.Test) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) Record(org.apache.pulsar.functions.api.Record) BeforeMethod(org.testng.annotations.BeforeMethod) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Slf4j(lombok.extern.slf4j.Slf4j) SerDe(org.apache.pulsar.functions.api.SerDe) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Setter(lombok.Setter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Assert.assertNull(org.testng.Assert.assertNull) Getter(lombok.Getter) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerBuilder(org.apache.pulsar.client.api.ProducerBuilder) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) Assert(org.testng.Assert) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) ProcessingGuarantees(org.apache.pulsar.common.functions.FunctionConfig.ProcessingGuarantees) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) Assert.fail(org.testng.Assert.fail) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.verify(org.mockito.Mockito.verify) Schema(org.apache.pulsar.client.api.Schema) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) MessageId(org.apache.pulsar.client.api.MessageId) SinkContext(org.apache.pulsar.io.core.SinkContext) Assert.assertTrue(org.testng.Assert.assertTrue) SchemaBuilder(org.apache.pulsar.client.api.schema.SchemaBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) Optional(java.util.Optional) SinkContext(org.apache.pulsar.io.core.SinkContext) TopicSchema(org.apache.pulsar.functions.source.TopicSchema) GenericSchema(org.apache.pulsar.client.api.schema.GenericSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SinkRecord(org.apache.pulsar.functions.instance.SinkRecord) PulsarSinkProcessorBase(org.apache.pulsar.functions.sink.PulsarSink.PulsarSinkProcessorBase) ComponentStatsManager(org.apache.pulsar.functions.instance.stats.ComponentStatsManager) GenericRecordBuilder(org.apache.pulsar.client.api.schema.GenericRecordBuilder) PulsarClient(org.apache.pulsar.client.api.PulsarClient) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord)

Aggregations

IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Objects (java.util.Objects)6 Optional (java.util.Optional)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Getter (lombok.Getter)6 Setter (lombok.Setter)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Consumer (org.apache.pulsar.client.api.Consumer)6 ConsumerBuilder (org.apache.pulsar.client.api.ConsumerBuilder)6 MessageId (org.apache.pulsar.client.api.MessageId)6 Producer (org.apache.pulsar.client.api.Producer)6 ProducerBuilder (org.apache.pulsar.client.api.ProducerBuilder)6 PulsarClient (org.apache.pulsar.client.api.PulsarClient)6 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 Schema (org.apache.pulsar.client.api.Schema)6 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)6 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)6 GenericRecordBuilder (org.apache.pulsar.client.api.schema.GenericRecordBuilder)6 GenericSchema (org.apache.pulsar.client.api.schema.GenericSchema)6