You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/11/15 21:10:30 UTC

[GitHub] [kafka] bbejeck opened a new pull request, #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

bbejeck opened a new pull request, #12861:
URL: https://github.com/apache/kafka/pull/12861

   With the addition of the new Processor API the newly added `FixedKeyProcessorNodeFactory` extends the `ProcessorNodeFactory` class.  The `ProcessorNodeFactory` had a private field `Set<String> stateStoreNames` initialized to an empty see.  The `FixedKeyProcessorNodeFactory` also had a private field `Set<String> stateStoreNames`.  
   
   When executing `InternalTopologyBuilder.build`  executing the `buildProcessorNode` method passed any node factory as `ProcessorNodeFactory` and the method references the `stateStoreNames` field, it's pointing to the superclass field, which is empty so the corresponding `StoreBuilder`(s) are never added - causing NPE in the topology.
   
   This PR makes the field `protected on `ProcessorNodeFactory` so `FixedKeyProcessorNodeFactory` inherits it.
   
   *Summary of testing strategy (including rationale)
   for the feature or bug fix. Unit and/or integration
   tests are expected for any behaviour change and
   system tests should be considered for larger changes.*
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on a diff in pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on code in PR #12861:
URL: https://github.com/apache/kafka/pull/12861#discussion_r1024098085


##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")

Review Comment:
   ack - I'll add test for using `addStateStore` instead of using `ConnectedStoreProvider` as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on PR #12861:
URL: https://github.com/apache/kafka/pull/12861#issuecomment-1317730593

   Failures unrelated


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck merged pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck merged PR #12861:
URL: https://github.com/apache/kafka/pull/12861


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] mjsax commented on a diff in pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
mjsax commented on code in PR #12861:
URL: https://github.com/apache/kafka/pull/12861#discussion_r1023379924


##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")

Review Comment:
   Should we expliclity call `addStateStore` instead of using the `ConnectedStoreProvider` interface (or maybe test both)?



##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")
+                .to("output", Produced.with(Serdes.String(), Serdes.String()));
+
+        final List<KeyValue<String, String>> words = Arrays.asList(KeyValue.pair("a", "foo"), KeyValue.pair("b", "bar"), KeyValue.pair("c", "baz"));
+        try (TopologyTestDriver testDriver = new TopologyTestDriver(builder.build())) {
+            final TestInputTopic<String, String>
+                    testDriverInputTopic =
+                    testDriver.createInputTopic("input", Serdes.String().serializer(), Serdes.String().serializer());
+
+            words.forEach(clk -> testDriverInputTopic.pipeInput(clk.key, clk.value));
+
+            final List<String> expectedOutput = asList("fooUpdated", "barUpdated", "bazUpdated");
+
+            final Deserializer<String> keyDeserializer = Serdes.String().deserializer();
+            final List<String> actualOutput =
+                    new ArrayList<>(testDriver.createOutputTopic("output", keyDeserializer, Serdes.String().deserializer()).readValuesToList());
+
+            Assertions.assertEquals(expectedOutput, actualOutput);
+        }
+    }
+    private static class TransformerSupplier implements FixedKeyProcessorSupplier<String, String, String> {
+        private final StoreBuilder<?> storeBuilder;
+
+        public TransformerSupplier(final StoreBuilder<?> storeBuilder) {
+            this.storeBuilder = storeBuilder;
+        }
+
+        @Override
+        public ContextualFixedKeyProcessor<String, String, String> get() {
+            return new ContextualFixedKeyProcessor<String, String, String>() {
+                KeyValueStore<String, String> store;
+                FixedKeyProcessorContext<String, String> context;
+
+                @Override
+                public void init(final FixedKeyProcessorContext<String, String> context) {
+                    super.init(context);
+                    store = context.getStateStore("store");
+                    this.context = context;
+                }
+
+                @Override
+                public void process(final FixedKeyRecord<String, String> record) {
+                    final String updated = store.get(record.key());
+                    store.putIfAbsent(record.key(), record.value() + "Updated");

Review Comment:
   Seems the only test guard is here, ie, if `store == null` we would crash? Should the test code above actually verify the store content in addition to the output topic content?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on a diff in pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on code in PR #12861:
URL: https://github.com/apache/kafka/pull/12861#discussion_r1024098926


##########
streams/src/test/java/org/apache/kafka/streams/kstream/internals/KStreamNewProcessorApiTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.streams.kstream.internals;
+
+import org.apache.kafka.common.serialization.Deserializer;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.TestInputTopic;
+import org.apache.kafka.streams.TopologyTestDriver;
+import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Produced;
+import org.apache.kafka.streams.processor.api.ContextualFixedKeyProcessor;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorContext;
+import org.apache.kafka.streams.processor.api.FixedKeyProcessorSupplier;
+import org.apache.kafka.streams.processor.api.FixedKeyRecord;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.Arrays.asList;
+
+
+public class KStreamNewProcessorApiTest {
+
+    @Test
+    @DisplayName("Test for using new Processor API and state stores with the DSL")
+    void shouldGetStateStoreWithNewProcessor() {
+        final StreamsBuilder builder = new StreamsBuilder();
+        final StoreBuilder<?> storeBuilder = Stores.keyValueStoreBuilder(Stores.inMemoryKeyValueStore("store"), Serdes.String(), Serdes.String());
+
+
+        builder.stream("input", Consumed.with(Serdes.String(), Serdes.String()))
+                .processValues(new TransformerSupplier(storeBuilder), "store")
+                .to("output", Produced.with(Serdes.String(), Serdes.String()));
+
+        final List<KeyValue<String, String>> words = Arrays.asList(KeyValue.pair("a", "foo"), KeyValue.pair("b", "bar"), KeyValue.pair("c", "baz"));
+        try (TopologyTestDriver testDriver = new TopologyTestDriver(builder.build())) {
+            final TestInputTopic<String, String>
+                    testDriverInputTopic =
+                    testDriver.createInputTopic("input", Serdes.String().serializer(), Serdes.String().serializer());
+
+            words.forEach(clk -> testDriverInputTopic.pipeInput(clk.key, clk.value));
+
+            final List<String> expectedOutput = asList("fooUpdated", "barUpdated", "bazUpdated");
+
+            final Deserializer<String> keyDeserializer = Serdes.String().deserializer();
+            final List<String> actualOutput =
+                    new ArrayList<>(testDriver.createOutputTopic("output", keyDeserializer, Serdes.String().deserializer()).readValuesToList());
+
+            Assertions.assertEquals(expectedOutput, actualOutput);
+        }
+    }
+    private static class TransformerSupplier implements FixedKeyProcessorSupplier<String, String, String> {
+        private final StoreBuilder<?> storeBuilder;
+
+        public TransformerSupplier(final StoreBuilder<?> storeBuilder) {
+            this.storeBuilder = storeBuilder;
+        }
+
+        @Override
+        public ContextualFixedKeyProcessor<String, String, String> get() {
+            return new ContextualFixedKeyProcessor<String, String, String>() {
+                KeyValueStore<String, String> store;
+                FixedKeyProcessorContext<String, String> context;
+
+                @Override
+                public void init(final FixedKeyProcessorContext<String, String> context) {
+                    super.init(context);
+                    store = context.getStateStore("store");
+                    this.context = context;
+                }
+
+                @Override
+                public void process(final FixedKeyRecord<String, String> record) {
+                    final String updated = store.get(record.key());
+                    store.putIfAbsent(record.key(), record.value() + "Updated");

Review Comment:
   ack - will test the store content as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on PR #12861:
URL: https://github.com/apache/kafka/pull/12861#issuecomment-1317228760

   Updates per comments - will merge once build completes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on PR #12861:
URL: https://github.com/apache/kafka/pull/12861#issuecomment-1317733576

   Merged #12861 into trunk


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on PR #12861:
URL: https://github.com/apache/kafka/pull/12861#issuecomment-1317781612

   Cherry-picked to 3.3


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [kafka] bbejeck commented on pull request #12861: KAFKA-14388 - Fixes the NPE when using the new Processor API with the DSL

Posted by GitBox <gi...@apache.org>.
bbejeck commented on PR #12861:
URL: https://github.com/apache/kafka/pull/12861#issuecomment-1315883975

   \cc @jeqo for review as well


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org