1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.lock; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.cache.Fqn; |
12 |
| import org.jboss.cache.Node; |
13 |
| import org.jboss.cache.NodeSPI; |
14 |
| |
15 |
| import java.util.ArrayList; |
16 |
| import java.util.Collection; |
17 |
| import java.util.Collections; |
18 |
| import java.util.HashSet; |
19 |
| import java.util.Iterator; |
20 |
| import java.util.Set; |
21 |
| import java.util.concurrent.TimeUnit; |
22 |
| import java.util.concurrent.locks.Lock; |
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 IdentityLock implements NodeLock |
66 |
| { |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| private boolean PRINT_LOCK_DETAILS = Boolean.getBoolean("print_lock_details"); |
72 |
| |
73 |
| private static final Log log = LogFactory.getLog(IdentityLock.class); |
74 |
| private static boolean trace = log.isTraceEnabled(); |
75 |
| private final LockStrategy lock_; |
76 |
| private final LockMap map_; |
77 |
| private final boolean mustReacquireRead_; |
78 |
| private NodeSPI<?, ?> node; |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
24
| public IdentityLock(NodeSPI node)
|
84 |
| { |
85 |
24
| this(LockStrategyFactory.getLockStrategy(), node);
|
86 |
24
| log.trace("Using default lock level");
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
180118
| public IdentityLock(IsolationLevel level, NodeSPI node)
|
95 |
| { |
96 |
180118
| this(LockStrategyFactory.getLockStrategy(level), node);
|
97 |
| } |
98 |
| |
99 |
180142
| private IdentityLock(LockStrategy strategy, NodeSPI node)
|
100 |
| { |
101 |
180142
| lock_ = strategy;
|
102 |
180142
| mustReacquireRead_ = strategy instanceof LockStrategyReadCommitted;
|
103 |
180142
| map_ = new LockMap();
|
104 |
180142
| this.node = node;
|
105 |
| } |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
0
| public Node getNode()
|
111 |
| { |
112 |
0
| return node;
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
158
| public Fqn getFqn()
|
119 |
| { |
120 |
158
| if (node == null)
|
121 |
| { |
122 |
0
| return null;
|
123 |
| } |
124 |
158
| return node.getFqn();
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
26278
| public Set getReaderOwners()
|
134 |
| { |
135 |
26278
| return map_.readerOwners();
|
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
26280
| public Object getWriterOwner()
|
144 |
| { |
145 |
26277
| return map_.writerOwner();
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
541994
| public boolean acquireWriteLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
|
161 |
| { |
162 |
541994
| if (trace)
|
163 |
| { |
164 |
0
| log.trace(new StringBuffer("acquiring WL: fqn=").append(getFqn()).append(", caller=").append(caller).
|
165 |
| append(", lock=").append(toString(PRINT_LOCK_DETAILS))); |
166 |
| } |
167 |
541994
| boolean flag = acquireWriteLock0(caller, timeout);
|
168 |
541968
| if (trace)
|
169 |
| { |
170 |
0
| log.trace(new StringBuffer("acquired WL: fqn=").append(getFqn()).append(", caller=").append(caller).
|
171 |
| append(", lock=").append(toString(PRINT_LOCK_DETAILS))); |
172 |
| } |
173 |
541968
| return flag;
|
174 |
| } |
175 |
| |
176 |
541994
| private boolean acquireWriteLock0(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
|
177 |
| { |
178 |
541994
| if (caller == null)
|
179 |
| { |
180 |
0
| throw new IllegalArgumentException("acquireWriteLock(): null caller");
|
181 |
| } |
182 |
| |
183 |
541994
| if (map_.isOwner(caller, LockMap.OWNER_WRITE))
|
184 |
| { |
185 |
85714
| if (trace)
|
186 |
| { |
187 |
0
| log.trace("acquireWriteLock(): caller already owns lock for " + getFqn() + " (caller=" + caller + ')');
|
188 |
| } |
189 |
85714
| return false;
|
190 |
| } |
191 |
| |
192 |
| |
193 |
456280
| if (map_.isOwner(caller, LockMap.OWNER_READ))
|
194 |
| { |
195 |
| |
196 |
14008
| Lock wLock;
|
197 |
14008
| try
|
198 |
| { |
199 |
14008
| if (trace)
|
200 |
| { |
201 |
0
| log.trace("upgrading RL to WL for " + caller + ", timeout=" + timeout + ", locks: " + map_.printInfo());
|
202 |
| } |
203 |
14008
| wLock = lock_.upgradeLockAttempt(timeout);
|
204 |
| } |
205 |
| catch (UpgradeException ue) |
206 |
| { |
207 |
1
| String errStr = "acquireWriteLock(): lock upgrade failed for " + getFqn() + " (caller=" + caller + ", lock info: " + toString(true) + ')';
|
208 |
1
| log.trace(errStr, ue);
|
209 |
1
| throw new UpgradeException(errStr, ue);
|
210 |
| } |
211 |
14007
| if (wLock == null)
|
212 |
| { |
213 |
1
| release(caller);
|
214 |
1
| map_.removeReader(caller);
|
215 |
1
| String errStr = "upgrade lock for " + getFqn() + " could not be acquired after " + timeout + " ms." +
|
216 |
| " Lock map ownership " + map_.printInfo() + " (caller=" + caller + ", lock info: " + toString(true) + ')'; |
217 |
1
| log.trace(errStr);
|
218 |
1
| throw new UpgradeException(errStr);
|
219 |
| } |
220 |
14006
| try
|
221 |
| { |
222 |
14006
| if (trace)
|
223 |
| { |
224 |
0
| log.trace("upgrading lock for " + getFqn());
|
225 |
| } |
226 |
14006
| map_.upgrade(caller);
|
227 |
| } |
228 |
| catch (OwnerNotExistedException ex) |
229 |
| { |
230 |
0
| throw new UpgradeException("Can't upgrade lock to WL for " + getFqn() + ", error in LockMap.upgrade()", ex);
|
231 |
| } |
232 |
| } |
233 |
| else |
234 |
| { |
235 |
| |
236 |
442272
| boolean rc = lock_.writeLock().tryLock(timeout, TimeUnit.MILLISECONDS);
|
237 |
| |
238 |
| |
239 |
442272
| if (!rc)
|
240 |
| { |
241 |
24
| String errStr = "write lock for " + getFqn() + " could not be acquired after " + timeout + " ms. " +
|
242 |
| "Locks: " + map_.printInfo() + " (caller=" + caller + ", lock info: " + toString(true) + ')'; |
243 |
24
| log.trace(errStr);
|
244 |
24
| throw new TimeoutException(errStr);
|
245 |
| } |
246 |
442248
| map_.setWriterIfNotNull(caller);
|
247 |
| } |
248 |
456254
| return true;
|
249 |
| } |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
7971537
| public boolean acquireReadLock(Object caller, long timeout) throws LockingException, TimeoutException, InterruptedException
|
261 |
| { |
262 |
7971537
| if (trace)
|
263 |
| { |
264 |
0
| log.trace(new StringBuffer("acquiring RL: fqn=").append(getFqn()).append(", caller=").append(caller).
|
265 |
| append(", lock=").append(toString(PRINT_LOCK_DETAILS))); |
266 |
| } |
267 |
7970719
| boolean flag = acquireReadLock0(caller, timeout);
|
268 |
7971484
| if (trace)
|
269 |
| { |
270 |
0
| log.trace(new StringBuffer("acquired RL: fqn=").append(getFqn()).append(", caller=").append(caller).
|
271 |
| append(", lock=").append(toString(PRINT_LOCK_DETAILS))); |
272 |
| } |
273 |
7971484
| return flag;
|
274 |
| } |
275 |
| |
276 |
7971537
| private boolean acquireReadLock0(Object caller, long timeout)
|
277 |
| throws LockingException, TimeoutException, InterruptedException |
278 |
| { |
279 |
7971529
| boolean rc;
|
280 |
| |
281 |
7971537
| if (caller == null)
|
282 |
| { |
283 |
0
| throw new IllegalArgumentException("owner is null");
|
284 |
| } |
285 |
| |
286 |
7971537
| boolean hasRead = false;
|
287 |
7971537
| boolean hasRequired = false;
|
288 |
7971537
| if (mustReacquireRead_)
|
289 |
| { |
290 |
176
| hasRequired = map_.isOwner(caller, LockMap.OWNER_WRITE);
|
291 |
176
| if (!hasRequired)
|
292 |
| { |
293 |
174
| hasRead = map_.isOwner(caller, LockMap.OWNER_READ);
|
294 |
| } |
295 |
| } |
296 |
7971361
| else if (map_.isOwner(caller, LockMap.OWNER_ANY))
|
297 |
| { |
298 |
1535194
| hasRequired = true;
|
299 |
| } |
300 |
| |
301 |
7971537
| if (hasRequired)
|
302 |
| { |
303 |
1535196
| if (trace)
|
304 |
| { |
305 |
0
| StringBuffer sb = new StringBuffer(64);
|
306 |
0
| sb.append("acquireReadLock(): caller ").append(caller).append(" already owns lock for ").append(getFqn());
|
307 |
0
| log.trace(sb.toString());
|
308 |
| } |
309 |
1535196
| return false;
|
310 |
| } |
311 |
| |
312 |
6436341
| rc = lock_.readLock().tryLock(timeout, TimeUnit.MILLISECONDS);
|
313 |
| |
314 |
| |
315 |
6436341
| if (!rc)
|
316 |
| { |
317 |
53
| StringBuffer sb = new StringBuffer();
|
318 |
53
| sb.append("read lock for ").append(getFqn()).append(" could not be acquired by ").append(caller);
|
319 |
53
| sb.append(" after ").append(timeout).append(" ms. " + "Locks: ").append(map_.printInfo());
|
320 |
53
| sb.append(", lock info: ").append(toString(true));
|
321 |
53
| String errMsg = sb.toString();
|
322 |
53
| log.trace(errMsg);
|
323 |
53
| throw new TimeoutException(errMsg);
|
324 |
| } |
325 |
| |
326 |
| |
327 |
6436284
| if (!hasRead)
|
328 |
| { |
329 |
6436238
| map_.addReader(caller);
|
330 |
| } |
331 |
6436288
| return true;
|
332 |
| } |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
6894514
| public void release(Object caller)
|
340 |
| { |
341 |
6894514
| if (caller == null)
|
342 |
| { |
343 |
0
| throw new IllegalArgumentException("IdentityLock.release(): null owner object.");
|
344 |
| } |
345 |
| |
346 |
| |
347 |
6894514
| if (map_.isOwner(caller, LockMap.OWNER_READ))
|
348 |
| { |
349 |
6422111
| map_.removeReader(caller);
|
350 |
6422111
| lock_.readLock().unlock();
|
351 |
| } |
352 |
472403
| else if (map_.isOwner(caller, LockMap.OWNER_WRITE))
|
353 |
| { |
354 |
455814
| map_.removeWriter();
|
355 |
455814
| lock_.writeLock().unlock();
|
356 |
| } |
357 |
| } |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
291
| public void releaseAll()
|
363 |
| { |
364 |
291
| try
|
365 |
| { |
366 |
291
| if ((map_.writerOwner()) != null)
|
367 |
| { |
368 |
| |
369 |
277
| lock_.writeLock().unlock();
|
370 |
| } |
371 |
| |
372 |
291
| map_.releaseReaderOwners(lock_);
|
373 |
| } |
374 |
| finally |
375 |
| { |
376 |
291
| map_.removeAll();
|
377 |
| } |
378 |
| } |
379 |
| |
380 |
| |
381 |
| |
382 |
| |
383 |
0
| public void releaseForce()
|
384 |
| { |
385 |
0
| releaseAll();
|
386 |
| } |
387 |
| |
388 |
| |
389 |
| |
390 |
| |
391 |
225564
| public boolean isReadLocked()
|
392 |
| { |
393 |
225564
| return map_.isReadLocked();
|
394 |
| } |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
225222
| public boolean isWriteLocked()
|
400 |
| { |
401 |
225222
| return map_.writerOwner() != null;
|
402 |
| } |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
225449
| public boolean isLocked()
|
408 |
| { |
409 |
225449
| return isReadLocked() || isWriteLocked();
|
410 |
| } |
411 |
| |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
| |
417 |
67
| public boolean isOwner(Object o)
|
418 |
| { |
419 |
67
| return map_.isOwner(o, LockMap.OWNER_ANY);
|
420 |
| } |
421 |
| |
422 |
54
| public String toString()
|
423 |
| { |
424 |
54
| return toString(false);
|
425 |
| } |
426 |
| |
427 |
212
| public String toString(boolean print_lock_details)
|
428 |
| { |
429 |
212
| StringBuffer sb = new StringBuffer();
|
430 |
212
| toString(sb, print_lock_details);
|
431 |
212
| return sb.toString();
|
432 |
| } |
433 |
| |
434 |
227
| public void toString(StringBuffer sb)
|
435 |
| { |
436 |
227
| toString(sb, false);
|
437 |
| } |
438 |
| |
439 |
439
| public void toString(StringBuffer sb, boolean print_lock_details)
|
440 |
| { |
441 |
439
| boolean printed_read_owners = false;
|
442 |
439
| Collection read_owners = lock_ != null ? getReaderOwners() : null;
|
443 |
439
| if (read_owners != null && read_owners.size() > 0)
|
444 |
| { |
445 |
| |
446 |
| |
447 |
| |
448 |
| |
449 |
247
| Iterator iter = read_owners.iterator();
|
450 |
247
| read_owners = new ArrayList(read_owners.size());
|
451 |
247
| while (iter.hasNext())
|
452 |
| { |
453 |
263
| read_owners.add(iter.next());
|
454 |
| } |
455 |
| |
456 |
247
| sb.append("read owners=").append(read_owners);
|
457 |
247
| printed_read_owners = true;
|
458 |
| } |
459 |
| else |
460 |
| { |
461 |
192
| read_owners = null;
|
462 |
| } |
463 |
| |
464 |
439
| Object write_owner = lock_ != null ? getWriterOwner() : null;
|
465 |
439
| if (write_owner != null)
|
466 |
| { |
467 |
192
| if (printed_read_owners)
|
468 |
| { |
469 |
0
| sb.append(", ");
|
470 |
| } |
471 |
192
| sb.append("write owner=").append(write_owner);
|
472 |
| } |
473 |
439
| if (read_owners == null && write_owner == null)
|
474 |
| { |
475 |
0
| sb.append("<unlocked>");
|
476 |
| } |
477 |
439
| if (print_lock_details)
|
478 |
| { |
479 |
158
| sb.append(" (").append(lock_.toString()).append(')');
|
480 |
| } |
481 |
| } |
482 |
| |
483 |
8513504
| public boolean acquire(Object caller, long timeout, NodeLock.LockType lock_type) throws LockingException, TimeoutException, InterruptedException
|
484 |
| { |
485 |
8513504
| try
|
486 |
| { |
487 |
8513504
| if (lock_type == NodeLock.LockType.NONE)
|
488 |
| { |
489 |
0
| return true;
|
490 |
| } |
491 |
8513504
| else if (lock_type == NodeLock.LockType.READ)
|
492 |
| { |
493 |
7971459
| return acquireReadLock(caller, timeout);
|
494 |
| } |
495 |
| else |
496 |
| { |
497 |
541981
| return acquireWriteLock(caller, timeout);
|
498 |
| } |
499 |
| } |
500 |
| catch (UpgradeException e) |
501 |
| { |
502 |
2
| StringBuffer buf = new StringBuffer("failure upgrading lock: fqn=").append(getFqn()).append(", caller=").append(caller).
|
503 |
| append(", lock=").append(toString(true)); |
504 |
2
| if (trace)
|
505 |
| { |
506 |
0
| log.trace(buf.toString());
|
507 |
| } |
508 |
2
| throw new UpgradeException(buf.toString(), e);
|
509 |
| } |
510 |
| catch (LockingException e) |
511 |
| { |
512 |
0
| StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(getFqn()).append(", caller=").append(caller).
|
513 |
| append(", lock=").append(toString(true)); |
514 |
0
| if (trace)
|
515 |
| { |
516 |
0
| log.trace(buf.toString());
|
517 |
| } |
518 |
0
| throw new LockingException(buf.toString(), e);
|
519 |
| } |
520 |
| catch (TimeoutException e) |
521 |
| { |
522 |
77
| StringBuffer buf = new StringBuffer("failure acquiring lock: fqn=").append(getFqn()).append(", caller=").append(caller).
|
523 |
| append(", lock=").append(toString(true)); |
524 |
77
| if (trace)
|
525 |
| { |
526 |
0
| log.trace(buf.toString());
|
527 |
| } |
528 |
77
| throw new TimeoutException(buf.toString(), e);
|
529 |
| } |
530 |
| } |
531 |
| |
532 |
30242
| public Set<NodeLock> acquireAll(Object caller, long timeout, LockType lock_type)
|
533 |
| throws LockingException, TimeoutException, InterruptedException |
534 |
| { |
535 |
30242
| boolean acquired;
|
536 |
| |
537 |
30242
| if (lock_type == LockType.NONE)
|
538 |
| { |
539 |
0
| return Collections.emptySet();
|
540 |
| } |
541 |
| |
542 |
30242
| Set<NodeLock> retval = new HashSet<NodeLock>();
|
543 |
30242
| acquired = acquire(caller, timeout, lock_type);
|
544 |
30229
| if (acquired)
|
545 |
| { |
546 |
15647
| retval.add(this);
|
547 |
| } |
548 |
| |
549 |
30229
| for (NodeSPI n : node.getChildrenDirect())
|
550 |
| { |
551 |
14280
| retval.addAll(n.getLock().acquireAll(caller, timeout, lock_type));
|
552 |
| } |
553 |
30215
| return retval;
|
554 |
| } |
555 |
| |
556 |
28639
| public void releaseAll(Object owner)
|
557 |
| { |
558 |
28639
| for (NodeSPI n : node.getChildrenDirect())
|
559 |
| { |
560 |
23848
| n.getLock().releaseAll(owner);
|
561 |
| } |
562 |
28639
| release(owner);
|
563 |
| } |
564 |
| |
565 |
1712
| private void printIndent(StringBuffer sb, int indent)
|
566 |
| { |
567 |
1712
| if (sb != null)
|
568 |
| { |
569 |
1712
| for (int i = 0; i < indent; i++)
|
570 |
| { |
571 |
9540
| sb.append(" ");
|
572 |
| } |
573 |
| } |
574 |
| } |
575 |
| |
576 |
1712
| public void printLockInfo(StringBuffer sb, int indent)
|
577 |
| { |
578 |
1712
| boolean locked = isLocked();
|
579 |
| |
580 |
1712
| printIndent(sb, indent);
|
581 |
1712
| sb.append(Fqn.SEPARATOR).append(node.getFqn().getLastElement());
|
582 |
1712
| if (locked)
|
583 |
| { |
584 |
227
| sb.append("\t(");
|
585 |
227
| toString(sb);
|
586 |
227
| sb.append(")");
|
587 |
| } |
588 |
| |
589 |
1712
| for (NodeSPI n : node.getChildrenDirect())
|
590 |
| { |
591 |
1190
| sb.append("\n");
|
592 |
1190
| n.getLock().printLockInfo(sb, indent + 4);
|
593 |
| } |
594 |
| } |
595 |
| |
596 |
| } |