Search in sources :

Example 1 with LongRunningAction

use of io.narayana.lra.coordinator.domain.model.LongRunningAction in project narayana by jbosstm.

the class Coordinator method getLRAStatus.

@GET
@Path("{LraId}/status")
@Produces(MediaType.TEXT_PLAIN)
@Operation(summary = "Obtain the status of an LRA as a string")
@APIResponses({ @APIResponse(responseCode = "200", description = "The LRA exists. The status is reported in the content body.", content = @Content(schema = @Schema(implementation = String.class)), headers = { @Header(ref = LRAConstants.NARAYANA_LRA_API_VERSION_HEADER_NAME) }), @APIResponse(responseCode = "404", description = "The coordinator has no knowledge of this LRA", content = @Content(schema = @Schema(implementation = String.class))), @APIResponse(responseCode = "417", description = "The requested version provided in HTTP Header is not supported by this end point", content = @Content(schema = @Schema(implementation = String.class))) })
public Response getLRAStatus(@Parameter(name = "LraId", description = "The unique identifier of the LRA." + "Expecting to be a valid URL where the participant can be contacted at. If not in URL format it will be considered " + "to be an id which will be declared to exist at URL where coordinator is deployed at.", required = true) @PathParam("LraId") String lraId, @Parameter(ref = LRAConstants.NARAYANA_LRA_API_VERSION_HEADER_NAME) @HeaderParam(LRAConstants.NARAYANA_LRA_API_VERSION_HEADER_NAME) @DefaultValue(CURRENT_API_VERSION_STRING) String version) throws NotFoundException {
    // throws NotFoundException -> response 404
    LongRunningAction transaction = lraService.getTransaction(toURI(lraId));
    LRAStatus status = transaction.getLRAStatus();
    if (status == null) {
        status = LRAStatus.Active;
    }
    return Response.ok().entity(status.name()).header(NARAYANA_LRA_API_VERSION_HEADER_NAME, version).build();
}
Also used : LRAStatus(org.eclipse.microprofile.lra.annotation.LRAStatus) LongRunningAction(io.narayana.lra.coordinator.domain.model.LongRunningAction) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) APIResponses(org.eclipse.microprofile.openapi.annotations.responses.APIResponses) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 2 with LongRunningAction

use of io.narayana.lra.coordinator.domain.model.LongRunningAction in project narayana by jbosstm.

the class LRAService method lraTrace.

private void lraTrace(URI lraId, String reason) {
    if (LRALogger.logger.isTraceEnabled()) {
        if (lraId != null && lras.containsKey(lraId)) {
            LongRunningAction lra = lras.get(lraId);
            LRALogger.logger.tracef("LRAService: '%s' (%s) in state %s: %s%n", reason, lra.getClientId(), ActionStatus.stringForm(lra.status()), lra.getId());
        } else {
            LRALogger.logger.tracef("LRAService: '%s', not found: %s%n", reason, lraId);
        }
    }
}
Also used : LongRunningAction(io.narayana.lra.coordinator.domain.model.LongRunningAction)

Example 3 with LongRunningAction

use of io.narayana.lra.coordinator.domain.model.LongRunningAction in project narayana by jbosstm.

the class LRAService method joinLRA.

public synchronized int joinLRA(StringBuilder recoveryUrl, URI lra, long timeLimit, String compensatorUrl, String linkHeader, String recoveryUrlBase, String compensatorData) {
    if (lra == null) {
        lraTrace(null, "Error missing LRA header in join request");
    } else {
        lraTrace(lra, "join LRA");
    }
    LongRunningAction transaction = getTransaction(lra);
    if (timeLimit < 0) {
        timeLimit = 0;
    }
    // Closing/Canceling (for the AfterLRA listeners)
    if (transaction.getLRAStatus() != LRAStatus.Active && !transaction.isRecovering()) {
        // validate that the party wanting to join with this LRA is a listener only:
        if (linkHeader != null) {
            Matcher relMatcher = LINK_REL_PATTERN.matcher(linkHeader);
            while (relMatcher.find()) {
                String key = relMatcher.group(1);
                if (key != null && key.equals("rel")) {
                    String rel = relMatcher.group(2) == null ? relMatcher.group(3) : relMatcher.group(2);
                    if (!LRAConstants.AFTER.equals(rel)) {
                        // participants are not allowed to join inactive LRAs
                        return Response.Status.PRECONDITION_FAILED.getStatusCode();
                    } else if (!transaction.isRecovering()) {
                        // listeners cannot be notified if the LRA has already ended
                        return Response.Status.PRECONDITION_FAILED.getStatusCode();
                    }
                }
            }
        }
    }
    LRAParticipantRecord participant;
    try {
        participant = transaction.enlistParticipant(lra, linkHeader != null ? linkHeader : compensatorUrl, recoveryUrlBase, timeLimit, compensatorData);
    } catch (UnsupportedEncodingException e) {
        return Response.Status.PRECONDITION_FAILED.getStatusCode();
    }
    if (participant == null || participant.getRecoveryURI() == null) {
        // probably already closing or cancelling
        return Response.Status.PRECONDITION_FAILED.getStatusCode();
    }
    String recoveryURI = participant.getRecoveryURI().toASCIIString();
    updateRecoveryURI(lra, participant.getParticipantURI(), recoveryURI, false);
    recoveryUrl.append(recoveryURI);
    return Response.Status.OK.getStatusCode();
}
Also used : Matcher(java.util.regex.Matcher) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LRAParticipantRecord(io.narayana.lra.coordinator.domain.model.LRAParticipantRecord) LongRunningAction(io.narayana.lra.coordinator.domain.model.LongRunningAction)

Example 4 with LongRunningAction

use of io.narayana.lra.coordinator.domain.model.LongRunningAction in project narayana by jbosstm.

the class LRAService method endLRA.

public LRAData endLRA(URI lraId, boolean compensate, boolean fromHierarchy) {
    lraTrace(lraId, "end LRA");
    LongRunningAction transaction = getTransaction(lraId);
    if (transaction.getLRAStatus() != LRAStatus.Active && !transaction.isRecovering() && transaction.isTopLevel()) {
        String errorMsg = String.format("%s: LRA is closing or closed: endLRA", lraId);
        throw new WebApplicationException(errorMsg, Response.status(Response.Status.PRECONDITION_FAILED).entity(errorMsg).build());
    }
    transaction.finishLRA(compensate);
    if (BasicAction.Current() != null) {
        if (LRALogger.logger.isInfoEnabled()) {
            LRALogger.logger.infof("LRAServicve.endLRA LRA %s ended but is still associated with %s%n", lraId, BasicAction.Current().get_uid().fileStringForm());
        }
    }
    finished(transaction, fromHierarchy);
    return transaction.getLRAData();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) LongRunningAction(io.narayana.lra.coordinator.domain.model.LongRunningAction)

Example 5 with LongRunningAction

use of io.narayana.lra.coordinator.domain.model.LongRunningAction in project narayana by jbosstm.

the class LRAService method leave.

public int leave(URI lraId, String compensatorUrl) {
    lraTrace(lraId, "leave LRA");
    LongRunningAction transaction = getTransaction(lraId);
    if (transaction.getLRAStatus() != LRAStatus.Active) {
        return Response.Status.PRECONDITION_FAILED.getStatusCode();
    }
    boolean wasForgotten;
    try {
        wasForgotten = transaction.forgetParticipant(compensatorUrl);
    } catch (Exception e) {
        String errorMsg = String.format("LRAService.forget %s failed on finding participant '%s'", lraId, compensatorUrl);
        throw new WebApplicationException(errorMsg, e, Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build());
    }
    if (wasForgotten) {
        return Response.Status.OK.getStatusCode();
    } else {
        String errorMsg = String.format("LRAService.forget %s failed as the participant was not found, compensator url '%s'", lraId, compensatorUrl);
        throw new WebApplicationException(errorMsg, Response.status(Response.Status.BAD_REQUEST).entity(errorMsg).build());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) URISyntaxException(java.net.URISyntaxException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LongRunningAction(io.narayana.lra.coordinator.domain.model.LongRunningAction)

Aggregations

LongRunningAction (io.narayana.lra.coordinator.domain.model.LongRunningAction)13 WebApplicationException (javax.ws.rs.WebApplicationException)5 FailedLongRunningAction (io.narayana.lra.coordinator.domain.model.FailedLongRunningAction)4 UidWrapper (com.arjuna.ats.arjuna.tools.osb.mbean.UidWrapper)3 URISyntaxException (java.net.URISyntaxException)3 Path (javax.ws.rs.Path)3 LRAStatus (org.eclipse.microprofile.lra.annotation.LRAStatus)3 Test (org.junit.Test)3 Uid (com.arjuna.ats.arjuna.common.Uid)2 OSEntryBean (com.arjuna.ats.arjuna.tools.osb.mbean.OSEntryBean)2 LRAParticipantRecord (io.narayana.lra.coordinator.domain.model.LRAParticipantRecord)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 GET (javax.ws.rs.GET)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2 Produces (javax.ws.rs.Produces)2 Operation (org.eclipse.microprofile.openapi.annotations.Operation)2 APIResponses (org.eclipse.microprofile.openapi.annotations.responses.APIResponses)2 com.arjuna.ats.arjuna.common.recoveryPropertyManager (com.arjuna.ats.arjuna.common.recoveryPropertyManager)1