Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize SettableByteEntityReader only when inputFormat is not null #9734

Merged
merged 4 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import org.apache.druid.data.input.InputFormat;
import org.apache.druid.java.util.common.parsers.DelimitedParser;
import org.apache.druid.java.util.common.parsers.Parser;

Expand Down Expand Up @@ -123,6 +124,12 @@ public Parser<String, Object> makeParser()
);
}

@Override
public InputFormat toInputFormat()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

{
return new DelimitedInputFormat(columns, listDelimiter, delimiter, hasHeaderRow, null, skipHeaderRows);
}

@Override
public ParseSpec withTimestampSpec(TimestampSpec spec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private InputFormat getGivenInputFormat()
return inputFormat;
}

@Nullable
public InputFormat getInputFormat(ParseSpec parseSpec)
{
return inputFormat == null ? Preconditions.checkNotNull(parseSpec, "parseSpec").toInputFormat() : inputFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public enum Status
private final SeekableStreamIndexTaskIOConfig<PartitionIdType, SequenceOffsetType> ioConfig;
private final SeekableStreamIndexTaskTuningConfig tuningConfig;
private final InputRowSchema inputRowSchema;
@Nullable
private final InputFormat inputFormat;
@Nullable
private final InputRowParser<ByteBuffer> parser;
Expand Down Expand Up @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

@jihoonson jihoonson Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

inputRowSchema,
task.getDataSchema().getTransformSpec(),
toolbox.getIndexingTmpDir()
);

initializeSequences();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@

package org.apache.druid.indexing.seekablestream;

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.ByteEntity;
import org.apache.druid.data.input.impl.InputRowParser;
import org.apache.druid.java.util.common.IAE;
import org.apache.druid.java.util.common.parsers.CloseableIterator;
import org.apache.druid.segment.transform.TransformSpec;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -38,20 +43,42 @@ class StreamChunkParser
{
@Nullable
private final InputRowParser<ByteBuffer> parser;
@Nullable
private final SettableByteEntityReader byteEntityReader;

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 should be set");
}
this.parser = parser;
this.byteEntityReader = byteEntityReader;
if (inputFormat != null) {
this.byteEntityReader = new SettableByteEntityReader(
inputFormat,
inputRowSchema,
transformSpec,
indexingTmpDir
);
} else {
this.byteEntityReader = null;
}
}

List<InputRow> parse(List<byte[]> streamChunk) throws IOException
{
if (parser != null) {
return parseWithParser(parser, streamChunk);
} else {
if (byteEntityReader != null) {
return parseWithInputFormat(byteEntityReader, streamChunk);
} else {
return parseWithParser(parser, streamChunk);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* 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
);
parseAndAssertResult(chunkParser);
}

@Test
public void testWithNullParserAndInputformatParseProperly() throws IOException
{
final JsonInputFormat inputFormat = new JsonInputFormat(JSONPathSpec.DEFAULT, Collections.emptyMap());
final StreamChunkParser chunkParser = new StreamChunkParser(
null,
inputFormat,
new InputRowSchema(TIMESTAMP_SPEC, DimensionsSpec.EMPTY, Collections.emptyList()),
TransformSpec.NONE,
temporaryFolder.newFolder()
);
parseAndAssertResult(chunkParser);
}

@Test
public void testWithNullParserAndNullInputformatFailToCreateParser()
{
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Either parser or inputFormat should be set");
final StreamChunkParser chunkParser = new StreamChunkParser(
null,
null,
null,
null,
null
);
}

@Test
public void testBothParserAndInputFormatParseProperlyUsingInputFormat() throws IOException
{
final InputRowParser<ByteBuffer> parser = new StringInputRowParser(
new NotConvertibleToInputFormatParseSpec(),
StringUtils.UTF8_STRING
);
final TrackingJsonInputFormat inputFormat = new TrackingJsonInputFormat(
JSONPathSpec.DEFAULT,
Collections.emptyMap()
);
final StreamChunkParser chunkParser = new StreamChunkParser(
parser,
inputFormat,
new InputRowSchema(TIMESTAMP_SPEC, DimensionsSpec.EMPTY, Collections.emptyList()),
TransformSpec.NONE,
temporaryFolder.newFolder()
);
parseAndAssertResult(chunkParser);
Assert.assertTrue(inputFormat.used);
}

private void parseAndAssertResult(StreamChunkParser chunkParser) throws IOException
{
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")));
}

private static class NotConvertibleToInputFormatParseSpec extends JSONParseSpec
{
private NotConvertibleToInputFormatParseSpec()
{
super(
TIMESTAMP_SPEC,
DimensionsSpec.EMPTY,
JSONPathSpec.DEFAULT,
Collections.emptyMap()
);
}

@Override
public InputFormat toInputFormat()
{
return null;
}
}

private static class TrackingJsonInputFormat extends JsonInputFormat
{
private boolean used;

private TrackingJsonInputFormat(@Nullable JSONPathSpec flattenSpec, @Nullable Map<String, Boolean> featureSpec)
{
super(flattenSpec, featureSpec);
}

@Override
public InputEntityReader createReader(InputRowSchema inputRowSchema, InputEntity source, File temporaryDirectory)
{
used = true;
return super.createReader(inputRowSchema, source, temporaryDirectory);
}
}
}