Skip to content

Commit 9434c1e

Browse files
authored
HDFS-15818. Fix TestFsDatasetImpl.testReadLockCanBeDisabledByConfig. Contributed by Leon Gao (apache#2679)
1 parent 2df2dfb commit 9434c1e

File tree

1 file changed

+25
-26
lines changed
  • hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl

1 file changed

+25
-26
lines changed

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
package org.apache.hadoop.hdfs.server.datanode.fsdataset.impl;
1919

2020
import java.io.InputStream;
21-
import java.util.Arrays;
22-
import java.util.Collections;
21+
import java.util.concurrent.TimeoutException;
2322
import java.util.function.Supplier;
2423

2524
import org.apache.hadoop.fs.DF;
@@ -227,7 +226,6 @@ public void setUp() throws IOException {
227226
assertEquals(NUM_INIT_VOLUMES, getNumVolumes());
228227
assertEquals(0, dataset.getNumFailedVolumes());
229228
}
230-
231229
@Test(timeout=10000)
232230
public void testReadLockEnabledByDefault()
233231
throws Exception {
@@ -269,11 +267,12 @@ public void run() {
269267
waiter.join();
270268
// The holder thread is still holding the lock, but the waiter can still
271269
// run as the lock is a shared read lock.
270+
// Otherwise test will timeout with deadlock.
272271
assertEquals(true, accessed.get());
273272
holder.interrupt();
274273
}
275274

276-
@Test(timeout=10000)
275+
@Test(timeout=20000)
277276
public void testReadLockCanBeDisabledByConfig()
278277
throws Exception {
279278
HdfsConfiguration conf = new HdfsConfiguration();
@@ -282,58 +281,58 @@ public void testReadLockCanBeDisabledByConfig()
282281
MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf)
283282
.numDataNodes(1).build();
284283
try {
284+
AtomicBoolean accessed = new AtomicBoolean(false);
285285
cluster.waitActive();
286286
DataNode dn = cluster.getDataNodes().get(0);
287287
final FsDatasetSpi<?> ds = DataNodeTestUtils.getFSDataset(dn);
288288

289289
CountDownLatch latch = new CountDownLatch(1);
290290
CountDownLatch waiterLatch = new CountDownLatch(1);
291-
// create a synchronized list and verify the order of elements.
292-
List<Integer> syncList =
293-
Collections.synchronizedList(new ArrayList<>());
294-
295-
296291
Thread holder = new Thread() {
297292
public void run() {
298-
latch.countDown();
299293
try (AutoCloseableLock l = ds.acquireDatasetReadLock()) {
300-
syncList.add(0);
301-
} catch (Exception e) {
302-
return;
303-
}
304-
try {
294+
latch.countDown();
295+
// wait for the waiter thread to access the lock.
305296
waiterLatch.await();
306-
syncList.add(2);
307-
} catch (InterruptedException e) {
297+
} catch (Exception e) {
308298
}
309299
}
310300
};
311301

312302
Thread waiter = new Thread() {
313303
public void run() {
314304
try {
315-
// wait for holder to get into the critical section.
305+
// Wait for holder to get ds read lock.
316306
latch.await();
317307
} catch (InterruptedException e) {
318308
waiterLatch.countDown();
309+
return;
319310
}
320311
try (AutoCloseableLock l = ds.acquireDatasetReadLock()) {
321-
syncList.add(1);
312+
accessed.getAndSet(true);
313+
// signal the holder thread.
322314
waiterLatch.countDown();
323315
} catch (Exception e) {
324316
}
325317
}
326318
};
327319
waiter.start();
328320
holder.start();
329-
330-
waiter.join();
321+
// Wait for sometime to make sure we are in deadlock,
322+
try {
323+
GenericTestUtils.waitFor(() ->
324+
accessed.get(),
325+
100, 10000);
326+
fail("Waiter thread should not execute.");
327+
} catch (TimeoutException e) {
328+
}
329+
// Release waiterLatch to exit deadlock.
330+
waiterLatch.countDown();
331331
holder.join();
332-
333-
// verify that the synchronized list has the correct sequence.
334-
assertEquals(
335-
"The sequence of checkpoints does not correspond to shared lock",
336-
syncList, Arrays.asList(0, 1, 2));
332+
waiter.join();
333+
// After releasing waiterLatch water
334+
// thread will be able to execute.
335+
assertTrue(accessed.get());
337336
} finally {
338337
cluster.shutdown();
339338
}

0 commit comments

Comments
 (0)