HBASE-25223 Use try-with-resources statement (#2592)

Signed-off-by: Wei-Chiu Chuang <weichiu@apache.org>
Signed-off-by: Viraj Jasani <vjasani@apache.org>
Signed-off-by: Duo Zhang <zhangduo@apache.org>
Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Minji Kim 2020-10-29 01:17:31 +09:00 committed by GitHub
parent 17f9aded2c
commit 735689d0f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 58 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}
});