Skip to content

Commit 147f833

Browse files
ZanderXuzengqiang.xu
authored andcommitted
HDFS-17457. [FGL] UTs support fine-grained locking (#6741)
1 parent 498df81 commit 147f833

File tree

40 files changed

+223
-161
lines changed

40 files changed

+223
-161
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterRpc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import org.apache.hadoop.hdfs.server.namenode.INodeDirectory;
122122
import org.apache.hadoop.hdfs.server.namenode.NameNode;
123123
import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter;
124+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
124125
import org.apache.hadoop.hdfs.server.namenode.snapshot.SnapshotTestHelper;
125126
import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations;
126127
import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations.BlockWithLocations;
@@ -1698,10 +1699,10 @@ public void testGetReplicatedBlockStats() throws Exception {
16981699
// mark a replica as corrupt
16991700
LocatedBlock block = NameNodeAdapter
17001701
.getBlockLocations(nameNode, testFile, 0, 1024).get(0);
1701-
namesystem.writeLock();
1702+
namesystem.writeLock(FSNamesystemLockMode.BM);
17021703
bm.findAndMarkBlockAsCorrupt(block.getBlock(), block.getLocations()[0],
17031704
"STORAGE_ID", "TEST");
1704-
namesystem.writeUnlock();
1705+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "findAndMarkBlockAsCorrupt");
17051706
BlockManagerTestUtil.updateState(bm);
17061707
DFSTestUtil.waitCorruptReplicas(fileSystem, namesystem,
17071708
new Path(testFile), block.getBlock(), 1);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.hadoop.hdfs.server.datanode.DataNode;
3636
import org.apache.hadoop.hdfs.server.datanode.DataNodeTestUtils;
3737
import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter;
38+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
3839
import org.junit.After;
3940
import org.junit.Test;
4041

@@ -175,7 +176,7 @@ public void testScheduledBlocksCounterDecrementOnDeletedBlock()
175176
.getBlockLocations(cluster.getNameNode(), filePath.toString(), 0, 1)
176177
.get(0);
177178
DatanodeInfo[] locs = block.getLocations();
178-
cluster.getNamesystem().writeLock();
179+
cluster.getNamesystem().writeLock(FSNamesystemLockMode.BM);
179180
try {
180181
bm.findAndMarkBlockAsCorrupt(block.getBlock(), locs[0], "STORAGE_ID",
181182
"TEST");
@@ -185,7 +186,8 @@ public void testScheduledBlocksCounterDecrementOnDeletedBlock()
185186
BlockManagerTestUtil.updateState(bm);
186187
assertEquals(1L, bm.getPendingReconstructionBlocksCount());
187188
} finally {
188-
cluster.getNamesystem().writeUnlock();
189+
cluster.getNamesystem().writeUnlock(FSNamesystemLockMode.BM,
190+
"findAndMarkBlockAsCorrupt");
189191
}
190192

191193
// 4. delete the file
@@ -238,13 +240,14 @@ public void testBlocksScheduledCounterOnTruncate() throws Exception {
238240
DataNodeTestUtils.setHeartbeatsDisabledForTests(dn, true);
239241
}
240242

241-
cluster.getNamesystem().writeLock();
243+
cluster.getNamesystem().writeLock(FSNamesystemLockMode.BM);
242244
try {
243245
BlockManagerTestUtil.computeAllPendingWork(bm);
244246
BlockManagerTestUtil.updateState(bm);
245247
assertEquals(1L, bm.getPendingReconstructionBlocksCount());
246248
} finally {
247-
cluster.getNamesystem().writeUnlock();
249+
cluster.getNamesystem().writeUnlock(FSNamesystemLockMode.BM,
250+
"testBlocksScheduledCounterOnTruncate");
248251
}
249252

250253
// 5.truncate the file whose block exists in pending reconstruction

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.hadoop.hdfs.server.datanode.InternalDataNodeTestUtils;
5252
import org.apache.hadoop.hdfs.server.namenode.FSNamesystem;
5353
import org.apache.hadoop.hdfs.server.namenode.NameNode;
54+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
5455
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
5556
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
5657
import org.apache.hadoop.test.GenericTestUtils;
@@ -159,13 +160,13 @@ public void testArrayOutOfBoundsException() throws Exception {
159160
DatanodeRegistration dnR = InternalDataNodeTestUtils.
160161
getDNRegistrationForBP(dataNode, blk.getBlockPoolId());
161162
FSNamesystem ns = cluster.getNamesystem();
162-
ns.writeLock();
163+
ns.writeLock(FSNamesystemLockMode.BM);
163164
try {
164165
cluster.getNamesystem().getBlockManager().findAndMarkBlockAsCorrupt(blk,
165166
new DatanodeInfoBuilder().setNodeID(dnR).build(), "TEST",
166167
"STORAGE_ID");
167168
} finally {
168-
ns.writeUnlock();
169+
ns.writeUnlock(FSNamesystemLockMode.BM, "testArrayOutOfBoundsException");
169170
}
170171

171172
// open the file
@@ -210,16 +211,16 @@ public void testCorruptionWithDiskFailure() throws Exception {
210211
FSNamesystem ns = cluster.getNamesystem();
211212
//fail the storage on that node which has the block
212213
try {
213-
ns.writeLock();
214+
ns.writeLock(FSNamesystemLockMode.BM);
214215
updateAllStorages(bm);
215216
} finally {
216-
ns.writeUnlock();
217+
ns.writeUnlock(FSNamesystemLockMode.BM, "testCorruptionWithDiskFailure");
217218
}
218-
ns.writeLock();
219+
ns.writeLock(FSNamesystemLockMode.BM);
219220
try {
220221
markAllBlocksAsCorrupt(bm, blk);
221222
} finally {
222-
ns.writeUnlock();
223+
ns.writeUnlock(FSNamesystemLockMode.BM, "testCorruptionWithDiskFailure");
223224
}
224225

225226
// open the file

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hadoop.hdfs.server.blockmanagement.BlockManagerSafeMode.BMSafeModeStatus;
3232
import org.apache.hadoop.hdfs.server.namenode.FSNamesystem;
3333
import org.apache.hadoop.hdfs.server.namenode.NameNode;
34+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
3435
import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
3536
import org.apache.hadoop.hdfs.server.protocol.StorageReport;
3637
import org.apache.hadoop.test.Whitebox;
@@ -50,23 +51,23 @@ public static void setNodeReplicationLimit(final BlockManager blockManager,
5051
/** @return the datanode descriptor for the given the given storageID. */
5152
public static DatanodeDescriptor getDatanode(final FSNamesystem ns,
5253
final String storageID) {
53-
ns.readLock();
54+
ns.readLock(FSNamesystemLockMode.BM);
5455
try {
5556
return ns.getBlockManager().getDatanodeManager().getDatanode(storageID);
5657
} finally {
57-
ns.readUnlock();
58+
ns.readUnlock(FSNamesystemLockMode.BM, "getDatanode");
5859
}
5960
}
6061

6162
public static Iterator<BlockInfo> getBlockIterator(final FSNamesystem ns,
6263
final String storageID, final int startBlock) {
63-
ns.readLock();
64+
ns.readLock(FSNamesystemLockMode.BM);
6465
try {
6566
DatanodeDescriptor dn =
6667
ns.getBlockManager().getDatanodeManager().getDatanode(storageID);
6768
return dn.getBlockIterator(startBlock);
6869
} finally {
69-
ns.readUnlock();
70+
ns.readUnlock(FSNamesystemLockMode.BM, "getBlockIterator");
7071
}
7172
}
7273

@@ -88,15 +89,15 @@ public static void updateState(final BlockManager blockManager) {
8889
*/
8990
public static int[] getReplicaInfo(final FSNamesystem namesystem, final Block b) {
9091
final BlockManager bm = namesystem.getBlockManager();
91-
namesystem.readLock();
92+
namesystem.readLock(FSNamesystemLockMode.BM);
9293
try {
9394
final BlockInfo storedBlock = bm.getStoredBlock(b);
9495
return new int[]{getNumberOfRacks(bm, b),
9596
bm.countNodes(storedBlock).liveReplicas(),
9697
bm.neededReconstruction.contains(storedBlock) ? 1 : 0,
9798
getNumberOfDomains(bm, b)};
9899
} finally {
99-
namesystem.readUnlock();
100+
namesystem.readUnlock(FSNamesystemLockMode.BM, "getReplicaInfo");
100101
}
101102
}
102103

@@ -247,7 +248,7 @@ public static int computeAllPendingWork(BlockManager bm) {
247248
*/
248249
public static void noticeDeadDatanode(NameNode nn, String dnName) {
249250
FSNamesystem namesystem = nn.getNamesystem();
250-
namesystem.writeLock();
251+
namesystem.writeLock(FSNamesystemLockMode.BM);
251252
try {
252253
DatanodeManager dnm = namesystem.getBlockManager().getDatanodeManager();
253254
HeartbeatManager hbm = dnm.getHeartbeatManager();
@@ -265,7 +266,7 @@ public static void noticeDeadDatanode(NameNode nn, String dnName) {
265266
hbm.heartbeatCheck();
266267
}
267268
} finally {
268-
namesystem.writeUnlock();
269+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "noticeDeadDatanode");
269270
}
270271
}
271272

@@ -302,12 +303,13 @@ public static void checkHeartbeat(BlockManager bm) {
302303
*/
303304
public static int checkHeartbeatAndGetUnderReplicatedBlocksCount(
304305
FSNamesystem namesystem, BlockManager bm) {
305-
namesystem.writeLock();
306+
namesystem.writeLock(FSNamesystemLockMode.BM);
306307
try {
307308
bm.getDatanodeManager().getHeartbeatManager().heartbeatCheck();
308309
return bm.getUnderReplicatedNotMissingBlocks();
309310
} finally {
310-
namesystem.writeUnlock();
311+
namesystem.writeUnlock(FSNamesystemLockMode.BM,
312+
"checkHeartbeatAndGetUnderReplicatedBlocksCount");
311313
}
312314
}
313315

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ public void testBlockManagerMachinesArray() throws Exception {
16241624
}
16251625
failedStorageDataNode.updateHeartbeat(reports.toArray(StorageReport
16261626
.EMPTY_ARRAY), 0L, 0L, 0, 0, null);
1627-
ns.writeLock();
1627+
ns.writeLock(FSNamesystemLockMode.BM);
16281628
DatanodeStorageInfo corruptStorageInfo= null;
16291629
for(int i=0; i<corruptStorageDataNode.getStorageInfos().length; i++) {
16301630
corruptStorageInfo = corruptStorageDataNode.getStorageInfos()[i];
@@ -1638,16 +1638,16 @@ public void testBlockManagerMachinesArray() throws Exception {
16381638
blockManager.findAndMarkBlockAsCorrupt(blk, corruptStorageDataNode,
16391639
corruptStorageInfo.getStorageID(),
16401640
CorruptReplicasMap.Reason.ANY.toString());
1641-
ns.writeUnlock();
1641+
ns.writeUnlock(FSNamesystemLockMode.BM, "testBlockManagerMachinesArray");
16421642
BlockInfo[] blockInfos = new BlockInfo[] {blockInfo};
1643-
ns.readLock();
1643+
ns.readLock(FSNamesystemLockMode.BM);
16441644
LocatedBlocks locatedBlocks =
16451645
blockManager.createLocatedBlocks(blockInfos, 3L, false, 0L, 3L,
16461646
false, false, null, null);
16471647
assertTrue("Located Blocks should exclude corrupt" +
16481648
"replicas and failed storages",
16491649
locatedBlocks.getLocatedBlocks().size() == 1);
1650-
ns.readUnlock();
1650+
ns.readUnlock(FSNamesystemLockMode.BM, "open");
16511651
} finally {
16521652
if (cluster != null) {
16531653
cluster.shutdown();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.concurrent.TimeoutException;
2525
import org.apache.hadoop.hdfs.server.datanode.InternalDataNodeTestUtils;
26+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
2627
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
@@ -602,11 +603,11 @@ public void testMultipleReplicasScheduledForUpgradeDomain() throws Exception {
602603

603604
static BlockReconstructionWork scheduleReconstruction(
604605
FSNamesystem fsn, BlockInfo block, int priority) {
605-
fsn.writeLock();
606+
fsn.writeLock(FSNamesystemLockMode.BM);
606607
try {
607608
return fsn.getBlockManager().scheduleReconstruction(block, priority);
608609
} finally {
609-
fsn.writeUnlock();
610+
fsn.writeUnlock(FSNamesystemLockMode.BM, "scheduleReconstruction");
610611
}
611612
}
612613

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.hadoop.hdfs.server.common.StorageInfo;
4141
import org.apache.hadoop.hdfs.server.datanode.DataNode;
4242
import org.apache.hadoop.hdfs.server.namenode.FSNamesystem;
43+
import org.apache.hadoop.hdfs.server.namenode.fgl.FSNamesystemLockMode;
4344
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
4445
import org.apache.hadoop.test.Whitebox;
4546
import org.apache.hadoop.util.VersionInfo;
@@ -131,7 +132,7 @@ private void verifyInvalidationWorkCounts(int blockInvalidateLimit) {
131132
public void testComputeInvalidateReplicas() throws Exception {
132133
final int blockInvalidateLimit = bm.getDatanodeManager()
133134
.getBlockInvalidateLimit();
134-
namesystem.writeLock();
135+
namesystem.writeLock(FSNamesystemLockMode.BM);
135136
try {
136137
for (int i=0; i<nodes.length; i++) {
137138
for(int j=0; j<3*blockInvalidateLimit+1; j++) {
@@ -142,7 +143,7 @@ public void testComputeInvalidateReplicas() throws Exception {
142143
}
143144
verifyInvalidationWorkCounts(blockInvalidateLimit);
144145
} finally {
145-
namesystem.writeUnlock();
146+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testComputeInvalidateReplicas");
146147
}
147148
}
148149

@@ -154,7 +155,7 @@ public void testComputeInvalidateReplicas() throws Exception {
154155
public void testComputeInvalidateStripedBlockGroups() throws Exception {
155156
final int blockInvalidateLimit =
156157
bm.getDatanodeManager().getBlockInvalidateLimit();
157-
namesystem.writeLock();
158+
namesystem.writeLock(FSNamesystemLockMode.BM);
158159
try {
159160
int nodeCount = ecPolicy.getNumDataUnits() + ecPolicy.getNumParityUnits();
160161
for (int i = 0; i < nodeCount; i++) {
@@ -167,7 +168,7 @@ public void testComputeInvalidateStripedBlockGroups() throws Exception {
167168
}
168169
verifyInvalidationWorkCounts(blockInvalidateLimit);
169170
} finally {
170-
namesystem.writeUnlock();
171+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testComputeInvalidateStripedBlockGroups");
171172
}
172173
}
173174

@@ -181,7 +182,7 @@ public void testComputeInvalidate() throws Exception {
181182
final int blockInvalidateLimit =
182183
bm.getDatanodeManager().getBlockInvalidateLimit();
183184
final Random random = new Random(System.currentTimeMillis());
184-
namesystem.writeLock();
185+
namesystem.writeLock(FSNamesystemLockMode.BM);
185186
try {
186187
int nodeCount = ecPolicy.getNumDataUnits() + ecPolicy.getNumParityUnits();
187188
for (int i = 0; i < nodeCount; i++) {
@@ -201,7 +202,7 @@ public void testComputeInvalidate() throws Exception {
201202
}
202203
verifyInvalidationWorkCounts(blockInvalidateLimit);
203204
} finally {
204-
namesystem.writeUnlock();
205+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testComputeInvalidate");
205206
}
206207
}
207208

@@ -212,7 +213,7 @@ public void testComputeInvalidate() throws Exception {
212213
*/
213214
@Test(timeout=120000)
214215
public void testDatanodeReformat() throws Exception {
215-
namesystem.writeLock();
216+
namesystem.writeLock(FSNamesystemLockMode.BM);
216217
try {
217218
// Change the datanode UUID to emulate a reformat
218219
String poolId = cluster.getNamesystem().getBlockPoolId();
@@ -234,7 +235,7 @@ public void testDatanodeReformat() throws Exception {
234235
assertEquals(0, bm.computeInvalidateWork(1));
235236
assertEquals(0, bm.getPendingDeletionBlocksCount());
236237
} finally {
237-
namesystem.writeUnlock();
238+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testDatanodeReformat");
238239
}
239240
}
240241

@@ -255,7 +256,7 @@ public void testDatanodeReRegistration() throws Exception {
255256
dfs.delete(ecFile, false);
256257
BlockManagerTestUtil.waitForMarkedDeleteQueueIsEmpty(
257258
cluster.getNamesystem(0).getBlockManager());
258-
namesystem.writeLock();
259+
namesystem.writeLock(FSNamesystemLockMode.BM);
259260
InvalidateBlocks invalidateBlocks;
260261
int totalStripedDataBlocks = totalBlockGroups * (ecPolicy.getNumDataUnits()
261262
+ ecPolicy.getNumParityUnits());
@@ -272,7 +273,7 @@ public void testDatanodeReRegistration() throws Exception {
272273
assertEquals("Unexpected invalidate count for striped block groups!",
273274
totalStripedDataBlocks, invalidateBlocks.getECBlocks());
274275
} finally {
275-
namesystem.writeUnlock();
276+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testDatanodeReRegistration");
276277
}
277278
// Re-register each DN and see that it wipes the invalidation work
278279
int totalBlockGroupsPerDataNode = totalBlockGroups;
@@ -284,14 +285,14 @@ public void testDatanodeReRegistration() throws Exception {
284285
new StorageInfo(HdfsServerConstants.NodeType.DATA_NODE),
285286
new ExportedBlockKeys(),
286287
VersionInfo.getVersion());
287-
namesystem.writeLock();
288+
namesystem.writeLock(FSNamesystemLockMode.BM);
288289
try {
289290
bm.getDatanodeManager().registerDatanode(reg);
290291
expected -= (totalReplicasPerDataNode + totalBlockGroupsPerDataNode);
291292
assertEquals("Expected number of invalidate blocks to decrease",
292293
(long) expected, invalidateBlocks.numBlocks());
293294
} finally {
294-
namesystem.writeUnlock();
295+
namesystem.writeUnlock(FSNamesystemLockMode.BM, "testDatanodeReRegistration");
295296
}
296297
}
297298
}

0 commit comments

Comments
 (0)