Search in sources :

Example 1 with Punctuator

use of org.apache.kafka.streams.processor.Punctuator in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCapturePunctuator.

@Test
public void shouldCapturePunctuator() {
    final Processor<String, Long> processor = new Processor<String, Long>() {

        @Override
        public void init(final ProcessorContext context) {
            context.schedule(1000L, PunctuationType.WALL_CLOCK_TIME, new Punctuator() {

                @Override
                public void punctuate(final long timestamp) {
                    context.commit();
                }
            });
        }

        @Override
        public void process(final String key, final Long value) {
        }

        @Override
        public void punctuate(final long timestamp) {
        }

        @Override
        public void close() {
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    final MockProcessorContext.CapturedPunctuator capturedPunctuator = context.scheduledPunctuators().get(0);
    assertEquals(1000L, capturedPunctuator.getIntervalMs());
    assertEquals(PunctuationType.WALL_CLOCK_TIME, capturedPunctuator.getType());
    assertFalse(capturedPunctuator.cancelled());
    final Punctuator punctuator = capturedPunctuator.getPunctuator();
    assertFalse(context.committed());
    punctuator.punctuate(1234L);
    assertTrue(context.committed());
}
Also used : Processor(org.apache.kafka.streams.processor.Processor) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) Punctuator(org.apache.kafka.streams.processor.Punctuator) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 2 with Punctuator

use of org.apache.kafka.streams.processor.Punctuator in project apache-kafka-on-k8s by banzaicloud.

the class StreamTaskTest method shouldWrapKafkaExceptionsWithStreamsExceptionAndAddContextWhenPunctuatingStreamTime.

@Test
public void shouldWrapKafkaExceptionsWithStreamsExceptionAndAddContextWhenPunctuatingStreamTime() {
    task = createStatelessTask(false);
    task.initializeStateStores();
    task.initializeTopology();
    try {
        task.punctuate(processorStreamTime, 1, PunctuationType.STREAM_TIME, new Punctuator() {

            @Override
            public void punctuate(long timestamp) {
                throw new KafkaException("KABOOM!");
            }
        });
        fail("Should've thrown StreamsException");
    } catch (final StreamsException e) {
        final String message = e.getMessage();
        assertTrue("message=" + message + " should contain processor", message.contains("processor '" + processorStreamTime.name() + "'"));
        assertThat(task.processorContext.currentNode(), nullValue());
    }
}
Also used : Punctuator(org.apache.kafka.streams.processor.Punctuator) StreamsException(org.apache.kafka.streams.errors.StreamsException) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 3 with Punctuator

use of org.apache.kafka.streams.processor.Punctuator in project apache-kafka-on-k8s by banzaicloud.

the class StreamTaskTest method shouldThrowIllegalStateExceptionOnScheduleIfCurrentNodeIsNull.

@Test(expected = IllegalStateException.class)
public void shouldThrowIllegalStateExceptionOnScheduleIfCurrentNodeIsNull() {
    task = createStatelessTask(false);
    task.schedule(1, PunctuationType.STREAM_TIME, new Punctuator() {

        @Override
        public void punctuate(long timestamp) {
        // no-op
        }
    });
}
Also used : Punctuator(org.apache.kafka.streams.processor.Punctuator) Test(org.junit.Test)

Example 4 with Punctuator

use of org.apache.kafka.streams.processor.Punctuator in project apache-kafka-on-k8s by banzaicloud.

the class StreamThreadTest method shouldPunctuateActiveTask.

@Test
public void shouldPunctuateActiveTask() {
    final List<Long> punctuatedStreamTime = new ArrayList<>();
    final List<Long> punctuatedWallClockTime = new ArrayList<>();
    final ProcessorSupplier<Object, Object> punctuateProcessor = new ProcessorSupplier<Object, Object>() {

        @Override
        public Processor<Object, Object> get() {
            return new Processor<Object, Object>() {

                @Override
                public void init(ProcessorContext context) {
                    context.schedule(100L, PunctuationType.STREAM_TIME, new Punctuator() {

                        @Override
                        public void punctuate(long timestamp) {
                            punctuatedStreamTime.add(timestamp);
                        }
                    });
                    context.schedule(100L, PunctuationType.WALL_CLOCK_TIME, new Punctuator() {

                        @Override
                        public void punctuate(long timestamp) {
                            punctuatedWallClockTime.add(timestamp);
                        }
                    });
                }

                @Override
                public void process(Object key, Object value) {
                }

                @SuppressWarnings("deprecation")
                @Override
                public void punctuate(long timestamp) {
                }

                @Override
                public void close() {
                }
            };
        }
    };
    internalStreamsBuilder.stream(Collections.singleton(topic1), consumed).process(punctuateProcessor);
    final StreamThread thread = createStreamThread(clientId, config, false);
    thread.setState(StreamThread.State.RUNNING);
    thread.rebalanceListener.onPartitionsRevoked(null);
    final List<TopicPartition> assignedPartitions = new ArrayList<>();
    final Map<TaskId, Set<TopicPartition>> activeTasks = new HashMap<>();
    // assign single partition
    assignedPartitions.add(t1p1);
    activeTasks.put(task1, Collections.singleton(t1p1));
    thread.taskManager().setAssignmentMetadata(activeTasks, Collections.<TaskId, Set<TopicPartition>>emptyMap());
    clientSupplier.consumer.assign(assignedPartitions);
    clientSupplier.consumer.updateBeginningOffsets(Collections.singletonMap(t1p1, 0L));
    thread.rebalanceListener.onPartitionsAssigned(assignedPartitions);
    thread.runOnce(-1);
    assertEquals(0, punctuatedStreamTime.size());
    assertEquals(0, punctuatedWallClockTime.size());
    mockTime.sleep(100L);
    for (long i = 0L; i < 10L; i++) {
        clientSupplier.consumer.addRecord(new ConsumerRecord<>(topic1, 1, i, i * 100L, TimestampType.CREATE_TIME, ConsumerRecord.NULL_CHECKSUM, ("K" + i).getBytes().length, ("V" + i).getBytes().length, ("K" + i).getBytes(), ("V" + i).getBytes()));
    }
    thread.runOnce(-1);
    assertEquals(1, punctuatedStreamTime.size());
    assertEquals(1, punctuatedWallClockTime.size());
    mockTime.sleep(100L);
    thread.runOnce(-1);
    // we should skip stream time punctuation, only trigger wall-clock time punctuation
    assertEquals(1, punctuatedStreamTime.size());
    assertEquals(2, punctuatedWallClockTime.size());
}
Also used : Processor(org.apache.kafka.streams.processor.Processor) TaskId(org.apache.kafka.streams.processor.TaskId) Set(java.util.Set) HashSet(java.util.HashSet) Punctuator(org.apache.kafka.streams.processor.Punctuator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessorSupplier(org.apache.kafka.streams.processor.ProcessorSupplier) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) TopicPartition(org.apache.kafka.common.TopicPartition) InternalStreamsBuilderTest(org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest) Test(org.junit.Test)

Example 5 with Punctuator

use of org.apache.kafka.streams.processor.Punctuator in project apache-kafka-on-k8s by banzaicloud.

the class PunctuationQueueTest method testPunctuationIntervalCustomAlignment.

@Test
public void testPunctuationIntervalCustomAlignment() {
    final TestProcessor processor = new TestProcessor();
    final ProcessorNode<String, String> node = new ProcessorNode<>("test", processor, null);
    final PunctuationQueue queue = new PunctuationQueue();
    final Punctuator punctuator = new Punctuator() {

        @Override
        public void punctuate(long timestamp) {
            node.processor().punctuate(timestamp);
        }
    };
    final PunctuationSchedule sched = new PunctuationSchedule(node, 50L, 100L, punctuator);
    final long now = sched.timestamp - 50L;
    queue.schedule(sched);
    ProcessorNodePunctuator processorNodePunctuator = new ProcessorNodePunctuator() {

        @Override
        public void punctuate(ProcessorNode node, long time, PunctuationType type, Punctuator punctuator) {
            punctuator.punctuate(time);
        }
    };
    queue.mayPunctuate(now, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(0, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 49L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(0, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 50L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(1, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 149L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(1, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 150L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(2, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1051L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(3, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1052L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(3, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1150L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(4, processor.punctuatedAt.size());
}
Also used : PunctuationType(org.apache.kafka.streams.processor.PunctuationType) Punctuator(org.apache.kafka.streams.processor.Punctuator) Test(org.junit.Test)

Aggregations

Punctuator (org.apache.kafka.streams.processor.Punctuator)8 Test (org.junit.Test)8 KafkaException (org.apache.kafka.common.KafkaException)2 StreamsException (org.apache.kafka.streams.errors.StreamsException)2 Processor (org.apache.kafka.streams.processor.Processor)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2 PunctuationType (org.apache.kafka.streams.processor.PunctuationType)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)1 AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)1 MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)1 ProcessorSupplier (org.apache.kafka.streams.processor.ProcessorSupplier)1 TaskId (org.apache.kafka.streams.processor.TaskId)1