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


##########
server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java:
##########
@@ -118,32 +128,158 @@
   SegmentLocalCacheManager(
       SegmentLoaderConfig config,
       @Nonnull StorageLocationSelectorStrategy strategy,
+      IndexIO indexIO,
       @Json ObjectMapper mapper
   )
   {
-    this(config.toStorageLocations(), config, strategy, mapper);
+    this(config.toStorageLocations(), config, strategy, indexIO, mapper);
   }
 
   /**
    * creates instance with default storage location selector strategy
    *
    * This ctor is mainly for test cases, including test cases in other modules
    */
-  @VisibleForTesting
   public SegmentLocalCacheManager(
       SegmentLoaderConfig config,
+      IndexIO indexIO,
       @Json ObjectMapper mapper
   )
   {
     this.config = config;
+    this.indexIO = indexIO;
     this.jsonMapper = mapper;
     this.locations = config.toStorageLocations();
     this.strategy = new 
LeastBytesUsedStorageLocationSelectorStrategy(locations);
     log.info("Using storage location strategy: [%s]", 
this.strategy.getClass().getSimpleName());
   }
 
 
-  static String getSegmentDir(DataSegment segment)
+  @Override
+  public boolean canHandleSegments()
+  {
+    return !(locations == null || locations.isEmpty());
+  }
+
+  @Override
+  public List<DataSegment> getCachedSegments() throws IOException
+  {
+    if (!canHandleSegments()) {
+      throw DruidException.defensive(
+          "canHandleSegments() is false. getCachedSegments() must be invoked 
only when canHandleSegments() returns true."
+      );
+    }
+    final File baseDir = getInfoDir();
+
+    List<DataSegment> cachedSegments = new ArrayList<>();
+    File[] segmentsToLoad = baseDir.listFiles();
+
+    int ignored = 0;
+
+    for (int i = 0; i < segmentsToLoad.length; i++) {
+      File file = segmentsToLoad[i];
+      log.info("Loading segment cache file [%d/%d][%s].", i + 1, 
segmentsToLoad.length, file);
+      try {
+        final DataSegment segment = jsonMapper.readValue(file, 
DataSegment.class);
+
+        if (!segment.getId().toString().equals(file.getName())) {
+          log.warn("Ignoring cache file[%s] for segment[%s].", file.getPath(), 
segment.getId());
+          ignored++;
+        } else if (isSegmentCached(segment)) {
+          cachedSegments.add(segment);
+        } else {
+          final SegmentId segmentId = segment.getId();
+          log.warn("Unable to find cache file for segment[%s]. Deleting lookup 
entry.", segmentId);
+          if (!removeInfoFile(segment)) {
+            log.warn(
+                "Unable to delete cache file[%s] for segment[%s].",
+                getInfoFileName(segment), segmentId);
+          }
+        }
+      }
+      catch (Exception e) {
+        log.makeAlert(e, "Failed to load segment from segmentInfo file")
+           .addData("file", file)
+           .emit();
+      }
+    }
+
+    if (ignored > 0) {
+      log.makeAlert("Ignored misnamed segment cache files on startup.")
+         .addData("numIgnored", ignored)
+         .emit();
+    }
+
+    return cachedSegments;
+  }
+
+  @Override
+  public void storeInfoFile(DataSegment segment) throws IOException
+  {
+    final File segmentInfoCacheFile = new File(getInfoDir(), 
segment.getId().toString());
+    if (!segmentInfoCacheFile.exists()) {
+      jsonMapper.writeValue(segmentInfoCacheFile, segment);

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/7380)



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