Search in sources :

Example 6 with WatermarkMessage

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

the class TestControlMessageSender method testSend.

@Test
public void testSend() {
    SystemStreamMetadata metadata = mock(SystemStreamMetadata.class);
    Map<Partition, SystemStreamMetadata.SystemStreamPartitionMetadata> partitionMetadata = new HashMap<>();
    partitionMetadata.put(new Partition(0), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(1), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(2), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(3), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    when(metadata.getSystemStreamPartitionMetadata()).thenReturn(partitionMetadata);
    StreamMetadataCache metadataCache = mock(StreamMetadataCache.class);
    when(metadataCache.getSystemStreamMetadata(anyObject(), anyBoolean())).thenReturn(metadata);
    SystemStream systemStream = new SystemStream("test-system", "test-stream");
    Set<Integer> partitions = new HashSet<>();
    MessageCollector collector = mock(MessageCollector.class);
    doAnswer(invocation -> {
        OutgoingMessageEnvelope envelope = (OutgoingMessageEnvelope) invocation.getArguments()[0];
        partitions.add((Integer) envelope.getPartitionKey());
        assertEquals(envelope.getSystemStream(), systemStream);
        return null;
    }).when(collector).send(any());
    ControlMessageSender sender = new ControlMessageSender(metadataCache);
    WatermarkMessage watermark = new WatermarkMessage(System.currentTimeMillis(), "task 0");
    sender.send(watermark, systemStream, collector);
    assertEquals(partitions.size(), 1);
}
Also used : StreamMetadataCache(org.apache.samza.system.StreamMetadataCache) Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) HashMap(java.util.HashMap) SystemStream(org.apache.samza.system.SystemStream) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) WatermarkMessage(org.apache.samza.system.WatermarkMessage) MessageCollector(org.apache.samza.task.MessageCollector) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with WatermarkMessage

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

the class TestControlMessageSender method testBroadcast.

@Test
public void testBroadcast() {
    SystemStreamMetadata metadata = mock(SystemStreamMetadata.class);
    Map<Partition, SystemStreamMetadata.SystemStreamPartitionMetadata> partitionMetadata = new HashMap<>();
    partitionMetadata.put(new Partition(0), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(1), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(2), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    partitionMetadata.put(new Partition(3), mock(SystemStreamMetadata.SystemStreamPartitionMetadata.class));
    when(metadata.getSystemStreamPartitionMetadata()).thenReturn(partitionMetadata);
    StreamMetadataCache metadataCache = mock(StreamMetadataCache.class);
    when(metadataCache.getSystemStreamMetadata(anyObject(), anyBoolean())).thenReturn(metadata);
    SystemStream systemStream = new SystemStream("test-system", "test-stream");
    Set<Integer> partitions = new HashSet<>();
    MessageCollector collector = mock(MessageCollector.class);
    doAnswer(invocation -> {
        OutgoingMessageEnvelope envelope = (OutgoingMessageEnvelope) invocation.getArguments()[0];
        partitions.add((Integer) envelope.getPartitionKey());
        assertEquals(envelope.getSystemStream(), systemStream);
        return null;
    }).when(collector).send(any());
    ControlMessageSender sender = new ControlMessageSender(metadataCache);
    WatermarkMessage watermark = new WatermarkMessage(System.currentTimeMillis(), "task 0");
    SystemStreamPartition ssp = new SystemStreamPartition(systemStream, new Partition(0));
    sender.broadcastToOtherPartitions(watermark, ssp, collector);
    assertEquals(partitions.size(), 3);
}
Also used : StreamMetadataCache(org.apache.samza.system.StreamMetadataCache) Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) HashMap(java.util.HashMap) SystemStream(org.apache.samza.system.SystemStream) SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) WatermarkMessage(org.apache.samza.system.WatermarkMessage) MessageCollector(org.apache.samza.task.MessageCollector) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) HashSet(java.util.HashSet) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 8 with WatermarkMessage

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

the class OperatorImpl method aggregateWatermark.

/**
 * Aggregate the {@link WatermarkMessage} from each ssp into a watermark. Then call onWatermark() if
 * a new watermark exits.
 * @param watermarkMessage a {@link WatermarkMessage} object
 * @param ssp {@link SystemStreamPartition} that the message is coming from.
 * @param collector message collector
 * @param coordinator task coordinator
 */
public final CompletionStage<Void> aggregateWatermark(WatermarkMessage watermarkMessage, SystemStreamPartition ssp, MessageCollector collector, TaskCoordinator coordinator) {
    LOG.debug("Received watermark {} from {}", watermarkMessage.getTimestamp(), ssp);
    watermarkStates.update(watermarkMessage, ssp);
    long watermark = watermarkStates.getWatermark(ssp.getSystemStream());
    CompletionStage<Void> watermarkFuture = CompletableFuture.completedFuture(null);
    if (currentWatermark < watermark) {
        LOG.debug("Got watermark {} from stream {}", watermark, ssp.getSystemStream());
        if (watermarkMessage.getTaskName() != null && shouldTaskBroadcastToOtherPartitions(ssp)) {
            // This is the aggregation task, which already received all the watermark messages from upstream
            // broadcast the watermark to all the peer partitions
            // additionally if elasiticty is enabled
            // then only one of the elastic tasks of the ssp will broadcast
            controlMessageSender.broadcastToOtherPartitions(new WatermarkMessage(watermark), ssp, collector);
        }
        // populate the watermark through the dag
        watermarkFuture = onWatermark(watermark, collector, coordinator).thenAccept(ignored -> watermarkStates.updateAggregateMetric(ssp, watermark));
    }
    return watermarkFuture;
}
Also used : ScheduledFunction(org.apache.samza.operators.functions.ScheduledFunction) MetricsConfig(org.apache.samza.config.MetricsConfig) LoggerFactory(org.slf4j.LoggerFactory) JobConfig(org.apache.samza.config.JobConfig) CompletableFuture(java.util.concurrent.CompletableFuture) TaskModel(org.apache.samza.job.model.TaskModel) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) TaskContext(org.apache.samza.context.TaskContext) WatermarkFunction(org.apache.samza.operators.functions.WatermarkFunction) Counter(org.apache.samza.metrics.Counter) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) CallbackScheduler(org.apache.samza.scheduler.CallbackScheduler) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) WatermarkMessage(org.apache.samza.system.WatermarkMessage) HighResolutionClock(org.apache.samza.util.HighResolutionClock) LinkedHashSet(java.util.LinkedHashSet) TaskName(org.apache.samza.container.TaskName) Logger(org.slf4j.Logger) Timer(org.apache.samza.metrics.Timer) Collection(java.util.Collection) ContainerContext(org.apache.samza.context.ContainerContext) Set(java.util.Set) Scheduler(org.apache.samza.operators.Scheduler) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Context(org.apache.samza.context.Context) CompletionStage(java.util.concurrent.CompletionStage) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Config(org.apache.samza.config.Config) Collections(java.util.Collections) InternalTaskContext(org.apache.samza.context.InternalTaskContext) WatermarkMessage(org.apache.samza.system.WatermarkMessage)

Example 9 with WatermarkMessage

use of org.apache.samza.system.WatermarkMessage 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

WatermarkMessage (org.apache.samza.system.WatermarkMessage)9 SystemStream (org.apache.samza.system.SystemStream)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 EndOfStreamMessage (org.apache.samza.system.EndOfStreamMessage)4 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)4 Partition (org.apache.samza.Partition)3 SamzaException (org.apache.samza.SamzaException)3 MessageCollector (org.apache.samza.task.MessageCollector)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 OpMessage (org.apache.beam.runners.samza.runtime.OpMessage)2 Config (org.apache.samza.config.Config)2 MapConfig (org.apache.samza.config.MapConfig)2 Context (org.apache.samza.context.Context)2