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 d0fc80337f8..f8e54c9c459 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 b54eab1372a..c059792ca68 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 61bf192eb89..9df33e13132 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 4f3df2fddc9..ae914f69b5c 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() { @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(); } } });