-
Notifications
You must be signed in to change notification settings - Fork 33
/
TransactionReaper.java
630 lines (567 loc) · 23.7 KB
/
TransactionReaper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
* Copyright (c) 2022 Contributors to Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.jmsserver.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.sun.messaging.jmq.io.Status;
import com.sun.messaging.jmq.jmsserver.Broker;
import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.core.BrokerAddress;
import com.sun.messaging.jmq.jmsserver.resources.BrokerResources;
import com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection;
import com.sun.messaging.jmq.jmsserver.util.BrokerException;
import com.sun.messaging.jmq.util.UID;
import com.sun.messaging.jmq.util.log.Logger;
import com.sun.messaging.jmq.util.timer.WakeupableTimer;
import com.sun.messaging.jmq.util.timer.TimerEventHandler;
import com.sun.messaging.jmq.jmsserver.BrokerStateHandler;
import com.sun.messaging.jmq.jmsservice.BrokerEvent;
class TransactionReaper implements TimerEventHandler {
TransactionList translist = null;
Logger logger = Globals.getLogger();
List<TIDEntry> committed = Collections.synchronizedList(new ArrayList<>());
List<TIDEntry> noremoves = Collections.synchronizedList(new ArrayList<>());
List<TIDEntry> clusterPCommitted = Collections.synchronizedList(new ArrayList<>());
List<TIDEntry> remoteCommitted = Collections.synchronizedList(new ArrayList<>());
List<TIDEntry> remoteRCommitted = Collections.synchronizedList(new ArrayList<>());
WakeupableTimer reapTimer = null;
enum ClusterPCommittedState {
UNPROCCESSED, PROCCESSED, TAKEOVER
}
static class TIDEntry {
TransactionUID tid = null;
boolean inprocessing = false;
ClusterPCommittedState pstate = ClusterPCommittedState.UNPROCCESSED;
boolean swipemark = false;
boolean swipeonly = false;
TIDEntry(TransactionUID id) {
tid = id;
}
TIDEntry(TransactionUID id, ClusterPCommittedState s) {
tid = id;
pstate = s;
}
@Override
public int hashCode() {
return tid.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TIDEntry) {
return tid.equals(((TIDEntry) obj).tid);
}
return false;
}
}
TransactionReaper(TransactionList tl) {
this.translist = tl;
}
private boolean needReapOne(List l) {
if (TransactionList.TXN_REAPLIMIT == 0) {
return true;
}
int size = l.size();
if (size > TransactionList.TXN_REAPLIMIT * (1.0 + ((float) TransactionList.TXN_REAPLIMIT_OVERTHRESHOLD) / 100)) {
return true;
}
return false;
}
public void addLocalTransaction(TransactionUID tid, boolean noremove) {
TIDEntry e = new TIDEntry(tid);
if (noremove) {
noremoves.add(e);
}
committed.add(e);
createTimer();
if (committed.size() > TransactionList.TXN_REAPLIMIT) {
reapTimer.wakeup();
}
if (needReapOne(committed)) {
run(true);
}
}
public void addClusterTransaction(TransactionUID tid, boolean noremove) {
addClusterTransaction(tid, noremove, false);
}
public void addClusterTransaction(TransactionUID tid, boolean noremove, boolean takeover) {
TIDEntry e = null;
if (!takeover) {
e = new TIDEntry(tid, ClusterPCommittedState.UNPROCCESSED);
} else {
e = new TIDEntry(tid, ClusterPCommittedState.TAKEOVER);
}
clusterPCommitted.add(e);
if (noremove) {
noremoves.add(e);
}
createTimer();
reapTimer.wakeup();
}
public void clusterTransactionCompleted(TransactionUID tid) {
createTimer();
reapTimer.wakeup();
if (!needReapOne(clusterPCommitted) && !needReapOne(committed)) {
return;
}
TransactionBroker[] brokers = null;
try {
brokers = translist.getClusterTransactionBrokers(tid);
} catch (Exception e) {
boolean logstack = true;
if (e instanceof BrokerException && ((BrokerException) e).getStatusCode() == Status.NOT_FOUND) {
logstack = false;
}
String emsg = translist.br.getKString(translist.br.W_UNABLE_GET_TXN_BROKERS_ON_TXN_COMPLETE, tid, e.getMessage());
if (logstack) {
logger.logStack(logger.WARNING, emsg, e);
} else {
if (translist.DEBUG) {
logger.log(logger.INFO, emsg + " - all completed");
}
}
}
boolean completed = true;
if (brokers == null) {
completed = false;
} else {
for (int i = 0; i < brokers.length; i++) {
if (!brokers[i].isCompleted()) {
completed = false;
}
}
}
if (completed) {
TIDEntry entry = null;
TIDEntry e = new TIDEntry(tid);
synchronized (clusterPCommitted) {
int ind = clusterPCommitted.indexOf(e);
if (ind >= 0) {
entry = clusterPCommitted.get(ind);
}
if (entry != null && entry.inprocessing) {
entry = null;
}
if (entry != null) {
entry.inprocessing = true;
}
}
if (entry != null) {
if (entry.pstate == ClusterPCommittedState.UNPROCCESSED) {
Globals.getConnectionManager().removeFromClientDataList(IMQConnection.TRANSACTION_LIST, entry.tid);
}
entry.pstate = ClusterPCommittedState.PROCCESSED;
clusterPCommitted.remove(entry);
entry.inprocessing = false;
committed.add(entry);
}
}
if (needReapOne(committed)) {
run(true);
}
}
public void addRemoteTransaction(TransactionUID tid, boolean recovery) {
TIDEntry e = new TIDEntry(tid);
if (!recovery) {
remoteCommitted.add(e);
} else {
remoteRCommitted.add(e);
}
createTimer();
if (remoteCommitted.size() > TransactionList.TXN_REAPLIMIT || remoteRCommitted.size() > TransactionList.TXN_REAPLIMIT) {
reapTimer.wakeup();
}
if (needReapOne(remoteCommitted) || needReapOne(remoteRCommitted)) {
run(true);
}
}
public boolean hasRemoteTransaction(TransactionUID tid) {
TIDEntry e = new TIDEntry(tid);
if (remoteCommitted.contains(e)) {
return true;
}
return remoteRCommitted.contains(e);
}
public synchronized void wakeupReaperTimer() {
if (reapTimer != null) {
reapTimer.wakeup();
}
}
private synchronized void createTimer() {
if (reapTimer == null) {
try {
String startString = Globals.getBrokerResources().getKString(BrokerResources.I_COMMITTED_TRAN_REAPER_THREAD_START,
TransactionList.TXN_REAPLIMIT, TransactionList.TXN_REAPINTERVAL / 1000);
String exitString = Globals.getBrokerResources().getKString(BrokerResources.I_COMMITTED_TRAN_REAPER_THREAD_EXIT);
reapTimer = new WakeupableTimer("TransactionReaper", this, TransactionList.TXN_REAPINTERVAL, TransactionList.TXN_REAPINTERVAL, startString,
exitString);
} catch (Throwable ex) {
String emsg = Globals.getBrokerResources().getKString(BrokerResources.E_TXN_REAPER_START, ex.getMessage());
logger.logStack(Logger.ERROR, emsg, ex);
Broker.getBroker().exit(Globals.getBrokerStateHandler().getRestartCode(), emsg, BrokerEvent.Type.RESTART, ex, false, true, false);
}
}
}
/***************************************************
* Methods for TimerEventHandler for WakeupableTimer
***************************************************/
@Override
public void handleOOMError(Throwable e) {
Globals.handleGlobalError(e, "OOM:TransactionReaper");
}
@Override
public void handleLogInfo(String msg) {
logger.log(Logger.INFO, msg);
}
@Override
public void handleLogWarn(String msg, Throwable e) {
if (e == null) {
logger.log(Logger.WARNING, msg);
} else {
logger.logStack(Logger.WARNING, msg, e);
}
}
@Override
public void handleLogError(String msg, Throwable e) {
if (e == null) {
logger.log(Logger.ERROR, msg);
} else {
logger.logStack(Logger.ERROR, msg, e);
}
}
@Override
public void handleTimerExit(Throwable e) {
if (BrokerStateHandler.isShuttingDown() || reapTimer == null) {
return;
}
String emsg = Globals.getBrokerResources().getKString(BrokerResources.E_TXN_REAPER_UNEXPECTED_EXIT, e.getMessage());
Broker.getBroker().exit(Globals.getBrokerStateHandler().getRestartCode(), emsg, BrokerEvent.Type.RESTART, e, false, true, false);
}
public synchronized void destroy() {
if (reapTimer != null) {
reapTimer.cancel();
reapTimer = null;
}
committed.clear();
remoteCommitted.clear();
remoteRCommitted.clear();
}
public Hashtable getDebugState(TransactionUID id) {
TIDEntry e = new TIDEntry(id);
Hashtable ht = new Hashtable();
if (committed.contains(e)) {
ht.put(id.toString(), TransactionState.toString(TransactionState.COMMITTED));
return ht;
}
if (clusterPCommitted.contains(e)) {
ht.put(id.toString() + "(cluster)", TransactionState.toString(TransactionState.COMMITTED));
return ht;
}
if (remoteCommitted.contains(e)) {
ht.put(id.toString() + "(remote)", TransactionState.toString(TransactionState.COMMITTED));
return ht;
}
if (remoteRCommitted.contains(e)) {
ht.put(id.toString() + "(remote-r)", TransactionState.toString(TransactionState.COMMITTED));
return ht;
}
return null;
}
public Hashtable getDebugState() {
Hashtable ht = new Hashtable();
List l = null;
ht.put("committedCount", Integer.valueOf(committed.size()));
synchronized (committed) {
l = new ArrayList(committed);
}
TIDEntry e = null;
Iterator<TIDEntry> itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
ht.put(e.tid.toString(), TransactionState.toString(TransactionState.COMMITTED) + ":" + e.inprocessing);
}
l.clear();
ht.put("clusterPCommittedCount", Integer.valueOf(clusterPCommitted.size()));
synchronized (clusterPCommitted) {
l = new ArrayList(clusterPCommitted);
}
itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
ht.put(e.tid.toString() + "(cluster)", TransactionState.toString(TransactionState.COMMITTED) + ":" + e.inprocessing + ":" + e.pstate);
}
l.clear();
ht.put("noremovesCount", Integer.valueOf(noremoves.size()));
synchronized (noremoves) {
l = new ArrayList(noremoves);
}
itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
ht.put(e.tid.toString(), TransactionState.toString(TransactionState.COMMITTED));
}
l.clear();
ht.put("remoteCommittedCount", Integer.valueOf(remoteCommitted.size()));
synchronized (remoteCommitted) {
l = new ArrayList(remoteCommitted);
}
itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
ht.put(e.tid.toString() + "(remote)", TransactionState.toString(TransactionState.COMMITTED) + ":" + e.inprocessing);
}
l.clear();
ht.put("remoteRCommittedCount", Integer.valueOf(remoteRCommitted.size()));
synchronized (remoteRCommitted) {
l = new ArrayList(remoteRCommitted);
}
itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
ht.put(e.tid.toString() + "(remote-r)", TransactionState.toString(TransactionState.COMMITTED) + ":" + e.inprocessing);
}
l.clear();
return ht;
}
private boolean clusterPCommittedHasUnprocessed() {
List l = null;
synchronized (clusterPCommitted) {
l = new ArrayList(clusterPCommitted);
}
TIDEntry e = null;
Iterator<TIDEntry> itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
if (e.pstate == ClusterPCommittedState.UNPROCCESSED || e.pstate == ClusterPCommittedState.TAKEOVER) {
return true;
}
}
l.clear();
return false;
}
private void clearSwipeMark(List<TIDEntry> list) {
List l = null;
synchronized (list) {
l = new ArrayList(list);
}
TIDEntry e = null;
Iterator<TIDEntry> itr = l.iterator();
while (itr.hasNext()) {
e = itr.next();
e.swipemark = false;
}
l.clear();
}
private TIDEntry getNextEntry(List<TIDEntry> l, int limit, boolean swipe) {
TIDEntry entry = null;
int i = 0, rsize = 0;
synchronized (l) {
rsize = l.size();
while (rsize > limit) {
entry = l.get(i++);
if (entry.inprocessing) {
rsize--;
entry = null;
continue;
}
if (!swipe && (entry.swipeonly || entry.swipemark)) {
rsize--;
entry = null;
continue;
}
if (swipe && entry.swipemark) {
rsize--;
entry = null;
continue;
}
entry.inprocessing = true;
if (swipe) {
entry.swipemark = true;
}
break;
}
}
return entry;
}
private void releaseNextEntry(List<TIDEntry> l, TIDEntry entry) {
if (l == null) {
entry.inprocessing = false;
return;
}
synchronized (l) {
entry.inprocessing = false;
}
}
@Override
public long runTask() {
try {
run(false);
} finally {
clearSwipeMark(clusterPCommitted);
clearSwipeMark(committed);
}
return 0L;
}
public void run(boolean oneOnly) {
if (!translist.isLoadComplete()) {
if (oneOnly) {
return;
}
try {
logger.log(logger.INFO, Globals.getBrokerResources().getString(BrokerResources.I_REAPER_WAIT_TXN_LOAD));
translist.loadCompleteLatch.await(TransactionList.TXN_REAPINTERVAL, TimeUnit.MILLISECONDS);
if (!translist.isLoadComplete()) {
return;
}
} catch (InterruptedException e) {
logger.log(logger.WARNING, "Transaction reaper thread is interrupted in waiting for transaction loading completion");
return;
}
}
ArrayList activatedBrokers = null;
ArrayList activatedPartitions = null;
if (!oneOnly) {
synchronized (translist.newlyActivatedBrokers) {
activatedBrokers = new ArrayList(translist.newlyActivatedBrokers);
translist.newlyActivatedBrokers.clear();
}
synchronized (translist.newlyActivatedPartitions) {
activatedPartitions = new ArrayList(translist.newlyActivatedPartitions);
translist.newlyActivatedPartitions.clear();
}
}
TIDEntry entry = null, tmpe = null;
int count = 0;
while (reapTimer != null) {
if (count > 0 && (entry == null || oneOnly)) {
break;
}
entry = null;
count++;
if (!oneOnly && (activatedBrokers.size() > 0 || activatedPartitions.size() > 0 || clusterPCommittedHasUnprocessed())) {
tmpe = getNextEntry(clusterPCommitted, 0, !oneOnly);
if (tmpe != null) {
entry = tmpe;
TransactionUID cpcTid = entry.tid;
ClusterPCommittedState processState = entry.pstate;
if (processState != null && processState == ClusterPCommittedState.UNPROCCESSED) {
Globals.getConnectionManager().removeFromClientDataList(IMQConnection.TRANSACTION_LIST, cpcTid);
}
try {
UID tranpid = translist.getPartitionedStore().getPartitionID();
TransactionBroker[] brokers = translist.getClusterTransactionBrokers(cpcTid);
boolean completed = true;
if (brokers != null) {
BrokerAddress to = null;
for (int j = 0; j < brokers.length; j++) {
if (!brokers[j].isCompleted()) {
completed = false;
to = brokers[j].getCurrentBrokerAddress();
if (to != null) {
if ((to.equals(Globals.getMyAddress()) && to.equals(brokers[j].getBrokerAddress()))
&& (!translist.DL.isPartitionMode() || tranpid.equals(brokers[j].getBrokerAddress().getStoreSessionUID()))) {
try {
translist.completeClusterTransactionBrokerState(cpcTid, TransactionState.COMMITTED, to, true);
} catch (Exception e) {
logger.logStack(logger.WARNING, "Unable to update transaction broker state for " + to + ", TUID=" + cpcTid, e);
}
if (!Globals.getHAEnabled()) {
continue;
}
}
if (activatedBrokers.size() == 0 && activatedPartitions.size() == 0 && processState != null
&& processState == ClusterPCommittedState.PROCCESSED) {
continue;
}
if (activatedBrokers.size() == 0 && !to.equals(Globals.getMyAddress()) && processState != null
&& processState != ClusterPCommittedState.TAKEOVER) {
continue;
}
if (activatedBrokers.size() > 0 && !activatedBrokers.contains(to.getMQAddress()) && !to.equals(Globals.getMyAddress())
&& processState != null && processState != ClusterPCommittedState.TAKEOVER) {
continue;
}
if (TransactionList.DEBUG_CLUSTER_TXN) {
logger.log(logger.INFO, "txnReaperThread: sendClusterTransactionInfo for TID=" + cpcTid + " to " + to);
}
Globals.getClusterBroadcast().sendClusterTransactionInfo(cpcTid.longValue(), to);
} else {
if (processState != null && processState != ClusterPCommittedState.PROCCESSED) {
logger.log(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.W_NOTIFY_TXN_COMPLETE_UNREACHABLE,
cpcTid.toString(), brokers[j].toString()));
}
}
}
}
} // if brokers != null;
entry.pstate = ClusterPCommittedState.PROCCESSED;
if (!completed) {
releaseNextEntry(clusterPCommitted, entry);
} else {
clusterPCommitted.remove(entry);
releaseNextEntry(null, entry);
committed.add(entry);
}
} catch (Throwable e) {
logger.logStack(logger.WARNING, e.getMessage(), e);
}
}
}
tmpe = getNextEntry(committed, TransactionList.TXN_REAPLIMIT, !oneOnly);
if (tmpe != null) {
entry = tmpe;
if (TransactionList.DEBUG_CLUSTER_TXN) {
logger.log(logger.INFO, "Cleaning up committed transaction " + entry.tid);
}
try {
try {
translist.reapTransactionID(entry.tid, noremoves.contains(entry));
} catch (BrokerException e) {
if (e.getStatusCode() != Status.NOT_FOUND) {
releaseNextEntry(committed, entry);
entry.swipeonly = true;
throw e;
}
logger.logStack(logger.WARNING, "Cleanup committed transaction: " + e.getMessage(), e);
}
committed.remove(entry);
noremoves.remove(entry);
} catch (Exception e) {
logger.logStack(logger.WARNING, "Failed to cleanup committed transaction " + entry.tid, e);
}
}
tmpe = getNextEntry(remoteCommitted, TransactionList.TXN_REAPLIMIT, !oneOnly);
if (tmpe != null) {
entry = tmpe;
remoteCommitted.remove(entry);
if (TransactionList.DEBUG_CLUSTER_TXN) {
logger.log(logger.INFO, "Cleaned up committed remote transaction " + entry.tid);
}
}
tmpe = getNextEntry(remoteRCommitted, TransactionList.TXN_REAPLIMIT, !oneOnly);
if (tmpe != null) {
entry = tmpe;
remoteRCommitted.remove(entry);
if (TransactionList.DEBUG_CLUSTER_TXN) {
logger.log(logger.INFO, "Cleaned up committed remote transaction " + entry.tid);
}
}
}
}
}