You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by ct...@apache.org on 2021/01/05 07:14:32 UTC

[accumulo] branch main updated: Remove unneeded resource warning suppressions (#1852)

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

ctubbsii pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new ede736f  Remove unneeded resource warning suppressions (#1852)
ede736f is described below

commit ede736f214dbab5ca272240b41019bb12ea3725c
Author: Christopher Tubbs <ct...@apache.org>
AuthorDate: Tue Jan 5 02:14:19 2021 -0500

    Remove unneeded resource warning suppressions (#1852)
    
    Work around the need to suppress unclosed resource warnings and remove
    the unneeded suppressions.
    
    For tests:
    * Use assertThrows and try-with-resources in tests to handle resource
      creation and cleanup, to avoid warnings about unclosed resources
    
    For ListTabletsCommand.java:
    * Use try-with-resources to ensure TabletsMetadata resource object is
      closed when done using it
    
    For SimpleGarbageCollector:
    * Rename GarbageCollectionEnvironment.getBlipIterator to .getBlipPaths
      and make it return a Stream (which is Closeable) instead of an
      Iterator (which isn't Closeable)
    * Set the onClose for the Stream returned from .getBlipPaths to close
      the scanner whose iterator it is wrapping
    * Ensure the Stream returned from .getBlipPaths is closed in a
      try-with-resources block
    * Remove use of Guava's Iterables (not needed, since we can use
      Stream.map directly now)
---
 .../accumulo/core/clientImpl/ScannerImplTest.java  | 34 +++++++++---------
 .../clientImpl/TabletServerBatchReaderTest.java    |  7 ++--
 .../accumulo/gc/GarbageCollectionAlgorithm.java    | 40 ++++++++++++----------
 .../accumulo/gc/GarbageCollectionEnvironment.java  |  2 +-
 .../apache/accumulo/gc/SimpleGarbageCollector.java | 16 ++++-----
 .../src/main/spotbugs/exclude-filter.xml           |  5 ---
 .../apache/accumulo/gc/GarbageCollectionTest.java  |  4 +--
 .../manager/src/main/spotbugs/exclude-filter.xml   |  5 ---
 .../shell/commands/ListTabletsCommand.java         | 34 +++++++++---------
 .../src/main/spotbugs/exclude-filter.xml           |  5 ---
 10 files changed, 71 insertions(+), 81 deletions(-)

diff --git a/core/src/test/java/org/apache/accumulo/core/clientImpl/ScannerImplTest.java b/core/src/test/java/org/apache/accumulo/core/clientImpl/ScannerImplTest.java
index e5b9c2c..1e21474 100644
--- a/core/src/test/java/org/apache/accumulo/core/clientImpl/ScannerImplTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/clientImpl/ScannerImplTest.java
@@ -19,8 +19,8 @@
 package org.apache.accumulo.core.clientImpl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
-import org.apache.accumulo.core.client.Scanner;
 import org.apache.accumulo.core.data.TableId;
 import org.apache.accumulo.core.security.Authorizations;
 import org.easymock.EasyMock;
@@ -38,34 +38,34 @@ public class ScannerImplTest {
 
   @Test
   public void testValidReadaheadValues() {
-    Scanner s = new ScannerImpl(context, TableId.of("foo"), Authorizations.EMPTY);
-    s.setReadaheadThreshold(0);
-    s.setReadaheadThreshold(10);
-    s.setReadaheadThreshold(Long.MAX_VALUE);
+    try (var s = new ScannerImpl(context, TableId.of("foo"), Authorizations.EMPTY)) {
+      s.setReadaheadThreshold(0);
+      s.setReadaheadThreshold(10);
+      s.setReadaheadThreshold(Long.MAX_VALUE);
 
-    assertEquals(Long.MAX_VALUE, s.getReadaheadThreshold());
-    s.close();
+      assertEquals(Long.MAX_VALUE, s.getReadaheadThreshold());
+    }
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test
   public void testInValidReadaheadValues() {
-    Scanner s = new ScannerImpl(context, TableId.of("foo"), Authorizations.EMPTY);
-    s.setReadaheadThreshold(-1);
-    s.close();
+    try (var s = new ScannerImpl(context, TableId.of("foo"), Authorizations.EMPTY)) {
+      assertThrows(IllegalArgumentException.class, () -> s.setReadaheadThreshold(-1));
+    }
   }
 
   @Test
   public void testGetAuthorizations() {
     Authorizations expected = new Authorizations("a,b");
-    Scanner s = new ScannerImpl(context, TableId.of("foo"), expected);
-    assertEquals(expected, s.getAuthorizations());
-    s.close();
+    try (var s = new ScannerImpl(context, TableId.of("foo"), expected)) {
+      assertEquals(expected, s.getAuthorizations());
+    }
   }
 
-  @SuppressWarnings("resource")
-  @Test(expected = IllegalArgumentException.class)
+  @Test
   public void testNullAuthorizationsFails() {
-    new ScannerImpl(context, TableId.of("foo"), null);
+    assertThrows(IllegalArgumentException.class,
+        () -> new ScannerImpl(context, TableId.of("foo"), null));
   }
 
 }
diff --git a/core/src/test/java/org/apache/accumulo/core/clientImpl/TabletServerBatchReaderTest.java b/core/src/test/java/org/apache/accumulo/core/clientImpl/TabletServerBatchReaderTest.java
index 7a78b29..33e3405 100644
--- a/core/src/test/java/org/apache/accumulo/core/clientImpl/TabletServerBatchReaderTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/clientImpl/TabletServerBatchReaderTest.java
@@ -19,6 +19,7 @@
 package org.apache.accumulo.core.clientImpl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 
 import org.apache.accumulo.core.client.BatchScanner;
 import org.apache.accumulo.core.data.TableId;
@@ -44,9 +45,9 @@ public class TabletServerBatchReaderTest {
     }
   }
 
-  @SuppressWarnings("resource")
-  @Test(expected = IllegalArgumentException.class)
+  @Test
   public void testNullAuthorizationsFails() {
-    new TabletServerBatchReader(context, TableId.of("foo"), null, 1);
+    assertThrows(IllegalArgumentException.class,
+        () -> new TabletServerBatchReader(context, TableId.of("foo"), null, 1));
   }
 }
diff --git a/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionAlgorithm.java b/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionAlgorithm.java
index cdf1186..595af24 100644
--- a/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionAlgorithm.java
+++ b/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionAlgorithm.java
@@ -29,6 +29,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.stream.Stream;
 
 import org.apache.accumulo.core.Constants;
 import org.apache.accumulo.core.client.TableNotFoundException;
@@ -135,33 +136,34 @@ public class GarbageCollectionAlgorithm {
           relativePaths.next().toLowerCase(Locale.ENGLISH).contains(Constants.BULK_PREFIX);
 
     if (checkForBulkProcessingFiles) {
-      Iterator<String> blipiter = gce.getBlipIterator();
+      try (Stream<String> blipStream = gce.getBlipPaths()) {
+        Iterator<String> blipiter = blipStream.iterator();
 
-      // WARNING: This block is IMPORTANT
-      // You MUST REMOVE candidates that are in the same folder as a bulk
-      // processing flag!
+        // WARNING: This block is IMPORTANT
+        // You MUST REMOVE candidates that are in the same folder as a bulk
+        // processing flag!
 
-      while (blipiter.hasNext()) {
-        String blipPath = blipiter.next();
-        blipPath = makeRelative(blipPath, 2);
+        while (blipiter.hasNext()) {
+          String blipPath = blipiter.next();
+          blipPath = makeRelative(blipPath, 2);
 
-        Iterator<String> tailIter = candidateMap.tailMap(blipPath).keySet().iterator();
+          Iterator<String> tailIter = candidateMap.tailMap(blipPath).keySet().iterator();
 
-        int count = 0;
+          int count = 0;
 
-        while (tailIter.hasNext()) {
-          if (tailIter.next().startsWith(blipPath)) {
-            count++;
-            tailIter.remove();
-          } else {
-            break;
+          while (tailIter.hasNext()) {
+            if (tailIter.next().startsWith(blipPath)) {
+              count++;
+              tailIter.remove();
+            } else {
+              break;
+            }
           }
-        }
 
-        if (count > 0)
-          log.debug("Folder has bulk processing flag: {}", blipPath);
+          if (count > 0)
+            log.debug("Folder has bulk processing flag: {}", blipPath);
+        }
       }
-
     }
 
     Iterator<Reference> iter = gce.getReferences().iterator();
diff --git a/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionEnvironment.java b/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionEnvironment.java
index 941344d..ae8ffbb 100644
--- a/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionEnvironment.java
+++ b/server/gc/src/main/java/org/apache/accumulo/gc/GarbageCollectionEnvironment.java
@@ -60,7 +60,7 @@ public interface GarbageCollectionEnvironment {
    *
    * @return The list of files for each bulk load currently in progress.
    */
-  Iterator<String> getBlipIterator() throws TableNotFoundException;
+  Stream<String> getBlipPaths() throws TableNotFoundException;
 
   static class Reference {
     public final TableId id;
diff --git a/server/gc/src/main/java/org/apache/accumulo/gc/SimpleGarbageCollector.java b/server/gc/src/main/java/org/apache/accumulo/gc/SimpleGarbageCollector.java
index 4776583..b5a165f 100644
--- a/server/gc/src/main/java/org/apache/accumulo/gc/SimpleGarbageCollector.java
+++ b/server/gc/src/main/java/org/apache/accumulo/gc/SimpleGarbageCollector.java
@@ -38,6 +38,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 
 import org.apache.accumulo.core.Constants;
 import org.apache.accumulo.core.client.AccumuloClient;
@@ -235,20 +236,19 @@ public class SimpleGarbageCollector extends AbstractServer implements Iface {
     }
 
     @Override
-    public Iterator<String> getBlipIterator() throws TableNotFoundException {
+    public Stream<String> getBlipPaths() throws TableNotFoundException {
 
       if (level == DataLevel.ROOT) {
-        return Collections.<String>emptySet().iterator();
+        return Stream.empty();
       }
 
-      @SuppressWarnings("resource")
-      IsolatedScanner scanner =
+      int blipPrefixLen = BlipSection.getRowPrefix().length();
+      var scanner =
           new IsolatedScanner(getContext().createScanner(level.metaTable(), Authorizations.EMPTY));
-
       scanner.setRange(BlipSection.getRange());
-
-      return Iterators.transform(scanner.iterator(), entry -> entry.getKey().getRow().toString()
-          .substring(BlipSection.getRowPrefix().length()));
+      return StreamSupport.stream(scanner.spliterator(), false)
+          .map(entry -> entry.getKey().getRow().toString().substring(blipPrefixLen))
+          .onClose(scanner::close);
     }
 
     @Override
diff --git a/server/manager/src/main/spotbugs/exclude-filter.xml b/server/gc/src/main/spotbugs/exclude-filter.xml
similarity index 84%
copy from server/manager/src/main/spotbugs/exclude-filter.xml
copy to server/gc/src/main/spotbugs/exclude-filter.xml
index 0a31f85..95744f7 100644
--- a/server/manager/src/main/spotbugs/exclude-filter.xml
+++ b/server/gc/src/main/spotbugs/exclude-filter.xml
@@ -28,9 +28,4 @@
     <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
     <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
   </Match>
-  <Match>
-    <!-- Must ignore these everywhere, because of a javac byte code generation bug -->
-    <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
-    <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
-  </Match>
 </FindBugsFilter>
diff --git a/server/gc/src/test/java/org/apache/accumulo/gc/GarbageCollectionTest.java b/server/gc/src/test/java/org/apache/accumulo/gc/GarbageCollectionTest.java
index 92c8fb9..cd557ae 100644
--- a/server/gc/src/test/java/org/apache/accumulo/gc/GarbageCollectionTest.java
+++ b/server/gc/src/test/java/org/apache/accumulo/gc/GarbageCollectionTest.java
@@ -61,8 +61,8 @@ public class GarbageCollectionTest {
     }
 
     @Override
-    public Iterator<String> getBlipIterator() {
-      return blips.iterator();
+    public Stream<String> getBlipPaths() {
+      return blips.stream();
     }
 
     @Override
diff --git a/server/manager/src/main/spotbugs/exclude-filter.xml b/server/manager/src/main/spotbugs/exclude-filter.xml
index 0a31f85..95744f7 100644
--- a/server/manager/src/main/spotbugs/exclude-filter.xml
+++ b/server/manager/src/main/spotbugs/exclude-filter.xml
@@ -28,9 +28,4 @@
     <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
     <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
   </Match>
-  <Match>
-    <!-- Must ignore these everywhere, because of a javac byte code generation bug -->
-    <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
-    <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
-  </Match>
 </FindBugsFilter>
diff --git a/shell/src/main/java/org/apache/accumulo/shell/commands/ListTabletsCommand.java b/shell/src/main/java/org/apache/accumulo/shell/commands/ListTabletsCommand.java
index 5226e8e..73facfe 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/commands/ListTabletsCommand.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/commands/ListTabletsCommand.java
@@ -234,23 +234,25 @@ public class ListTabletsCommand extends Command {
     final ClientContext context = shellState.getContext();
     Set<TServerInstance> liveTserverSet = TabletMetadata.getLiveTServers(context);
 
-    for (var md : TabletsMetadata.builder().forTable(tableInfo.id).build(context)) {
-      TabletRowInfo.Factory factory = new TabletRowInfo.Factory(tableInfo.name, md.getExtent());
-      var fileMap = md.getFilesMap();
-      factory.numFiles(fileMap.size());
-      long entries = 0L;
-      long size = 0L;
-      for (DataFileValue dfv : fileMap.values()) {
-        entries += dfv.getNumEntries();
-        size += dfv.getSize();
+    try (var tabletsMetadata = TabletsMetadata.builder().forTable(tableInfo.id).build(context)) {
+      for (var md : tabletsMetadata) {
+        TabletRowInfo.Factory factory = new TabletRowInfo.Factory(tableInfo.name, md.getExtent());
+        var fileMap = md.getFilesMap();
+        factory.numFiles(fileMap.size());
+        long entries = 0L;
+        long size = 0L;
+        for (DataFileValue dfv : fileMap.values()) {
+          entries += dfv.getNumEntries();
+          size += dfv.getSize();
+        }
+        factory.numEntries(entries);
+        factory.size(size);
+        factory.numWalLogs(md.getLogs().size());
+        factory.dir(md.getDirName());
+        factory.location(md.getLocation());
+        factory.status(md.getTabletState(liveTserverSet).toString());
+        results.add(factory.build());
       }
-      factory.numEntries(entries);
-      factory.size(size);
-      factory.numWalLogs(md.getLogs().size());
-      factory.dir(md.getDirName());
-      factory.location(md.getLocation());
-      factory.status(md.getTabletState(liveTserverSet).toString());
-      results.add(factory.build());
     }
 
     return results;
diff --git a/server/manager/src/main/spotbugs/exclude-filter.xml b/shell/src/main/spotbugs/exclude-filter.xml
similarity index 84%
copy from server/manager/src/main/spotbugs/exclude-filter.xml
copy to shell/src/main/spotbugs/exclude-filter.xml
index 0a31f85..95744f7 100644
--- a/server/manager/src/main/spotbugs/exclude-filter.xml
+++ b/shell/src/main/spotbugs/exclude-filter.xml
@@ -28,9 +28,4 @@
     <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
     <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
   </Match>
-  <Match>
-    <!-- Must ignore these everywhere, because of a javac byte code generation bug -->
-    <!-- https://github.com/spotbugs/spotbugs/issues/756 -->
-    <Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" />
-  </Match>
 </FindBugsFilter>