Search in sources :

Example 1 with PunctuationType

use of org.apache.kafka.streams.processor.PunctuationType 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)

Example 2 with PunctuationType

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

the class PunctuationQueueTest method testPunctuationInterval.

@Test
public void testPunctuationInterval() {
    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, 0L, 100L, punctuator);
    final long now = sched.timestamp - 100L;
    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 + 99L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(0, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 100L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(1, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 199L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(1, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 200L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(2, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1001L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(3, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1002L, PunctuationType.STREAM_TIME, processorNodePunctuator);
    assertEquals(3, processor.punctuatedAt.size());
    queue.mayPunctuate(now + 1100L, 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)

Example 3 with PunctuationType

use of org.apache.kafka.streams.processor.PunctuationType in project kafka by apache.

the class WordCountTransformerTest method test.

@Test
public void test() {
    final MockProcessorContext<String, String> context = new MockProcessorContext<>();
    // Create and initialize the transformer under test; including its provided store
    final WordCountTransformerDemo.MyTransformerSupplier supplier = new WordCountTransformerDemo.MyTransformerSupplier();
    for (final StoreBuilder<?> storeBuilder : supplier.stores()) {
        final StateStore store = storeBuilder.withLoggingDisabled().build();
        store.init(context.getStateStoreContext(), store);
        context.getStateStoreContext().register(store, null);
    }
    final Transformer<String, String, KeyValue<String, String>> transformer = supplier.get();
    transformer.init(new org.apache.kafka.streams.processor.MockProcessorContext() {

        @Override
        public <S extends StateStore> S getStateStore(final String name) {
            return context.getStateStore(name);
        }

        @Override
        public <K, V> void forward(final K key, final V value) {
            context.forward(new Record<>((String) key, (String) value, 0L));
        }

        @Override
        public Cancellable schedule(final Duration interval, final PunctuationType type, final Punctuator callback) {
            return context.schedule(interval, type, callback);
        }
    });
    // send a record to the transformer
    transformer.transform("key", "alpha beta\tgamma\n\talpha");
    // note that the transformer does not forward during transform()
    assertTrue(context.forwarded().isEmpty());
    // now, we trigger the punctuator, which iterates over the state store and forwards the contents.
    context.scheduledPunctuators().get(0).getPunctuator().punctuate(0L);
    // finally, we can verify the output.
    final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> capturedForwards = context.forwarded();
    final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> expected = asList(new MockProcessorContext.CapturedForward<>(new Record<>("alpha", "2", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("beta", "1", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("gamma", "1", 0L)));
    assertThat(capturedForwards, is(expected));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Cancellable(org.apache.kafka.streams.processor.Cancellable) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Record(org.apache.kafka.streams.processor.api.Record) Punctuator(org.apache.kafka.streams.processor.Punctuator) StateStore(org.apache.kafka.streams.processor.StateStore) Duration(java.time.Duration) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) Test(org.junit.jupiter.api.Test)

Aggregations

PunctuationType (org.apache.kafka.streams.processor.PunctuationType)3 Punctuator (org.apache.kafka.streams.processor.Punctuator)3 Test (org.junit.Test)2 Duration (java.time.Duration)1 KeyValue (org.apache.kafka.streams.KeyValue)1 Cancellable (org.apache.kafka.streams.processor.Cancellable)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 MockProcessorContext (org.apache.kafka.streams.processor.api.MockProcessorContext)1 Record (org.apache.kafka.streams.processor.api.Record)1 Test (org.junit.jupiter.api.Test)1