1717 */
1818package org .apache .hadoop .hdfs .server .datanode .fsdataset .impl ;
1919
20- import java .util .Arrays ;
21- import java .util .Collections ;
20+ import java .util .concurrent .TimeoutException ;
2221import java .util .function .Supplier ;
2322import com .google .common .collect .Lists ;
2423
@@ -240,11 +239,12 @@ public void run() {
240239 waiter .join ();
241240 // The holder thread is still holding the lock, but the waiter can still
242241 // run as the lock is a shared read lock.
242+ // Otherwise test will timeout with deadlock.
243243 assertEquals (true , accessed .get ());
244244 holder .interrupt ();
245245 }
246246
247- @ Test (timeout =10000 )
247+ @ Test (timeout =20000 )
248248 public void testReadLockCanBeDisabledByConfig ()
249249 throws Exception {
250250 HdfsConfiguration conf = new HdfsConfiguration ();
@@ -253,58 +253,58 @@ public void testReadLockCanBeDisabledByConfig()
253253 MiniDFSCluster cluster = new MiniDFSCluster .Builder (conf )
254254 .numDataNodes (1 ).build ();
255255 try {
256+ AtomicBoolean accessed = new AtomicBoolean (false );
256257 cluster .waitActive ();
257258 DataNode dn = cluster .getDataNodes ().get (0 );
258259 final FsDatasetSpi <?> ds = DataNodeTestUtils .getFSDataset (dn );
259260
260261 CountDownLatch latch = new CountDownLatch (1 );
261262 CountDownLatch waiterLatch = new CountDownLatch (1 );
262- // create a synchronized list and verify the order of elements.
263- List <Integer > syncList =
264- Collections .synchronizedList (new ArrayList <>());
265-
266-
267263 Thread holder = new Thread () {
268264 public void run () {
269- latch .countDown ();
270265 try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
271- syncList .add (0 );
272- } catch (Exception e ) {
273- return ;
274- }
275- try {
266+ latch .countDown ();
267+ // wait for the waiter thread to access the lock.
276268 waiterLatch .await ();
277- syncList .add (2 );
278- } catch (InterruptedException e ) {
269+ } catch (Exception e ) {
279270 }
280271 }
281272 };
282273
283274 Thread waiter = new Thread () {
284275 public void run () {
285276 try {
286- // wait for holder to get into the critical section .
277+ // Wait for holder to get ds read lock .
287278 latch .await ();
288279 } catch (InterruptedException e ) {
289280 waiterLatch .countDown ();
281+ return ;
290282 }
291283 try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
292- syncList .add (1 );
284+ accessed .getAndSet (true );
285+ // signal the holder thread.
293286 waiterLatch .countDown ();
294287 } catch (Exception e ) {
295288 }
296289 }
297290 };
298291 waiter .start ();
299292 holder .start ();
300-
301- waiter .join ();
293+ // Wait for sometime to make sure we are in deadlock,
294+ try {
295+ GenericTestUtils .waitFor (() ->
296+ accessed .get (),
297+ 100 , 10000 );
298+ fail ("Waiter thread should not execute." );
299+ } catch (TimeoutException e ) {
300+ }
301+ // Release waiterLatch to exit deadlock.
302+ waiterLatch .countDown ();
302303 holder .join ();
303-
304- // verify that the synchronized list has the correct sequence.
305- assertEquals (
306- "The sequence of checkpoints does not correspond to shared lock" ,
307- syncList , Arrays .asList (0 , 1 , 2 ));
304+ waiter .join ();
305+ // After releasing waiterLatch water
306+ // thread will be able to execute.
307+ assertTrue (accessed .get ());
308308 } finally {
309309 cluster .shutdown ();
310310 }
0 commit comments