You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2021/06/07 19:22:35 UTC

[datasketches-memory] 04/04: More Exceptions cleanup.

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

leerho pushed a commit to branch AddExceptions
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git

commit 8eb133eff4a715f26ac2761278ca80842437f603
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Mon Jun 7 12:21:03 2021 -0700

    More Exceptions cleanup.
---
 .../datasketches/memory/internal/NioBits.java      | 39 ++++++++++------------
 .../datasketches/memory/internal/UnsafeUtil.java   |  6 ++--
 .../test/AllocateDirectWritableMapMemoryTest.java  |  5 ++-
 .../datasketches/memory/test/MemoryTest.java       | 16 +++------
 .../datasketches/memory/test/UnsafeUtilTest.java   | 10 +++---
 tools/MemoryCheckstyle.xml                         |  2 +-
 6 files changed, 34 insertions(+), 44 deletions(-)

diff --git a/src/main/java/org/apache/datasketches/memory/internal/NioBits.java b/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
index 3b79c58..73d95cc 100644
--- a/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
+++ b/src/main/java/org/apache/datasketches/memory/internal/NioBits.java
@@ -129,37 +129,32 @@ final class NioBits {
   // -XX:MaxDirectMemorySize limits the total capacity rather than the
   // actual memory usage, which will differ when buffers are page aligned.
   static void reserveMemory(final long allocationSize, final long capacity) {
-    try {
-      reserveUnreserve(allocationSize, capacity, NIO_BITS_RESERVE_MEMORY_METHOD);
-    } catch (final Exception e) {
-      throw new RuntimeException("Could not invoke java.nio.Bits.reserveMemory(...): "
-          + "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
-    }
+    reserveUnreserve(allocationSize, capacity, NIO_BITS_RESERVE_MEMORY_METHOD);
   }
 
   static void unreserveMemory(final long allocationSize, final long capacity) {
-    try {
-      reserveUnreserve(allocationSize, capacity, NIO_BITS_UNRESERVE_MEMORY_METHOD);
-    } catch (final Exception e) {
-      throw new RuntimeException("Could not invoke java.nio.Bits.unreserveMemory(...): "
-          + "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
-    }
+    reserveUnreserve(allocationSize, capacity, NIO_BITS_UNRESERVE_MEMORY_METHOD);
   }
 
-  private static void reserveUnreserve(long allocationSize, long capacity, final Method method)
-      throws Exception {
+  private static void reserveUnreserve(long allocationSize, long capacity, final Method method) {
     Util.zeroCheck(capacity, "capacity");
     // 1GB is a pretty "safe" limit.
     final long chunkSizeLimit = 1L << 30;
-    while (capacity > 0) {
-      final long chunk = Math.min(capacity, chunkSizeLimit);
-      if (capacity == chunk) {
-        method.invoke(null, allocationSize, (int) capacity); //JDK 16 remove cast to int
-      } else {
-        method.invoke(null, chunk, (int) chunk); //JDK 16 remove cast to int
+    try {
+      while (capacity > 0) {
+        final long chunk = Math.min(capacity, chunkSizeLimit);
+        if (capacity == chunk) {
+          method.invoke(null, allocationSize, (int) capacity); //JDK 16 remove cast to int
+        } else {
+          method.invoke(null, chunk, (int) chunk); //JDK 16 remove cast to int
+        }
+        capacity -= chunk;
+        allocationSize -= chunk;
       }
-      capacity -= chunk;
-      allocationSize -= chunk;
+    } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+      throw new RuntimeException(
+          "Could not invoke java.nio.Bits.unreserveMemory(...) OR java.nio.Bits.reserveMemory(...): "
+          + "allocationSize = " + allocationSize + ", capacity = " + capacity, e);
     }
   }
 }
diff --git a/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java b/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java
index eac8c11..435d5fb 100644
--- a/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java
+++ b/src/main/java/org/apache/datasketches/memory/internal/UnsafeUtil.java
@@ -136,10 +136,10 @@ public final class UnsafeUtil {
       parts = parts[0].split("\\."); //split out the number groups
       p0 = Integer.parseInt(parts[0]); //the first number group
       p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0; //2nd number group, or 0
-    } catch (final NumberFormatException e) {
-      throw new RuntimeException("Improper Java -version string: " + jdkVer + "\n" + e);
+    } catch (final NumberFormatException | ArrayIndexOutOfBoundsException  e) {
+      throw new IllegalArgumentException("Improper Java -version string: " + jdkVer + "\n" + e);
     }
-    //checkJavaVersion(jdkVer, p0, p1);
+    //checkJavaVersion(jdkVer, p0, p1); //TODO Optional to omit this.
     return new int[] {p0, p1};
   }
 
diff --git a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
index 23c915c..40c4ef3 100644
--- a/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
@@ -34,6 +34,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.ByteOrder;
 
@@ -63,7 +64,9 @@ public class AllocateDirectWritableMapMemoryTest {
   private static boolean isFileReadOnly(final File file) {
     try {
       return (boolean) IS_FILE_READ_ONLY.invoke(null, file);
-    } catch (Exception e) { throw new RuntimeException(e); }
+    } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 
+      throw new RuntimeException(e); 
+    }
   }
   
   @BeforeClass
diff --git a/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java b/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
index d9f76d3..2711123 100644
--- a/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
@@ -322,33 +322,25 @@ public class MemoryTest {
   }
 
   @Test(expectedExceptions = AssertionError.class)
-  public void checkParentUseAfterFree() {
+  public void checkParentUseAfterFree() throws Exception {
     int bytes = 64 * 8;
     @SuppressWarnings("resource") //intentionally not using try-with-resouces here
     WritableHandle wh = WritableMemory.allocateDirect(bytes);
     WritableMemory wmem = wh.getWritable();
-    try {
-      wh.close();
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
+    wh.close();
     //with -ea assert: Memory not valid.
     //with -da sometimes segfaults, sometimes passes!
     wmem.getLong(0);
   }
 
   @Test(expectedExceptions = AssertionError.class)
-  public void checkRegionUseAfterFree() {
+  public void checkRegionUseAfterFree() throws Exception {
     int bytes = 64;
     @SuppressWarnings("resource") //intentionally not using try-with-resouces here
     WritableHandle wh = WritableMemory.allocateDirect(bytes);
     Memory wmem = wh.get();
     Memory region = wmem.region(0L, bytes);
-    try {
-      wh.close();
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
+    wh.close();
     //with -ea assert: Memory not valid.
     //with -da sometimes segfaults, sometimes passes!
     region.getByte(0);
diff --git a/src/test/java/org/apache/datasketches/memory/test/UnsafeUtilTest.java b/src/test/java/org/apache/datasketches/memory/test/UnsafeUtilTest.java
index aa99412..4070321 100644
--- a/src/test/java/org/apache/datasketches/memory/test/UnsafeUtilTest.java
+++ b/src/test/java/org/apache/datasketches/memory/test/UnsafeUtilTest.java
@@ -58,7 +58,7 @@ public class UnsafeUtilTest {
       p = UnsafeUtil.parseJavaVersion(jdkVer);
       UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
       fail();
-    } catch (Exception e) {
+    } catch (IllegalArgumentException e) {
       println("" + e);
     }
 
@@ -67,7 +67,7 @@ public class UnsafeUtilTest {
       p = UnsafeUtil.parseJavaVersion(jdkVer);
       UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
       fail();
-    } catch (Exception e) {
+    } catch (IllegalArgumentException e) {
       println("" + e);
     }
     try {
@@ -75,7 +75,7 @@ public class UnsafeUtilTest {
       p = UnsafeUtil.parseJavaVersion(jdkVer);
       UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
       fail();
-    } catch (Exception e) {
+    } catch (IllegalArgumentException e) {
       println("" + e);
     }
     try {
@@ -83,7 +83,7 @@ public class UnsafeUtilTest {
       p = UnsafeUtil.parseJavaVersion(jdkVer);
       UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
       fail();
-    } catch (Exception e) {
+    } catch (IllegalArgumentException e) {
       println("" + e);
     }
     try {
@@ -91,7 +91,7 @@ public class UnsafeUtilTest {
       p = UnsafeUtil.parseJavaVersion(jdkVer);
       UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
       fail();
-    } catch (Exception e) {
+    } catch (IllegalArgumentException e) {
       println("" + e);
     }
   }
diff --git a/tools/MemoryCheckstyle.xml b/tools/MemoryCheckstyle.xml
index 7dbd33f..29738c0 100644
--- a/tools/MemoryCheckstyle.xml
+++ b/tools/MemoryCheckstyle.xml
@@ -212,7 +212,7 @@ under the License.
     <module name="JavadocTagContinuationIndentation">
       <property name="severity" value="ignore"/>
       <property name="offset" value="0"/>
-      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+      <!-- <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/> -->
     </module>
         
     <module name="NonEmptyAtclauseDescription"/>

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