Jackie-Jiang commented on code in PR #19378: URL: https://github.com/apache/pinot/pull/19378#discussion_r3890265348
########## 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: Minor: this name implies that the general codegen path creates no temp directory, but the test supplies a local `file:` JAR while remote JARs deliberately take a different path. Please scope the name to local-JAR behavior (for example, `testCodeGenDecoderWithLocalJarDoesNotCreateTempDir`) and document/test the remote lifetime separately. ########## pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoder.java: ########## @@ -104,4 +123,29 @@ public static Descriptors.Descriptor getDescriptorForProtoClass(ClassLoader prot Class<? extends Message> updateMessage = (Class<Message>) protoMessageClsLoader.loadClass(protoClassName); return (Descriptors.Descriptor) updateMessage.getMethod("getDescriptor").invoke(null); } + + /// Resolves a file path (URI string) to a local [File]. For local files (no scheme or `file://` scheme), + /// the original file is returned directly. For remote files, the file is copied to a local temporary + /// directory. The caller is responsible for the lifetime of the returned file - for remote files, the + /// backing temp directory is intentionally NOT cleaned up because the JAR must remain accessible for + /// lazy class loading at decode time. + private static File resolveToLocalFile(String filePath) + throws Exception { + URI fileURI = URI.create(filePath); + String scheme = fileURI.getScheme(); + if (scheme == null || PinotFSFactory.LOCAL_PINOT_FS_SCHEME.equals(scheme)) { + return new File(fileURI.getPath()); + } + PinotFS pinotFS = PinotFSFactory.create(scheme); + Path localTmpDir = Files.createTempDirectory(ProtoBufUtils.TMP_DIR_PREFIX); + try { + File localFile = new File(localTmpDir.toFile(), new File(fileURI.getPath()).getName()); + LOGGER.info("Copying protocol buffer JAR from {} to {}", filePath, localFile.getAbsolutePath()); + pinotFS.copyToLocalFile(fileURI, localFile); + return localFile; Review Comment: Remote JARs are copied into a fresh temp directory for every decoder, but the returned `File` loses ownership of that directory. Neither successful segment offload nor failures later in `init()` close the classloader and delete it, so segment rollovers and initialization retries can accumulate directories and inodes for the server lifetime—the same leak class this PR is intended to fix. Please retain an owned localized-JAR/classloader resource and release it on initialization failure and segment offload, or cache/share localized JARs by URI and version. ########## 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: Minor: `readAllBytes()` creates an unbounded descriptor-sized allocation for each decoder/reader initialization, which can be amplified when many partitions initialize concurrently. Since protobuf supports parsing from an `InputStream`, consider returning/opening a closeable stream and parsing it directly instead of materializing the full byte array. ########## 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() + throws Exception { + Set<Path> before = listProtobufTempDirs(); + + Map<String, String> decoderProps = new HashMap<>(); + URL jarFile = getClass().getClassLoader().getResource("sample.jar"); Review Comment: All five new tests obtain classpath resources and therefore exercise only `file:` URIs. They do not cover a non-file `PinotFS`, the remote-JAR copy branch, or partial-copy failure cleanup, so a regression in the production object-store path can pass this suite. Please register a fake PinotFS scheme and cover remote descriptor reads plus successful and partially failed remote JAR copies, asserting stream closure and no unintended residual temp directory. -- 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]
