Skip to content

Commit

Permalink
JBTM-3781 Narayana doesn't implement mp.lra.propagation.active config…
Browse files Browse the repository at this point in the history
… property
  • Loading branch information
xstefank authored and mmusgrov committed Jan 2, 2024
1 parent 3f6fb54 commit 63357b1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
5 changes: 5 additions & 0 deletions rts/lra/jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<artifactId>microprofile-lra-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.ws.rs.client.ClientRequestFilter;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;
import org.eclipse.microprofile.config.ConfigProvider;

import java.net.URI;

Expand All @@ -34,6 +35,13 @@
@Provider
public class ClientLRARequestFilter implements ClientRequestFilter {

private boolean canPropagate;

public ClientLRARequestFilter() {
canPropagate = ConfigProvider.getConfig()
.getOptionalValue("mp.lra.propagation.active", Boolean.class).orElse(true);
}

@Override
public void filter(ClientRequestContext context) {
MultivaluedMap<String, Object> headers = context.getHeaders();
Expand All @@ -46,13 +54,17 @@ public void filter(ClientRequestContext context) {
URI lraId = Current.peek();

if (lraId != null) {
headers.putSingle(LRA_HTTP_CONTEXT_HEADER, lraId);
context.setProperty(LRA_HTTP_CONTEXT_HEADER, lraId);
if (canPropagate) {
headers.putSingle(LRA_HTTP_CONTEXT_HEADER, lraId);
context.setProperty(LRA_HTTP_CONTEXT_HEADER, lraId);
}
} else {
Object lraContext = context.getProperty(LRA_HTTP_CONTEXT_HEADER);

if (lraContext != null) {
headers.putSingle(LRA_HTTP_CONTEXT_HEADER, lraContext);
if (canPropagate) {
headers.putSingle(LRA_HTTP_CONTEXT_HEADER, lraContext);
}
} else {
headers.remove(LRA_HTTP_CONTEXT_HEADER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2023, 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.arquillian;

import io.narayana.lra.arquillian.resource.LRAUnawareResource;
import io.narayana.lra.arquillian.resource.SimpleLRAParticipant;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.net.URI;
import java.net.URL;

import static org.junit.Assert.assertNotEquals;

public class LRAPropagationIT extends TestBase {
private static final Logger log = Logger.getLogger(LRAPropagationIT.class);

@ArquillianResource
public URL baseURL;

@Rule
public TestName testName = new TestName();

@Override
public void before() {
super.before();
log.info("Running test " + testName.getMethodName());
}

@Deployment
public static WebArchive deploy() {
return Deployer.deploy(LRAPropagationIT.class.getSimpleName(), LRAUnawareResource.class, SimpleLRAParticipant.class)
.addAsManifestResource(
new StringAsset("mp.lra.propagation.active=false"), "microprofile-config.properties");
}

@Test
public void noLRATest() throws WebApplicationException {
URI lraId = lraClient.startLRA(LRAPropagationIT.class.getName());

URI returnedLraId = invokeInTransaction(baseURL, LRAUnawareResource.ROOT_PATH,
LRAUnawareResource.RESOURCE_PATH, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());

assertNotEquals("While calling non-LRA method the resource should not propagate the LRA id when mp.lra.propagation.active=false",
lraId, returnedLraId);

lraClient.closeLRA(lraId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.narayana.lra.arquillian.resource;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;

@ApplicationScoped
@Path(LRAUnawareResource.ROOT_PATH)
public class LRAUnawareResource {

public static final String ROOT_PATH = "/lra-unaware";
public static final String RESOURCE_PATH = "/lra-work";

@Context
private UriInfo context;

@GET
@Path(RESOURCE_PATH)
public Response doInLRA() {
try (Client client = ClientBuilder.newClient()) {
// SimpleLRAParticipant.START_LRA_PATH has REQUIRED LRA type meaning that
// if this method propagates LRA context it will be reused in SimpleLRAParticipant
// but if it doesn't, a new LRA will be started
return client.target(context.getBaseUri())
.path(SimpleLRAParticipant.SIMPLE_PARTICIPANT_RESOURCE_PATH)
.path(SimpleLRAParticipant.START_LRA_PATH)
.request()
.get();
}
}
}

0 comments on commit 63357b1

Please sign in to comment.