Skip to content

Commit f0ba16b

Browse files
authored
HDDS-14161. getBlockLocations returns null for zero-byte files (#9491)
1 parent ca1289b commit f0ba16b

5 files changed

Lines changed: 63 additions & 2 deletions

File tree

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractOzoneFileSystemTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,15 @@ public void testOzoneManagerListLocatedStatusAndListStatus() throws IOException
21432143
assertFalse(fileStatuses[0] instanceof LocatedFileStatus);
21442144
}
21452145

2146+
@Test
2147+
public void testOzoneManagerListLocatedStatusForZeroByteFile() throws IOException {
2148+
String directory = RandomStringUtils.secure().nextAlphanumeric(5);
2149+
String filePath = RandomStringUtils.secure().nextAlphanumeric(5);
2150+
Path path = createPath("/" + directory + "/" + filePath);
2151+
2152+
OzoneFileSystemTests.listLocatedStatusForZeroByteFile(fs, path);
2153+
}
2154+
21462155
@Test
21472156
void testOzoneManagerFileSystemInterface() throws IOException {
21482157
String dirPath = RandomStringUtils.secure().nextAlphanumeric(5);

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/AbstractRootedOzoneFileSystemTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ void testDeleteCreatesFakeParentDir() throws Exception {
404404
assertTrue(fs.delete(grandparent, true));
405405
}
406406

407+
@Test
408+
void testListLocatedStatusForZeroByteFile() throws Exception {
409+
Path parent = new Path(bucketPath, "testListLocatedStatusForZeroByteFile");
410+
Path path = new Path(parent, "key1");
411+
412+
try {
413+
OzoneFileSystemTests.listLocatedStatusForZeroByteFile(fs, path);
414+
} finally {
415+
// Cleanup
416+
fs.delete(parent, true);
417+
}
418+
}
419+
407420
@Test
408421
void testListStatus() throws Exception {
409422
Path parent = new Path(bucketPath, "testListStatus");

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/OzoneFileSystemTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_SCHEME;
2525
import static org.assertj.core.api.Assertions.assertThat;
2626
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2729

2830
import java.io.IOException;
2931
import java.net.URI;
3032
import java.util.Objects;
3133
import java.util.Set;
3234
import java.util.TreeSet;
35+
import org.apache.hadoop.fs.BlockLocation;
3336
import org.apache.hadoop.fs.FileStatus;
3437
import org.apache.hadoop.fs.FileSystem;
38+
import org.apache.hadoop.fs.LocatedFileStatus;
3539
import org.apache.hadoop.fs.Path;
3640
import org.apache.hadoop.fs.RemoteIterator;
3741
import org.apache.hadoop.fs.contract.ContractTestUtils;
@@ -125,4 +129,37 @@ static void createKeyWithECReplicationConfiguration(OzoneConfiguration inputConf
125129
ContractTestUtils.touch(fileSystem, keyPath);
126130
}
127131
}
132+
133+
public static void listLocatedStatusForZeroByteFile(FileSystem fs, Path path) throws IOException {
134+
// create empty file
135+
ContractTestUtils.touch(fs, path);
136+
137+
RemoteIterator<LocatedFileStatus> listLocatedStatus = fs.listLocatedStatus(path);
138+
int count = 0;
139+
140+
while (listLocatedStatus.hasNext()) {
141+
LocatedFileStatus locatedFileStatus = listLocatedStatus.next();
142+
assertEquals(0, locatedFileStatus.getLen());
143+
BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
144+
assertNotNull(blockLocations);
145+
assertEquals(0, blockLocations.length);
146+
147+
count++;
148+
}
149+
assertEquals(1, count);
150+
151+
count = 0;
152+
RemoteIterator<FileStatus> listStatus = fs.listStatusIterator(path);
153+
while (listStatus.hasNext()) {
154+
FileStatus fileStatus = listStatus.next();
155+
assertEquals(0, fileStatus.getLen());
156+
assertFalse(fileStatus instanceof LocatedFileStatus);
157+
count++;
158+
}
159+
assertEquals(1, count);
160+
161+
FileStatus[] fileStatuses = fs.listStatus(path.getParent());
162+
assertEquals(1, fileStatuses.length);
163+
assertFalse(fileStatuses[0] instanceof LocatedFileStatus);
164+
}
128165
}

hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicOzoneFileSystem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,8 @@ public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f)
954954
throws IOException {
955955
incrementCounter(Statistic.INVOCATION_LIST_LOCATED_STATUS);
956956
return new OzoneFileStatusIterator<>(f,
957-
(stat) -> stat instanceof LocatedFileStatus ? (LocatedFileStatus) stat : new LocatedFileStatus(stat, null),
957+
(stat) -> stat instanceof LocatedFileStatus ? (LocatedFileStatus) stat :
958+
new LocatedFileStatus(stat, stat.isFile() ? new BlockLocation[0] : null),
958959
false);
959960
}
960961

hadoop-ozone/ozonefs-common/src/main/java/org/apache/hadoop/fs/ozone/BasicRootedOzoneFileSystem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,8 @@ public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f)
11841184
throws IOException {
11851185
incrementCounter(Statistic.INVOCATION_LIST_LOCATED_STATUS);
11861186
return new OzoneFileStatusIterator<>(f,
1187-
(stat) -> stat instanceof LocatedFileStatus ? (LocatedFileStatus) stat : new LocatedFileStatus(stat, null),
1187+
(stat) -> stat instanceof LocatedFileStatus ? (LocatedFileStatus) stat :
1188+
new LocatedFileStatus(stat, stat.isFile() ? new BlockLocation[0] : null),
11881189
false);
11891190
}
11901191

0 commit comments

Comments
 (0)