1 |
| package org.jboss.cache.loader.deadlock; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestSuite; |
5 |
| import org.jboss.cache.CacheImpl; |
6 |
| import org.jboss.cache.DefaultCacheFactory; |
7 |
| import org.jboss.cache.Fqn; |
8 |
| import org.jboss.cache.config.Configuration; |
9 |
| import org.jboss.cache.config.Configuration.CacheMode; |
10 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
11 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
12 |
| import org.jboss.cache.loader.AbstractCacheLoaderTestBase; |
13 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
14 |
| |
15 |
| import javax.naming.Context; |
16 |
| import javax.naming.InitialContext; |
17 |
| import javax.transaction.UserTransaction; |
18 |
| import java.util.Properties; |
19 |
| import java.util.concurrent.CountDownLatch; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| public class ConcurrentCreationDeadlockTest extends AbstractCacheLoaderTestBase |
66 |
| { |
67 |
| |
68 |
| |
69 |
| |
70 |
| private static final int NUM_WORKERS = 10; |
71 |
| |
72 |
| |
73 |
| |
74 |
| private static final int NUM_RUNS = 100; |
75 |
| |
76 |
| |
77 |
| |
78 |
| private static final int NUM_FQNS_PER_RUN = 10; |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| private static final Properties PROPERTIES; |
84 |
| |
85 |
| |
86 |
| |
87 |
| private static final String CONTEXT_FACTORY = |
88 |
| "org.jboss.cache.transaction.DummyContextFactory"; |
89 |
| |
90 |
| |
91 |
| |
92 |
| private String m_contextFactory = null; |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| private static volatile Exception mcl_exception = null; |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| private CacheImpl cache = null; |
103 |
| |
104 |
| static |
105 |
| { |
106 |
1
| PROPERTIES = new Properties();
|
107 |
1
| PROPERTIES.put(Context.INITIAL_CONTEXT_FACTORY,
|
108 |
| "org.jboss.cache.transaction.DummyContextFactory"); |
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
2
| public ConcurrentCreationDeadlockTest(String name)
|
117 |
| { |
118 |
2
| super(name);
|
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
2
| public void setUp() throws Exception
|
125 |
| { |
126 |
2
| super.setUp();
|
127 |
2
| mcl_exception = null;
|
128 |
2
| m_contextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
129 |
2
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
|
130 |
2
| DummyTransactionManager.getInstance();
|
131 |
2
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
132 |
| |
133 |
2
| XmlConfigurationParser parser = new XmlConfigurationParser();
|
134 |
2
| Configuration c = UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC);
|
135 |
2
| cache.setConfiguration(c);
|
136 |
| |
137 |
2
| c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
2
| public void tearDown() throws Exception
|
144 |
| { |
145 |
2
| super.tearDown();
|
146 |
2
| DummyTransactionManager.destroy();
|
147 |
2
| cache.stop();
|
148 |
2
| cache = null;
|
149 |
2
| if (m_contextFactory != null)
|
150 |
| { |
151 |
1
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
|
152 |
| m_contextFactory); |
153 |
1
| m_contextFactory = null;
|
154 |
| } |
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
2
| private void startCache(Configuration.CacheMode cacheMode, String cacheLoaderClass)
|
165 |
| throws Exception |
166 |
| { |
167 |
2
| cache.getConfiguration().setCacheMode(cacheMode);
|
168 |
2
| if (cacheLoaderClass != null)
|
169 |
| { |
170 |
2
| cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", cacheLoaderClass, "", false, false, false));
|
171 |
| } |
172 |
2
| cache.create();
|
173 |
2
| cache.start();
|
174 |
| } |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
0
| public void disabledtestLocalNoCacheLoader1Modification() throws Exception
|
184 |
| { |
185 |
0
| startCache(Configuration.CacheMode.LOCAL, null);
|
186 |
0
| performTest(1);
|
187 |
| } |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
0
| public void disabledtestLocalNoCacheLoader2Modifications()
|
197 |
| throws Exception |
198 |
| { |
199 |
0
| startCache(Configuration.CacheMode.LOCAL, null);
|
200 |
0
| performTest(2);
|
201 |
| } |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
0
| public void disabledtestLocalCacheLoader1Modification() throws Exception
|
211 |
| { |
212 |
0
| startCache(Configuration.CacheMode.LOCAL, "org.jboss.cache.loader.DummyCacheLoader");
|
213 |
0
| performTest(1);
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
1
| public void testLocalCacheLoader2Modifications() throws Exception
|
224 |
| { |
225 |
1
| startCache(Configuration.CacheMode.LOCAL, "org.jboss.cache.loader.DummyCacheLoader");
|
226 |
1
| performTest(2);
|
227 |
| } |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
0
| public void disabledtestReplAsyncNoCacheLoader1Modification()
|
237 |
| throws Exception |
238 |
| { |
239 |
0
| startCache(Configuration.CacheMode.REPL_ASYNC, null);
|
240 |
0
| performTest(1);
|
241 |
| } |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
0
| public void disabledtestReplAsyncNoCacheLoader2Modifications()
|
251 |
| throws Exception |
252 |
| { |
253 |
0
| startCache(Configuration.CacheMode.REPL_ASYNC, null);
|
254 |
0
| performTest(2);
|
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
0
| public void disabledtestReplAsyncCacheLoader1Modification()
|
265 |
| throws Exception |
266 |
| { |
267 |
0
| startCache(Configuration.CacheMode.REPL_ASYNC, "org.jboss.cache.loader.DummyCacheLoader");
|
268 |
0
| performTest(1);
|
269 |
| } |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
0
| public void disabledtestReplAsyncCacheLoader2Modifications()
|
280 |
| throws Exception |
281 |
| { |
282 |
0
| startCache(Configuration.CacheMode.REPL_ASYNC, "org.jboss.cache.loader.DummyCacheLoader");
|
283 |
0
| performTest(2);
|
284 |
| } |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
0
| public void disabledtestReplSyncNoCacheLoader1Modification()
|
294 |
| throws Exception |
295 |
| { |
296 |
0
| startCache(Configuration.CacheMode.REPL_SYNC, null);
|
297 |
0
| performTest(1);
|
298 |
| } |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
0
| public void disabledtestReplSyncNoCacheLoader2Modifications()
|
308 |
| throws Exception |
309 |
| { |
310 |
0
| startCache(Configuration.CacheMode.REPL_SYNC, null);
|
311 |
0
| performTest(2);
|
312 |
| } |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
0
| public void disabledtestReplSyncCacheLoader1Modification() throws Exception
|
322 |
| { |
323 |
0
| startCache(Configuration.CacheMode.REPL_SYNC, "org.jboss.cache.loader.DummyCacheLoader");
|
324 |
0
| performTest(1);
|
325 |
| } |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
| |
332 |
| |
333 |
| |
334 |
1
| public void testReplSyncCacheLoader2Modifications()
|
335 |
| throws Exception |
336 |
| { |
337 |
1
| startCache(Configuration.CacheMode.REPL_SYNC, "org.jboss.cache.loader.DummyCacheLoader");
|
338 |
1
| performTest(2);
|
339 |
| } |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
2
| private void performTest(int modificationsPerTx) throws Exception
|
348 |
| { |
349 |
2
| for (int i = 0; i < NUM_RUNS; i++)
|
350 |
| { |
351 |
105
| if (mcl_exception != null)
|
352 |
| { |
353 |
| |
354 |
1
| fail("Due to an exception: " + mcl_exception);
|
355 |
| } |
356 |
| |
357 |
104
| Worker[] t = new Worker[NUM_WORKERS];
|
358 |
104
| CountDownLatch latch = new CountDownLatch(1);
|
359 |
104
| for (int j = 0; j < t.length; j++)
|
360 |
| { |
361 |
1040
| t[j] = new Worker(latch, NUM_FQNS_PER_RUN * i,
|
362 |
| NUM_FQNS_PER_RUN, modificationsPerTx); |
363 |
1040
| t[j].start();
|
364 |
| } |
365 |
| |
366 |
104
| latch.countDown();
|
367 |
| |
368 |
104
| for (int j = 0; j < t.length; j++)
|
369 |
| { |
370 |
1040
| t[j].join();
|
371 |
| } |
372 |
| } |
373 |
| } |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
| |
381 |
10363
| private UserTransaction getTransaction() throws Exception
|
382 |
| { |
383 |
10363
| return (UserTransaction) new InitialContext(PROPERTIES)
|
384 |
| .lookup("UserTransaction"); |
385 |
| } |
386 |
| |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
| |
392 |
20728
| private void log(String msg)
|
393 |
| { |
394 |
20728
| System.out.println(System.currentTimeMillis() + " "
|
395 |
| + Thread.currentThread() + " " + msg); |
396 |
| } |
397 |
| |
398 |
| |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| private class Worker extends Thread |
406 |
| { |
407 |
| |
408 |
| |
409 |
| |
410 |
| private final CountDownLatch m_latch; |
411 |
| |
412 |
| |
413 |
| |
414 |
| private final int m_start; |
415 |
| |
416 |
| |
417 |
| |
418 |
| private final int m_count; |
419 |
| |
420 |
| |
421 |
| |
422 |
| private final int m_modificationsPerTx; |
423 |
| |
424 |
| |
425 |
| |
426 |
| |
427 |
| private int m_state; |
428 |
| |
429 |
| |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
1040
| public Worker(CountDownLatch latch, int start, int count, int modificationsPerTx)
|
439 |
| { |
440 |
1040
| m_latch = latch;
|
441 |
1040
| m_start = start;
|
442 |
1040
| m_count = count;
|
443 |
1040
| m_state = -1;
|
444 |
1040
| m_modificationsPerTx = modificationsPerTx;
|
445 |
| } |
446 |
| |
447 |
| |
448 |
| |
449 |
| |
450 |
1040
| public void run()
|
451 |
| { |
452 |
1040
| try
|
453 |
| { |
454 |
| |
455 |
1040
| m_latch.await();
|
456 |
1040
| for (int i = m_start; i < m_start + m_count; i++)
|
457 |
| { |
458 |
10364
| m_state = -1;
|
459 |
10364
| log("enter " + i);
|
460 |
10364
| if (checkAndSetState())
|
461 |
| { |
462 |
1
| return;
|
463 |
| } |
464 |
10363
| long time = System.currentTimeMillis();
|
465 |
10363
| UserTransaction tx = getTransaction();
|
466 |
10363
| tx.begin();
|
467 |
10363
| if (checkAndSetState())
|
468 |
| { |
469 |
0
| return;
|
470 |
| } |
471 |
| |
472 |
| |
473 |
10363
| Fqn fqn = new Fqn(new Fqn("NODE"), i);
|
474 |
10363
| for (int m = 0; m < m_modificationsPerTx; m++)
|
475 |
| { |
476 |
20719
| cache.put(fqn, new Integer(m), new Integer(i));
|
477 |
20714
| if (checkAndSetState())
|
478 |
| { |
479 |
2
| return;
|
480 |
| } |
481 |
| } |
482 |
10356
| tx.commit();
|
483 |
10356
| if (checkAndSetState())
|
484 |
| { |
485 |
0
| return;
|
486 |
| } |
487 |
10356
| time = System.currentTimeMillis() - time;
|
488 |
10356
| log("leave " + i + " took " + time + " msec");
|
489 |
| } |
490 |
| } |
491 |
| catch (Exception e) |
492 |
| { |
493 |
5
| log("caught exception in state " + m_state + ": " + e);
|
494 |
5
| mcl_exception = e;
|
495 |
| } |
496 |
| } |
497 |
| |
498 |
| |
499 |
| |
500 |
| |
501 |
| |
502 |
| |
503 |
51797
| private boolean checkAndSetState()
|
504 |
| { |
505 |
51797
| if (mcl_exception != null)
|
506 |
| { |
507 |
| |
508 |
3
| log("detected failure in state " + m_state);
|
509 |
3
| return true;
|
510 |
| } |
511 |
51794
| m_state++;
|
512 |
51794
| return false;
|
513 |
| } |
514 |
| } |
515 |
| |
516 |
1
| public static Test suite()
|
517 |
| { |
518 |
| |
519 |
1
| return new TestSuite(ConcurrentCreationDeadlockTest.class);
|
520 |
| |
521 |
| } |
522 |
| |
523 |
| |
524 |
0
| public static void main(String[] args) throws Exception
|
525 |
| { |
526 |
| |
527 |
0
| junit.textui.TestRunner.run(suite());
|
528 |
| |
529 |
| } |
530 |
| } |