mayankshriv commented on code in PR #19378:
URL: https://github.com/apache/pinot/pull/19378#discussion_r3899635198
##########
pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufUtils.java:
##########
@@ -20,59 +20,36 @@
import com.google.protobuf.Descriptors;
import com.google.protobuf.ProtobufInternalUtils;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
-import java.nio.file.Files;
-import java.nio.file.Path;
import org.apache.pinot.spi.filesystem.PinotFS;
import org.apache.pinot.spi.filesystem.PinotFSFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public class ProtoBufUtils {
- private static final Logger LOGGER =
LoggerFactory.getLogger(ProtoBufUtils.class);
public static final String TMP_DIR_PREFIX = "pinot-protobuf";
public static final String PB_OUTER_CLASS_SUFFIX = "OuterClass";
private ProtoBufUtils() {
}
- public static File getFileCopiedToLocal(String filePath)
+ /// Reads the contents of a descriptor file (local or remote) into a byte
array. The file is read via
+ /// [PinotFS#open] and the stream is closed before returning - no temporary
files are created.
+ ///
+ /// @param descriptorFilePath URI string pointing to a `.desc` protobuf
descriptor file
+ /// @return the raw bytes of the descriptor file
+ public static byte[] readDescriptorFileBytes(String descriptorFilePath)
throws Exception {
- URI fileURI = URI.create(filePath);
+ URI fileURI = URI.create(descriptorFilePath);
String scheme = fileURI.getScheme();
if (scheme == null) {
scheme = PinotFSFactory.LOCAL_PINOT_FS_SCHEME;
}
- if (PinotFSFactory.isSchemeSupported(scheme)) {
- PinotFS pinotFS = PinotFSFactory.create(scheme);
- Path localTmpDir = Files.createTempDirectory(TMP_DIR_PREFIX +
System.currentTimeMillis());
- File localFile = createLocalFile(fileURI, localTmpDir.toFile());
- LOGGER.info("Copying protocol buffer jar/descriptor file from source: {}
to dst: {}", filePath,
- localFile.getAbsolutePath());
- pinotFS.copyToLocalFile(fileURI, localFile);
- return localFile;
- } else {
- throw new RuntimeException(String.format("Scheme: %s not supported in
PinotFSFactory"
- + " for protocol buffer jar/descriptor file: %s.", scheme,
filePath));
+ PinotFS pinotFS = PinotFSFactory.create(scheme);
+ try (InputStream in = pinotFS.open(fileURI)) {
+ return in.readAllBytes();
Review Comment:
Done. Replaced `readDescriptorFileBytes()` with `openDescriptorFile()` that
returns an `InputStream` directly. Both callers (`ProtoBufMessageDecoder` and
`ProtoBufRecordReader`) now parse from the stream without materializing the
full byte array:
- `ProtoBufMessageDecoder`: `DynamicSchema.parseFrom(InputStream)`
- `ProtoBufRecordReader`: `FileDescriptorSet.parseFrom(InputStream)`
Both use try-with-resources so the stream is closed promptly.
##########
pinot-plugins/pinot-input-format/pinot-protobuf/src/test/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufTempFileLeakTest.java:
##########
@@ -0,0 +1,211 @@
+/**
+ * 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.pinot.plugin.inputformat.protobuf;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.testng.annotations.Test;
+
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufCodeGenMessageDecoder.PROTOBUF_JAR_FILE_PATH;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufCodeGenMessageDecoder.PROTO_CLASS_NAME;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.createComplexTypeRecord;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.getComplexTypeObject;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.getFieldsInSampleRecord;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.getSampleRecordMessage;
+import static
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.getSourceFieldsForComplexType;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+
+/// Verifies that protobuf decoder and reader operations do not leak temporary
directories.
+///
+/// Each test snapshots the set of `pinot-protobuf*` directories in the system
temp directory before the operation,
+/// then asserts that no new ones remain afterward. The functional assertions
confirm the operation itself still works
+/// correctly.
+public class ProtoBufTempFileLeakTest {
+ private static final Path TEMP_DIR =
Path.of(System.getProperty("java.io.tmpdir"));
+
+ /// [ProtoBufMessageDecoder#init] with a descriptor file should not leak a
temp directory.
+ /// This is the streaming consumer path - called once per consumer init.
+ @Test
+ public void testMessageDecoderInitDoesNotLeakTempDir()
+ throws Exception {
+ Set<Path> before = listProtobufTempDirs();
+
+ Map<String, String> decoderProps = new HashMap<>();
+ URL descriptorFile =
getClass().getClassLoader().getResource("sample.desc");
+ decoderProps.put("descriptorFile", descriptorFile.toURI().toString());
+ ProtoBufMessageDecoder decoder = new ProtoBufMessageDecoder();
+ decoder.init(decoderProps, getFieldsInSampleRecord(), "");
+
+ // Verify functional correctness - decoding still works
+ Sample.SampleRecord sampleRecord = getSampleRecordMessage();
+ GenericRow destination = new GenericRow();
+ decoder.decode(sampleRecord.toByteArray(), destination);
+ assertEquals(destination.getValue("email"), "[email protected]");
+ assertEquals(destination.getValue("name"), "Alice");
+ assertEquals(destination.getValue("id"), 18);
+
+ assertNoNewTempDirs(before);
+ }
+
+ /// [ProtoBufMessageDecoder#init] with a complex descriptor should not leak
a temp directory.
+ @Test
+ public void testMessageDecoderComplexDescriptorDoesNotLeakTempDir()
+ throws Exception {
+ Set<Path> before = listProtobufTempDirs();
+
+ Map<String, String> decoderProps = new HashMap<>();
+ URL descriptorFile =
getClass().getClassLoader().getResource("complex_types.desc");
+ decoderProps.put("descriptorFile", descriptorFile.toURI().toString());
+ ProtoBufMessageDecoder decoder = new ProtoBufMessageDecoder();
+ decoder.init(decoderProps, getSourceFieldsForComplexType(), "");
+
+ // Verify functional correctness
+ Map<String, Object> inputRecord = createComplexTypeRecord();
+ GenericRow destination = new GenericRow();
+ decoder.decode(getComplexTypeObject(inputRecord).toByteArray(),
destination);
+ assertNotNull(destination.getValue("string_field"));
+ assertEquals(destination.getValue("string_field"), "hello");
+
+ assertNoNewTempDirs(before);
+ }
+
+ /// [ProtoBufCodeGenMessageDecoder#init] with a JAR file should not leak a
temp directory.
+ /// This is the streaming consumer codegen path.
+ @Test
+ public void testCodeGenDecoderInitDoesNotLeakTempDir()
Review Comment:
Fair point. Renamed to `testCodeGenDecoderWithLocalJarDoesNotLeakTempDir`
and `testCodeGenDecoderWithLocalComplexJarDoesNotLeakTempDir`. The remote JAR
behavior is now covered by separate dedicated tests
(`testCodeGenDecoderWithRemoteJarCreatesLocalCopy`, etc.).
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]