Search in sources :

Example 16 with SystemConsumer

use of org.apache.samza.system.SystemConsumer in project samza by apache.

the class TestInMemorySystem method testConsumerRespectsOffset.

@Test
public void testConsumerRespectsOffset() {
    PageViewEvent event = new PageViewEvent(TEST_MEMBER_X, PAGE_ID_X, System.currentTimeMillis());
    PageViewEvent event1 = new PageViewEvent(TEST_MEMBER_Y, PAGE_ID_Y, System.currentTimeMillis());
    produceMessages(event);
    SystemConsumer consumer = systemFactory.getConsumer(SYSTEM_NAME, config, mockRegistry);
    Set<SystemStreamPartition> sspsToPoll = IntStream.range(0, PARTITION_COUNT).mapToObj(partition -> new SystemStreamPartition(SYSTEM_STREAM, new Partition(partition))).collect(Collectors.toSet());
    // register the consumer for ssps
    for (SystemStreamPartition ssp : sspsToPoll) {
        consumer.register(ssp, "0");
    }
    List<PageViewEvent> results = consumeMessages(consumer, sspsToPoll);
    assertEquals(1, results.size());
    assertTrue(results.contains(event));
    // nothing to poll
    results = consumeMessages(consumer, sspsToPoll);
    assertEquals(0, results.size());
    produceMessages(event1);
    // got new message. check if the offset has progressed
    results = consumeMessages(consumer, sspsToPoll);
    assertEquals(1, results.size());
    assertTrue(results.contains(event1));
}
Also used : IntStream(java.util.stream.IntStream) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) SystemConsumer(org.apache.samza.system.SystemConsumer) SystemProducer(org.apache.samza.system.SystemProducer) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) SystemAdmin(org.apache.samza.system.SystemAdmin) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) MapConfig(org.apache.samza.config.MapConfig) SystemConsumer(org.apache.samza.system.SystemConsumer) Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 17 with SystemConsumer

use of org.apache.samza.system.SystemConsumer in project samza by apache.

the class TestInMemorySystem method testNullMessageWithValidMessageKey.

@Test
public void testNullMessageWithValidMessageKey() {
    final String messageKey = "validKey";
    SystemProducer systemProducer = systemFactory.getProducer(SYSTEM_NAME, config, mockRegistry);
    systemProducer.send(SOURCE, new OutgoingMessageEnvelope(SYSTEM_STREAM, messageKey, null));
    SystemConsumer consumer = systemFactory.getConsumer(SYSTEM_NAME, config, mockRegistry);
    Set<SystemStreamPartition> sspsToPoll = IntStream.range(0, PARTITION_COUNT).mapToObj(partition -> new SystemStreamPartition(SYSTEM_STREAM, new Partition(partition))).collect(Collectors.toSet());
    // register the consumer for ssps
    for (SystemStreamPartition ssp : sspsToPoll) {
        consumer.register(ssp, "0");
    }
    List<IncomingMessageEnvelope> results = consumeRawMessages(consumer, sspsToPoll);
    assertEquals(1, results.size());
    assertEquals(results.get(0).getKey(), messageKey);
    assertNull(results.get(0).getMessage());
}
Also used : IntStream(java.util.stream.IntStream) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) Partition(org.apache.samza.Partition) Set(java.util.Set) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) SystemConsumer(org.apache.samza.system.SystemConsumer) SystemProducer(org.apache.samza.system.SystemProducer) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) SystemAdmin(org.apache.samza.system.SystemAdmin) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) Config(org.apache.samza.config.Config) Assert(org.junit.Assert) MapConfig(org.apache.samza.config.MapConfig) SystemConsumer(org.apache.samza.system.SystemConsumer) Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) SystemProducer(org.apache.samza.system.SystemProducer) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 18 with SystemConsumer

use of org.apache.samza.system.SystemConsumer in project beam by apache.

the class SamzaImpulseSystemTest method testSamzaImpulseSystemConsumer.

@Test
public void testSamzaImpulseSystemConsumer() throws Exception {
    SystemConsumer consumer = new SamzaImpulseSystemFactory().getConsumer("default-system", new MapConfig(), null);
    Map<SystemStreamPartition, List<IncomingMessageEnvelope>> result = consumer.poll(Collections.singleton(sspForPartition(0)), 100);
    Assert.assertEquals(1, result.size());
    Assert.assertTrue(result.containsKey(sspForPartition(0)));
    List<IncomingMessageEnvelope> messageEnvelopes = result.get(sspForPartition(0));
    Assert.assertEquals(3, messageEnvelopes.size());
    Assert.assertTrue(messageEnvelopes.get(0).getMessage() instanceof OpMessage);
    OpMessage impulseEvent = (OpMessage) messageEnvelopes.get(0).getMessage();
    Assert.assertEquals(OpMessage.Type.ELEMENT, impulseEvent.getType());
    Assert.assertTrue(messageEnvelopes.get(1).getMessage() instanceof WatermarkMessage);
    Assert.assertTrue(messageEnvelopes.get(2).isEndOfStream());
}
Also used : SystemConsumer(org.apache.samza.system.SystemConsumer) OpMessage(org.apache.beam.runners.samza.runtime.OpMessage) WatermarkMessage(org.apache.samza.system.WatermarkMessage) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) List(java.util.List) MapConfig(org.apache.samza.config.MapConfig) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Aggregations

SystemConsumer (org.apache.samza.system.SystemConsumer)18 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)17 SystemStream (org.apache.samza.system.SystemStream)12 HashMap (java.util.HashMap)11 SystemAdmin (org.apache.samza.system.SystemAdmin)10 Map (java.util.Map)9 List (java.util.List)8 Partition (org.apache.samza.Partition)8 Config (org.apache.samza.config.Config)8 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)8 MapConfig (org.apache.samza.config.MapConfig)7 Test (org.junit.Test)7 Set (java.util.Set)6 File (java.io.File)5 SamzaException (org.apache.samza.SamzaException)5 TaskConfig (org.apache.samza.config.TaskConfig)5 TaskName (org.apache.samza.container.TaskName)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 TaskModel (org.apache.samza.job.model.TaskModel)4