forked from jbosstm/narayana
/
LRARecord.java
805 lines (652 loc) · 29.1 KB
/
LRARecord.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package io.narayana.lra.coordinator.domain.model;
import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.coordinator.AbstractRecord;
import com.arjuna.ats.arjuna.coordinator.RecordType;
import com.arjuna.ats.arjuna.coordinator.TwoPhaseOutcome;
import com.arjuna.ats.arjuna.state.InputObjectState;
import com.arjuna.ats.arjuna.state.OutputObjectState;
import io.narayana.lra.client.Current;
import io.narayana.lra.coordinator.domain.service.LRAService;
import io.narayana.lra.logging.LRALogger;
import org.eclipse.microprofile.lra.annotation.CompensatorStatus;
import org.eclipse.microprofile.lra.client.GenericLRAException;
import org.eclipse.microprofile.lra.client.InvalidLRAIdException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import static org.eclipse.microprofile.lra.client.LRAClient.LRA_HTTP_HEADER;
import static org.eclipse.microprofile.lra.client.LRAClient.LRA_HTTP_RECOVERY_HEADER;
public class LRARecord extends AbstractRecord implements Comparable<AbstractRecord> {
private static String TYPE_NAME = "/StateManager/AbstractRecord/LRARecord";
private static long PARTICIPANT_TIMEOUT = 1; // number of seconds to wait for requests
private Transaction lra;
private URL lraId;
private URL recoveryURL;
private String participantPath;
private URL completeURI;
private URL compensateURI;
private URL statusURI;
private URL forgetURI;
private String responseData;
private LocalTime cancelOn; // TODO make sure this acted upon during restore_state()
private String compensatorData;
private ScheduledFuture<?> scheduledAbort;
private LRAService lraService;
private CompensatorStatus status;
boolean accepted;
public LRARecord() {
}
LRARecord(LRAService lraService, String lraId, String linkURI, String compensatorData) {
super(new Uid());
try {
// if compensateURI is a link parse it into compensate,complete and status urls
if (linkURI.startsWith("<")) {
linkURI = cannonicalForm(linkURI);
Exception[] parseException = { null };
Arrays.stream(linkURI.split(",")).forEach((linkStr) -> {
Exception e = parseLink(linkStr);
if (e != null)
parseException[0] = e;
});
if (parseException[0] != null)
throw new InvalidLRAIdException(lraId, "Invalid link URL", parseException[0]);
} else {
this.compensateURI = new URL(String.format("%s/compensate", linkURI));
this.completeURI = new URL(String.format("%s/complete", linkURI));
this.statusURI = new URL(String.format("%s", linkURI));
this.forgetURI = new URL(String.format("%s", linkURI));
}
this.lraId = new URL(lraId);
this.lraService = lraService;
this.participantPath = linkURI;
this.recoveryURL = null;
this.compensatorData = compensatorData;
} catch (MalformedURLException e) {
LRALogger.i18NLogger.error_invalidFormatToCreateLRARecord(lraId, linkURI);
throw new InvalidLRAIdException(lraId, "Invalid LRA id", e);
}
}
String getParticipantPath() {
return participantPath;
}
private static String cannonicalForm(String linkStr) {
if (linkStr.indexOf(',') == -1)
return linkStr;
SortedMap<String, String> lm = new TreeMap<>();
Arrays.stream(linkStr.split(",")).forEach(link -> lm.put(Link.valueOf(link).getRel(), link));
StringBuilder sb = new StringBuilder();
lm.forEach((k, v) -> appendLink(sb, v));
return sb.toString();
}
private static StringBuilder appendLink(StringBuilder b, String value) {
if (b.length() != 0)
b.append(',');
return b.append(value);
}
static String extractCompensator(URL lraId, String linkStr) {
for (String lnk : linkStr.split(",")) {
Link link;
try {
link = Link.valueOf(lnk);
} catch (Exception e) {
throw new GenericLRAException(lraId, Response.Status.PRECONDITION_FAILED.getStatusCode(),
String.format("Invalid compensator join request: cannot parse link '%s'", linkStr), e);
}
if ("compensate".equals(link.getRel()))
return link.getUri().toString();
}
return linkStr;
}
private MalformedURLException parseLink(String linkStr) {
Link link = Link.valueOf(linkStr);
String rel = link.getRel();
String uri = link.getUri().toString();
try {
if ("compensate".equals(rel)) {
compensateURI = new URL(uri);
} else if ("complete".equals(rel)) {
completeURI = new URL(uri);
} else if ("status".equals(rel)) {
statusURI = new URL(uri);
} else if ("forget".equals(rel)) {
forgetURI = new URL(uri);
} else if ("participant".equals(rel)) {
compensateURI = new URL(uri + "/compensate");
completeURI = new URL(uri + "/complete");
statusURI = forgetURI = new URL(uri);
}
return null;
} catch (MalformedURLException e) {
return e;
}
}
@Override
public int topLevelPrepare() {
return TwoPhaseOutcome.PREPARE_OK;
}
private void topLevelAbortFromTimer() {
if (lra != null) {
lra.timedOut(this);
} else {
doEnd(true);
}
}
@Override
// NB if a participant needs to know
// if the lra completed then it can ask the io.narayana.lra.coordinator. A 404 status implies:
// - all compensators completed ok, or
// - all compensators compensated ok
// This participant can infer which possibility happened since it will have been told to complete or compensate
public int topLevelAbort() {
return doEnd(true);
}
@Override
public int topLevelOnePhaseCommit() {
return topLevelCommit();
}
@Override
public int topLevelCommit() {
return doEnd(false);
}
private int doEnd(boolean compensate) {
assert lraService != null;
ReentrantLock lock = lraService.lockTransaction(lraId);
try {
return tryDoEnd(compensate);
} finally {
lock.unlock();
}
}
private int tryDoEnd(boolean compensate) {
URL endPath;
// cancel any timer associated with this participant
if (scheduledAbort != null) {
// NB this could have been called from the scheduler so don't cancel our self!
scheduledAbort.cancel(false);
scheduledAbort = null;
}
if (CompensatorStatus.Compensating.equals(status))
compensate = true;
if (compensate) {
if (isCompensated())
return TwoPhaseOutcome.FINISH_OK; // the participant has already compensated
endPath = compensateURI; // we are going to ask the participant to compensate
status = CompensatorStatus.Compensating;
} else {
if (isCompelete() || completeURI == null) {
status = CompensatorStatus.Completed;
return TwoPhaseOutcome.FINISH_OK; // the participant has already completed
}
endPath = completeURI; // we are going to ask the participant to complete
status = CompensatorStatus.Completing;
}
// NB trying to compensate when already completed is allowed (for nested LRAs)
int httpStatus = -1;
if (accepted) {
// the participant has previously returned a HTTP 200 Accepted response
// to indicate that it is in progress in which case the status URL
// must be valid so try that first for the status
int twoPhaseOutcome = retryGetEndStatus(endPath, compensate);
if (twoPhaseOutcome != -1)
return twoPhaseOutcome;
} else {
httpStatus = tryLocalEndInvocation(endPath); // see if participant is in the same JVM
}
if (httpStatus == -1) {
// the local invocation was not made so fallback to using JAX-RS
Client client = ClientBuilder.newClient();
WebTarget target = client.target(URI.create(endPath.toExternalForm()));
try {
// ask the participant to complete or compensate
Future<Response> asyncResponse = target.request()
.header(LRA_HTTP_HEADER, lraId.toExternalForm())
.header(LRA_HTTP_RECOVERY_HEADER, recoveryURL.toExternalForm())
.property(LRA_HTTP_HEADER, lraId) // make the context available to the jaxrs filters
.async()
.put(Entity.entity(compensatorData, MediaType.APPLICATION_JSON));
// the catch block below catches any Timeout exception
Response response = asyncResponse.get(PARTICIPANT_TIMEOUT, TimeUnit.SECONDS);
httpStatus = response.getStatus();
accepted = httpStatus == Response.Status.ACCEPTED.getStatusCode();
if (accepted && statusURI == null) {
// the participant could not finish immediately and we have no status URL so one should be
// present in the Location header
Object lh = response.getHeaders().getFirst(HttpHeaders.LOCATION);
if (lh != null)
try {
statusURI = new URL((String) lh);
} catch (MalformedURLException e) {
if (LRALogger.logger.isInfoEnabled()) {
LRALogger.logger.infof("LRARecord.doEnd missing Location header on ACCEPTED response %s failed: %s",
target.getUri(), e.getMessage());
}
}
}
if (httpStatus == Response.Status.NOT_FOUND.getStatusCode()) {
updateStatus(compensate);
return TwoPhaseOutcome.FINISH_OK; // the participant must have finished ok but we lost the response
}
if (response.hasEntity())
responseData = response.readEntity(String.class);
} catch (Exception e) {
if (LRALogger.logger.isInfoEnabled()) {
LRALogger.logger.infof("LRARecord.doEnd put %s failed: %s",
target.getUri(), e.getMessage());
}
} finally {
client.close();
}
}
if (httpStatus == Response.Status.OK.getStatusCode() && responseData != null) {
// see if any content contains a failed status
if (compensate && CompensatorStatus.FailedToCompensate.name().equals(responseData)) {
return reportFailure(true, endPath.toExternalForm());
} else if (!compensate && CompensatorStatus.FailedToComplete.name().equals(responseData)) {
return reportFailure(false, endPath.toExternalForm());
}
}
if (httpStatus != Response.Status.OK.getStatusCode()
&& httpStatus != Response.Status.NO_CONTENT.getStatusCode()
&& !accepted) {
if (LRALogger.logger.isDebugEnabled()) {
LRALogger.logger.debugf("LRARecord.doEnd put %s failed with status: %d",
endPath, httpStatus);
}
if (compensate) {
status = CompensatorStatus.Compensating; // recovery will figure out the status via the status url
/*
* We are mapping compensate onto Abort. TwoPhaseCoordinator uses presumed abort
* so if we were to return FINISH_ERROR recovery would not replay the log.
* To force the record to be eligible for recovery we return a heuristic hazard.
*/
return TwoPhaseOutcome.HEURISTIC_HAZARD;
}
status = CompensatorStatus.Completing; // recovery will figure out the status via the status url
return TwoPhaseOutcome.FINISH_ERROR;
}
updateStatus(compensate);
// if the the request is still in progress (ie accepted is true) let recovery finish it
return accepted ? TwoPhaseOutcome.HEURISTIC_HAZARD : TwoPhaseOutcome.FINISH_OK;
}
private void updateStatus(boolean compensate) {
if (compensate)
status = accepted ? CompensatorStatus.Compensating : CompensatorStatus.Compensated;
else
status = accepted ? CompensatorStatus.Completing : CompensatorStatus.Completed;
}
private int reportFailure(boolean compensate, String endPath) {
status = compensate ? CompensatorStatus.FailedToCompensate : CompensatorStatus.FailedToComplete;
LRALogger.logger.warnf("LRARecord: participant %s reported a failure to %s",
endPath, compensate ? "compensate" : "complete");
// permanently failed so tell recovery to ignore us in the future. TODO could move it to
// another list for reporting
return TwoPhaseOutcome.FINISH_OK;
}
private int retryGetEndStatus(URL endPath, boolean compensate) {
assert accepted;
// the participant has previously returned a HTTP 202 Accepted response so the status URL
// must be valid - try that first for the status
// first check that this isn't a nested coordinator running locally
URL nestedLraId = extractParentLRA(endPath);
if (nestedLraId != null && lraService != null) {
Transaction transaction = lraService.getTransaction(nestedLraId);
if (transaction != null) {
CompensatorStatus cStatus = transaction.getLRAStatus();
if (cStatus == null) {
LRALogger.logger.warnf("LRARecord.retryGetEndStatus: local LRA %s accepted but has a null status",
endPath);
return -1; // shouldn't happen since it imples it's still be active - force end to be called
}
switch (cStatus) {
case Completed:
case Compensated:
return TwoPhaseOutcome.FINISH_OK;
case Completing:
case Compensating:
return TwoPhaseOutcome.HEURISTIC_HAZARD;
case FailedToCompensate:
case FailedToComplete:
return TwoPhaseOutcome.FINISH_ERROR;
}
}
} else if (statusURI != null) {
// it is a standard participant - check the status URL
Client client = ClientBuilder.newClient();
try {
WebTarget target = client.target(statusURI.toURI());
// since this method is called from the recovery thread do not block
Future<Response> asyncResponse = target.request()
.header(LRA_HTTP_HEADER, lraId.toExternalForm())
.header(LRA_HTTP_RECOVERY_HEADER, recoveryURL.toExternalForm())
.property(LRA_HTTP_HEADER, lraId) // make the context available to the jaxrs filters
.async()
.get();
// if the attempt times out the catch block below will return a heuristic
Response response = asyncResponse.get(PARTICIPANT_TIMEOUT, TimeUnit.SECONDS);
if (response.getStatus() == Response.Status.PRECONDITION_FAILED.getStatusCode()) {
// the participant never got the end request resend it
return -1;
}
if (response.getStatus() == Response.Status.OK.getStatusCode() &&
response.hasEntity()) {
// the participant is available again and has reported its status
String s = response.readEntity(String.class);
status = CompensatorStatus.valueOf(s);
switch (status) {
case Completed:
case Compensated:
return TwoPhaseOutcome.FINISH_OK;
case Completing:
case Compensating:
// still in progress - make sure recovery keeps retrying it
return TwoPhaseOutcome.HEURISTIC_HAZARD;
case FailedToCompensate:
case FailedToComplete:
// the participant could not finish - log a warning and forget
LRALogger.logger.warnf(
"LRARecord.doEnd(compensate %b) get status %s did not finish: %s: WILL NOT RETRY",
compensate, target.getUri(), status);
if (forgetURI != null)
forgetURI = statusURI; // forget is equivalent to HTTP delete on the status URL
if (forgetURI != null) {
try {
// let the participant know he can clean up
WebTarget target2 = client.target(forgetURI.toURI());
Future<Response> asyncResponse2 = target2.request()
.header(LRA_HTTP_HEADER, lraId.toExternalForm())
.header(LRA_HTTP_RECOVERY_HEADER, recoveryURL.toExternalForm())
.property(LRA_HTTP_HEADER, lraId) // make the context available to the jaxrs filters
.async()
.delete();
if (asyncResponse2.get(PARTICIPANT_TIMEOUT, TimeUnit.SECONDS).getStatus() ==
Response.Status.OK.getStatusCode())
return TwoPhaseOutcome.FINISH_OK;
} catch (Exception e) {
if (LRALogger.logger.isInfoEnabled()) {
LRALogger.logger.infof("LRARecord.doEnd forget URI %s is invalid (%s)",
forgetURI, e.getMessage());
}
// TODO write a test to ensure that recovery only retries the forget request
return TwoPhaseOutcome.HEURISTIC_HAZARD; // force recovery to keep retrying
} finally {
Current.pop();
}
} else {
LRALogger.logger.warnf(
"LRARecord.doEnd(%b) LRA: %s: cannot forget %s: missing forget URI",
compensate, lraId, statusURI, status);
}
if (compensate)
return TwoPhaseOutcome.FINISH_OK;
return TwoPhaseOutcome.FINISH_OK;
}
}
} catch (Throwable e) {
if (LRALogger.logger.isInfoEnabled()) {
LRALogger.logger.infof("LRARecord.doEnd status URI %s is invalid (%s)",
statusURI, e.getMessage());
}
return TwoPhaseOutcome.HEURISTIC_HAZARD; // force recovery to keep retrying
} finally {
Current.pop();
client.close();
}
}
return -1;
}
// see if the participant is an LRA in the same VM as the coordinator
private URL extractParentLRA(URL endPath) {
if (lraService != null) {
String[] segments = endPath.getPath().split("/");
int pCnt = segments.length;
if (pCnt > 1) {
String cId;
try {
cId = URLDecoder.decode(segments[pCnt - 2], "UTF-8");
return lraService.hasTransaction(cId) ? new URL(cId) : null;
} catch (UnsupportedEncodingException | MalformedURLException ignore) {
}
}
}
return null;
}
private int tryLocalEndInvocation(URL endPath) {
URL cId = extractParentLRA(endPath);
if (cId != null) {
String[] segments = endPath.getPath().split("/");
int pCnt = segments.length;
// this is a call from a parent LRA to end the nested LRA:
boolean isCompensate = "compensate".equals(segments[pCnt - 1]);
boolean isComplete = "complete".equals(segments[pCnt - 1]);
int httpStatus;
if (!isCompensate && !isComplete) {
if (LRALogger.logger.isInfoEnabled()) {
LRALogger.logger.infof("LRARecord.doEnd invalid nested participant url %s" +
"(should be compensate or complete)",
endPath.toExternalForm());
}
httpStatus = Response.Status.BAD_REQUEST.getStatusCode();
} else {
LRAStatus inVMStatus = lraService.endLRA(cId, isCompensate, true);
httpStatus = inVMStatus.getHttpStatus();
try {
responseData = inVMStatus.getEncodedResponseData();
} catch (IOException ignore) {
}
}
return httpStatus;
}
// fall back to using JAX-RS
return -1;
}
boolean forget() {
return true;
}
private boolean isCompelete() {
return status != null && status == CompensatorStatus.Completed;
}
private boolean isCompensated() {
return status != null && status == CompensatorStatus.Compensated;
}
String getResponseData() {
return responseData;
}
@Override
public boolean save_state(OutputObjectState os, int t) {
if (super.save_state(os, t)) {
try {
packURL(os, lraId);
packURL(os, compensateURI);
packURL(os, recoveryURL);
packURL(os, completeURI);
packURL(os, statusURI);
packURL(os, forgetURI);
packStatus(os);
os.packString(participantPath);
os.packString(compensatorData);
} catch (IOException e) {
return false;
}
}
return true;
}
@Override
public boolean restore_state(InputObjectState os, int t) {
if (super.restore_state(os, t)) {
try {
lraId = unpackURL(os);
compensateURI = unpackURL(os);
recoveryURL = unpackURL(os);
completeURI = unpackURL(os);
statusURI = unpackURL(os);
forgetURI = unpackURL(os);
unpackStatus(os);
participantPath = os.unpackString();
compensatorData = os.unpackString();
accepted = status == CompensatorStatus.Completing || status == CompensatorStatus.Compensating;
} catch (IOException e) {
return false;
}
}
return true;
}
private void packStatus(OutputObjectState os) throws IOException {
if (status == null) {
os.packBoolean(false);
} else {
os.packBoolean(true);
os.packInt(status.ordinal());
}
}
private void unpackStatus(InputObjectState os) throws IOException {
status = os.unpackBoolean() ? CompensatorStatus.values()[os.unpackInt()] : null;
}
private void packURL(OutputObjectState os, URL url) throws IOException {
if (url == null) {
os.packBoolean(false);
} else {
os.packBoolean(true);
os.packString(url.toExternalForm());
}
}
private URL unpackURL(InputObjectState os) throws IOException {
return os.unpackBoolean() ? new URL(os.unpackString()) : null;
}
private static int getTypeId() {
return RecordType.USER_DEF_FIRST0; // RecordType.LRA_RECORD; TODO we depend on swarm for narayana which is using an earlier version
}
public int typeIs() {
return getTypeId();
}
public int nestedAbort() {
return TwoPhaseOutcome.FINISH_OK;
}
public int nestedCommit() {
return TwoPhaseOutcome.FINISH_OK;
}
public int nestedPrepare() {
return TwoPhaseOutcome.PREPARE_OK; // do nothing
}
public int nestedOnePhaseCommit() {
return TwoPhaseOutcome.FINISH_ERROR;
}
public String type() {
return TYPE_NAME;
}
public boolean doSave() {
return true;
}
public void merge(AbstractRecord a) {
}
public void alter(AbstractRecord a) {
}
public boolean shouldAdd(AbstractRecord a) {
return (a.typeIs() == typeIs());
}
public boolean shouldAlter(AbstractRecord a) {
return false;
}
public boolean shouldMerge(AbstractRecord a) {
return false;
}
public boolean shouldReplace(AbstractRecord a) {
return false;
}
@Override
public Object value() {
return null; // LRA does not support heuristics
}
@Override
public void setValue(Object o) {
}
@Override
public int compareTo(AbstractRecord other) {
if (lessThan(other))
return -1;
if (greaterThan(other))
return 1;
return 0;
}
boolean setTimeLimit(ScheduledExecutorService scheduler, long timeLimit, Transaction lra) {
this.lra = lra;
return scheduleCancelation(this::topLevelAbortFromTimer, scheduler, timeLimit);
}
private boolean scheduleCancelation(Runnable runnable, ScheduledExecutorService scheduler, Long timeLimit) {
if ((scheduledAbort != null && !scheduledAbort.cancel(false)))
return false;
if (timeLimit > 0) {
cancelOn = LocalTime.now().plusNanos(timeLimit * 1000000);
scheduledAbort = scheduler.schedule(runnable, timeLimit, TimeUnit.MILLISECONDS);
} else {
cancelOn = null;
scheduledAbort = null;
}
return true;
}
public URL getRecoveryCoordinatorURL() {
return recoveryURL;
}
public String getParticipantURL() {
return participantPath;
}
void setRecoveryURL(String recoveryURL) {
try {
this.recoveryURL = new URL(recoveryURL);
} catch (MalformedURLException e) {
throw new InvalidLRAIdException(recoveryURL, "Invalid recovery id", e);
}
}
void setRecoveryURL(String recoveryUrlBase, String txId, String coordinatorId) {
setRecoveryURL(recoveryUrlBase + txId + '/' + coordinatorId);
}
public String getCompensator() {
return compensateURI.toExternalForm();
}
void setLRAService(LRAService lraService) {
this.lraService = lraService;
}
public void setLraService(LRAService lraService) {
this.lraService = lraService;
}
}