Skip to content

Commit 43b113d

Browse files
committed
HDFS-15574. Remove unnecessary sort of block list in DirectoryScanner. Contributed by Stephen O'Donnell.
(cherry picked from commit aa582cc)
1 parent 3c932dd commit 43b113d

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DirectoryScanner.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.FilenameFilter;
2323
import java.io.IOException;
2424
import java.util.Arrays;
25-
import java.util.Collections;
2625
import java.util.HashMap;
2726
import java.util.LinkedList;
2827
import java.util.List;
@@ -404,9 +403,8 @@ private void scan() {
404403
diffs.put(bpid, diffRecord);
405404

406405
statsRecord.totalBlocks = blockpoolReport.length;
407-
final List<ReplicaInfo> bl = dataset.getFinalizedBlocks(bpid);
408-
Collections.sort(bl); // Sort based on blockId
409-
406+
final List<ReplicaInfo> bl = dataset.getSortedFinalizedBlocks(bpid);
407+
410408
int d = 0; // index for blockpoolReport
411409
int m = 0; // index for memReprot
412410
while (m < bl.size() && d < blockpoolReport.length) {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/FsDatasetSpi.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,17 @@ StorageReport[] getStorageReports(String bpid)
237237
VolumeFailureSummary getVolumeFailureSummary();
238238

239239
/**
240-
* Gets a list of references to the finalized blocks for the given block pool.
240+
* Gets a sorted list of references to the finalized blocks for the given
241+
* block pool. The list is sorted by blockID.
241242
* <p>
242243
* Callers of this function should call
243244
* {@link FsDatasetSpi#acquireDatasetLock} to avoid blocks' status being
244245
* changed during list iteration.
245246
* </p>
246247
* @return a list of references to the finalized blocks for the given block
247-
* pool.
248+
* pool. The list is sorted by blockID.
248249
*/
249-
List<ReplicaInfo> getFinalizedBlocks(String bpid);
250+
List<ReplicaInfo> getSortedFinalizedBlocks(String bpid);
250251

251252
/**
252253
* Check whether the in-memory block record matches the block on the disk,

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,17 +1918,18 @@ public Map<DatanodeStorage, BlockListAsLongs> getBlockReports(String bpid) {
19181918
}
19191919

19201920
/**
1921-
* Gets a list of references to the finalized blocks for the given block pool.
1921+
* Gets a list of references to the finalized blocks for the given block pool,
1922+
* sorted by blockID.
19221923
* <p>
19231924
* Callers of this function should call
19241925
* {@link FsDatasetSpi#acquireDatasetLock} to avoid blocks' status being
19251926
* changed during list iteration.
19261927
* </p>
19271928
* @return a list of references to the finalized blocks for the given block
1928-
* pool.
1929+
* pool. The list is sorted by blockID.
19291930
*/
19301931
@Override
1931-
public List<ReplicaInfo> getFinalizedBlocks(String bpid) {
1932+
public List<ReplicaInfo> getSortedFinalizedBlocks(String bpid) {
19321933
try (AutoCloseableLock lock = datasetLock.acquire()) {
19331934
final List<ReplicaInfo> finalized = new ArrayList<ReplicaInfo>(
19341935
volumeMap.size(bpid));

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestCrcCorruption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void thistest(Configuration conf, DFSTestUtil util) throws Exception {
173173
final DataNode dn = cluster.getDataNodes().get(dnIdx);
174174
final String bpid = cluster.getNamesystem().getBlockPoolId();
175175
List<ReplicaInfo> replicas =
176-
dn.getFSDataset().getFinalizedBlocks(bpid);
176+
dn.getFSDataset().getSortedFinalizedBlocks(bpid);
177177
assertTrue("Replicas do not exist", !replicas.isEmpty());
178178

179179
for (int idx = 0; idx < replicas.size(); idx++) {

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestReconstructStripedFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ private void testErasureCodingWorkerXmitsWeight(
539539
writeFile(fs, "/ec-xmits-weight", fileLen);
540540

541541
DataNode dn = cluster.getDataNodes().get(0);
542-
int corruptBlocks = dn.getFSDataset().getFinalizedBlocks(
542+
int corruptBlocks = dn.getFSDataset().getSortedFinalizedBlocks(
543543
cluster.getNameNode().getNamesystem().getBlockPoolId()).size();
544544
int expectedXmits = corruptBlocks * expectedWeight;
545545

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/SimulatedFSDataset.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,7 @@ public StorageReport[] getStorageReports(String bpid) {
15021502
}
15031503

15041504
@Override
1505-
public List<ReplicaInfo> getFinalizedBlocks(String bpid) {
1505+
public List<ReplicaInfo> getSortedFinalizedBlocks(String bpid) {
15061506
throw new UnsupportedOperationException();
15071507
}
15081508

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/extdataset/ExternalDatasetImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Map<String, Object> getVolumeInfoMap() {
9090
}
9191

9292
@Override
93-
public List<ReplicaInfo> getFinalizedBlocks(String bpid) {
93+
public List<ReplicaInfo> getSortedFinalizedBlocks(String bpid) {
9494
return null;
9595
}
9696

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import java.io.Writer;
8181
import java.nio.charset.StandardCharsets;
8282
import java.util.ArrayList;
83+
import java.util.Random;
8384
import java.util.concurrent.CountDownLatch;
8485
import java.util.HashSet;
8586
import java.util.List;
@@ -470,6 +471,41 @@ public void testAddVolumeFailureReleasesInUseLock() throws IOException {
470471

471472
FsDatasetTestUtil.assertFileLockReleased(badDir.toString());
472473
}
474+
475+
@Test
476+
/**
477+
* This test is here primarily to catch any case where the datanode replica
478+
* map structure is changed to a new structure which is not sorted and hence
479+
* reading the blocks from it directly would not be sorted.
480+
*/
481+
public void testSortedFinalizedBlocksAreSorted() throws IOException {
482+
this.conf = new HdfsConfiguration();
483+
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();
484+
try {
485+
cluster.waitActive();
486+
DataNode dn = cluster.getDataNodes().get(0);
487+
488+
FsDatasetSpi<?> ds = DataNodeTestUtils.getFSDataset(dn);
489+
ds.addBlockPool(BLOCKPOOL, conf);
490+
491+
// Load 1000 blocks with random blockIDs
492+
for (int i=0; i<=1000; i++) {
493+
ExtendedBlock eb = new ExtendedBlock(
494+
BLOCKPOOL, new Random().nextInt(), 1000, 1000 + i);
495+
cluster.getFsDatasetTestUtils(0).createFinalizedReplica(eb);
496+
}
497+
// Get the sorted blocks and validate the arrayList is sorted
498+
List<ReplicaInfo> replicaList = ds.getSortedFinalizedBlocks(BLOCKPOOL);
499+
for (int i=0; i<replicaList.size() - 1; i++) {
500+
if (replicaList.get(i).compareTo(replicaList.get(i+1)) > 0) {
501+
// Not sorted so fail the test
502+
fail("ArrayList is not sorted, and it should be");
503+
}
504+
}
505+
} finally {
506+
cluster.shutdown();
507+
}
508+
}
473509

474510
@Test
475511
public void testDeletingBlocks() throws IOException {

0 commit comments

Comments
 (0)