diff --git a/extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParser.java b/extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParser.java index b75e4002850..81ddb444092 100644 --- a/extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParser.java +++ b/extensions-core/protobuf-extensions/src/main/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParser.java @@ -22,6 +22,7 @@ package org.apache.druid.data.input.protobuf; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.github.os72.protobuf.dynamic.DynamicSchema; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -53,7 +54,7 @@ public class ProtobufInputRowParser implements ByteBufferInputRowParser private final ParseSpec parseSpec; private final String descriptorFilePath; private final String protoMessageType; - private final Descriptor descriptor; + private Descriptor descriptor; private Parser parser; private final List dimensions; @@ -67,7 +68,6 @@ public class ProtobufInputRowParser implements ByteBufferInputRowParser this.parseSpec = parseSpec; this.descriptorFilePath = descriptorFilePath; this.protoMessageType = protoMessageType; - this.descriptor = getDescriptor(descriptorFilePath); this.dimensions = parseSpec.getDimensionsSpec().getDimensionNames(); } @@ -83,6 +83,14 @@ public class ProtobufInputRowParser implements ByteBufferInputRowParser return new ProtobufInputRowParser(parseSpec, descriptorFilePath, protoMessageType); } + @VisibleForTesting + void initDescriptor() + { + if (this.descriptor == null) { + this.descriptor = getDescriptor(descriptorFilePath); + } + } + @Override public List parseBatch(ByteBuffer input) { @@ -90,6 +98,7 @@ public class ProtobufInputRowParser implements ByteBufferInputRowParser // parser should be created when it is really used to avoid unnecessary initialization of the underlying // parseSpec. parser = parseSpec.makeParser(); + initDescriptor(); } String json; try { diff --git a/extensions-core/protobuf-extensions/src/test/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParserTest.java b/extensions-core/protobuf-extensions/src/test/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParserTest.java index 92cefd34f17..081fc23a24f 100644 --- a/extensions-core/protobuf-extensions/src/test/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParserTest.java +++ b/extensions-core/protobuf-extensions/src/test/java/org/apache/druid/data/input/protobuf/ProtobufInputRowParserTest.java @@ -83,7 +83,7 @@ public class ProtobufInputRowParserTest //configure parser with desc file, and specify which file name to use @SuppressWarnings("unused") // expected to create parser without exception ProtobufInputRowParser parser = new ProtobufInputRowParser(parseSpec, "prototest.desc", "ProtoTestEvent"); - + parser.initDescriptor(); } @@ -93,7 +93,7 @@ public class ProtobufInputRowParserTest //configure parser with desc file, and specify which file name to use @SuppressWarnings("unused") // expected to create parser without exception ProtobufInputRowParser parser = new ProtobufInputRowParser(parseSpec, "prototest.desc", "prototest.ProtoTestEvent"); - + parser.initDescriptor(); } @@ -103,7 +103,7 @@ public class ProtobufInputRowParserTest //configure parser with desc file @SuppressWarnings("unused") // expected exception ProtobufInputRowParser parser = new ProtobufInputRowParser(parseSpec, "prototest.desc", "BadName"); - + parser.initDescriptor(); } @Test(expected = ParseException.class) @@ -112,6 +112,7 @@ public class ProtobufInputRowParserTest //configure parser with non existent desc file @SuppressWarnings("unused") // expected exception ProtobufInputRowParser parser = new ProtobufInputRowParser(parseSpec, "file:/nonexist.desc", "BadName"); + parser.initDescriptor(); } @Test @@ -120,6 +121,7 @@ public class ProtobufInputRowParserTest // For the backward compatibility, protoMessageType allows null when the desc file has only one message type. @SuppressWarnings("unused") // expected to create parser without exception ProtobufInputRowParser parser = new ProtobufInputRowParser(parseSpec, "prototest.desc", null); + parser.initDescriptor(); } @Test