github-advanced-security[bot] commented on code in PR #19282:
URL: https://github.com/apache/druid/pull/19282#discussion_r3055774771


##########
processing/src/test/java/org/apache/druid/segment/file/PartialSegmentFileMapperV10Test.java:
##########
@@ -0,0 +1,612 @@
+/*
+ * 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.segment.file;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.io.Files;
+import com.google.common.primitives.Ints;
+import org.apache.druid.java.util.common.FileUtils;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.TestHelper;
+import org.apache.druid.segment.data.CompressionStrategy;
+import org.apache.druid.segment.loading.SegmentRangeReader;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+
+class PartialSegmentFileMapperV10Test
+{
+  private static final ObjectMapper JSON_MAPPER = TestHelper.makeJsonMapper();
+
+  @TempDir
+  File tempDir;
+
+  @Test
+  void testMapFileDownloadsOnDemand() throws IOException
+  {
+    final File segmentFile = buildTestSegment(20, CompressionStrategy.NONE);
+    final File cacheDir = newCacheDir("demand");
+
+    final CountingRangeReader rangeReader = new 
CountingRangeReader(segmentFile.getParentFile());
+
+    try (PartialSegmentFileMapperV10 mapper = createMapper(rangeReader, 
cacheDir)) {
+      // reset count after create fetched metadata
+      rangeReader.resetCount();
+
+      // access a single file - should trigger exactly one download
+      ByteBuffer buf = mapper.mapFile("5");
+      Assertions.assertNotNull(buf);
+      Assertions.assertEquals(0, buf.position());
+      Assertions.assertEquals(4, buf.remaining());
+      Assertions.assertEquals(5, buf.getInt());
+      Assertions.assertEquals(1, rangeReader.getReadCount());
+      Assertions.assertEquals(4, mapper.getDownloadedBytes());
+
+      // access the same file again - should NOT trigger another download
+      ByteBuffer buf2 = mapper.mapFile("5");
+      Assertions.assertNotNull(buf2);
+      Assertions.assertEquals(5, buf2.getInt());
+      Assertions.assertEquals(1, rangeReader.getReadCount());
+    }
+  }
+
+  @Test
+  void testMapFileCompressedMetadata() throws IOException
+  {
+    final File segmentFile = buildTestSegment(20, CompressionStrategy.ZSTD);
+    final File cacheDir = newCacheDir("compressed");
+
+    final CountingRangeReader rangeReader = new 
CountingRangeReader(segmentFile.getParentFile());
+
+    try (PartialSegmentFileMapperV10 mapper = createMapper(rangeReader, 
cacheDir)) {
+      for (int i = 0; i < 20; ++i) {
+        ByteBuffer buf = mapper.mapFile(String.valueOf(i));
+        Assertions.assertNotNull(buf);
+        Assertions.assertEquals(i, buf.getInt());
+      }
+    }
+  }
+
+  @Test
+  void testMapFileReturnsNullForMissingFile() throws IOException
+  {
+    final File segmentFile = buildTestSegment(5, CompressionStrategy.NONE);
+    final File cacheDir = newCacheDir("missing");
+
+    final DirectoryBackedRangeReader rangeReader = new 
DirectoryBackedRangeReader(segmentFile.getParentFile());
+
+    try (PartialSegmentFileMapperV10 mapper = createMapper(rangeReader, 
cacheDir)) {
+      Assertions.assertNull(mapper.mapFile("nonexistent"));
+    }
+  }
+
+  @Test
+  void testGetInternalFilenames() throws IOException
+  {
+    final File segmentFile = buildTestSegment(10, CompressionStrategy.NONE);
+    final File cacheDir = newCacheDir("filenames");
+
+    final DirectoryBackedRangeReader rangeReader = new 
DirectoryBackedRangeReader(segmentFile.getParentFile());
+
+    try (PartialSegmentFileMapperV10 mapper = createMapper(rangeReader, 
cacheDir)) {
+      Set<String> expected = new HashSet<>();
+      for (int i = 0; i < 10; ++i) {
+        expected.add(String.valueOf(i));
+      }
+      Assertions.assertEquals(expected, mapper.getInternalFilenames());
+    }
+  }
+
+  @Test
+  void testEnsureFilesAvailable() throws IOException
+  {
+    final File segmentFile = buildTestSegment(10, CompressionStrategy.NONE);
+    final File cacheDir = newCacheDir("ensure");
+
+    final CountingRangeReader rangeReader = new 
CountingRangeReader(segmentFile.getParentFile());
+
+    try (PartialSegmentFileMapperV10 mapper = createMapper(rangeReader, 
cacheDir)) {
+      rangeReader.resetCount();
+
+      Set<String> filesToLoad = Set.of("2", "5", "7");
+      mapper.ensureFilesAvailable(filesToLoad);
+
+      // should have downloaded exactly 3 files
+      Assertions.assertEquals(3, rangeReader.getReadCount());
+      Assertions.assertEquals(12, mapper.getDownloadedBytes());
+
+      // accessing these files should not trigger additional downloads
+      for (String name : filesToLoad) {
+        ByteBuffer buf = mapper.mapFile(name);
+        Assertions.assertNotNull(buf);
+        Assertions.assertEquals(Integer.parseInt(name), buf.getInt());

Review Comment:
   ## CodeQL / Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/10959)



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

Reply via email to