You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2020/10/28 16:17:56 UTC

[hbase] branch master updated: HBASE-25223 Use try-with-resources statement (#2592)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 735689d  HBASE-25223 Use try-with-resources statement (#2592)
735689d is described below

commit 735689d0f7d346f09f5de0a999e340c776ed82d6
Author: Minji Kim <mi...@gmail.com>
AuthorDate: Thu Oct 29 01:17:31 2020 +0900

    HBASE-25223 Use try-with-resources statement (#2592)
    
    
    Signed-off-by: Wei-Chiu Chuang <we...@apache.org>
    Signed-off-by: Viraj Jasani <vj...@apache.org>
    Signed-off-by: Duo Zhang <zh...@apache.org>
    Signed-off-by: stack <st...@apache.org>
---
 .../hadoop/hbase/snapshot/CreateSnapshot.java      | 23 +++++--------------
 .../hbase/snapshot/SnapshotDescriptionUtils.java   | 26 +++++-----------------
 .../hadoop/hbase/snapshot/SnapshotManifest.java    | 11 ++-------
 .../hadoop/hbase/snapshot/SnapshotManifestV2.java  | 17 +++++---------
 4 files changed, 19 insertions(+), 58 deletions(-)

diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/CreateSnapshot.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/CreateSnapshot.java
index d0fc803..f8e54c9 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/CreateSnapshot.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/CreateSnapshot.java
@@ -67,24 +67,13 @@ public class CreateSnapshot extends AbstractHBaseTool {
 
     @Override
     protected int doWork() throws Exception {
-        Connection connection = null;
-        Admin admin = null;
-        try {
-            connection = ConnectionFactory.createConnection(getConf());
-            admin = connection.getAdmin();
-            admin.snapshot(new SnapshotDescription(snapshotName, tableName, snapshotType));
+        try (Connection connection = ConnectionFactory.createConnection(getConf());
+              Admin admin = connection.getAdmin()) {
+          admin.snapshot(new SnapshotDescription(snapshotName, tableName, snapshotType));
         } catch (Exception e) {
-            System.err.println("failed to take the snapshot: " + e.getMessage());
-            return -1;
-        } finally {
-            if (admin != null) {
-                admin.close();
-            }
-            if (connection != null) {
-                connection.close();
-            }
+          System.err.println("failed to take the snapshot: " + e.getMessage());
+          return -1;
         }
-        return 0;
+      return 0;
     }
-
 }
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotDescriptionUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotDescriptionUtils.java
index b54eab1..c059792 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotDescriptionUtils.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotDescriptionUtils.java
@@ -343,13 +343,8 @@ public final class SnapshotDescriptionUtils {
     FsPermission perms = CommonFSUtils.getFilePermissions(fs, fs.getConf(),
       HConstants.DATA_FILE_UMASK_KEY);
     Path snapshotInfo = new Path(workingDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
-    try {
-      FSDataOutputStream out = CommonFSUtils.create(fs, snapshotInfo, perms, true);
-      try {
-        snapshot.writeTo(out);
-      } finally {
-        out.close();
-      }
+    try (FSDataOutputStream out = CommonFSUtils.create(fs, snapshotInfo, perms, true)){
+      snapshot.writeTo(out);
     } catch (IOException e) {
       // if we get an exception, try to remove the snapshot info
       if (!fs.delete(snapshotInfo, false)) {
@@ -370,15 +365,8 @@ public final class SnapshotDescriptionUtils {
   public static SnapshotDescription readSnapshotInfo(FileSystem fs, Path snapshotDir)
       throws CorruptedSnapshotException {
     Path snapshotInfo = new Path(snapshotDir, SNAPSHOTINFO_FILE);
-    try {
-      FSDataInputStream in = null;
-      try {
-        in = fs.open(snapshotInfo);
-        SnapshotDescription desc = SnapshotDescription.parseFrom(in);
-        return desc;
-      } finally {
-        if (in != null) in.close();
-      }
+    try (FSDataInputStream in = fs.open(snapshotInfo)){
+      return SnapshotDescription.parseFrom(in);
     } catch (IOException e) {
       throw new CorruptedSnapshotException("Couldn't read snapshot info from:" + snapshotInfo, e);
     }
@@ -434,10 +422,8 @@ public final class SnapshotDescriptionUtils {
   }
 
   public static boolean isSecurityAvailable(Configuration conf) throws IOException {
-    try (Connection conn = ConnectionFactory.createConnection(conf)) {
-      try (Admin admin = conn.getAdmin()) {
-        return admin.tableExists(PermissionStorage.ACL_TABLE_NAME);
-      }
+    try (Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin()) {
+      return admin.tableExists(PermissionStorage.ACL_TABLE_NAME);
     }
   }
 
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifest.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifest.java
index 61bf192..9df33e1 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifest.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifest.java
@@ -582,11 +582,8 @@ public final class SnapshotManifest {
    */
   private void writeDataManifest(final SnapshotDataManifest manifest)
       throws IOException {
-    FSDataOutputStream stream = workingDirFs.create(new Path(workingDir, DATA_MANIFEST_NAME));
-    try {
+    try (FSDataOutputStream stream = workingDirFs.create(new Path(workingDir, DATA_MANIFEST_NAME))) {
       manifest.writeTo(stream);
-    } finally {
-      stream.close();
     }
   }
 
@@ -594,9 +591,7 @@ public final class SnapshotManifest {
    * Read the SnapshotDataManifest file
    */
   private SnapshotDataManifest readDataManifest() throws IOException {
-    FSDataInputStream in = null;
-    try {
-      in = workingDirFs.open(new Path(workingDir, DATA_MANIFEST_NAME));
+    try (FSDataInputStream in = workingDirFs.open(new Path(workingDir, DATA_MANIFEST_NAME))) {
       CodedInputStream cin = CodedInputStream.newInstance(in);
       cin.setSizeLimit(manifestSizeLimit);
       return SnapshotDataManifest.parseFrom(cin);
@@ -604,8 +599,6 @@ public final class SnapshotManifest {
       return null;
     } catch (InvalidProtocolBufferException e) {
       throw new CorruptedSnapshotException("unable to parse data manifest " + e.getMessage(), e);
-    } finally {
-      if (in != null) in.close();
     }
   }
 
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifestV2.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifestV2.java
index 4f3df2f..ae914f6 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifestV2.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifestV2.java
@@ -93,12 +93,9 @@ public final class SnapshotManifestV2 {
       FileSystem workingDirFs = snapshotDir.getFileSystem(this.conf);
       if (workingDirFs.exists(snapshotDir)) {
         SnapshotRegionManifest manifest = region.build();
-        FSDataOutputStream stream = workingDirFs.create(
-            getRegionManifestPath(snapshotDir, manifest));
-        try {
+        try (FSDataOutputStream stream = workingDirFs.create(
+            getRegionManifestPath(snapshotDir, manifest))) {
           manifest.writeTo(stream);
-        } finally {
-          stream.close();
         }
       } else {
         LOG.warn("can't write manifest without parent dir, maybe it has been deleted by master?");
@@ -157,14 +154,10 @@ public final class SnapshotManifestV2 {
       completionService.submit(new Callable<SnapshotRegionManifest>() {
         @Override
         public SnapshotRegionManifest call() throws IOException {
-          FSDataInputStream stream = fs.open(st.getPath());
-          CodedInputStream cin = CodedInputStream.newInstance(stream);
-          cin.setSizeLimit(manifestSizeLimit);
-
-          try {
+          try (FSDataInputStream stream = fs.open(st.getPath())) {
+            CodedInputStream cin = CodedInputStream.newInstance(stream);
+            cin.setSizeLimit(manifestSizeLimit);
             return SnapshotRegionManifest.parseFrom(cin);
-          } finally {
-            stream.close();
           }
         }
       });