1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.transaction.locking; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collection; |
21 | |
import java.util.Collections; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.Map; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.commons.transaction.util.LoggerFacade; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class GenericLockManager implements LockManager, LockManager2 { |
41 | |
|
42 | |
public static final long DEFAULT_TIMEOUT = 30000; |
43 | |
public static final long DEFAULT_CHECK_THRESHHOLD = 500; |
44 | |
|
45 | |
|
46 | 26 | protected Map globalOwners = Collections.synchronizedMap(new HashMap()); |
47 | |
|
48 | |
|
49 | 26 | protected Map globalLocks = new HashMap(); |
50 | |
|
51 | |
|
52 | 26 | protected Map effectiveGlobalTimeouts = Collections.synchronizedMap(new HashMap()); |
53 | |
|
54 | 26 | protected Set timedOutOwners = Collections.synchronizedSet(new HashSet()); |
55 | |
|
56 | 26 | protected int maxLockLevel = -1; |
57 | |
protected LoggerFacade logger; |
58 | |
protected long globalTimeoutMSecs; |
59 | |
protected long checkThreshhold; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public GenericLockManager(int maxLockLevel, LoggerFacade logger, long timeoutMSecs, |
81 | 26 | long checkThreshholdMSecs) throws IllegalArgumentException { |
82 | 26 | if (maxLockLevel < 1) |
83 | 0 | throw new IllegalArgumentException("The maximum lock level must be at least 1 (" |
84 | |
+ maxLockLevel + " was specified)"); |
85 | 26 | this.maxLockLevel = maxLockLevel; |
86 | 26 | this.logger = logger.createLogger("Locking"); |
87 | 26 | this.globalTimeoutMSecs = timeoutMSecs; |
88 | 26 | this.checkThreshhold = checkThreshholdMSecs; |
89 | 26 | } |
90 | |
|
91 | |
public GenericLockManager(int maxLockLevel, LoggerFacade logger, long timeoutMSecs) |
92 | |
throws IllegalArgumentException { |
93 | 25 | this(maxLockLevel, logger, timeoutMSecs, DEFAULT_CHECK_THRESHHOLD); |
94 | 25 | } |
95 | |
|
96 | |
public GenericLockManager(int maxLockLevel, LoggerFacade logger) |
97 | |
throws IllegalArgumentException { |
98 | 10 | this(maxLockLevel, logger, DEFAULT_TIMEOUT); |
99 | 10 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public void startGlobalTimeout(Object ownerId, long timeoutMSecs) { |
106 | 50 | long now = System.currentTimeMillis(); |
107 | 50 | long timeout = now + timeoutMSecs; |
108 | 50 | effectiveGlobalTimeouts.put(ownerId, new Long(timeout)); |
109 | 50 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public boolean tryLock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant) { |
116 | 50 | timeoutCheck(ownerId); |
117 | |
|
118 | 25 | GenericLock lock = (GenericLock) atomicGetOrCreateLock(resourceId); |
119 | 25 | boolean acquired = lock.tryLock(ownerId, targetLockLevel, |
120 | |
reentrant ? GenericLock.COMPATIBILITY_REENTRANT : GenericLock.COMPATIBILITY_NONE, |
121 | |
false); |
122 | |
|
123 | 25 | if (acquired) { |
124 | 25 | addOwner(ownerId, lock); |
125 | |
} |
126 | 25 | return acquired; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public boolean checkLock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant) { |
134 | 0 | timeoutCheck(ownerId); |
135 | 0 | boolean possible = true; |
136 | |
|
137 | 0 | GenericLock lock = (GenericLock) getLock(resourceId); |
138 | 0 | if (lock != null) { |
139 | 0 | possible = lock.test(ownerId, targetLockLevel, |
140 | |
reentrant ? GenericLock.COMPATIBILITY_REENTRANT |
141 | |
: GenericLock.COMPATIBILITY_NONE); |
142 | |
} |
143 | 0 | return possible; |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public boolean hasLock(Object ownerId, Object resourceId, int lockLevel) { |
151 | 0 | timeoutCheck(ownerId); |
152 | 0 | boolean owned = false; |
153 | |
|
154 | 0 | GenericLock lock = (GenericLock) getLock(resourceId); |
155 | 0 | if (lock != null) { |
156 | 0 | owned = lock.has(ownerId, lockLevel); |
157 | |
} |
158 | 0 | return owned; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void lock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant) |
166 | |
throws LockException { |
167 | 542 | lock(ownerId, resourceId, targetLockLevel, reentrant, globalTimeoutMSecs); |
168 | 538 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public void lock(Object ownerId, Object resourceId, int targetLockLevel, boolean reentrant, |
175 | |
long timeoutMSecs) throws LockException { |
176 | 663 | lock(ownerId, resourceId, targetLockLevel, reentrant ? GenericLock.COMPATIBILITY_REENTRANT |
177 | |
: GenericLock.COMPATIBILITY_NONE, false, timeoutMSecs); |
178 | 614 | } |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public void lock(Object ownerId, Object resourceId, int targetLockLevel, int compatibility, |
185 | |
boolean preferred, long timeoutMSecs) throws LockException { |
186 | 3134 | timeoutCheck(ownerId); |
187 | 3132 | GenericLock lock = (GenericLock) atomicGetOrCreateLock(resourceId); |
188 | 3132 | doLock(lock, ownerId, resourceId, targetLockLevel, compatibility, preferred, timeoutMSecs); |
189 | 2953 | } |
190 | |
|
191 | |
protected void doLock(GenericLock lock, Object ownerId, Object resourceId, int targetLockLevel, |
192 | |
int compatibility, boolean preferred, long timeoutMSecs) |
193 | |
{ |
194 | 3132 | long now = System.currentTimeMillis(); |
195 | 3132 | long waitEnd = now + timeoutMSecs; |
196 | |
|
197 | 3132 | timeoutCheck(ownerId); |
198 | |
|
199 | 3132 | GenericLock.LockOwner lockWaiter = new GenericLock.LockOwner(ownerId, targetLockLevel, |
200 | |
compatibility, preferred); |
201 | |
|
202 | 3132 | boolean acquired = false; |
203 | |
try { |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | 3132 | if (checkThreshhold != -1 && timeoutMSecs > checkThreshhold) { |
210 | 2040 | acquired = lock |
211 | |
.acquire(ownerId, targetLockLevel, true, compatibility, |
212 | |
preferred, checkThreshhold); |
213 | 2040 | timeoutMSecs -= checkThreshhold; |
214 | |
} else { |
215 | 1092 | acquired = lock |
216 | |
.acquire(ownerId, targetLockLevel, false, compatibility, |
217 | |
preferred, checkThreshhold); |
218 | |
|
219 | |
} |
220 | 3132 | if (acquired) { |
221 | 2724 | addOwner(ownerId, lock); |
222 | 2724 | return; |
223 | |
} |
224 | 0 | } catch (InterruptedException e) { |
225 | 0 | throw new LockException("Interrupted", LockException.CODE_INTERRUPTED, resourceId); |
226 | 408 | } |
227 | |
try { |
228 | 408 | lock.registerWaiter(lockWaiter); |
229 | |
|
230 | 408 | boolean deadlock = wouldDeadlock(ownerId, new HashSet()); |
231 | 408 | if (deadlock) { |
232 | 176 | throw new LockException("Lock would cause deadlock", |
233 | |
LockException.CODE_DEADLOCK_VICTIM, resourceId); |
234 | |
} |
235 | |
|
236 | 232 | now = System.currentTimeMillis(); |
237 | 489 | while (!acquired && waitEnd > now) { |
238 | |
|
239 | |
|
240 | 257 | releaseTimedOutOwners(); |
241 | |
|
242 | |
|
243 | |
|
244 | 257 | Set conflicts = lock.getConflictingOwners(ownerId, targetLockLevel, compatibility); |
245 | 257 | long nextConflictTimeout = getNextGlobalConflictTimeout(conflicts); |
246 | 257 | if (nextConflictTimeout != -1 && nextConflictTimeout < waitEnd) { |
247 | 30 | timeoutMSecs = nextConflictTimeout - now; |
248 | |
|
249 | 30 | timeoutMSecs += timeoutMSecs / 10; |
250 | |
} else { |
251 | 227 | timeoutMSecs = waitEnd - now; |
252 | |
} |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | 257 | synchronized (lock) { |
258 | 257 | acquired = lock.acquire(ownerId, targetLockLevel, true, compatibility, |
259 | |
preferred, timeoutMSecs); |
260 | 257 | lock.registerWaiter(lockWaiter); |
261 | 257 | } |
262 | 257 | now = System.currentTimeMillis(); |
263 | 257 | } |
264 | 232 | if (!acquired) { |
265 | 3 | throw new LockException("Lock wait timed out", LockException.CODE_TIMED_OUT, |
266 | |
resourceId); |
267 | |
} else { |
268 | 229 | addOwner(ownerId, lock); |
269 | |
} |
270 | 0 | } catch (InterruptedException e) { |
271 | 0 | throw new LockException("Interrupted", LockException.CODE_INTERRUPTED, resourceId); |
272 | |
} finally { |
273 | 408 | lock.unregisterWaiter(lockWaiter); |
274 | 229 | } |
275 | 229 | } |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public int getLevel(Object ownerId, Object resourceId) { |
282 | 2 | timeoutCheck(ownerId); |
283 | 2 | GenericLock lock = (GenericLock) getLock(resourceId); |
284 | 2 | if (lock != null) { |
285 | 2 | return lock.getLockLevel(ownerId); |
286 | |
} else { |
287 | 0 | return 0; |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public boolean release(Object ownerId, Object resourceId) { |
296 | 26 | timeoutCheck(ownerId); |
297 | 26 | boolean released = false; |
298 | |
|
299 | 26 | GenericLock lock = (GenericLock) getLock(resourceId); |
300 | 26 | if (lock != null) { |
301 | 26 | released = lock.release(ownerId); |
302 | 26 | removeOwner(ownerId, lock); |
303 | |
} |
304 | 26 | return released; |
305 | |
} |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
public void releaseAll(Object ownerId) { |
312 | 1894 | releaseAllNoTimeOutReset(ownerId); |
313 | |
|
314 | 1894 | timedOutOwners.remove(ownerId); |
315 | 1894 | effectiveGlobalTimeouts.remove(ownerId); |
316 | 1894 | } |
317 | |
|
318 | |
protected void releaseAllNoTimeOutReset(Object ownerId) { |
319 | 1919 | Set locks = (Set) globalOwners.get(ownerId); |
320 | 1919 | if (locks != null) { |
321 | |
Collection locksCopy; |
322 | |
|
323 | |
|
324 | |
|
325 | 1689 | synchronized (locks) { |
326 | 1689 | locksCopy = new ArrayList(locks); |
327 | 1689 | } |
328 | 1689 | for (Iterator it = locksCopy.iterator(); it.hasNext();) { |
329 | 2308 | GenericLock lock = (GenericLock) it.next(); |
330 | 2308 | lock.release(ownerId); |
331 | 2308 | locks.remove(lock); |
332 | 2308 | } |
333 | |
} |
334 | 1919 | removeOwnerWithoutLocks(ownerId); |
335 | 1919 | } |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public Set getAll(Object ownerId) { |
342 | 187 | Set locks = (Set) globalOwners.get(ownerId); |
343 | 187 | if (locks == null) { |
344 | 124 | return new HashSet(); |
345 | |
} else { |
346 | 63 | return locks; |
347 | |
} |
348 | |
} |
349 | |
|
350 | |
protected void addOwner(Object ownerId, GenericLock lock) { |
351 | 2978 | synchronized (globalOwners) { |
352 | 2978 | Set locks = (Set) globalOwners.get(ownerId); |
353 | 2978 | if (locks == null) { |
354 | 1709 | locks = Collections.synchronizedSet(new HashSet()); |
355 | 1709 | globalOwners.put(ownerId, locks); |
356 | |
} |
357 | 2978 | locks.add(lock); |
358 | 2978 | } |
359 | 2978 | } |
360 | |
|
361 | |
protected void removeOwner(Object ownerId, GenericLock lock) { |
362 | 26 | Set locks = (Set) globalOwners.get(ownerId); |
363 | 26 | if (locks != null) { |
364 | 26 | locks.remove(lock); |
365 | |
} |
366 | 26 | removeOwnerWithoutLocks(ownerId); |
367 | 26 | } |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
protected boolean wouldDeadlock(Object ownerId, Set path) { |
391 | 698 | path.add(ownerId); |
392 | |
|
393 | 698 | Set locks = (Set) globalOwners.get(ownerId); |
394 | 698 | if (locks != null) { |
395 | |
Collection locksCopy; |
396 | |
|
397 | |
|
398 | 555 | synchronized (locks) { |
399 | 555 | locksCopy = new ArrayList(locks); |
400 | 555 | } |
401 | 555 | for (Iterator i = locksCopy.iterator(); i.hasNext();) { |
402 | 650 | GenericLock mylock = (GenericLock) i.next(); |
403 | |
|
404 | 650 | Collection conflicts = mylock.getConflictingWaiters(ownerId); |
405 | 650 | if (conflicts != null) { |
406 | 453 | for (Iterator j = conflicts.iterator(); j.hasNext();) { |
407 | 466 | Object waitingOwnerId = j.next(); |
408 | 466 | if (path.contains(waitingOwnerId)) { |
409 | 176 | return true; |
410 | 290 | } else if (wouldDeadlock(waitingOwnerId, path)) { |
411 | 211 | return true; |
412 | |
} |
413 | 79 | } |
414 | |
} |
415 | 263 | } |
416 | |
} |
417 | 311 | path.remove(ownerId); |
418 | 311 | return false; |
419 | |
} |
420 | |
|
421 | |
protected boolean releaseTimedOutOwners() { |
422 | 257 | boolean released = false; |
423 | 257 | synchronized (effectiveGlobalTimeouts) { |
424 | 257 | for (Iterator it = effectiveGlobalTimeouts.entrySet().iterator(); it.hasNext();) { |
425 | 106 | Map.Entry entry = (Map.Entry) it.next(); |
426 | 106 | Object ownerId = entry.getKey(); |
427 | 106 | long timeout = ((Long) entry.getValue()).longValue(); |
428 | 106 | long now = System.currentTimeMillis(); |
429 | 106 | if (timeout < now) { |
430 | 25 | releaseAllNoTimeOutReset(ownerId); |
431 | 25 | timedOutOwners.add(ownerId); |
432 | 25 | released = true; |
433 | |
} |
434 | 106 | } |
435 | 257 | } |
436 | 257 | return released; |
437 | |
} |
438 | |
|
439 | |
protected boolean timeOut(Object ownerId) { |
440 | 6344 | Long timeout = (Long)effectiveGlobalTimeouts.get(ownerId); |
441 | 6344 | long now = System.currentTimeMillis(); |
442 | 6344 | if (timeout != null && timeout.longValue() < now) { |
443 | 27 | releaseAll(ownerId); |
444 | 27 | timedOutOwners.add(ownerId); |
445 | 27 | return true; |
446 | |
} else { |
447 | 6317 | return false; |
448 | |
} |
449 | |
} |
450 | |
|
451 | |
protected long getNextGlobalConflictTimeout(Set conflicts) { |
452 | 257 | long minTimeout = -1; |
453 | 257 | long now = System.currentTimeMillis(); |
454 | 257 | if (conflicts != null) { |
455 | 133 | synchronized (effectiveGlobalTimeouts) { |
456 | 133 | for (Iterator it = effectiveGlobalTimeouts.entrySet().iterator(); it.hasNext();) { |
457 | 41 | Map.Entry entry = (Map.Entry) it.next(); |
458 | 41 | Object ownerId = entry.getKey(); |
459 | 41 | if (conflicts.contains(ownerId)) { |
460 | 30 | long timeout = ((Long) entry.getValue()).longValue(); |
461 | 30 | if (minTimeout == -1 || timeout < minTimeout) { |
462 | 30 | minTimeout = timeout; |
463 | |
} |
464 | |
} |
465 | 41 | } |
466 | 133 | } |
467 | |
} |
468 | 257 | return minTimeout; |
469 | |
} |
470 | |
|
471 | |
public MultiLevelLock getLock(Object resourceId) { |
472 | 3310 | synchronized (globalLocks) { |
473 | 3310 | return (MultiLevelLock) globalLocks.get(resourceId); |
474 | 0 | } |
475 | |
} |
476 | |
|
477 | |
public MultiLevelLock atomicGetOrCreateLock(Object resourceId) { |
478 | 3157 | synchronized (globalLocks) { |
479 | 3157 | MultiLevelLock lock = getLock(resourceId); |
480 | 3157 | if (lock == null) { |
481 | 53 | lock = createLock(resourceId); |
482 | |
} |
483 | 3157 | return lock; |
484 | 0 | } |
485 | |
} |
486 | |
|
487 | |
public void removeLock(MultiLevelLock lock) { |
488 | 0 | synchronized (globalLocks) { |
489 | 0 | globalLocks.remove(lock); |
490 | 0 | } |
491 | 0 | } |
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
public Collection getLocks() { |
499 | 0 | synchronized (globalLocks) { |
500 | 0 | return globalLocks.values(); |
501 | 0 | } |
502 | |
} |
503 | |
|
504 | |
public synchronized String toString() { |
505 | 0 | StringBuffer buf = new StringBuffer(1000); |
506 | 0 | for (Iterator it = globalLocks.values().iterator(); it.hasNext();) { |
507 | 0 | GenericLock lock = (GenericLock) it.next(); |
508 | 0 | buf.append(lock.toString()).append('\n'); |
509 | 0 | } |
510 | 0 | return buf.toString(); |
511 | |
} |
512 | |
|
513 | |
protected GenericLock createLock(Object resourceId) { |
514 | 13 | synchronized (globalLocks) { |
515 | 13 | GenericLock lock = new GenericLock(resourceId, maxLockLevel, logger); |
516 | 13 | globalLocks.put(resourceId, lock); |
517 | 13 | return lock; |
518 | 0 | } |
519 | |
} |
520 | |
|
521 | |
protected void timeoutCheck(Object ownerId) throws LockException { |
522 | 6344 | timeOut(ownerId); |
523 | 6344 | if (timedOutOwners.contains(ownerId)) { |
524 | 27 | throw new LockException( |
525 | |
"All locks of owner " |
526 | |
+ ownerId |
527 | |
+ " have globally timed out." |
528 | |
+ " You will not be able to to continue with this owner until you call releaseAll.", |
529 | |
LockException.CODE_TIMED_OUT, null); |
530 | |
} |
531 | 6317 | } |
532 | |
|
533 | |
protected void removeOwnerWithoutLocks(Object ownerId) { |
534 | 1945 | synchronized (globalOwners) { |
535 | 1945 | Set locks = (Set) globalOwners.get(ownerId); |
536 | 1945 | if (locks == null || locks.isEmpty()) { |
537 | 1944 | globalOwners.remove(ownerId); |
538 | |
} |
539 | 1945 | } |
540 | 1945 | } |
541 | |
} |