24
24
import java .util .ArrayList ;
25
25
import java .util .List ;
26
26
import java .util .Random ;
27
+ import java .util .concurrent .ExecutionException ;
28
+ import java .util .concurrent .ExecutorService ;
29
+ import java .util .concurrent .Executors ;
30
+ import java .util .concurrent .Future ;
27
31
import java .util .concurrent .atomic .AtomicBoolean ;
28
32
29
33
/**
34
38
* - Each TestDBResource starts a group of threads (16 in total) for concurrent testing,
35
39
* with the following responsibilities:
36
40
* <p>
37
- * - 5 threads perform concurrent write operations. Each thread randomly selects a table and executes
38
- * 1 million updateOneRandomTable operations on that table. Acquiring the intent write lock on the db and
39
- * then the write lock on the table is required before updating the table.
41
+ * - 5 threads perform concurrent write operations. Each thread randomly selects a table and executes
42
+ * 1 million updateOneRandomTable operations on that table. Acquiring the intent write lock on the db and
43
+ * then the write lock on the table is required before updating the table.
40
44
* <p>
41
- * - 5 threads perform concurrent write operations. Each thread executes 1 million updateAllTables
42
- * operations on the db. Acquiring the write lock on the db is required before executing this action.
45
+ * - 5 threads perform concurrent write operations. Each thread executes 1 million updateAllTables
46
+ * operations on the db. Acquiring the write lock on the db is required before executing this action.
43
47
* <p>
44
- * - 3 threads perform concurrent read operations. Each thread acquires a read lock on the db,
45
- * then randomly selects a table and verifies whether the two counters of that table are equal.
46
- * If not, Assert.assertEquals() fails.
48
+ * - 3 threads perform concurrent read operations. Each thread acquires a read lock on the db,
49
+ * then randomly selects a table and verifies whether the two counters of that table are equal.
50
+ * If not, Assert.assertEquals() fails.
47
51
* <p>
48
- * - 3 threads perform concurrent read operations. Each thread acquires an intent read lock on the db and
49
- * a read lock on the table. It then verifies whether the two counters of that table are equal.
50
- * If not, Assert.assertEquals() fails.
52
+ * - 3 threads perform concurrent read operations. Each thread acquires an intent read lock on the db and
53
+ * a read lock on the table. It then verifies whether the two counters of that table are equal.
54
+ * If not, Assert.assertEquals() fails.
51
55
* <p>
52
56
* - Finally, verify that the sum of counter1 for all tables under each db is 2 million.
53
57
* If not, Assert.assertEquals() fails.
@@ -62,7 +66,6 @@ public class LockManagerAllLockModesRandomTest {
62
66
private static final long TABLE_ID_START = 10000 ;
63
67
private static final long DB_ID_START = 20000 ;
64
68
65
-
66
69
/**
67
70
* We will acquire intensive lock or non-intensive lock on DB resource.
68
71
* When acquiring intensive lock, we will also acquire specific non-intensive lock on the table to update it.
@@ -127,6 +130,7 @@ public void incrTwoCounters() {
127
130
/**
128
131
* We always increase the two counters together, so we will assert whether the two counters are equal to test
129
132
* the correctness of the lock manager.
133
+ *
130
134
* @return the two counters in a pair
131
135
*/
132
136
public Pair <Long , Long > getTwoCounters () {
@@ -197,7 +201,7 @@ public void testAllLockModesConcurrent() throws InterruptedException {
197
201
locker = new Locker ();
198
202
locker .lock (db .getId (), LockType .INTENTION_SHARED );
199
203
locker .lock (db .getTableByIndex (tableIndex ).getId (), LockType .READ );
200
- Pair <Long , Long > result = db .getOneRandomTable ( ).getTwoCounters ();
204
+ Pair <Long , Long > result = db .getTableByIndex ( tableIndex ).getTwoCounters ();
201
205
Assert .assertEquals (result .first , result .second );
202
206
} catch (LockException e ) {
203
207
Assert .fail ();
@@ -279,24 +283,41 @@ public void testAllLockModesConcurrent() throws InterruptedException {
279
283
} // end for single db
280
284
} // enf for all dbs
281
285
282
- // Start all threads.
283
- for (Thread t : allThreadList ) {
284
- t .start ();
286
+ ExecutorService executor = Executors .newFixedThreadPool (100 );
287
+
288
+ List <Future <?>> wf = new ArrayList <>();
289
+ for (Thread thread : writeThreadList ) {
290
+ Future <?> f = executor .submit (thread );
291
+ wf .add (f );
292
+ }
293
+
294
+ List <Future <?>> rf = new ArrayList <>();
295
+ for (Thread thread : readThreadList ) {
296
+ Future <?> f = executor .submit (thread );
297
+ rf .add (f );
285
298
}
286
299
287
300
// Wait for write threads end.
288
- for (Thread t : writeThreadList ) {
289
- t .join ();
301
+ for (Future <?> f : wf ) {
302
+ try {
303
+ f .get ();
304
+ } catch (ExecutionException e ) {
305
+ Assert .fail (e .getMessage ());
306
+ }
290
307
}
291
308
System .out .println ("All write threads end." );
292
309
293
310
readerStop .set (true );
294
311
// Wait for read threads end.
295
- for (Thread t : readThreadList ) {
296
- t .join ();
312
+ for (Future <?> f : rf ) {
313
+ try {
314
+ f .get ();
315
+ } catch (ExecutionException e ) {
316
+ Assert .fail (e .getMessage ());
317
+ }
297
318
}
298
- System .out .println ("All read threads end." );
299
319
320
+ System .out .println ("All read threads end." );
300
321
301
322
// Verify the correctness of the lock manager.
302
323
for (TestDBResource db : dbs ) {
@@ -311,6 +332,5 @@ public void testAllLockModesConcurrent() throws InterruptedException {
311
332
Assert .assertEquals (30 * NUM_TEST_OPERATIONS , counter1Sum );
312
333
Assert .assertEquals (30 * NUM_TEST_OPERATIONS , counter2Sum );
313
334
}
314
-
315
335
}
316
336
}
0 commit comments