HBASE-27670 Improve FSUtils to directly obtain FSDataOutputStream (#5064)

Co-authored-by: alanzhao <alanzhao@126.com>
Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
alan.zhao 2023-03-04 21:46:45 +08:00 committed by GitHub
parent d2b0074f7a
commit 59fdaa28f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 18 deletions

View File

@ -85,7 +85,6 @@ import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.hdfs.protocol.LocatedBlock; import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.ipc.RemoteException; import org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.util.Progressable;
import org.apache.yetus.audience.InterfaceAudience; import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -198,27 +197,20 @@ public final class FSUtils {
if (fs instanceof HFileSystem) { if (fs instanceof HFileSystem) {
FileSystem backingFs = ((HFileSystem) fs).getBackingFs(); FileSystem backingFs = ((HFileSystem) fs).getBackingFs();
if (backingFs instanceof DistributedFileSystem) { if (backingFs instanceof DistributedFileSystem) {
// Try to use the favoredNodes version via reflection to allow backwards-
// compatibility.
short replication = Short.parseShort(conf.get(ColumnFamilyDescriptorBuilder.DFS_REPLICATION, short replication = Short.parseShort(conf.get(ColumnFamilyDescriptorBuilder.DFS_REPLICATION,
String.valueOf(ColumnFamilyDescriptorBuilder.DEFAULT_DFS_REPLICATION))); String.valueOf(ColumnFamilyDescriptorBuilder.DEFAULT_DFS_REPLICATION)));
try { DistributedFileSystem.HdfsDataOutputStreamBuilder builder =
return (FSDataOutputStream) (DistributedFileSystem.class ((DistributedFileSystem) backingFs).createFile(path).recursive().permission(perm)
.getDeclaredMethod("create", Path.class, FsPermission.class, boolean.class, int.class, .create();
short.class, long.class, Progressable.class, InetSocketAddress[].class) if (favoredNodes != null) {
.invoke(backingFs, path, perm, true, CommonFSUtils.getDefaultBufferSize(backingFs), builder.favoredNodes(favoredNodes);
replication > 0 ? replication : CommonFSUtils.getDefaultReplication(backingFs, path),
CommonFSUtils.getDefaultBlockSize(backingFs, path), null, favoredNodes));
} catch (InvocationTargetException ite) {
// Function was properly called, but threw it's own exception.
throw new IOException(ite.getCause());
} catch (NoSuchMethodException e) {
LOG.debug("DFS Client does not support most favored nodes create; using default create");
LOG.trace("Ignoring; use default create", e);
} catch (IllegalArgumentException | SecurityException | IllegalAccessException e) {
LOG.debug("Ignoring (most likely Reflection related exception) " + e);
} }
if (replication > 0) {
builder.replication(replication);
}
return builder.build();
} }
} }
return CommonFSUtils.create(fs, path, perm, true); return CommonFSUtils.create(fs, path, perm, true);
} }