You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ja...@apache.org on 2023/12/14 19:49:25 UTC

(pinot) branch master updated: Improve resource management with try-with-resource blocks. Fix for #12129 (#12130)

This is an automated email from the ASF dual-hosted git repository.

jackie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new f22de0a4be Improve resource management with try-with-resource blocks.  Fix for #12129 (#12130)
f22de0a4be is described below

commit f22de0a4be910af09556666df3aa44ae78d59187
Author: Tim Veil <32...@users.noreply.github.com>
AuthorDate: Thu Dec 14 14:49:18 2023 -0500

    Improve resource management with try-with-resource blocks.  Fix for #12129 (#12130)
    
    This commit modifies several classes to use a try-with-resource block for file operations, conforming with best practices for Java resource management. Previously, file resources such as RandomAccessFile and FileOutputStream were not included in a try-with-resource statement, which could lead to resource leaks if exceptions occurred. The changes ensure these resources are properly closed after usage.
---
 .../creator/impl/inv/RangeIndexCreator.java        |  5 ++-
 .../pinot/segment/spi/memory/PinotByteBuffer.java  |  6 ++-
 .../java/org/apache/pinot/tools/SpeedTest.java     | 49 ++++++++++++----------
 3 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/RangeIndexCreator.java b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/RangeIndexCreator.java
index 827fe8deca..423800309f 100644
--- a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/RangeIndexCreator.java
+++ b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/creator/impl/inv/RangeIndexCreator.java
@@ -30,6 +30,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.commons.io.FileUtils;
@@ -321,6 +322,7 @@ public final class RangeIndexCreator implements CombinedInvertedIndexCreator {
     try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(_rangeIndexFile));
         DataOutputStream header = new DataOutputStream(bos);
         FileOutputStream fos = new FileOutputStream(_rangeIndexFile);
+        FileChannel channel = fos.getChannel();
         DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(fos))) {
 
       //VERSION
@@ -358,7 +360,8 @@ public final class RangeIndexCreator implements CombinedInvertedIndexCreator {
       long bitmapOffset = bytesWritten + bitmapOffsetHeaderSize;
       header.writeLong(bitmapOffset);
       bytesWritten += Long.BYTES;
-      fos.getChannel().position(bitmapOffset);
+
+      channel.position(bitmapOffset);
 
       for (int i = 0; i < ranges.size(); i++) {
         Pair<Integer, Integer> range = ranges.get(i);
diff --git a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PinotByteBuffer.java b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PinotByteBuffer.java
index 0905bc0d03..1cb357ea44 100644
--- a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PinotByteBuffer.java
+++ b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PinotByteBuffer.java
@@ -276,12 +276,14 @@ public class PinotByteBuffer extends PinotDataBuffer {
       throws IOException {
     assert offset <= Integer.MAX_VALUE;
     assert size <= Integer.MAX_VALUE;
-    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
+    try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
+        FileChannel channel = randomAccessFile.getChannel()) {
+
       ByteBuffer duplicate = _buffer.duplicate();
       int start = (int) offset;
       int end = start + (int) size;
       ((Buffer) duplicate).position(start).limit(end);
-      randomAccessFile.getChannel().read(duplicate, srcOffset);
+      channel.read(duplicate, srcOffset);
     }
   }
 
diff --git a/pinot-tools/src/main/java/org/apache/pinot/tools/SpeedTest.java b/pinot-tools/src/main/java/org/apache/pinot/tools/SpeedTest.java
index 432cdeaa94..d1ed6efb74 100644
--- a/pinot-tools/src/main/java/org/apache/pinot/tools/SpeedTest.java
+++ b/pinot-tools/src/main/java/org/apache/pinot/tools/SpeedTest.java
@@ -23,6 +23,7 @@ import java.io.FileOutputStream;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
 import java.nio.channels.FileChannel.MapMode;
 
 
@@ -44,31 +45,33 @@ public class SpeedTest {
   private void init()
       throws Exception {
     // write a temp file
-    FileOutputStream fout = new FileOutputStream(FILE_NAME);
-    DataOutputStream out = new DataOutputStream(fout);
-    _heapStorage = new int[SIZE];
-    _directMemory = ByteBuffer.allocateDirect(SIZE * 4);
-    for (int i = 0; i < SIZE; i++) {
-      int data = (int) (Math.random() * Integer.MAX_VALUE);
-      out.writeInt(data);
-      _heapStorage[i] = data;
-      _directMemory.putInt(data);
+    try (FileOutputStream fout = new FileOutputStream(FILE_NAME);
+          DataOutputStream out = new DataOutputStream(fout)) {
+      _heapStorage = new int[SIZE];
+      _directMemory = ByteBuffer.allocateDirect(SIZE * 4);
+      for (int i = 0; i < SIZE; i++) {
+        int data = (int) (Math.random() * Integer.MAX_VALUE);
+        out.writeInt(data);
+        _heapStorage[i] = data;
+        _directMemory.putInt(data);
+      }
     }
-    out.close();
-    RandomAccessFile raf = new RandomAccessFile(FILE_NAME, "rw");
-    _mmappedByteBuffer = raf.getChannel().map(MapMode.READ_WRITE, 0L, raf.length());
-    _mmappedByteBuffer.load();
+    try (RandomAccessFile raf = new RandomAccessFile(FILE_NAME, "rw");
+          FileChannel channel = raf.getChannel()) {
+      _mmappedByteBuffer = channel.map(MapMode.READ_WRITE, 0L, raf.length());
+      _mmappedByteBuffer.load();
 
-    int toSelect = (int) (SIZE * READ_PERCENTAGE);
-    _readIndices = new int[toSelect];
-    int ind = 0;
-    for (int i = 0; i < SIZE && toSelect > 0; i++) {
-      double probOfSelection = toSelect * 1.0 / (SIZE - i);
-      double random = Math.random();
-      if (random < probOfSelection) {
-        _readIndices[ind] = i;
-        toSelect = toSelect - 1;
-        ind = ind + 1;
+      int toSelect = (int) (SIZE * READ_PERCENTAGE);
+      _readIndices = new int[toSelect];
+      int ind = 0;
+      for (int i = 0; i < SIZE && toSelect > 0; i++) {
+        double probOfSelection = toSelect * 1.0 / (SIZE - i);
+        double random = Math.random();
+        if (random < probOfSelection) {
+          _readIndices[ind] = i;
+          toSelect = toSelect - 1;
+          ind = ind + 1;
+        }
       }
     }
   }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org