Copilot commented on code in PR #10741:
URL: https://github.com/apache/ozone/pull/10741#discussion_r3583162760
##########
hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java:
##########
@@ -1160,17 +1171,25 @@ public FileStatus[] globStatus(Path pathPattern,
PathFilter filter)
}
@Override
- @SuppressWarnings("deprecation")
public boolean isDirectory(Path f) throws IOException {
incrementCounter(Statistic.INVOCATION_IS_DIRECTORY);
- return super.isDirectory(f);
+ try {
+ // headOp: only the entry type is needed, so skip the pipeline refresh.
+ return convertFileStatus(getFileStatusAdapter(f, true)).isDirectory();
+ } catch (FileNotFoundException e) {
+ return false;
+ }
}
@Override
- @SuppressWarnings("deprecation")
public boolean isFile(Path f) throws IOException {
incrementCounter(Statistic.INVOCATION_IS_FILE);
- return super.isFile(f);
+ try {
+ // headOp: only the entry type is needed, so skip the pipeline refresh.
+ return convertFileStatus(getFileStatusAdapter(f, true)).isFile();
+ } catch (FileNotFoundException e) {
+ return false;
+ }
Review Comment:
`isDirectory`/`isFile` currently convert the `FileStatusAdapter` into a
Hadoop `FileStatus` just to read the type. That extra allocation/work
(permissions, `LocatedFileStatus` check, etc.) partially undermines the point
of the head-op fast path. Since `FileStatusAdapter` already exposes
`isDir()`/`isFile()` and this class already relies on those semantics elsewhere
(eg `getContentSummaryInSpan`), you can return the type directly from the
adapter without conversion.
##########
hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestRootedOzoneFileSystemHeadOp.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.fs.ozone;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.net.URI;
+import org.apache.hadoop.fs.BlockLocation;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.om.exceptions.OMException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * Unit tests for the head-op (metadata-only) type checks on OFS
+ * ({@link BasicRootedOzoneFileSystem#isDirectory}/{@link
+ * BasicRootedOzoneFileSystem#isFile}) added in HDDS-15678. Uses a mock adapter
+ * so no cluster is required.
+ */
+public class TestRootedOzoneFileSystemHeadOp {
+
+ private BasicRootedOzoneClientAdapterImpl adapter;
+ private BasicRootedOzoneFileSystem fs;
+
+ /** Test FS that injects a mock adapter instead of connecting to OM. */
+ private final class MockAdapterFs extends BasicRootedOzoneFileSystem {
+ @Override
+ protected OzoneClientAdapter createAdapter(ConfigurationSource conf,
+ String omHost, int omPort) {
+ return adapter;
+ }
+ }
+
+ @BeforeEach
+ public void setUp() throws IOException {
+ adapter = mock(BasicRootedOzoneClientAdapterImpl.class);
+ fs = new MockAdapterFs();
+ fs.initialize(URI.create("ofs://om/"), new OzoneConfiguration());
+ }
+
+ private static FileStatusAdapter status(Path path, boolean isDir) {
+ return new FileStatusAdapter(0L, 0L, path, isDir, (short) 3, 0L, 0L, 0L,
+ (short) 0, "user", "group", null, new BlockLocation[0], false, false);
+ }
+
+ private void stubStatus(boolean isDir) throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
+ anyString(), anyBoolean()))
+ .thenAnswer(inv -> status(inv.getArgument(2), isDir));
+ }
+
+ private void stubThrow(IOException e) throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
+ anyString(), anyBoolean())).thenThrow(e);
+ }
+
+ @Test
+ public void isDirectoryUsesHeadOp() throws IOException {
+ stubStatus(true);
+ Path dir = new Path("/vol/bucket/dir");
+
+ assertTrue(fs.isDirectory(dir));
+ assertFalse(fs.isFile(dir));
+
+ ArgumentCaptor<Boolean> headOp = ArgumentCaptor.forClass(Boolean.class);
+ verify(adapter, org.mockito.Mockito.atLeastOnce()).getFileStatus(
+ anyString(), any(URI.class), any(Path.class), anyString(),
+ headOp.capture());
+ assertTrue(headOp.getValue(), "isDirectory/isFile must request headOp");
Review Comment:
This test only asserts `headOp.getValue()` (the last captured invocation).
Since `isDirectory(dir)` and `isFile(dir)` each call into the adapter, the test
could still pass if one of them accidentally used `headOp=false` as long as the
last call used `true`. Assert that *all* captured values are `true` to make the
test actually cover both type-check methods.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]