1717 */
1818package org .apache .hadoop .hdfs .server .datanode .fsdataset .impl ;
1919
20+ import java .util .Arrays ;
21+ import java .util .Collections ;
2022import java .util .function .Supplier ;
2123import com .google .common .collect .Lists ;
2224
@@ -197,9 +199,9 @@ public void setUp() throws IOException {
197199 assertEquals (0 , dataset .getNumFailedVolumes ());
198200 }
199201
200- @ Test
202+ @ Test ( timeout = 10000 )
201203 public void testReadLockEnabledByDefault ()
202- throws IOException , InterruptedException {
204+ throws Exception {
203205 final FsDatasetSpi ds = dataset ;
204206 AtomicBoolean accessed = new AtomicBoolean (false );
205207 CountDownLatch latch = new CountDownLatch (1 );
@@ -209,37 +211,42 @@ public void testReadLockEnabledByDefault()
209211 public void run () {
210212 try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
211213 latch .countDown ();
212- sleep (10000 );
214+ // wait for the waiter thread to access the lock.
215+ waiterLatch .await ();
213216 } catch (Exception e ) {
214217 }
215218 }
216219 };
217220
218221 Thread waiter = new Thread () {
219222 public void run () {
220- try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
223+ try {
224+ latch .await ();
225+ } catch (InterruptedException e ) {
221226 waiterLatch .countDown ();
227+ return ;
228+ }
229+ try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
222230 accessed .getAndSet (true );
231+ // signal the holder thread.
232+ waiterLatch .countDown ();
223233 } catch (Exception e ) {
224234 }
225235 }
226236 };
227-
228- holder .start ();
229- latch .await ();
230237 waiter .start ();
231- waiterLatch .await ();
238+ holder .start ();
239+ holder .join ();
240+ waiter .join ();
232241 // The holder thread is still holding the lock, but the waiter can still
233242 // run as the lock is a shared read lock.
234243 assertEquals (true , accessed .get ());
235244 holder .interrupt ();
236- holder .join ();
237- waiter .join ();
238245 }
239246
240247 @ Test (timeout =10000 )
241248 public void testReadLockCanBeDisabledByConfig ()
242- throws IOException , InterruptedException {
249+ throws Exception {
243250 HdfsConfiguration conf = new HdfsConfiguration ();
244251 conf .setBoolean (
245252 DFSConfigKeys .DFS_DATANODE_LOCK_READ_WRITE_ENABLED_KEY , false );
@@ -252,41 +259,52 @@ public void testReadLockCanBeDisabledByConfig()
252259
253260 CountDownLatch latch = new CountDownLatch (1 );
254261 CountDownLatch waiterLatch = new CountDownLatch (1 );
255- AtomicBoolean accessed = new AtomicBoolean (false );
262+ // create a synchronized list and verify the order of elements.
263+ List <Integer > syncList =
264+ Collections .synchronizedList (new ArrayList <>());
265+
256266
257267 Thread holder = new Thread () {
258268 public void run () {
269+ latch .countDown ();
259270 try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
260- latch .countDown ();
261- sleep (10000 );
271+ syncList .add (0 );
262272 } catch (Exception e ) {
273+ return ;
274+ }
275+ try {
276+ waiterLatch .await ();
277+ syncList .add (2 );
278+ } catch (InterruptedException e ) {
263279 }
264280 }
265281 };
266282
267283 Thread waiter = new Thread () {
268284 public void run () {
285+ try {
286+ // wait for holder to get into the critical section.
287+ latch .await ();
288+ } catch (InterruptedException e ) {
289+ waiterLatch .countDown ();
290+ }
269291 try (AutoCloseableLock l = ds .acquireDatasetReadLock ()) {
270- accessed . getAndSet ( true );
292+ syncList . add ( 1 );
271293 waiterLatch .countDown ();
272294 } catch (Exception e ) {
273295 }
274296 }
275297 };
276-
277- holder .start ();
278- latch .await ();
279298 waiter .start ();
280- Thread .sleep (200 );
281- // Waiting thread should not have been able to update the variable
282- // as the read lock is disabled and hence an exclusive lock.
283- assertEquals (false , accessed .get ());
284- holder .interrupt ();
285- holder .join ();
286- waiterLatch .await ();
287- // After the holder thread exits, the variable is updated.
288- assertEquals (true , accessed .get ());
299+ holder .start ();
300+
289301 waiter .join ();
302+ 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 ));
290308 } finally {
291309 cluster .shutdown ();
292310 }
0 commit comments