Copilot commented on code in PR #8960:
URL: https://github.com/apache/hbase/pull/8960#discussion_r4793413119
##########
hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java:
##########
@@ -110,6 +112,41 @@ public final class FSUtils {
private static final String THREAD_POOLSIZE =
"hbase.client.localityCheck.threadPoolSize";
private static final int DEFAULT_THREAD_POOLSIZE = 2;
+ private enum OpenFileSupport {
+ SUPPORTED,
+ LEGACY_OPEN,
+ TRANSPARENT
+ }
+
+ private static final ClassValue<OpenFileSupport> OPEN_FILE_SUPPORT_BY_CLASS =
+ new ClassValue<OpenFileSupport>() {
+ @Override
+ protected OpenFileSupport computeValue(Class<?> type) {
+ if (!FilterFileSystem.class.isAssignableFrom(type)) {
+ return OpenFileSupport.SUPPORTED;
+ }
+ try {
+ boolean overridesOpen = type.getMethod("open", Path.class,
int.class).getDeclaringClass()
+ != FilterFileSystem.class;
+ boolean overridesOpenFile =
+ type.getMethod("openFile", Path.class).getDeclaringClass() !=
FilterFileSystem.class;
+ if (overridesOpenFile) {
+ return OpenFileSupport.SUPPORTED;
+ }
Review Comment:
An inherited `openFile` override is treated as if it belonged to the
configured wrapper. If a builder-aware parent delegates `openFile` to its
backing filesystem and its child overrides only `open(Path, int)`, this returns
`SUPPORTED` and bypasses the child's legacy behavior. Compare the declaring
classes and fall back when the legacy override is more derived than `openFile`.
##########
hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java:
##########
@@ -110,6 +112,41 @@ public final class FSUtils {
private static final String THREAD_POOLSIZE =
"hbase.client.localityCheck.threadPoolSize";
private static final int DEFAULT_THREAD_POOLSIZE = 2;
+ private enum OpenFileSupport {
+ SUPPORTED,
+ LEGACY_OPEN,
+ TRANSPARENT
+ }
+
+ private static final ClassValue<OpenFileSupport> OPEN_FILE_SUPPORT_BY_CLASS =
+ new ClassValue<OpenFileSupport>() {
+ @Override
+ protected OpenFileSupport computeValue(Class<?> type) {
+ if (!FilterFileSystem.class.isAssignableFrom(type)) {
+ return OpenFileSupport.SUPPORTED;
+ }
+ try {
+ boolean overridesOpen = type.getMethod("open", Path.class,
int.class).getDeclaringClass()
+ != FilterFileSystem.class;
+ boolean overridesOpenFile =
+ type.getMethod("openFile", Path.class).getDeclaringClass() !=
FilterFileSystem.class;
+ if (overridesOpenFile) {
+ return OpenFileSupport.SUPPORTED;
+ }
+ if (overridesOpen) {
+ LOG.info("FileSystem wrapper {} overrides open(Path, int) but not
openFile(Path); "
+ + "using legacy open() instead of the faster openFile() path",
type.getName());
+ return OpenFileSupport.LEGACY_OPEN;
Review Comment:
The new raw-local-file smoke test never exercises legacy-wrapper detection,
so it would still pass if `openForRead` bypassed a wrapper's `open(Path, int)`
override. Add regression tests that count calls to that override through both
`HFileSystem` and its no-checksum filesystem, including a transparent outer
wrapper. These tests can use legacy APIs and compile under both Hadoop profiles.
##########
hbase-server/src/main/java/org/apache/hadoop/hbase/util/FSUtils.java:
##########
@@ -216,6 +253,133 @@ public static FSDataOutputStream create(Configuration
conf, FileSystem fs, Path
return create(conf, fs, path, perm, favoredNodes, true);
}
+ /**
+ * Opens a file for reading. When {@code fileStatus} is supplied and the
{@link FileSystem}
+ * supports {@code FileSystem.openFile().withFileStatus()}, that path is
used so the client can
+ * skip a redundant metadata lookup (for example {@code getFileInfo}).
+ * @param fs filesystem
+ * @param path file to open
+ * @param fileStatus status from a recent {@code listStatus} / {@code
getFileStatus}, or
+ * {@code null}
+ * @return input stream for the file
+ * @throws IOException if the file cannot be opened
+ */
+ public static FSDataInputStream openForRead(FileSystem fs, Path path,
FileStatus fileStatus)
+ throws IOException {
+ if (fileStatus != null && supportsOpenFile(fs)) {
+ try {
+ CompletableFuture<FSDataInputStream> future = openFile(fs, path,
fileStatus);
+ try {
+ return future.get();
+ } catch (InterruptedException e) {
+ // The open may still complete asynchronously; close whatever stream
it produces since
+ // the caller will never receive it. Do not cancel the future, as a
cancelled future
+ // would discard a stream that is completed later without closing it.
+ future.whenComplete((in, t) -> IOUtils.cleanupWithLogger(LOG, in));
+ Thread.currentThread().interrupt();
+ throw (InterruptedIOException) new
InterruptedIOException("Interrupted opening " + path)
+ .initCause(e);
+ }
+ } catch (UnsupportedOperationException | ExecutionException e) {
+ if (
+ e instanceof ExecutionException
+ && !(e.getCause() instanceof UnsupportedOperationException)
+ ) {
+ Throwable cause = e.getCause();
+ if (cause instanceof IOException) {
+ throw (IOException) cause;
+ }
+ throw new IOException("Failed opening " + path, cause);
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("openFile().withFileStatus() not supported or failed for
{}, "
+ + "fail back with open()", fs.getUri(), e);
+ }
+ }
+ }
+ return fs.open(path);
+ }
+
+ @SuppressWarnings("unchecked")
+ private static CompletableFuture<FSDataInputStream> openFile(FileSystem fs,
Path path,
+ FileStatus fileStatus) throws IOException {
+ try {
+ Object builder = OpenFileMethods.OPEN_FILE.invoke(fs, path);
+ builder = OpenFileMethods.WITH_FILE_STATUS.invoke(builder, fileStatus);
+ return (CompletableFuture<FSDataInputStream>)
OpenFileMethods.BUILD.invoke(builder);
+ } catch (IllegalAccessException e) {
+ throw new IOException("Unable to access openFile for " + path, e);
+ } catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ if (cause instanceof UnsupportedOperationException) {
+ throw (UnsupportedOperationException) cause;
+ }
+ if (cause instanceof IOException) {
+ throw (IOException) cause;
+ }
+ if (cause instanceof RuntimeException) {
+ throw (RuntimeException) cause;
+ }
+ if (cause instanceof Error) {
+ throw (Error) cause;
+ }
+ throw new IOException("Failed opening " + path, cause);
+ }
+ }
+
+ /**
+ * Returns whether it is safe to use {@code FileSystem.openFile(Path)}
without bypassing a
+ * {@link FilterFileSystem} wrapper's legacy {@link FileSystem#open(Path,
int)} override. Hadoop 2
+ * does not provide the builder API and always uses the legacy path.
+ */
+ private static boolean supportsOpenFile(FileSystem fs) throws IOException {
+ if (!OpenFileMethods.isAvailable()) {
+ return false;
+ }
+ FileSystem effectiveFs = fs instanceof HFileSystem ? ((HFileSystem)
fs).getBackingFs() : fs;
Review Comment:
Unwrapping every `HFileSystem` here skips override detection for subclasses
that override only `open(Path, int)`. With a supplied status, their custom read
behavior is bypassed because the inherited
[`FilterFileSystem.openFile`](https://github.com/apache/hadoop/blob/05f08c4b8e1b70aaa2864a97ff63b7374cde7b01/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FilterFileSystem.java#L709-L721)
forwards to the backing filesystem. Start traversal at `fs`: ordinary
`HFileSystem` instances are already classified as transparent, while subclass
overrides will be checked.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]