This is an automated email from the ASF dual-hosted git repository.
adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new a5473401285 HDDS-17714. OFS getFileStatus should not refresh pipelines
or return block locations (#11971)
a5473401285 is described below
commit a547340128534c53b9e9a8b48003faf6983720ef
Author: Andrey Yarovoy <[email protected]>
AuthorDate: Sat Nov 14 14:11:33 2026 -0500
HDDS-17714. OFS getFileStatus should not refresh pipelines or return block
locations (#11971)
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../fs/ozone/AbstractOzoneFileSystemTest.java | 14 +--
.../ozone/AbstractRootedOzoneFileSystemTest.java | 13 +++
.../hadoop/fs/ozone/OzoneFileSystemTestBase.java | 27 ++++++
.../fs/ozone/BasicOzoneClientAdapterImpl.java | 9 +-
.../hadoop/fs/ozone/BasicOzoneFileSystem.java | 56 +++++++++---
.../ozone/BasicRootedOzoneClientAdapterImpl.java | 9 +-
.../fs/ozone/BasicRootedOzoneFileSystem.java | 36 ++++++--
.../fs/ozone/FileStatusWithoutBlockLocations.java | 58 ++++++++++++
.../ozone/TestBasicOzoneClientAdapterHeadOp.java | 65 +++++++++++++
.../TestBasicRootedOzoneClientAdapterHeadOp.java | 67 ++++++++++++++
.../hadoop/fs/ozone/TestOzoneFileSystemHeadOp.java | 101 ++++++++++++++++++++-
.../fs/ozone/TestRootedOzoneFileSystemHeadOp.java | 101 ++++++++++++++++++++-
12 files changed, 512 insertions(+), 44 deletions(-)
diff --git
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java
index 998834d6222..dfce65b3469 100644
---
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java
+++
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java
@@ -1859,21 +1859,14 @@ void testOzoneManagerFileSystemInterface() throws
IOException {
}
@Test
- public void testOzoneManagerLocatedFileStatus() throws IOException {
+ public void testOzoneManagerFileBlockLocations() throws IOException {
String data = RandomStringUtils.secure().nextAlphanumeric(20);
String filePath = RandomStringUtils.secure().nextAlphanumeric(5);
Path path = createPath("/" + filePath);
try (FSDataOutputStream stream = fs.create(path)) {
stream.writeBytes(data);
}
- FileStatus status = fs.getFileStatus(path);
- LocatedFileStatus locatedFileStatus =
assertInstanceOf(LocatedFileStatus.class, status);
-
assertThat(locatedFileStatus.getBlockLocations().length).isGreaterThanOrEqualTo(1);
-
- for (BlockLocation blockLocation : locatedFileStatus.getBlockLocations()) {
- assertThat(blockLocation.getNames().length).isGreaterThanOrEqualTo(1);
- assertThat(blockLocation.getHosts().length).isGreaterThanOrEqualTo(1);
- }
+ fileBlockLocations(fs, path, data.length());
}
@Test
@@ -1892,8 +1885,7 @@ void testBlockOffsetsWithMultiBlockFile() throws
Exception {
stream.writeBytes(data);
}
FileStatus status = fs.getFileStatus(path);
- LocatedFileStatus locatedFileStatus =
assertInstanceOf(LocatedFileStatus.class, status);
- BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
+ BlockLocation[] blockLocations = fs.getFileBlockLocations(status, 0,
status.getLen());
assertEquals(0, blockLocations[0].getOffset());
assertEquals(blockSize, blockLocations[1].getOffset());
diff --git
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java
index 107fefad264..9101cef0bef 100644
---
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java
+++
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java
@@ -280,6 +280,19 @@ void testUserHomeDirectory() {
userOfs.getHomeDirectory());
}
+ /**
+ * Like HDFS, getFileStatus has no block locations (HDDS-17714):
getFileBlockLocations fetches them, for a status
+ * and for a path.
+ */
+ @Test
+ void testGetFileBlockLocations() throws Exception {
+ Path path = new Path(bucketPath, "testGetFileBlockLocations");
+ byte[] data = ContractTestUtils.dataset(1024, 'a', 26);
+ ContractTestUtils.createFile(fs, path, true, data);
+
+ fileBlockLocations(fs, path, data.length);
+ }
+
@Test
void testCreateDoesNotAddParentDirKeys() throws Exception {
Path grandparent = new Path(bucketPath,
diff --git
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java
index 344cb441d4f..7d48107bbb4 100644
---
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java
+++
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTestBase.java
@@ -167,6 +167,33 @@ static void listLocatedStatusForZeroByteFile(FileSystem
fs, Path path) throws IO
assertFalse(fileStatuses[0] instanceof LocatedFileStatus);
}
+ /**
+ * Like HDFS, getFileStatus returns no block locations and
getFileBlockLocations fetches them; for a file deleted
+ * after its getFileStatus, getFileBlockLocations fails with
FileNotFoundException. Deletes the file.
+ */
+ static void fileBlockLocations(FileSystem fs, Path path, long length) throws
IOException {
+ FileStatus status = fs.getFileStatus(path);
+ assertThat(status).isNotInstanceOf(LocatedFileStatus.class);
+ assertEquals(length, status.getLen());
+
+ BlockLocation[] expected =
fs.listLocatedStatus(path).next().getBlockLocations();
+ assertThat(expected.length).isGreaterThanOrEqualTo(1);
+ for (BlockLocation[] blockLocations : new BlockLocation[][] {
+ fs.getFileBlockLocations(status, 0, status.getLen()),
fs.getFileBlockLocations(path, 0, status.getLen())}) {
+ assertEquals(expected.length, blockLocations.length);
+ for (int i = 0; i < expected.length; i++) {
+
assertThat(blockLocations[i].getNames().length).isGreaterThanOrEqualTo(1);
+ // OM shuffles datanodes at the same distance, so the two lookups can
order the hosts differently
+
assertThat(blockLocations[i].getHosts()).containsExactlyInAnyOrder(expected[i].getHosts());
+ assertEquals(expected[i].getOffset(), blockLocations[i].getOffset());
+ assertEquals(expected[i].getLength(), blockLocations[i].getLength());
+ }
+ }
+
+ fs.delete(path, false);
+ assertThrows(FileNotFoundException.class, () ->
fs.getFileBlockLocations(status, 0, status.getLen()));
+ }
+
void createKeyWithECReplicationConfig(Path root, OzoneConfiguration conf)
throws IOException {
Path testKeyPath = new Path(root, "testKey");
createKeyWithECReplicationConfiguration(conf, testKeyPath);
diff --git
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneClientAdapterImpl.java
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneClientAdapterImpl.java
index 685eb00b68a..d04033da062 100644
---
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneClientAdapterImpl.java
+++
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneClientAdapterImpl.java
@@ -425,7 +425,8 @@ public FileStatusAdapter getFileStatus(String key, URI uri,
try {
incrementCounter(Statistic.OBJECTS_QUERY, 1);
OzoneFileStatus status = bucket.getFileStatus(key, headOp);
- return toFileStatusAdapter(status, userName, uri, qualifiedPath);
+ // An OM without HDDS-15678 still returns the block locations of a head
op, without pipelines: ignore them.
+ return toFileStatusAdapter(status, userName, uri, qualifiedPath,
!headOp);
} catch (OMException e) {
if (e.getResult() == OMException.ResultCodes.FILE_NOT_FOUND) {
@@ -461,7 +462,7 @@ public ListStatusResult<FileStatusAdapter>
listStatusPage(String keyName, boolea
.map(status -> toFileStatusAdapter(status, username, uri,
workingDir));
}
return bucket.listStatusPage(keyName, recursive, startKey, numEntries)
- .map(status -> toFileStatusAdapter(status, username, uri,
workingDir));
+ .map(status -> toFileStatusAdapter(status, username, uri,
workingDir, true));
} catch (OMException e) {
if (e.getResult() == OMException.ResultCodes.FILE_NOT_FOUND) {
throw new FileNotFoundException(e.getMessage());
@@ -540,7 +541,7 @@ public BasicKeyInfo next() {
}
private FileStatusAdapter toFileStatusAdapter(OzoneFileStatus status,
- String owner, URI defaultUri, Path workingDir) {
+ String owner, URI defaultUri, Path workingDir, boolean
withBlockLocations) {
OmKeyInfo keyInfo = status.getKeyInfo();
short replication = (short) keyInfo.getReplicationConfig()
.getRequiredNodes();
@@ -565,7 +566,7 @@ private FileStatusAdapter
toFileStatusAdapter(OzoneFileStatus status,
StringUtils.defaultIfEmpty(keyInfo.getOwnerName(), owner),
owner,
null,
- getBlockLocations(status),
+ getBlockLocations(withBlockLocations ? status : null),
OzoneClientUtils.isKeyEncrypted(keyInfo),
isEc,
ecPolicy
diff --git
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
index 49376e33bd6..0456f377ea4 100644
---
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
+++
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java
@@ -848,16 +848,20 @@ public long getDefaultBlockSize() {
OZONE_SCM_BLOCK_SIZE, OZONE_SCM_BLOCK_SIZE_DEFAULT, StorageUnit.BYTES);
}
+ /**
+ * Like HDFS, the status has no block locations: OM skips the pipeline
refresh (headOp).
+ * {@link #getFileBlockLocations} fetches the locations when they are asked
for.
+ */
@Override
public FileStatus getFileStatus(Path f) throws IOException {
- return getFileStatus(f, false);
+ return getFileStatus(f, true);
}
/**
- * @param headOp when true, requests a metadata-only (type) check so the OM
- * skips the pipeline refresh (SCM round-trip) and datanode
- * sorting. Used by {@link #isDirectory(Path)}/{@link
#isFile(Path)},
- * which only need the entry type.
+ * @param headOp when true, requests a metadata-only check so the OM skips
the
+ * pipeline refresh (SCM round-trip) and datanode sorting, and
the
+ * status has no block locations. Used by {@link
#getFileStatus(Path)},
+ * {@link #isDirectory(Path)} and {@link #isFile(Path)}.
*/
private FileStatus getFileStatus(Path f, boolean headOp) throws IOException {
incrementCounter(Statistic.INVOCATION_GET_FILE_STATUS, 1);
@@ -867,8 +871,9 @@ private FileStatus getFileStatus(Path f, boolean headOp)
throws IOException {
String key = pathToKey(qualifiedPath);
FileStatus fileStatus = null;
try {
- fileStatus = convertFileStatus(
- adapter.getFileStatus(key, uri, qualifiedPath, getUsername(),
headOp));
+ FileStatusAdapter status = adapter.getFileStatus(key, uri,
qualifiedPath, getUsername(), headOp);
+ fileStatus = headOp ? new
FileStatusWithoutBlockLocations(convertFileStatus(status),
status.isErasureCoded())
+ : convertFileStatus(status);
} catch (IOException ex) {
if (ex instanceof OMException) {
if (((OMException) ex).getResult()
@@ -887,18 +892,45 @@ public BlockLocation[] getFileBlockLocations(FileStatus
fileStatus,
throws IOException {
if (fileStatus instanceof LocatedFileStatus) {
return getBlockLocationsInRange((LocatedFileStatus) fileStatus, start,
len);
- } else {
- return super.getFileBlockLocations(fileStatus, start, len);
+ }
+ if (needsBlockLocations(fileStatus, start, len)) {
+ // getFileStatus returns no block locations: fetch them by path, as HDFS
does.
+ return getFileBlockLocations(fileStatus.getPath(), start, len);
+ }
+ return super.getFileBlockLocations(fileStatus, start, len);
+ }
+
+ @Override
+ public BlockLocation[] getFileBlockLocations(Path p, long start, long len)
throws IOException {
+ checkBlockLocationsRange(start, len);
+ // One GetFileStatus with block locations, instead of getFileStatus
followed by another for the locations.
+ FileStatus fileStatus = getFileStatus(p, false);
+ if (fileStatus instanceof LocatedFileStatus) {
+ return getBlockLocationsInRange((LocatedFileStatus) fileStatus, start,
len);
+ }
+ return super.getFileBlockLocations(fileStatus, start, len);
+ }
+
+ /** Rejects the range like {@link
org.apache.hadoop.fs.FileSystem#getFileBlockLocations(FileStatus, long, long)}.
*/
+ static void checkBlockLocationsRange(long start, long len) {
+ if (start < 0 || len < 0) {
+ throw new IllegalArgumentException("Invalid start or len parameter");
}
}
+ /**
+ * Whether the block locations of the range must be fetched for a status
from {@link #getFileStatus(Path)}.
+ */
+ static boolean needsBlockLocations(FileStatus fileStatus, long start, long
len) {
+ return fileStatus instanceof FileStatusWithoutBlockLocations &&
fileStatus.isFile()
+ && start >= 0 && len >= 0 && start < fileStatus.getLen();
+ }
+
/**
* Like HDFS, return only the blocks that overlap [start, start + len), or
the block containing start if len is 0.
*/
static BlockLocation[] getBlockLocationsInRange(LocatedFileStatus
fileStatus, long start, long len) {
- if (start < 0 || len < 0) {
- throw new IllegalArgumentException("Invalid start or len parameter");
- }
+ checkBlockLocationsRange(start, len);
BlockLocation[] blocks = fileStatus.getBlockLocations();
if (blocks == null) {
return null;
diff --git
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneClientAdapterImpl.java
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneClientAdapterImpl.java
index c899d0507cf..5709318c115 100644
---
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneClientAdapterImpl.java
+++
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneClientAdapterImpl.java
@@ -770,8 +770,9 @@ private FileStatusAdapter getFileStatusForKeyOrSnapshot(
OzoneBucket bucket = getBucket(ofsPath, false);
status = bucket.getFileStatus(key, headOp);
}
+ // An OM without HDDS-15678 still returns the block locations of a
head op, without pipelines: ignore them.
return toFileStatusAdapter(status, userName, uri, qualifiedPath,
- ofsPath.getNonKeyPath());
+ ofsPath.getNonKeyPath(), !headOp);
}
} catch (OMException e) {
if (e.getResult() == OMException.ResultCodes.FILE_NOT_FOUND) {
@@ -1097,7 +1098,7 @@ public ListStatusResult<FileStatusAdapter>
listStatusPage(String pathStr, boolea
}
} else {
for (OzoneFileStatus status : statuses) {
- result.add(toFileStatusAdapter(status, username, uri, workingDir,
ofsPathPrefix));
+ result.add(toFileStatusAdapter(status, username, uri, workingDir,
ofsPathPrefix, true));
}
}
@@ -1184,7 +1185,7 @@ public BasicKeyInfo next() {
}
private FileStatusAdapter toFileStatusAdapter(OzoneFileStatus status,
- String owner, URI defaultUri, Path workingDir, String ofsPathPrefix) {
+ String owner, URI defaultUri, Path workingDir, String ofsPathPrefix,
boolean withBlockLocations) {
OmKeyInfo keyInfo = status.getKeyInfo();
short replication = (short) keyInfo.getReplicationConfig()
.getRequiredNodes();
@@ -1209,7 +1210,7 @@ private FileStatusAdapter
toFileStatusAdapter(OzoneFileStatus status,
StringUtils.defaultIfEmpty(keyInfo.getOwnerName(), owner),
owner,
null,
- getBlockLocations(status),
+ getBlockLocations(withBlockLocations ? status : null),
OzoneClientUtils.isKeyEncrypted(keyInfo),
isEc,
ecPolicy
diff --git
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
index c4db47e29df..46d96ced203 100644
---
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
+++
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java
@@ -1116,10 +1116,17 @@ public long getDefaultBlockSize() {
OZONE_SCM_BLOCK_SIZE, OZONE_SCM_BLOCK_SIZE_DEFAULT, StorageUnit.BYTES);
}
+ /**
+ * Like HDFS, the status has no block locations: OM skips the pipeline
refresh (headOp).
+ * {@link #getFileBlockLocations} fetches the locations when they are asked
for.
+ */
@Override
public FileStatus getFileStatus(Path f) throws IOException {
return TracingUtil.executeInNewSpan("ofs getFileStatus",
- () -> convertFileStatus(getFileStatusAdapter(f)));
+ () -> {
+ FileStatusAdapter status = getFileStatusAdapter(f, true);
+ return new
FileStatusWithoutBlockLocations(convertFileStatus(status),
status.isErasureCoded());
+ });
}
public FileStatusAdapter getFileStatusAdapter(Path f) throws IOException {
@@ -1127,10 +1134,10 @@ public FileStatusAdapter getFileStatusAdapter(Path f)
throws IOException {
}
/**
- * @param headOp when true, requests a metadata-only (type) check so the OM
- * skips the pipeline refresh (SCM round-trip) and datanode
- * sorting. Used by {@link #isDirectory(Path)}/{@link
#isFile(Path)},
- * which only need the entry type.
+ * @param headOp when true, requests a metadata-only check so the OM skips
the
+ * pipeline refresh (SCM round-trip) and datanode sorting, and
the
+ * status has no block locations. Used by {@link
#getFileStatus(Path)},
+ * {@link #isDirectory(Path)} and {@link #isFile(Path)}.
*/
public FileStatusAdapter getFileStatusAdapter(Path f, boolean headOp)
throws IOException {
@@ -1167,9 +1174,24 @@ public BlockLocation[] getFileBlockLocations(FileStatus
fileStatus,
throws IOException {
if (fileStatus instanceof LocatedFileStatus) {
return BasicOzoneFileSystem.getBlockLocationsInRange((LocatedFileStatus)
fileStatus, start, len);
- } else {
- return super.getFileBlockLocations(fileStatus, start, len);
}
+ if (BasicOzoneFileSystem.needsBlockLocations(fileStatus, start, len)) {
+ // getFileStatus returns no block locations: fetch them by path, as HDFS
does.
+ return getFileBlockLocations(fileStatus.getPath(), start, len);
+ }
+ return super.getFileBlockLocations(fileStatus, start, len);
+ }
+
+ @Override
+ public BlockLocation[] getFileBlockLocations(Path p, long start, long len)
throws IOException {
+ BasicOzoneFileSystem.checkBlockLocationsRange(start, len);
+ // One GetFileStatus with block locations, instead of getFileStatus
followed by another for the locations.
+ FileStatus fileStatus = TracingUtil.executeInNewSpan("ofs
getFileBlockLocations",
+ () -> convertFileStatus(getFileStatusAdapter(p, false)));
+ if (fileStatus instanceof LocatedFileStatus) {
+ return BasicOzoneFileSystem.getBlockLocationsInRange((LocatedFileStatus)
fileStatus, start, len);
+ }
+ return super.getFileBlockLocations(fileStatus, start, len);
}
@Override
diff --git
a/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/FileStatusWithoutBlockLocations.java
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/FileStatusWithoutBlockLocations.java
new file mode 100644
index 00000000000..835726bc328
--- /dev/null
+++
b/hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/FileStatusWithoutBlockLocations.java
@@ -0,0 +1,58 @@
+/*
+ * 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 java.io.IOException;
+import org.apache.hadoop.fs.FileStatus;
+
+/**
+ * The status that ofs and o3fs {@code getFileStatus} return. Like HDFS, it
has no block locations, and
+ * {@code getFileBlockLocations} fetches them from OM for it.
+ * <p>
+ * A status from {@code listStatus} has no block locations either, but {@code
getFileBlockLocations} keeps returning a
+ * placeholder location for it without an OM call, so that a listing that asks
for the locations of every file (as
+ * Spark does) does not pay an OM call per file.
+ */
+final class FileStatusWithoutBlockLocations extends FileStatus {
+ private static final long serialVersionUID = 1L;
+
+ private final boolean encrypted;
+ private final boolean erasureCoded;
+
+ /**
+ * @param erasureCoded the erasure coded flag of the {@link
FileStatusAdapter}: passed separately because the
+ * ozonefs-hadoop2 {@code constructFileStatus} builds {@code status}
with the Hadoop 2 constructor, which has no
+ * erasure coded flag, and {@code FileStatus#isErasureCoded} does not
exist in Hadoop 2
+ */
+ FileStatusWithoutBlockLocations(FileStatus status, boolean erasureCoded)
throws IOException {
+ // the copy constructor does not copy the encrypted and erasure coded flags
+ super(status);
+ this.encrypted = status.isEncrypted();
+ this.erasureCoded = erasureCoded;
+ }
+
+ @Override
+ public boolean isEncrypted() {
+ return encrypted;
+ }
+
+ @Override
+ public boolean isErasureCoded() {
+ return erasureCoded;
+ }
+}
diff --git
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneClientAdapterHeadOp.java
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneClientAdapterHeadOp.java
index 71a99524fae..232fb2de291 100644
---
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneClientAdapterHeadOp.java
+++
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicOzoneClientAdapterHeadOp.java
@@ -17,6 +17,8 @@
package org.apache.hadoop.fs.ozone;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -33,12 +35,19 @@
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Collections;
+import java.util.UUID;
import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.client.RatisReplicationConfig;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
+import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
import org.apache.hadoop.ozone.client.OzoneBucket;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -93,6 +102,62 @@ private static OzoneFileStatus fileStatus(boolean isDir) {
return new OzoneFileStatus(keyInfo, 512, isDir);
}
+ /** A file with one 10 byte block, on a pipeline of datanode dn1 if {@code
pipeline}. */
+ private static OzoneFileStatus fileStatusWithBlock(boolean pipeline) {
+ OmKeyLocationInfo.Builder block = new OmKeyLocationInfo.Builder()
+ .setBlockID(new BlockID(1, 1))
+ .setLength(10);
+ if (pipeline) {
+ DatanodeDetails dn = DatanodeDetails.newBuilder()
+ .setUuid(UUID.randomUUID())
+ .setHostName("dn1")
+ .setIpAddress("10.0.0.1")
+ .addPort(DatanodeDetails.newStandalonePort(9859))
+ .build();
+ block.setPipeline(Pipeline.newBuilder()
+ .setId(PipelineID.randomId())
+
.setReplicationConfig(RatisReplicationConfig.getInstance(HddsProtos.ReplicationFactor.ONE))
+ .setState(Pipeline.PipelineState.OPEN)
+ .setNodes(Collections.singletonList(dn))
+ .build());
+ }
+ OmKeyInfo keyInfo = new OmKeyInfo.Builder()
+ .setVolumeName("vol")
+ .setBucketName("bucket")
+ .setKeyName("key")
+ .setReplicationConfig(RatisReplicationConfig.getInstance(
+ HddsProtos.ReplicationFactor.THREE))
+ .setOmKeyLocationInfos(Collections.singletonList(
+ new OmKeyLocationInfoGroup(0,
Collections.singletonList(block.build()))))
+ .setDataSize(10)
+ .setCreationTime(0)
+ .setModificationTime(0)
+ .setAcls(Collections.emptyList())
+ .build();
+ return new OzoneFileStatus(keyInfo, 512, false);
+ }
+
+ /**
+ * An OM without HDDS-15678 returns the block locations of a head op,
unrefreshed and without pipelines (HDDS-17714).
+ */
+ @Test
+ public void headOpIgnoresBlockLocations() throws IOException {
+ when(bucket.getFileStatus(anyString(),
eq(true))).thenReturn(fileStatusWithBlock(false));
+
+ FileStatusAdapter status = adapter.getFileStatus("key", URI_O3FS,
WORKING_DIR, "user", true);
+ assertEquals(10, status.getLength());
+ assertEquals(0, status.getBlockLocations().length);
+ }
+
+ @Test
+ public void fullStatusHasBlockLocations() throws IOException {
+ when(bucket.getFileStatus(anyString(),
eq(false))).thenReturn(fileStatusWithBlock(true));
+
+ FileStatusAdapter status = adapter.getFileStatus("key", URI_O3FS,
WORKING_DIR, "user", false);
+ assertEquals(1, status.getBlockLocations().length);
+ assertArrayEquals(new String[] {"dn1"},
status.getBlockLocations()[0].getHosts());
+ }
+
@Test
public void headOpOverloadThreadsHeadOp() throws IOException {
when(bucket.getFileStatus(anyString(), anyBoolean()))
diff --git
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicRootedOzoneClientAdapterHeadOp.java
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicRootedOzoneClientAdapterHeadOp.java
index 25e067556d2..1b7187c2710 100644
---
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicRootedOzoneClientAdapterHeadOp.java
+++
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestBasicRootedOzoneClientAdapterHeadOp.java
@@ -17,6 +17,8 @@
package org.apache.hadoop.fs.ozone;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -37,9 +39,14 @@
import java.net.URI;
import java.time.Instant;
import java.util.Collections;
+import java.util.UUID;
import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.client.RatisReplicationConfig;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
+import org.apache.hadoop.hdds.scm.pipeline.PipelineID;
import org.apache.hadoop.ozone.OFSPath;
import org.apache.hadoop.ozone.OzoneManagerVersion;
import org.apache.hadoop.ozone.client.ObjectStore;
@@ -48,6 +55,8 @@
import org.apache.hadoop.ozone.client.protocol.ClientProtocol;
import org.apache.hadoop.ozone.om.exceptions.OMException;
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -109,6 +118,64 @@ private static OzoneFileStatus fileStatus(boolean isDir) {
return new OzoneFileStatus(keyInfo, 512, isDir);
}
+ /** A file with one 10 byte block, on a pipeline of datanode dn1 if {@code
pipeline}. */
+ private static OzoneFileStatus fileStatusWithBlock(boolean pipeline) {
+ OmKeyLocationInfo.Builder block = new OmKeyLocationInfo.Builder()
+ .setBlockID(new BlockID(1, 1))
+ .setLength(10);
+ if (pipeline) {
+ DatanodeDetails dn = DatanodeDetails.newBuilder()
+ .setUuid(UUID.randomUUID())
+ .setHostName("dn1")
+ .setIpAddress("10.0.0.1")
+ .addPort(DatanodeDetails.newStandalonePort(9859))
+ .build();
+ block.setPipeline(Pipeline.newBuilder()
+ .setId(PipelineID.randomId())
+
.setReplicationConfig(RatisReplicationConfig.getInstance(HddsProtos.ReplicationFactor.ONE))
+ .setState(Pipeline.PipelineState.OPEN)
+ .setNodes(Collections.singletonList(dn))
+ .build());
+ }
+ OmKeyInfo keyInfo = new OmKeyInfo.Builder()
+ .setVolumeName("vol")
+ .setBucketName("bucket")
+ .setKeyName("key")
+ .setReplicationConfig(RatisReplicationConfig.getInstance(
+ HddsProtos.ReplicationFactor.THREE))
+ .setOmKeyLocationInfos(Collections.singletonList(
+ new OmKeyLocationInfoGroup(0,
Collections.singletonList(block.build()))))
+ .setDataSize(10)
+ .setCreationTime(0)
+ .setModificationTime(0)
+ .setAcls(Collections.emptyList())
+ .build();
+ return new OzoneFileStatus(keyInfo, 512, false);
+ }
+
+ /**
+ * An OM without HDDS-15678 returns the block locations of a head op,
unrefreshed and without pipelines (HDDS-17714).
+ */
+ @Test
+ public void headOpIgnoresBlockLocations() throws IOException {
+ when(proxy.getOzoneFileStatus(anyString(), anyString(), anyString(),
eq(true)))
+ .thenReturn(fileStatusWithBlock(false));
+
+ FileStatusAdapter status = adapter.getFileStatus("/vol/bucket/key",
URI_OFS, WORKING_DIR, "user", true);
+ assertEquals(10, status.getLength());
+ assertEquals(0, status.getBlockLocations().length);
+ }
+
+ @Test
+ public void fullStatusHasBlockLocations() throws IOException {
+ when(proxy.getOzoneFileStatus(anyString(), anyString(), anyString(),
eq(false)))
+ .thenReturn(fileStatusWithBlock(true));
+
+ FileStatusAdapter status = adapter.getFileStatus("/vol/bucket/key",
URI_OFS, WORKING_DIR, "user", false);
+ assertEquals(1, status.getBlockLocations().length);
+ assertArrayEquals(new String[] {"dn1"},
status.getBlockLocations()[0].getHosts());
+ }
+
@Test
public void keyPathThreadsHeadOp() throws IOException {
when(proxy.getOzoneFileStatus(anyString(), anyString(), anyString(),
anyBoolean()))
diff --git
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFileSystemHeadOp.java
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFileSystemHeadOp.java
index d0dd8084394..9c2eb58d994 100644
---
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFileSystemHeadOp.java
+++
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestOzoneFileSystemHeadOp.java
@@ -17,7 +17,10 @@
package org.apache.hadoop.fs.ozone;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -26,6 +29,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -33,6 +37,8 @@
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.fs.BlockLocation;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
@@ -48,6 +54,12 @@
*/
public class TestOzoneFileSystemHeadOp {
+ private static final BlockLocation[] BLOCKS = {
+ new BlockLocation(new String[] {"dn1:9859"}, new String[] {"dn1"}, 0,
100),
+ new BlockLocation(new String[] {"dn2:9859"}, new String[] {"dn2"}, 100,
100),
+ new BlockLocation(new String[] {"dn3:9859"}, new String[] {"dn3"}, 200,
50),
+ };
+
private BasicOzoneClientAdapterImpl adapter;
private BasicOzoneFileSystem fs;
@@ -110,11 +122,94 @@ public void isFileUsesHeadOp() throws IOException {
}
@Test
- public void fullGetFileStatusDoesNotUseHeadOp() throws IOException {
+ public void getFileStatusUsesHeadOp() throws IOException {
stubStatus(false);
- fs.getFileStatus(new Path("/file"));
+ FileStatus status = fs.getFileStatus(new Path("/file"));
+ assertFalse(status instanceof LocatedFileStatus);
verify(adapter).getFileStatus(anyString(), any(URI.class), any(Path.class),
- anyString(), eq(false));
+ anyString(), eq(true));
+ }
+
+ /** A 250 byte file: OM returns its block locations only for a full status.
*/
+ private void stubFileWithBlocks() throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
anyString(), anyBoolean()))
+ .thenAnswer(inv -> new FileStatusAdapter(250L, 750L,
inv.getArgument(2), false, (short) 3, 100L, 0L, 0L,
+ (short) 0, "user", "group", null, inv.<Boolean>getArgument(4) ?
new BlockLocation[0] : BLOCKS,
+ false, false));
+ }
+
+ private void verifyStatusCalls(int headOps, int fullStatuses) throws
IOException {
+ verify(adapter, times(headOps)).getFileStatus(anyString(), any(URI.class),
any(Path.class), anyString(),
+ eq(true));
+ verify(adapter, times(fullStatuses)).getFileStatus(anyString(),
any(URI.class), any(Path.class), anyString(),
+ eq(false));
+ }
+
+ @Test
+ public void getFileStatusKeepsAttributes() throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
anyString(), anyBoolean()))
+ .thenAnswer(inv -> new FileStatusAdapter(250L, 750L,
inv.getArgument(2), false, (short) 3, 100L, 7L, 7L,
+ (short) 0, "owner", "group", null, new BlockLocation[0], true,
true));
+
+ FileStatus status = fs.getFileStatus(new Path("/file"));
+ assertFalse(status instanceof LocatedFileStatus);
+ assertEquals(250, status.getLen());
+ assertEquals(7, status.getModificationTime());
+ assertEquals("owner", status.getOwner());
+ assertTrue(status.isFile());
+ assertTrue(status.isEncrypted());
+ assertTrue(status.isErasureCoded());
+ }
+
+ @Test
+ public void getFileBlockLocationsOfStatusFetchesLocations() throws
IOException {
+ stubFileWithBlocks();
+ FileStatus status = fs.getFileStatus(new Path("/file"));
+
+ assertThat(fs.getFileBlockLocations(status, 0,
250)).containsExactly(BLOCKS);
+ assertThat(fs.getFileBlockLocations(status, 150,
10)).containsExactly(BLOCKS[1]);
+ // like HDFS, an empty range returns the block containing start
+ assertThat(fs.getFileBlockLocations(status, 150,
0)).containsExactly(BLOCKS[1]);
+ verifyStatusCalls(1, 3);
+ }
+
+ @Test
+ public void getFileBlockLocationsOfPathRejectsInvalidRangeWithoutRpc()
throws IOException {
+ stubFileWithBlocks();
+ Path file = new Path("/file");
+
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(file, -1, 10));
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(file, 0, -1));
+ verifyStatusCalls(0, 0);
+ }
+
+ @Test
+ public void getFileBlockLocationsOfPathUsesOneRpc() throws IOException {
+ stubFileWithBlocks();
+
+ assertThat(fs.getFileBlockLocations(new Path("/file"), 100,
150)).containsExactly(BLOCKS[1], BLOCKS[2]);
+ verifyStatusCalls(0, 1);
+ }
+
+ @Test
+ public void getFileBlockLocationsWithoutBlocksInRangeDoesNotFetch() throws
IOException {
+ stubFileWithBlocks();
+ FileStatus status = fs.getFileStatus(new Path("/file"));
+ FileStatus empty = new FileStatus(0, false, 3, 100, 0, status.getPath());
+ FileStatus dir = new FileStatus(0, true, 0, 0, 0, status.getPath());
+
+ assertThat(fs.getFileBlockLocations(status, 250, 10)).isEmpty();
+ assertThat(fs.getFileBlockLocations(empty, 0, 10)).isEmpty();
+ assertThat(fs.getFileBlockLocations(dir, 0, 10)).isEmpty();
+ assertNull(fs.getFileBlockLocations((FileStatus) null, 0, 10));
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(status, -1, 10));
+ // a status from listStatus keeps the placeholder location, so a listing
that asks for the locations of every file
+ // (like Spark) does not pay an RPC per file
+ FileStatus listed = new FileStatus(250, false, 3, 100, 0,
status.getPath());
+ BlockLocation[] placeholder = fs.getFileBlockLocations(listed, 0, 250);
+ assertThat(placeholder).hasSize(1);
+ assertThat(placeholder[0].getHosts()).containsExactly("localhost");
+ verifyStatusCalls(1, 0);
}
@Test
diff --git
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestRootedOzoneFileSystemHeadOp.java
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestRootedOzoneFileSystemHeadOp.java
index ab844b4350a..9637e111ec5 100644
---
a/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestRootedOzoneFileSystemHeadOp.java
+++
b/hadoop-ozone/ozonefs-common/src/test/java/org/apache/hadoop/fs/ozone/TestRootedOzoneFileSystemHeadOp.java
@@ -17,7 +17,10 @@
package org.apache.hadoop.fs.ozone;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -28,12 +31,15 @@
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.times;
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.FileStatus;
+import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdds.conf.ConfigurationSource;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
@@ -50,6 +56,12 @@
*/
public class TestRootedOzoneFileSystemHeadOp {
+ private static final BlockLocation[] BLOCKS = {
+ new BlockLocation(new String[] {"dn1:9859"}, new String[] {"dn1"}, 0,
100),
+ new BlockLocation(new String[] {"dn2:9859"}, new String[] {"dn2"}, 100,
100),
+ new BlockLocation(new String[] {"dn3:9859"}, new String[] {"dn3"}, 200,
50),
+ };
+
private BasicRootedOzoneClientAdapterImpl adapter;
private BasicRootedOzoneFileSystem fs;
@@ -112,11 +124,94 @@ public void isFileUsesHeadOp() throws IOException {
}
@Test
- public void fullGetFileStatusDoesNotUseHeadOp() throws IOException {
+ public void getFileStatusUsesHeadOp() throws IOException {
stubStatus(false);
- fs.getFileStatus(new Path("/vol/bucket/file"));
+ FileStatus status = fs.getFileStatus(new Path("/vol/bucket/file"));
+ assertFalse(status instanceof LocatedFileStatus);
verify(adapter).getFileStatus(anyString(), any(URI.class), any(Path.class),
- anyString(), eq(false));
+ anyString(), eq(true));
+ }
+
+ /** A 250 byte file: OM returns its block locations only for a full status.
*/
+ private void stubFileWithBlocks() throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
anyString(), anyBoolean()))
+ .thenAnswer(inv -> new FileStatusAdapter(250L, 750L,
inv.getArgument(2), false, (short) 3, 100L, 0L, 0L,
+ (short) 0, "user", "group", null, inv.<Boolean>getArgument(4) ?
new BlockLocation[0] : BLOCKS,
+ false, false));
+ }
+
+ private void verifyStatusCalls(int headOps, int fullStatuses) throws
IOException {
+ verify(adapter, times(headOps)).getFileStatus(anyString(), any(URI.class),
any(Path.class), anyString(),
+ eq(true));
+ verify(adapter, times(fullStatuses)).getFileStatus(anyString(),
any(URI.class), any(Path.class), anyString(),
+ eq(false));
+ }
+
+ @Test
+ public void getFileStatusKeepsAttributes() throws IOException {
+ when(adapter.getFileStatus(anyString(), any(URI.class), any(Path.class),
anyString(), anyBoolean()))
+ .thenAnswer(inv -> new FileStatusAdapter(250L, 750L,
inv.getArgument(2), false, (short) 3, 100L, 7L, 7L,
+ (short) 0, "owner", "group", null, new BlockLocation[0], true,
true));
+
+ FileStatus status = fs.getFileStatus(new Path("/vol/bucket/file"));
+ assertFalse(status instanceof LocatedFileStatus);
+ assertEquals(250, status.getLen());
+ assertEquals(7, status.getModificationTime());
+ assertEquals("owner", status.getOwner());
+ assertTrue(status.isFile());
+ assertTrue(status.isEncrypted());
+ assertTrue(status.isErasureCoded());
+ }
+
+ @Test
+ public void getFileBlockLocationsOfStatusFetchesLocations() throws
IOException {
+ stubFileWithBlocks();
+ FileStatus status = fs.getFileStatus(new Path("/vol/bucket/file"));
+
+ assertThat(fs.getFileBlockLocations(status, 0,
250)).containsExactly(BLOCKS);
+ assertThat(fs.getFileBlockLocations(status, 150,
10)).containsExactly(BLOCKS[1]);
+ // like HDFS, an empty range returns the block containing start
+ assertThat(fs.getFileBlockLocations(status, 150,
0)).containsExactly(BLOCKS[1]);
+ verifyStatusCalls(1, 3);
+ }
+
+ @Test
+ public void getFileBlockLocationsOfPathRejectsInvalidRangeWithoutRpc()
throws IOException {
+ stubFileWithBlocks();
+ Path file = new Path("/vol/bucket/file");
+
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(file, -1, 10));
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(file, 0, -1));
+ verifyStatusCalls(0, 0);
+ }
+
+ @Test
+ public void getFileBlockLocationsOfPathUsesOneRpc() throws IOException {
+ stubFileWithBlocks();
+
+ assertThat(fs.getFileBlockLocations(new Path("/vol/bucket/file"), 100,
150)).containsExactly(BLOCKS[1], BLOCKS[2]);
+ verifyStatusCalls(0, 1);
+ }
+
+ @Test
+ public void getFileBlockLocationsWithoutBlocksInRangeDoesNotFetch() throws
IOException {
+ stubFileWithBlocks();
+ FileStatus status = fs.getFileStatus(new Path("/vol/bucket/file"));
+ FileStatus empty = new FileStatus(0, false, 3, 100, 0, status.getPath());
+ FileStatus dir = new FileStatus(0, true, 0, 0, 0, status.getPath());
+
+ assertThat(fs.getFileBlockLocations(status, 250, 10)).isEmpty();
+ assertThat(fs.getFileBlockLocations(empty, 0, 10)).isEmpty();
+ assertThat(fs.getFileBlockLocations(dir, 0, 10)).isEmpty();
+ assertNull(fs.getFileBlockLocations((FileStatus) null, 0, 10));
+ assertThrows(IllegalArgumentException.class, () ->
fs.getFileBlockLocations(status, -1, 10));
+ // a status from listStatus keeps the placeholder location, so a listing
that asks for the locations of every file
+ // (like Spark) does not pay an RPC per file
+ FileStatus listed = new FileStatus(250, false, 3, 100, 0,
status.getPath());
+ BlockLocation[] placeholder = fs.getFileBlockLocations(listed, 0, 250);
+ assertThat(placeholder).hasSize(1);
+ assertThat(placeholder[0].getHosts()).containsExactly("localhost");
+ verifyStatusCalls(1, 0);
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]