Skip to content

Commit 405b65a

Browse files
liubingxingBogdan Stolojan
authored andcommitted
HDFS-16352. return the real datanode numBlocks in #getDatanodeStorageReport (apache#3714). Contributed by liubingxing.
Signed-off-by: He Xiaoqiao <[email protected]> (cherry picked from commit d8dea6f)
1 parent 9b173ee commit 405b65a

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/protocol/DatanodeInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,10 @@ public static class DatanodeInfoBuilder {
698698
private long nonDfsUsed = 0L;
699699
private long lastBlockReportTime = 0L;
700700
private long lastBlockReportMonotonic = 0L;
701-
private int numBlocks;
702-
701+
private int numBlocks = 0;
703702

703+
// Please use setNumBlocks explicitly to set numBlocks as this method doesn't have
704+
// sufficient info about numBlocks
704705
public DatanodeInfoBuilder setFrom(DatanodeInfo from) {
705706
this.capacity = from.getCapacity();
706707
this.dfsUsed = from.getDfsUsed();
@@ -717,7 +718,6 @@ public DatanodeInfoBuilder setFrom(DatanodeInfo from) {
717718
this.upgradeDomain = from.getUpgradeDomain();
718719
this.lastBlockReportTime = from.getLastBlockReportTime();
719720
this.lastBlockReportMonotonic = from.getLastBlockReportMonotonic();
720-
this.numBlocks = from.getNumBlocks();
721721
setNodeID(from);
722722
return this;
723723
}

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/DatanodeManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,8 @@ public DatanodeStorageReport[] getDatanodeStorageReport(
21822182
for (int i = 0; i < reports.length; i++) {
21832183
final DatanodeDescriptor d = datanodes.get(i);
21842184
reports[i] = new DatanodeStorageReport(
2185-
new DatanodeInfoBuilder().setFrom(d).build(), d.getStorageReports());
2185+
new DatanodeInfoBuilder().setFrom(d).setNumBlocks(d.numBlocks()).build(),
2186+
d.getStorageReports());
21862187
}
21872188
return reports;
21882189
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestNameNodeRpcServerMethods.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
import java.io.IOException;
2121

2222
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.hadoop.fs.FSDataOutputStream;
24+
import org.apache.hadoop.fs.Path;
2325
import org.apache.hadoop.fs.UnresolvedLinkException;
26+
import org.apache.hadoop.hdfs.DistributedFileSystem;
2427
import org.apache.hadoop.hdfs.HdfsConfiguration;
2528
import org.apache.hadoop.hdfs.MiniDFSCluster;
29+
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
30+
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorageReport;
2631
import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocols;
2732
import org.apache.hadoop.security.AccessControlException;
2833
import org.apache.hadoop.test.GenericTestUtils;
@@ -31,6 +36,8 @@
3136
import org.junit.Before;
3237
import org.junit.Test;
3338

39+
import static org.junit.Assert.assertEquals;
40+
3441
public class TestNameNodeRpcServerMethods {
3542
private static NamenodeProtocols nnRpc;
3643
private static Configuration conf;
@@ -83,4 +90,27 @@ public void testDeleteSnapshotWhenSnapshotNameIsEmpty() throws Exception {
8390

8491
}
8592

93+
@Test
94+
public void testGetDatanodeStorageReportWithNumBLocksNotZero() throws Exception {
95+
int buffSize = 1024;
96+
long blockSize = 1024 * 1024;
97+
String file = "/testFile";
98+
DistributedFileSystem dfs = cluster.getFileSystem();
99+
FSDataOutputStream outputStream = dfs.create(
100+
new Path(file), true, buffSize, (short)1, blockSize);
101+
byte[] outBuffer = new byte[buffSize];
102+
for (int i = 0; i < buffSize; i++) {
103+
outBuffer[i] = (byte) (i & 0x00ff);
104+
}
105+
outputStream.write(outBuffer);
106+
outputStream.close();
107+
108+
int numBlocks = 0;
109+
DatanodeStorageReport[] reports
110+
= nnRpc.getDatanodeStorageReport(HdfsConstants.DatanodeReportType.ALL);
111+
for (DatanodeStorageReport r : reports) {
112+
numBlocks += r.getDatanodeInfo().getNumBlocks();
113+
}
114+
assertEquals(1, numBlocks);
115+
}
86116
}

0 commit comments

Comments
 (0)