You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/04/21 17:40:48 UTC

[GitHub] [druid] jihoonson opened a new pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

jihoonson opened a new pull request #9734:
URL: https://github.com/apache/druid/pull/9734


   Fixes #9728 
   
   ### Description
   
   `SettableByteEntityReader` has a null check in its constructor which throws NPE when `inputFormat` is null. This PR changes to creating it when `inputFormat` is not null.
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
      - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [x] added documentation for new or modified features or behaviors.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/licenses.yaml)
   - [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster.


----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r414214266



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/SeekableStreamIndexTaskRunner.java
##########
@@ -372,12 +373,10 @@ private TaskStatus runInternal(TaskToolbox toolbox) throws Exception
     // Now we can initialize StreamChunkReader with the given toolbox.
     final StreamChunkParser parser = new StreamChunkParser(
         this.parser,
-        new SettableByteEntityReader(
-            inputFormat,
-            inputRowSchema,
-            task.getDataSchema().getTransformSpec(),
-            toolbox.getIndexingTmpDir()
-        )
+        inputFormat,

Review comment:
       > I think it would be good to add some tests for this class since it looks like the existing unit tests don't cover this class at all. For example, there's nothing ensuring that `parser` and `inputFormat` are both not `null` when they're passed to the `StreamChunkParser` constructor.
   
   Hmm, I'm not sure I understood your comment. I believe this class is being tested in both `KafkaIndexTaskTest` and `KinesisIndexTaskTest`. Either `parser` or `inputFormat` can be set for kafka indexing service to work. If both are set, it prefers `inputFormat` over `parser.
   
   > Also, it looks like we only have tests that ensure that real time ingestion works with JSON. We should add a tests to make sure real time ingestion never breaks in the future for Avro, TSV, etc.
   
   Those tests seem useful, but I don't think adding them should be done in this PR since it's a sort of out of scope; the issue is creating a `SettableByteEntityReader` can fail with any parser in which `toInputFormat` is missing. Also, it could be a big change even bigger than this PR. I will add them in a follow-up PR.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412487818



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(

Review comment:
       Changed to nullable.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
jihoonson commented on pull request #9734:
URL: https://github.com/apache/druid/pull/9734#issuecomment-618739429


   I tested this PR by feeding Avro data into local Druid cluster.


----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412487615



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");

Review comment:
       Oops, thanks.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r414261225



##########
File path: core/src/main/java/org/apache/druid/data/input/impl/DelimitedParseSpec.java
##########
@@ -123,6 +124,12 @@ public int getSkipHeaderRows()
     );
   }
 
+  @Override
+  public InputFormat toInputFormat()

Review comment:
       Sounds good. I will add some tests with TSV in a follow-up PR.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] ccaominh commented on a change in pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
ccaominh commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412496213



##########
File path: core/src/main/java/org/apache/druid/data/input/impl/DelimitedParseSpec.java
##########
@@ -123,6 +124,12 @@ public int getSkipHeaderRows()
     );
   }
 
+  @Override
+  public InputFormat toInputFormat()

Review comment:
       Where is the test that makes sure TSV is handled properly now? Also do we already have a similar test for CSV?

##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/SeekableStreamIndexTaskRunner.java
##########
@@ -372,12 +373,10 @@ private TaskStatus runInternal(TaskToolbox toolbox) throws Exception
     // Now we can initialize StreamChunkReader with the given toolbox.
     final StreamChunkParser parser = new StreamChunkParser(
         this.parser,
-        new SettableByteEntityReader(
-            inputFormat,
-            inputRowSchema,
-            task.getDataSchema().getTransformSpec(),
-            toolbox.getIndexingTmpDir()
-        )
+        inputFormat,

Review comment:
       I think it would be good to add some tests for this class since it looks like the existing unit tests don't cover this class at all. For example, there's nothing ensuring that `parser` and `inputFormat` are both not `null` when they're passed to the `StreamChunkParser` constructor.
   
   Also, it looks like we only have tests that ensure that real time ingestion works with JSON. We should add a tests to make sure real time ingestion never breaks in the future for Avro, TSV, etc. 

##########
File path: indexing-service/src/test/java/org/apache/druid/indexing/seekablestream/StreamChunkParserTest.java
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.druid.indexing.seekablestream;
+
+import com.google.common.collect.Iterables;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.data.input.InputEntityReader;
+import org.apache.druid.data.input.InputFormat;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.InputRowParser;
+import org.apache.druid.data.input.impl.JSONParseSpec;
+import org.apache.druid.data.input.impl.JsonInputFormat;
+import org.apache.druid.data.input.impl.StringInputRowParser;
+import org.apache.druid.data.input.impl.TimestampSpec;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.parsers.JSONPathSpec;
+import org.apache.druid.segment.transform.TransformSpec;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class StreamChunkParserTest
+{
+  private static final TimestampSpec TIMESTAMP_SPEC = new TimestampSpec(null, null, null);
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void testWithParserAndNullInputformatParseProperly() throws IOException
+  {
+    final InputRowParser<ByteBuffer> parser = new StringInputRowParser(
+        new NotConvertibleToInputFormatParseSpec(),
+        StringUtils.UTF8_STRING
+    );
+    final StreamChunkParser chunkParser = new StreamChunkParser(
+        parser,
+        // Set nulls for all parameters below since inputFormat will be never used.
+        null,
+        null,
+        null,
+        null
+    );
+    final String json = "{\"timestamp\": \"2020-01-01\", \"dim\": \"val\", \"met\": \"val2\"}";
+    List<InputRow> parsedRows = chunkParser.parse(Collections.singletonList(json.getBytes(StringUtils.UTF8_STRING)));
+    Assert.assertEquals(1, parsedRows.size());
+    InputRow row = parsedRows.get(0);
+    Assert.assertEquals(DateTimes.of("2020-01-01"), row.getTimestamp());
+    Assert.assertEquals("val", Iterables.getOnlyElement(row.getDimension("dim")));
+    Assert.assertEquals("val2", Iterables.getOnlyElement(row.getDimension("met")));

Review comment:
       This is repeated for two of the tests below, so may be worth factoring out.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r414214225



##########
File path: core/src/main/java/org/apache/druid/data/input/impl/DelimitedParseSpec.java
##########
@@ -123,6 +124,12 @@ public int getSkipHeaderRows()
     );
   }
 
+  @Override
+  public InputFormat toInputFormat()

Review comment:
       What do you mean by "handling TSV properly"? This method does nothing useful for now. I think we should remove this method.

##########
File path: indexing-service/src/test/java/org/apache/druid/indexing/seekablestream/StreamChunkParserTest.java
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.druid.indexing.seekablestream;
+
+import com.google.common.collect.Iterables;
+import org.apache.druid.data.input.InputEntity;
+import org.apache.druid.data.input.InputEntityReader;
+import org.apache.druid.data.input.InputFormat;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowSchema;
+import org.apache.druid.data.input.impl.DimensionsSpec;
+import org.apache.druid.data.input.impl.InputRowParser;
+import org.apache.druid.data.input.impl.JSONParseSpec;
+import org.apache.druid.data.input.impl.JsonInputFormat;
+import org.apache.druid.data.input.impl.StringInputRowParser;
+import org.apache.druid.data.input.impl.TimestampSpec;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.parsers.JSONPathSpec;
+import org.apache.druid.segment.transform.TransformSpec;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import javax.annotation.Nullable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+public class StreamChunkParserTest
+{
+  private static final TimestampSpec TIMESTAMP_SPEC = new TimestampSpec(null, null, null);
+
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void testWithParserAndNullInputformatParseProperly() throws IOException
+  {
+    final InputRowParser<ByteBuffer> parser = new StringInputRowParser(
+        new NotConvertibleToInputFormatParseSpec(),
+        StringUtils.UTF8_STRING
+    );
+    final StreamChunkParser chunkParser = new StreamChunkParser(
+        parser,
+        // Set nulls for all parameters below since inputFormat will be never used.
+        null,
+        null,
+        null,
+        null
+    );
+    final String json = "{\"timestamp\": \"2020-01-01\", \"dim\": \"val\", \"met\": \"val2\"}";
+    List<InputRow> parsedRows = chunkParser.parse(Collections.singletonList(json.getBytes(StringUtils.UTF8_STRING)));
+    Assert.assertEquals(1, parsedRows.size());
+    InputRow row = parsedRows.get(0);
+    Assert.assertEquals(DateTimes.of("2020-01-01"), row.getTimestamp());
+    Assert.assertEquals("val", Iterables.getOnlyElement(row.getDimension("dim")));
+    Assert.assertEquals("val2", Iterables.getOnlyElement(row.getDimension("met")));

Review comment:
       Done.

##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/SeekableStreamIndexTaskRunner.java
##########
@@ -372,12 +373,10 @@ private TaskStatus runInternal(TaskToolbox toolbox) throws Exception
     // Now we can initialize StreamChunkReader with the given toolbox.
     final StreamChunkParser parser = new StreamChunkParser(
         this.parser,
-        new SettableByteEntityReader(
-            inputFormat,
-            inputRowSchema,
-            task.getDataSchema().getTransformSpec(),
-            toolbox.getIndexingTmpDir()
-        )
+        inputFormat,

Review comment:
       > I think it would be good to add some tests for this class since it looks like the existing unit tests don't cover this class at all. For example, there's nothing ensuring that `parser` and `inputFormat` are both not `null` when they're passed to the `StreamChunkParser` constructor.
   
   Hmm, I'm not sure I understood your comment. I believe this class is being tested in both `KafkaIndexTaskTest` and `KinesisIndexTaskTest`. Either `parser` or `inputFormat` can be set for kafka indexing service to work. If both are set, it prefers `inputFormat` over `parser.
   
   > Also, it looks like we only have tests that ensure that real time ingestion works with JSON. We should add a tests to make sure real time ingestion never breaks in the future for Avro, TSV, etc.
   
   Those tests seem useful, but I don't think adding them should be done in this PR since it's out of scope; the issue is creating a `SettableByteEntityReader` can fail with any parser in which `toInputFormat` is missing. I will add them in a follow-up PR.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] ccaominh commented on a change in pull request #9734: Initialize SettableByteEntityReader only when inputFormat is not null

Posted by GitBox <gi...@apache.org>.
ccaominh commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r414233724



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/SeekableStreamIndexTaskRunner.java
##########
@@ -372,12 +373,10 @@ private TaskStatus runInternal(TaskToolbox toolbox) throws Exception
     // Now we can initialize StreamChunkReader with the given toolbox.
     final StreamChunkParser parser = new StreamChunkParser(
         this.parser,
-        new SettableByteEntityReader(
-            inputFormat,
-            inputRowSchema,
-            task.getDataSchema().getTransformSpec(),
-            toolbox.getIndexingTmpDir()
-        )
+        inputFormat,

Review comment:
       Cool, I didn't look at the coverage from `KafkaIndexTaskTest` and `KinesisIndexTaskTest`, so my comment can be disregarded.
   
   Adding the integration tests in a follow up PR sounds good to me since you manually verified your fix for the scenario that uncovered this bug.

##########
File path: core/src/main/java/org/apache/druid/data/input/impl/DelimitedParseSpec.java
##########
@@ -123,6 +124,12 @@ public int getSkipHeaderRows()
     );
   }
 
+  @Override
+  public InputFormat toInputFormat()

Review comment:
       Sorry, my comment was more about adding integration tests for streaming ingestion with the various formats, which I believe we're missing. This can be addressed in a follow up PR that tackles the issue comprehensively.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] jihoonson commented on a change in pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

Posted by GitBox <gi...@apache.org>.
jihoonson commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412488057



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(
+        inputFormat,
+        inputRowSchema,
+        transformSpec,
+        indexingTmpDir
+    ));
   }
 
   List<InputRow> parse(List<byte[]> streamChunk) throws IOException
   {
     if (parser != null) {
       return parseWithParser(parser, streamChunk);
     } else {
-      return parseWithInputFormat(byteEntityReader, streamChunk);
+      return parseWithInputFormat(lazyByteEntityReaderSupplier.get(), streamChunk);

Review comment:
       Changed to prefer inputFormat.




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] clintropolis commented on a change in pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

Posted by GitBox <gi...@apache.org>.
clintropolis commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412475710



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(
+        inputFormat,
+        inputRowSchema,
+        transformSpec,
+        indexingTmpDir
+    ));
   }
 
   List<InputRow> parse(List<byte[]> streamChunk) throws IOException
   {
     if (parser != null) {

Review comment:
       I know this isn't new, but why do we prefer the parser over the byteEntityReader if both are set?




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


[GitHub] [druid] clintropolis commented on a change in pull request #9734: Lazy initialization of SettableByteEntityReader to avoid NPE

Posted by GitBox <gi...@apache.org>.
clintropolis commented on a change in pull request #9734:
URL: https://github.com/apache/druid/pull/9734#discussion_r412472637



##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");

Review comment:
       nit: this error message should be "Either parser or inputFormat **should** be set" maybe since this is checking if both are null?

##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(
+        inputFormat,
+        inputRowSchema,
+        transformSpec,
+        indexingTmpDir
+    ));
   }
 
   List<InputRow> parse(List<byte[]> streamChunk) throws IOException
   {
     if (parser != null) {
       return parseWithParser(parser, streamChunk);
     } else {
-      return parseWithInputFormat(byteEntityReader, streamChunk);
+      return parseWithInputFormat(lazyByteEntityReaderSupplier.get(), streamChunk);

Review comment:
       Would it be better to prefer the inputFormat if it exists?

##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(

Review comment:
       nit: Why a supplier instead of just making `byteEntityReader` `@Nullable`? it seems like it would not be used if `parser` is not null.

##########
File path: indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/StreamChunkParser.java
##########
@@ -38,20 +45,38 @@
 {
   @Nullable
   private final InputRowParser<ByteBuffer> parser;
-  private final SettableByteEntityReader byteEntityReader;
+  private final Supplier<SettableByteEntityReader> lazyByteEntityReaderSupplier; // lazy initializer
 
-  StreamChunkParser(@Nullable InputRowParser<ByteBuffer> parser, SettableByteEntityReader byteEntityReader)
+  /**
+   * Either parser or inputFormat shouldn't be null.
+   */
+  StreamChunkParser(
+      @Nullable InputRowParser<ByteBuffer> parser,
+      @Nullable InputFormat inputFormat,
+      InputRowSchema inputRowSchema,
+      TransformSpec transformSpec,
+      File indexingTmpDir
+  )
   {
+    if (parser == null && inputFormat == null) {
+      throw new IAE("Either parser or inputFormat shouldn't be set");
+    }
     this.parser = parser;
-    this.byteEntityReader = byteEntityReader;
+    // Create a lazy initializer since it will fail to create a SettableByteEntityReader if inputFormat is null
+    this.lazyByteEntityReaderSupplier = Suppliers.memoize(() -> new SettableByteEntityReader(
+        inputFormat,
+        inputRowSchema,
+        transformSpec,
+        indexingTmpDir
+    ));
   }
 
   List<InputRow> parse(List<byte[]> streamChunk) throws IOException
   {
     if (parser != null) {

Review comment:
       I know this isn't new, but why do we prefer the parser over the byteEntityReader if both are set?




----------------------------------------------------------------
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org