/
CommitMarkableResourceRecord.java
585 lines (507 loc) · 17.2 KB
/
CommitMarkableResourceRecord.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
/*
Copyright The Narayana Authors
SPDX-License-Identifier: Apache-2.0
*/
package com.arjuna.ats.internal.jta.resources.arjunacore;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;
import jakarta.transaction.RollbackException;
import jakarta.transaction.Status;
import jakarta.transaction.Synchronization;
import jakarta.transaction.SystemException;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import com.arjuna.ats.arjuna.exceptions.ObjectStoreException;
import com.arjuna.ats.internal.jta.resources.XAResourceErrorHandler;
import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple;
import org.jboss.tm.ConnectableResource;
import org.jboss.tm.XAResourceWrapper;
import com.arjuna.ats.arjuna.ObjectType;
import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.coordinator.AbstractRecord;
import com.arjuna.ats.arjuna.coordinator.BasicAction;
import com.arjuna.ats.arjuna.coordinator.RecordType;
import com.arjuna.ats.arjuna.coordinator.TwoPhaseOutcome;
import com.arjuna.ats.arjuna.coordinator.TxControl;
import com.arjuna.ats.arjuna.logging.tsLogger;
import com.arjuna.ats.arjuna.recovery.RecoveryManager;
import com.arjuna.ats.arjuna.recovery.RecoveryModule;
import com.arjuna.ats.arjuna.state.InputObjectState;
import com.arjuna.ats.arjuna.state.OutputObjectState;
import com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule;
import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple;
import com.arjuna.ats.internal.jta.xa.XID;
import com.arjuna.ats.jta.common.JTAEnvironmentBean;
import com.arjuna.ats.jta.logging.jtaLogger;
import com.arjuna.ats.jta.utils.XAHelper;
import com.arjuna.ats.jta.xa.XidImple;
import com.arjuna.common.internal.util.propertyservice.BeanPopulator;
/**
* The CommitMarkableResourceRecord does not support nested transactions
*
* If the database is down forever that a CommitMarkableResourceRecord is linked
* to, it will have the side effect of never expiring a RecoverAtomicAction.
*
* The CommitMarkableResourceRecord assumes the following table has been
* created:
*
* syb:
*
* create table xids (xid varbinary(144), transactionManagerID varchar(64),
* actionuid varbinary(28))
*
* ora:
*
* create table xids (xid RAW(144), transactionManagerID varchar(64), actionuid
* RAW(28))
*
* psql:
*
* create table xids (xid bytea, transactionManagerID varchar(64), actionuid
* bytea)
*
* h2:
*
* create table xids (xid varbinary(144), transactionManagerID varchar(64),
* actionuid varbinary(28))
*
* sybase notes: sp_configure "lock scheme",0,datarows
*/
public class CommitMarkableResourceRecord extends AbstractRecord {
private final String tableName;
private Xid xid;
private ConnectableResource connectableResource;
private boolean onePhase = false;
private String commitMarkableJndiName;
private boolean committed;
private int heuristic;
private BasicAction basicAction;
private String productName;
private String productVersion;
private boolean hasCompleted;
private static CommitMarkableResourceRecordRecoveryModule commitMarkableResourceRecoveryModule;
private static final JTAEnvironmentBean jtaEnvironmentBean = BeanPopulator
.getDefaultInstance(JTAEnvironmentBean.class);
private static final Map<String, String> commitMarkableResourceTableNameMap = jtaEnvironmentBean
.getCommitMarkableResourceTableNameMap();
private static final String defaultTableName = jtaEnvironmentBean
.getDefaultCommitMarkableTableName();
private boolean isPerformImmediateCleanupOfBranches = jtaEnvironmentBean
.isPerformImmediateCleanupOfCommitMarkableResourceBranches();
private Connection preparedConnection;
private static final boolean isNotifyRecoveryModuleOfCompletedBranches = jtaEnvironmentBean
.isNotifyCommitMarkableResourceRecoveryModuleOfCompleteBranches();
private static final Map<String, Boolean> isPerformImmediateCleanupOfCommitMarkableResourceBranchesMap = jtaEnvironmentBean
.getPerformImmediateCleanupOfCommitMarkableResourceBranchesMap();
static {
commitMarkableResourceRecoveryModule = null;
RecoveryManager recMan = RecoveryManager.manager();
Vector recoveryModules = recMan.getModules();
if (recoveryModules != null) {
Enumeration modules = recoveryModules.elements();
while (modules.hasMoreElements()) {
RecoveryModule m = (RecoveryModule) modules.nextElement();
if (m instanceof CommitMarkableResourceRecordRecoveryModule) {
commitMarkableResourceRecoveryModule = (CommitMarkableResourceRecordRecoveryModule) m;
break;
}
}
}
}
/**
* For recovery
*/
public CommitMarkableResourceRecord() {
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.CommitMarkableResourceRecord (), record id=" + order());
}
heuristic = TwoPhaseOutcome.FINISH_OK;
tableName = null;
}
public CommitMarkableResourceRecord(TransactionImple tx,
ConnectableResource xaResource, final Xid xid,
BasicAction basicAction) throws IllegalStateException,
RollbackException, SystemException {
super(new Uid(), null, ObjectType.ANDPERSISTENT);
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.CommitMarkableResourceRecord ( " + tx + ", " + xaResource + ", "
+ xid + ", " + basicAction + " ), record id=" + order());
}
this.connectableResource = xaResource;
XAResourceWrapper xaResourceWrapper = ((XAResourceWrapper) xaResource);
this.commitMarkableJndiName = xaResourceWrapper.getJndiName();
this.productName = xaResourceWrapper.getProductName();
this.productVersion = xaResourceWrapper.getProductVersion();
this.xid = xid;
this.basicAction = basicAction;
heuristic = TwoPhaseOutcome.FINISH_OK;
String tableName = commitMarkableResourceTableNameMap
.get(commitMarkableJndiName);
if (tableName != null) {
this.tableName = tableName;
} else {
this.tableName = defaultTableName;
}
Boolean boolean1 = isPerformImmediateCleanupOfCommitMarkableResourceBranchesMap
.get(commitMarkableJndiName);
if (boolean1 != null) {
isPerformImmediateCleanupOfBranches = boolean1;
}
if (isPerformImmediateCleanupOfBranches) {
// a session synch may enlist a CMR in a transaction so this sycnh must be correctly ordered
new TransactionSynchronizationRegistryImple()
.registerInterposedSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(int status) {
if (!onePhase && status == Status.STATUS_COMMITTED) {
Connection connection = null;
try {
connection = ((Connection) connectableResource
.getConnection());
connection.setAutoCommit(false);
String sql = "DELETE from "
+ CommitMarkableResourceRecord.this.tableName
+ " where xid in (?)";
PreparedStatement prepareStatement = connection
.prepareStatement(sql);
try {
XID toSave = ((XidImple) xid).getXID();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(
baos);
dos.writeInt(toSave.formatID);
dos.writeInt(toSave.gtrid_length);
dos.writeInt(toSave.bqual_length);
dos.writeInt(toSave.data.length);
dos.write(toSave.data);
dos.flush();
prepareStatement.setBytes(1, baos.toByteArray());
if (prepareStatement.executeUpdate() != 1) {
tsLogger.logger
.error("Update was not successfull");
connection.rollback();
} else {
connection.commit();
}
} catch (IOException e) {
tsLogger.logger
.warn("Could not generate prepareStatement paramaters",
e);
} finally {
try {
prepareStatement.close();
} catch (SQLException e) {
tsLogger.logger
.warn("Could not close the prepared statement",
e);
}
}
} catch (Throwable e1) {
tsLogger.logger
.warn("Could not delete CommitMarkableResourceRecord entry, will rely on RecoveryModule",
e1);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
tsLogger.logger
.warn("Could not close the preparedConnection",
e);
}
}
}
}
}
});
} else if (isNotifyRecoveryModuleOfCompletedBranches) {
// a session synch may enlist a CMR in a transaction so this sycnh must be correctly ordered
new TransactionSynchronizationRegistryImple()
.registerInterposedSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(int status) {
if (!onePhase && status == Status.STATUS_COMMITTED) {
commitMarkableResourceRecoveryModule
.notifyOfCompletedBranch(
commitMarkableJndiName, xid);
}
}
});
}
}
public String getProductName() {
return productName;
}
public String getProductVersion() {
return productVersion;
}
public String getJndiName() {
return commitMarkableJndiName;
}
public void updateOutcome(boolean committed) {
this.hasCompleted = true;
this.committed = committed;
}
/**
* We need to save this so we know there was a ConnectableResource in the
* intentions list.
*/
public boolean doSave() {
return true;
}
public boolean save_state(OutputObjectState os, int t) {
boolean res = false;
try {
// We store these information so that during recovery we can query
// the resource
// manager to see if it had committed prior to any potential crash
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("pack: " + commitMarkableJndiName);
}
os.packString(commitMarkableJndiName);
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("pack: " + xid);
}
XidImple.pack(os, xid);
os.packBoolean(hasCompleted);
if (hasCompleted) {
os.packBoolean(committed);
// os.packInt(heuristic);
}
os.packString(productName);
os.packString(productVersion);
res = super.save_state(os, t);
} catch (Exception e) {
jtaLogger.logger.warn(
"Could not save_state: " + XAHelper.xidToString(xid), e);
}
return res;
}
public boolean restore_state(InputObjectState os, int t) {
boolean res = false;
try {
commitMarkableJndiName = os.unpackString();
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("unpack: " + commitMarkableJndiName);
}
xid = XidImple.unpack(os);
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("unpack: " + xid);
}
if (os.unpackBoolean()) {
committed = os.unpackBoolean();
// heuristic = os.unpackInt();
} else {
// This will return true if the
// CommitMarkableRecoveryModule is
// between phases and the XID
// has not been GC'd
committed = commitMarkableResourceRecoveryModule.wasCommitted(commitMarkableJndiName, xid);
}
productName = os.unpackString();
productVersion = os.unpackString();
res = super.restore_state(os, t);
} catch (Exception e) {
String resInfo = connectableResource == null ? "" : connectableResource.toString();
jtaLogger.i18NLogger.warn_resources_arjunacore_restorestateerror(
resInfo, XAHelper.xidToString(xid), e);
}
return res;
}
/**
* This will add the required recovery data about this resource into the
* resources preparedConnection. If the preparedConnection is in read only
* mode, we do not need to persist this information.
*/
public int topLevelPrepare() {
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.topLevelPrepare for " + this + ", record id=" + order());
}
try {
PreparedStatement prepareStatement = null;
preparedConnection = (Connection) connectableResource
.getConnection();
try {
prepareStatement = preparedConnection
.prepareStatement("insert into "
+ tableName
+ " (xid, transactionManagerID, actionuid) values (?,?,?)");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
XID toSave = ((XidImple) xid).getXID();
dos.writeInt(toSave.formatID);
dos.writeInt(toSave.gtrid_length);
dos.writeInt(toSave.bqual_length);
dos.writeInt(toSave.data.length);
dos.write(toSave.data);
dos.flush();
prepareStatement.setBytes(1, baos.toByteArray());
prepareStatement.setString(2, TxControl.getXANodeName());
prepareStatement.setBytes(3, basicAction.get_uid().getBytes());
if (prepareStatement.executeUpdate() != 1) {
tsLogger.logger.warn("Update was not successful");
removeConnection();
return TwoPhaseOutcome.PREPARE_NOTOK;
}
} finally {
if (prepareStatement != null)
prepareStatement.close();
}
return TwoPhaseOutcome.PREPARE_OK;
} catch (Throwable t) {
tsLogger.logger.error(
"Could not add recovery data to the 1PC resource", t);
return TwoPhaseOutcome.PREPARE_NOTOK;
}
}
public int topLevelAbort() {
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.topLevelAbort for " + this + ", record id=" + order());
}
try {
try {
// This can never be null as it can only ever be called before
// crash
// when we have a reference
// on a connectableResource still. Although topLevelAbort can be
// called for RecoverAtomicAction, it
// can only do that for resources after the head position in the
// preparedList, we know this resource
// must be first
((XAResource) connectableResource).rollback(xid);
hasCompleted = true;
committed = false;
return TwoPhaseOutcome.FINISH_OK;
} catch (XAException e) {
XAResourceErrorHandler handler = new XAResourceErrorHandler(e, (XAResource) connectableResource, xid);
heuristic = handler.handleCMRRollbackError();
return heuristic;
} catch (Throwable e) {
jtaLogger.i18NLogger.warn_resources_arjunacore_rollbackerror(XAHelper.xidToString(xid),
connectableResource.toString(), "-", e);
return TwoPhaseOutcome.FINISH_ERROR;
}
} finally {
removeConnection();
}
}
public int topLevelCommit() {
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.topLevelCommit for " + this + ", record id=" + order());
}
return commit(false);
}
public int topLevelOnePhaseCommit() {
if (tsLogger.logger.isTraceEnabled()) {
tsLogger.logger.trace("CommitMarkableResourceRecord.topLevelOnePhaseCommit for " + this + ", record id=" + order());
}
return commit(true);
}
private int commit(boolean onePhase) {
// As this can be called during recovery we check to see if we have the
// pre-crash reference
this.onePhase = onePhase;
if (connectableResource != null) {
try {
((XAResource) connectableResource).commit(xid, false);
hasCompleted = true;
committed = true;
return TwoPhaseOutcome.FINISH_OK;
} catch (XAException e) {
XAResourceErrorHandler handler = new XAResourceErrorHandler(e, (XAResource) connectableResource, xid);
heuristic = handler.handleCMRCommitError(onePhase);
committed = handler.isCommitted();
return heuristic;
} catch (Throwable e) {
jtaLogger.i18NLogger.warn_resources_arjunacore_commitxaerror(XAHelper.xidToString(xid),
connectableResource.toString(), "-", e);
return TwoPhaseOutcome.FINISH_ERROR;
} finally {
if (!isPerformImmediateCleanupOfBranches) {
removeConnection();
}
}
} else {
// This is a recovery scenario
if (committed) {
return TwoPhaseOutcome.FINISH_OK;
} else {
return TwoPhaseOutcome.HEURISTIC_ROLLBACK;
}
}
}
private final void removeConnection() {
if (preparedConnection != null) {
try {
preparedConnection.close();
preparedConnection = null;
} catch (SQLException e) {
tsLogger.logger.warn("Could not close the preparedConnection", e);
}
}
}
@Override
public boolean forgetHeuristic() {
// This is a no-op for CMR resources
return true;
}
public int getHeuristic() {
return heuristic;
}
public Uid order() {
return Uid.minUid();
}
public boolean propagateOnCommit() {
return false;
}
public int typeIs() {
return RecordType.COMMITMARKABLERESOURCE;
}
public String type() {
return "/StateManager/AbstractRecord/CommitMarkableResourceRecord";
}
public Object value() {
return connectableResource;
}
public void setValue(Object o) {
}
public int nestedAbort() {
return TwoPhaseOutcome.FINISH_OK;
}
public int nestedCommit() {
return TwoPhaseOutcome.FINISH_ERROR;
}
public int nestedPrepare() {
return TwoPhaseOutcome.PREPARE_NOTOK;
}
public void merge(AbstractRecord a) {
}
public void alter(AbstractRecord a) {
}
public boolean shouldAdd(AbstractRecord a) {
return false;
}
public boolean shouldAlter(AbstractRecord a) {
return false;
}
public boolean shouldMerge(AbstractRecord a) {
return false;
}
public boolean shouldReplace(AbstractRecord a) {
return false;
}
}