1 |
| package org.jboss.cache.lock; |
2 |
| |
3 |
| import org.apache.commons.logging.Log; |
4 |
| import org.apache.commons.logging.LogFactory; |
5 |
| import org.jboss.cache.CacheImpl; |
6 |
| import org.jboss.cache.Node; |
7 |
| import org.jboss.cache.NodeSPI; |
8 |
| import org.jboss.cache.statetransfer.StateTransferManager; |
9 |
| import org.jboss.cache.transaction.GlobalTransaction; |
10 |
| import org.jboss.cache.transaction.TransactionTable; |
11 |
| |
12 |
| import javax.transaction.Status; |
13 |
| import javax.transaction.Transaction; |
14 |
| import javax.transaction.TransactionManager; |
15 |
| import java.util.Iterator; |
16 |
| |
17 |
| public abstract class LockUtil |
18 |
| { |
19 |
| private final static Log log = LogFactory.getLog(StateTransferManager.class); |
20 |
| |
21 |
| private static interface TransactionLockStatus extends Status |
22 |
| { |
23 |
| public static final int STATUS_BROKEN = Integer.MIN_VALUE; |
24 |
| } |
25 |
| |
26 |
1
| public static boolean breakTransactionLock(NodeLock lock,
|
27 |
| GlobalTransaction gtx, |
28 |
| boolean localTx, |
29 |
| CacheImpl cache) |
30 |
| { |
31 |
1
| TransactionTable tx_table = cache.getTransactionTable();
|
32 |
1
| TransactionManager tm = cache.getTransactionManager();
|
33 |
| |
34 |
1
| boolean broken = false;
|
35 |
1
| int tryCount = 0;
|
36 |
1
| int lastStatus = TransactionLockStatus.STATUS_BROKEN;
|
37 |
| |
38 |
1
| while (!broken && lock.isOwner(gtx))
|
39 |
| { |
40 |
1
| int status = breakTransactionLock(gtx, lock, tx_table, tm, localTx, lastStatus, tryCount);
|
41 |
1
| if (status == TransactionLockStatus.STATUS_BROKEN)
|
42 |
| { |
43 |
0
| broken = true;
|
44 |
| } |
45 |
1
| else if (status != lastStatus)
|
46 |
| { |
47 |
1
| tryCount = 0;
|
48 |
| } |
49 |
1
| lastStatus = status;
|
50 |
| |
51 |
1
| tryCount++;
|
52 |
| } |
53 |
| |
54 |
1
| return broken;
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
0
| public static void forceAcquireLock(NodeSPI<?, ?> node,
|
69 |
| Object newOwner, |
70 |
| CacheImpl cache, |
71 |
| boolean lockChildren) |
72 |
| { |
73 |
| |
74 |
0
| NodeLock lock = node.getLock();
|
75 |
0
| boolean acquired = lock.isOwner(newOwner);
|
76 |
| |
77 |
0
| if (!acquired && log.isDebugEnabled())
|
78 |
| { |
79 |
0
| log.debug("Force acquiring lock on node " + node.getFqn());
|
80 |
| } |
81 |
| |
82 |
0
| TransactionTable tx_table = cache.getTransactionTable();
|
83 |
0
| TransactionManager tm = cache.getTransactionManager();
|
84 |
0
| Object localAddress = cache.getLocalAddress();
|
85 |
0
| boolean serializable = cache.getConfiguration().getIsolationLevel() == IsolationLevel.SERIALIZABLE;
|
86 |
| |
87 |
0
| while (!acquired)
|
88 |
| { |
89 |
0
| Object curOwner = null;
|
90 |
0
| boolean attempted = false;
|
91 |
| |
92 |
| |
93 |
| |
94 |
0
| while (!acquired && ((curOwner = lock.getWriterOwner()) != null))
|
95 |
| { |
96 |
0
| acquired = acquireLockFromOwner(node, lock, curOwner, newOwner, tx_table, tm, localAddress);
|
97 |
0
| attempted = true;
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
0
| if (!acquired && serializable)
|
103 |
| { |
104 |
0
| Iterator it = lock.getReaderOwners().iterator();
|
105 |
0
| if (it.hasNext())
|
106 |
| { |
107 |
0
| curOwner = it.next();
|
108 |
0
| acquired = acquireLockFromOwner(node, lock, curOwner, newOwner, tx_table, tm, localAddress);
|
109 |
0
| attempted = true;
|
110 |
| |
111 |
| |
112 |
| |
113 |
| } |
114 |
| } |
115 |
| |
116 |
0
| if (!acquired && !attempted)
|
117 |
| { |
118 |
| |
119 |
| |
120 |
0
| try
|
121 |
| { |
122 |
0
| acquired = lock.acquire(newOwner, 1, NodeLock.LockType.READ);
|
123 |
| } |
124 |
| catch (Exception ignored) |
125 |
| { |
126 |
| } |
127 |
| } |
128 |
| } |
129 |
| |
130 |
| |
131 |
0
| if (lockChildren)
|
132 |
| { |
133 |
0
| for (NodeSPI n : node.getChildrenDirect())
|
134 |
| { |
135 |
0
| forceAcquireLock(n, newOwner, cache, true);
|
136 |
| } |
137 |
| } |
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
0
| private static boolean acquireLockFromOwner(Node node,
|
151 |
| NodeLock lock, |
152 |
| Object curOwner, |
153 |
| Object newOwner, |
154 |
| TransactionTable tx_table, |
155 |
| TransactionManager tm, |
156 |
| Object localAddress) |
157 |
| { |
158 |
0
| if (log.isTraceEnabled())
|
159 |
| { |
160 |
0
| log.trace("Attempting to acquire lock for node " + node.getFqn() +
|
161 |
| " from owner " + curOwner); |
162 |
| } |
163 |
| |
164 |
0
| boolean acquired = false;
|
165 |
0
| boolean broken = false;
|
166 |
0
| int tryCount = 0;
|
167 |
0
| int lastStatus = TransactionLockStatus.STATUS_BROKEN;
|
168 |
| |
169 |
0
| while (!broken && !acquired)
|
170 |
| { |
171 |
0
| if (curOwner instanceof GlobalTransaction)
|
172 |
| { |
173 |
0
| GlobalTransaction gtx = (GlobalTransaction) curOwner;
|
174 |
0
| boolean local = gtx.getAddress().equals(localAddress);
|
175 |
0
| int status = breakTransactionLock(gtx, lock, tx_table, tm, local, lastStatus, tryCount);
|
176 |
0
| if (status == TransactionLockStatus.STATUS_BROKEN)
|
177 |
| { |
178 |
0
| broken = true;
|
179 |
| } |
180 |
0
| else if (status != lastStatus)
|
181 |
| { |
182 |
0
| tryCount = 0;
|
183 |
| } |
184 |
0
| lastStatus = status;
|
185 |
| } |
186 |
0
| else if (tryCount > 0)
|
187 |
| { |
188 |
0
| lock.release(curOwner);
|
189 |
0
| broken = true;
|
190 |
| } |
191 |
| |
192 |
0
| if (broken && log.isTraceEnabled())
|
193 |
| { |
194 |
0
| log.trace("Broke lock for node " + node.getFqn() +
|
195 |
| " held by owner " + curOwner); |
196 |
| } |
197 |
| |
198 |
0
| try
|
199 |
| { |
200 |
0
| acquired = lock.acquire(newOwner, 1, NodeLock.LockType.READ);
|
201 |
| } |
202 |
| catch (Exception ignore) |
203 |
| { |
204 |
| } |
205 |
| |
206 |
0
| tryCount++;
|
207 |
| } |
208 |
| |
209 |
0
| return acquired;
|
210 |
| } |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
1
| private static int breakTransactionLock(GlobalTransaction gtx,
|
238 |
| NodeLock lock, |
239 |
| TransactionTable tx_table, |
240 |
| TransactionManager tm, |
241 |
| boolean localTx, |
242 |
| int lastStatus, |
243 |
| int tryCount) |
244 |
| { |
245 |
1
| int status = Status.STATUS_UNKNOWN;
|
246 |
1
| Transaction tx = tx_table.getLocalTransaction(gtx);
|
247 |
1
| if (tx != null)
|
248 |
| { |
249 |
1
| try
|
250 |
| { |
251 |
1
| status = tx.getStatus();
|
252 |
| |
253 |
1
| if (status != lastStatus)
|
254 |
| { |
255 |
1
| tryCount = 0;
|
256 |
| } |
257 |
| |
258 |
1
| switch (status)
|
259 |
| { |
260 |
1
| case Status.STATUS_ACTIVE:
|
261 |
0
| case Status.STATUS_MARKED_ROLLBACK:
|
262 |
0
| case Status.STATUS_PREPARING:
|
263 |
0
| case Status.STATUS_UNKNOWN:
|
264 |
1
| if (tryCount == 0)
|
265 |
| { |
266 |
1
| if (log.isTraceEnabled())
|
267 |
| { |
268 |
0
| log.trace("Attempting to break transaction lock held " +
|
269 |
| " by " + gtx + " by rolling back local tx"); |
270 |
| } |
271 |
| |
272 |
1
| tm.resume(tx);
|
273 |
1
| try
|
274 |
| { |
275 |
1
| tx.rollback();
|
276 |
| } |
277 |
| finally |
278 |
| { |
279 |
1
| tm.suspend();
|
280 |
| } |
281 |
| |
282 |
| } |
283 |
0
| else if (tryCount > 100)
|
284 |
| { |
285 |
| |
286 |
| |
287 |
0
| lock.release(gtx);
|
288 |
0
| status = TransactionLockStatus.STATUS_BROKEN;
|
289 |
| } |
290 |
1
| break;
|
291 |
| |
292 |
0
| case Status.STATUS_COMMITTING:
|
293 |
0
| case Status.STATUS_ROLLING_BACK:
|
294 |
| |
295 |
0
| if (tryCount < 10)
|
296 |
| { |
297 |
0
| break;
|
298 |
| } |
299 |
| |
300 |
| |
301 |
0
| case Status.STATUS_COMMITTED:
|
302 |
0
| case Status.STATUS_ROLLEDBACK:
|
303 |
0
| case Status.STATUS_NO_TRANSACTION:
|
304 |
0
| lock.release(gtx);
|
305 |
0
| status = TransactionLockStatus.STATUS_BROKEN;
|
306 |
0
| break;
|
307 |
| |
308 |
0
| case Status.STATUS_PREPARED:
|
309 |
| |
310 |
| |
311 |
| |
312 |
0
| if (tryCount == 0 && localTx)
|
313 |
| { |
314 |
| |
315 |
0
| if (log.isTraceEnabled())
|
316 |
| { |
317 |
0
| log.trace("Attempting to break transaction lock held " +
|
318 |
| "by " + gtx + " by marking local tx as " + |
319 |
| "rollback-only"); |
320 |
| } |
321 |
0
| tx.setRollbackOnly();
|
322 |
0
| break;
|
323 |
| } |
324 |
0
| else if (tryCount < 10)
|
325 |
| { |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
| |
331 |
0
| break;
|
332 |
| } |
333 |
| |
334 |
| |
335 |
0
| default:
|
336 |
0
| lock.release(gtx);
|
337 |
0
| status = TransactionLockStatus.STATUS_BROKEN;
|
338 |
| } |
339 |
| } |
340 |
| catch (Exception e) |
341 |
| { |
342 |
0
| log.error("Exception breaking locks held by " + gtx, e);
|
343 |
0
| lock.release(gtx);
|
344 |
0
| status = TransactionLockStatus.STATUS_BROKEN;
|
345 |
| } |
346 |
| } |
347 |
| else |
348 |
| { |
349 |
| |
350 |
| |
351 |
0
| if (gtx == lock.getWriterOwner()
|
352 |
| || lock.getReaderOwners().contains(gtx)) |
353 |
| { |
354 |
| |
355 |
0
| lock.release(gtx);
|
356 |
0
| status = TransactionLockStatus.STATUS_BROKEN;
|
357 |
| } |
358 |
| } |
359 |
| |
360 |
1
| return status;
|
361 |
| } |
362 |
| |
363 |
| } |