Skip to content

Commit

Permalink
JBTM-3755 Support for passing participant data to LRA callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmusgrov committed Mar 17, 2023
1 parent e22e44b commit a774f79
Show file tree
Hide file tree
Showing 21 changed files with 769 additions and 81 deletions.
23 changes: 23 additions & 0 deletions narayana-bom/pom.xml
Expand Up @@ -884,10 +884,33 @@
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>${version.microprofile.config-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config</artifactId>
<version>${version.io.smallrye.smallrye-config}</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</exclusion>
<exclusion>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.fault-tolerance</groupId>
<artifactId>microprofile-fault-tolerance-api</artifactId>
<version>${version.microprofile.fault-tolerance}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.lra</groupId>
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Expand Up @@ -201,6 +201,7 @@
<version.httpcomponents>4.5.14</version.httpcomponents>
<version.io.mashona>1.0.0.Beta1</version.io.mashona>
<version.io.narayana.checkstyle-config>1.0.1.Final</version.io.narayana.checkstyle-config>
<version.io.smallrye.smallrye-config>1.5.0</version.io.smallrye.smallrye-config>
<version.jakarta.activation.jakarta.activation-api>2.1.0</version.jakarta.activation.jakarta.activation-api>
<version.jakarta.annotation.jakarta-annotation-api>2.1.1</version.jakarta.annotation.jakarta-annotation-api>
<version.jakarta.ejb.jakarta-ejb-api>4.0.1</version.jakarta.ejb.jakarta-ejb-api>
Expand Down Expand Up @@ -234,6 +235,7 @@
<version.maven-surefire-plugin>3.0.0-M7</version.maven-surefire-plugin>
<version.maven.checkstyle-plugin>3.1.2</version.maven.checkstyle-plugin>
<version.maven.jandex-plugin>1.0.6</version.maven.jandex-plugin>
<version.microprofile.config-api>3.0.2</version.microprofile.config-api>
<version.microprofile.fault-tolerance>4.0.2</version.microprofile.fault-tolerance>
<version.microprofile.lra>2.0-RC1</version.microprofile.lra>
<version.mysql>8.0.25</version.mysql>
Expand Down
@@ -0,0 +1,22 @@
/*
* Copyright The Narayana Authors
*
* SPDX-License-Identifier: LGPL-2.1-only
*/

package io.narayana.lra.client;

import jakarta.enterprise.context.RequestScoped;

@RequestScoped
public class LRAParticipantData {
private String data;

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}
Expand Up @@ -80,6 +80,8 @@
import static io.narayana.lra.LRAConstants.FORGET;
import static io.narayana.lra.LRAConstants.LEAVE;
import static io.narayana.lra.LRAConstants.NARAYANA_LRA_API_VERSION_HEADER_NAME;
import static io.narayana.lra.LRAConstants.NARAYANA_LRA_PARTICIPANT_DATA_HEADER_NAME;
import static io.narayana.lra.LRAConstants.NARAYANA_LRA_PARTICIPANT_LINK_HEADER_NAME;
import static io.narayana.lra.LRAConstants.PARENT_LRA_PARAM_NAME;
import static io.narayana.lra.LRAConstants.RECOVERY_COORDINATOR_PATH_NAME;
import static io.narayana.lra.LRAConstants.STATUS;
Expand Down Expand Up @@ -365,11 +367,19 @@ public URI startLRA(URI parentLRA, String clientID, Long timeout, ChronoUnit uni
}

public void cancelLRA(URI lraId) throws WebApplicationException {
endLRA(lraId, false);
endLRA(lraId, false, null, null);
}

public void closeLRA(URI lraId) throws WebApplicationException {
endLRA(lraId, true);
endLRA(lraId, true, null, null);
}

public void cancelLRA(URI lraId, String compensator, String userData) throws WebApplicationException {
endLRA(lraId, false, compensator, userData);
}

public void closeLRA(URI lraId, String compensator, String userData) throws WebApplicationException {
endLRA(lraId, true, compensator, userData);
}

/**
Expand All @@ -390,6 +400,14 @@ public void closeLRA(URI lraId) throws WebApplicationException {
public URI joinLRA(URI lraId, Long timeLimit,
URI compensateUri, URI completeUri, URI forgetUri, URI leaveUri, URI afterUri, URI statusUri,
String compensatorData) throws WebApplicationException {
return enlistCompensator(lraId, timeLimit, "",
compensateUri, completeUri,
forgetUri, leaveUri, afterUri, statusUri,
null);
}
public URI joinLRA(URI lraId, Long timeLimit,
URI compensateUri, URI completeUri, URI forgetUri, URI leaveUri, URI afterUri, URI statusUri,
StringBuilder compensatorData) throws WebApplicationException {
return enlistCompensator(lraId, timeLimit, "",
compensateUri, completeUri,
forgetUri, leaveUri, afterUri, statusUri,
Expand All @@ -407,7 +425,7 @@ public URI joinLRA(URI lraId, Long timeLimit,
* @throws WebApplicationException if the LRA coordinator failed to enlist the participant
*/
public URI joinLRA(URI lraId, Long timeLimit,
URI participantUri, String compensatorData) throws WebApplicationException {
URI participantUri, StringBuilder compensatorData) throws WebApplicationException {
validateURI(participantUri, false, "Invalid participant URL: %s");
StringBuilder linkHeaderValue
= makeLink(new StringBuilder(), null, "participant", participantUri.toASCIIString());
Expand Down Expand Up @@ -685,7 +703,7 @@ private static StringBuilder makeLink(StringBuilder b, String uriPrefix, String
private URI enlistCompensator(URI lraUri, Long timelimit, String uriPrefix,
URI compensateUri, URI completeUri,
URI forgetUri, URI leaveUri, URI afterUri, URI statusUri,
String compensatorData) {
StringBuilder compensatorData) {
validateURI(completeUri, true, "Invalid complete URL: %s");
validateURI(compensateUri, true, "Invalid compensate URL: %s");
validateURI(leaveUri, true, "Invalid status URL: %s");
Expand All @@ -711,7 +729,7 @@ private URI enlistCompensator(URI lraUri, Long timelimit, String uriPrefix,
return enlistCompensator(lraUri, timelimit, linkHeaderValue.toString(), compensatorData);
}

private URI enlistCompensator(URI uri, Long timelimit, String linkHeader, String compensatorData) {
public URI enlistCompensator(URI uri, Long timelimit, String linkHeader, StringBuilder compensatorData) {
// register with the coordinator
// put the lra id in an http header
Client client = null;
Expand All @@ -736,6 +754,7 @@ private URI enlistCompensator(URI uri, Long timelimit, String linkHeader, String
.queryParam(TIMELIMIT_PARAM_NAME, timelimit)
.request()
.header(NARAYANA_LRA_API_VERSION_HEADER_NAME, LRAConstants.CURRENT_API_VERSION_STRING)
.header(NARAYANA_LRA_PARTICIPANT_DATA_HEADER_NAME, compensatorData)
.header("Link", linkHeader)
.async()
.put(Entity.text(compensatorData == null ? linkHeader : compensatorData))
Expand All @@ -759,6 +778,13 @@ private URI enlistCompensator(URI uri, Long timelimit, String linkHeader, String
}

String recoveryUrl = null;
String prevParticipantData = response.getHeaderString(NARAYANA_LRA_PARTICIPANT_DATA_HEADER_NAME);

if (compensatorData != null && prevParticipantData != null) {
compensatorData.setLength(0);
compensatorData.append(prevParticipantData);
}

try {
recoveryUrl = response.getHeaderString(LRA_HTTP_RECOVERY_HEADER);
return new URI(recoveryUrl);
Expand All @@ -781,7 +807,7 @@ private URI enlistCompensator(URI uri, Long timelimit, String linkHeader, String
}
}

private void endLRA(URI lra, boolean confirm) throws WebApplicationException {
private void endLRA(URI lra, boolean confirm, String compensator, String userData) throws WebApplicationException {
Client client = null;
Response response = null;

Expand All @@ -795,6 +821,8 @@ private void endLRA(URI lra, boolean confirm) throws WebApplicationException {
.path(confirm ? String.format(CLOSE_PATH, lraUid) : String.format(CANCEL_PATH, lraUid))
.request()
.header(NARAYANA_LRA_API_VERSION_HEADER_NAME, LRAConstants.CURRENT_API_VERSION_STRING)
.header(NARAYANA_LRA_PARTICIPANT_LINK_HEADER_NAME, compensator)
.header(NARAYANA_LRA_PARTICIPANT_DATA_HEADER_NAME, userData)
.async()
.put(Entity.text(""))
.get(END_TIMEOUT, TimeUnit.SECONDS);
Expand Down
10 changes: 9 additions & 1 deletion rts/lra/coordinator/pom.xml
Expand Up @@ -25,10 +25,18 @@
<groupId>org.eclipse.microprofile.lra</groupId>
<artifactId>microprofile-lra-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.fault-tolerance</groupId>
<artifactId>microprofile-fault-tolerance-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.narayana.rts</groupId>
Expand Down

0 comments on commit a774f79

Please sign in to comment.