Skip to content

Commit 844b766

Browse files
committed
HDFS-14889. Ability to check if a block has a replica on provided storage. Contributed by Ashvin Agrawal. (#1573)"
1 parent 9700e20 commit 844b766

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ public int getCapacity() {
181181
/** @return true if there is no datanode storage associated with the block */
182182
abstract boolean hasNoStorage();
183183

184+
/**
185+
* Checks whether this block has a Provided replica.
186+
* @return true if this block has a replica on Provided storage.
187+
*/
188+
abstract boolean isProvided();
189+
184190
/**
185191
* Find specified DatanodeStorageInfo.
186192
* @return DatanodeStorageInfo or null if not found.

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.base.Preconditions;
2121
import org.apache.hadoop.classification.InterfaceAudience;
22+
import org.apache.hadoop.fs.StorageType;
2223
import org.apache.hadoop.hdfs.protocol.Block;
2324
import org.apache.hadoop.hdfs.protocol.BlockType;
2425

@@ -80,6 +81,19 @@ boolean removeStorage(DatanodeStorageInfo storage) {
8081
return true;
8182
}
8283

84+
@Override
85+
boolean isProvided() {
86+
int len = getCapacity();
87+
for (int idx = 0; idx < len; idx++) {
88+
DatanodeStorageInfo storage = getStorageInfo(idx);
89+
if (storage != null
90+
&& storage.getStorageType().equals(StorageType.PROVIDED)) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
8397
@Override
8498
public int numNodes() {
8599
assert this.storages != null : "BlockInfo is not initialized";

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ final boolean hasNoStorage() {
244244
return true;
245245
}
246246

247+
/**
248+
* Striped blocks on Provided Storage is not supported. All blocks on
249+
* Provided storage are assumed to be "contiguous".
250+
*/
251+
@Override
252+
boolean isProvided() {
253+
return false;
254+
}
255+
247256
/**
248257
* This class contains datanode storage information and block index in the
249258
* block group.

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockInfo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
import static org.apache.hadoop.hdfs.server.namenode.INodeId.INVALID_INODE_ID;
2121
import static org.hamcrest.core.Is.is;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.when;
2224

25+
import org.apache.hadoop.fs.StorageType;
2326
import org.slf4j.Logger;
2427
import org.slf4j.LoggerFactory;
2528
import org.apache.hadoop.hdfs.DFSTestUtil;
@@ -64,6 +67,39 @@ public void testAddStorage() throws Exception {
6467
Assert.assertEquals(storage, blockInfo.getStorageInfo(0));
6568
}
6669

70+
@Test
71+
public void testAddProvidedStorage() throws Exception {
72+
// block with only provided storage
73+
BlockInfo blockInfo = new BlockInfoContiguous((short) 3);
74+
DatanodeStorageInfo providedStorage = mock(DatanodeStorageInfo.class);
75+
when(providedStorage.getStorageType()).thenReturn(StorageType.PROVIDED);
76+
boolean added = blockInfo.addStorage(providedStorage, blockInfo);
77+
Assert.assertTrue(added);
78+
Assert.assertEquals(providedStorage, blockInfo.getStorageInfo(0));
79+
Assert.assertTrue(blockInfo.isProvided());
80+
}
81+
82+
@Test
83+
public void testAddTwoStorageTypes() throws Exception {
84+
// block with only disk storage
85+
BlockInfo blockInfo = new BlockInfoContiguous((short) 3);
86+
DatanodeStorageInfo diskStorage = mock(DatanodeStorageInfo.class);
87+
DatanodeDescriptor mockDN = mock(DatanodeDescriptor.class);
88+
when(diskStorage.getDatanodeDescriptor()).thenReturn(mockDN);
89+
when(diskStorage.getStorageType()).thenReturn(StorageType.DISK);
90+
boolean added = blockInfo.addStorage(diskStorage, blockInfo);
91+
Assert.assertTrue(added);
92+
Assert.assertEquals(diskStorage, blockInfo.getStorageInfo(0));
93+
Assert.assertFalse(blockInfo.isProvided());
94+
95+
// now add provided storage
96+
DatanodeStorageInfo providedStorage = mock(DatanodeStorageInfo.class);
97+
when(providedStorage.getStorageType()).thenReturn(StorageType.PROVIDED);
98+
added = blockInfo.addStorage(providedStorage, blockInfo);
99+
Assert.assertTrue(added);
100+
Assert.assertTrue(blockInfo.isProvided());
101+
}
102+
67103
@Test
68104
public void testReplaceStorage() throws Exception {
69105

0 commit comments

Comments
 (0)