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