Search in sources :

Example 6 with BranchStatus

use of io.seata.core.model.BranchStatus in project seata by seata.

the class BranchReportRequestCodec method encode.

@Override
public <T> void encode(T t, ByteBuf out) {
    BranchReportRequest branchReportRequest = (BranchReportRequest) t;
    String xid = branchReportRequest.getXid();
    long branchId = branchReportRequest.getBranchId();
    BranchStatus status = branchReportRequest.getStatus();
    String resourceId = branchReportRequest.getResourceId();
    String applicationData = branchReportRequest.getApplicationData();
    BranchType branchType = branchReportRequest.getBranchType();
    byte[] applicationDataBytes = null;
    if (applicationData != null) {
        applicationDataBytes = applicationData.getBytes(UTF8);
    }
    // 1. xid
    if (xid != null) {
        byte[] bs = xid.getBytes(UTF8);
        out.writeShort((short) bs.length);
        if (bs.length > 0) {
            out.writeBytes(bs);
        }
    } else {
        out.writeShort((short) 0);
    }
    // 2. Branch Id
    out.writeLong(branchId);
    // 3. Branch Status
    out.writeByte(status.getCode());
    // 4. Resource Id
    if (resourceId != null) {
        byte[] bs = resourceId.getBytes(UTF8);
        out.writeShort((short) bs.length);
        if (bs.length > 0) {
            out.writeBytes(bs);
        }
    } else {
        out.writeShort((short) 0);
    }
    // 5. Application Data
    if (applicationData != null) {
        out.writeInt(applicationDataBytes.length);
        if (applicationDataBytes.length > 0) {
            out.writeBytes(applicationDataBytes);
        }
    } else {
        out.writeInt(0);
    }
    // 6. branchType
    out.writeByte(branchType.ordinal());
}
Also used : BranchType(io.seata.core.model.BranchType) BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest) BranchStatus(io.seata.core.model.BranchStatus)

Example 7 with BranchStatus

use of io.seata.core.model.BranchStatus in project seata by seata.

the class XAModeTest2 method initRM.

private void initRM() throws Throwable {
    // init RM
    DefaultResourceManager.get();
    // mock the RM of XA
    DefaultResourceManager.mockResourceManager(BranchType.XA, new ResourceManagerXA() {

        @Override
        public void registerResource(Resource resource) {
            dataSourceCache.put(resource.getResourceId(), resource);
        }

        @Override
        public Long branchRegister(BranchType branchType, String resourceId, String clientId, String xid, String applicationData, String lockKeys) throws TransactionException {
            return mockBranchId;
        }

        @Override
        public void branchReport(BranchType branchType, String xid, long branchId, BranchStatus status, String applicationData) throws TransactionException {
        }
    });
}
Also used : TransactionException(io.seata.core.exception.TransactionException) BranchType(io.seata.core.model.BranchType) ResourceManagerXA(io.seata.rm.datasource.xa.ResourceManagerXA) XAResource(javax.transaction.xa.XAResource) Resource(io.seata.core.model.Resource) BranchStatus(io.seata.core.model.BranchStatus)

Example 8 with BranchStatus

use of io.seata.core.model.BranchStatus in project XHuiCloud by sindaZeng.

the class SagaCore method doGlobalCommit.

@Override
public boolean doGlobalCommit(GlobalSession globalSession, boolean retrying) throws TransactionException {
    try {
        BranchStatus branchStatus = branchCommit(globalSession, SessionHelper.newBranch(BranchType.SAGA, globalSession.getXid(), -1, getSagaResourceId(globalSession), globalSession.getStatus().name()));
        switch(branchStatus) {
            case PhaseTwo_Committed:
                removeAllBranches(globalSession);
                LOGGER.info("Successfully committed SAGA global[" + globalSession.getXid() + "]");
                break;
            case PhaseTwo_Rollbacked:
                LOGGER.info("Successfully rollbacked SAGA global[" + globalSession.getXid() + "]");
                removeAllBranches(globalSession);
                SessionHelper.endRollbacked(globalSession);
                return false;
            case PhaseTwo_RollbackFailed_Retryable:
                LOGGER.error("By [{}], failed to rollback SAGA global [{}], will retry later.", branchStatus, globalSession.getXid());
                SessionHolder.getRetryCommittingSessionManager().removeGlobalSession(globalSession);
                globalSession.queueToRetryRollback();
                return false;
            case PhaseOne_Failed:
                LOGGER.error("By [{}], finish SAGA global [{}]", branchStatus, globalSession.getXid());
                removeAllBranches(globalSession);
                globalSession.changeStatus(GlobalStatus.Finished);
                globalSession.end();
                return false;
            case PhaseTwo_CommitFailed_Unretryable:
                if (globalSession.canBeCommittedAsync()) {
                    LOGGER.error("By [{}], failed to commit SAGA global [{}]", branchStatus, globalSession.getXid());
                    break;
                } else {
                    SessionHelper.endCommitFailed(globalSession);
                    LOGGER.error("Finally, failed to commit SAGA global[{}]", globalSession.getXid());
                    return false;
                }
            default:
                if (!retrying) {
                    globalSession.queueToRetryCommit();
                    return false;
                } else {
                    LOGGER.error("Failed to commit SAGA global[{}], will retry later.", globalSession.getXid());
                    return false;
                }
        }
    } catch (Exception ex) {
        LOGGER.error("Failed to commit global[" + globalSession.getXid() + "]", ex);
        if (!retrying) {
            globalSession.queueToRetryRollback();
        }
        throw new TransactionException(ex);
    }
    return true;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalTransactionException(io.seata.core.exception.GlobalTransactionException) BranchStatus(io.seata.core.model.BranchStatus) TransactionException(io.seata.core.exception.TransactionException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) GlobalTransactionException(io.seata.core.exception.GlobalTransactionException)

Example 9 with BranchStatus

use of io.seata.core.model.BranchStatus in project seata by seata.

the class DefaultCoordinatorTest method branchCommit.

@Test
public void branchCommit() throws TransactionException {
    BranchStatus result = null;
    String xid = null;
    GlobalSession globalSession = null;
    try {
        xid = core.begin(applicationId, txServiceGroup, txName, timeout);
        Long branchId = core.branchRegister(BranchType.AT, resourceId, clientId, xid, applicationData, lockKeys_1);
        globalSession = SessionHolder.findGlobalSession(xid);
        result = core.branchCommit(globalSession, globalSession.getBranch(branchId));
    } catch (TransactionException e) {
        Assertions.fail(e.getMessage());
    }
    Assertions.assertEquals(result, BranchStatus.PhaseTwo_Committed);
    globalSession = SessionHolder.findGlobalSession(xid);
    Assertions.assertNotNull(globalSession);
    globalSession.end();
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalSession(io.seata.server.session.GlobalSession) BranchStatus(io.seata.core.model.BranchStatus) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with BranchStatus

use of io.seata.core.model.BranchStatus in project seata by seata.

the class AsyncWorkerTest method branchCommit.

@Test
void branchCommit() {
    BranchStatus status = worker.branchCommit("test", 0, null);
    assertEquals(BranchStatus.PhaseTwo_Committed, status, "should return PhaseTwo_Committed");
}
Also used : BranchStatus(io.seata.core.model.BranchStatus) Test(org.junit.jupiter.api.Test)

Aggregations

BranchStatus (io.seata.core.model.BranchStatus)19 TransactionException (io.seata.core.exception.TransactionException)14 GlobalSession (io.seata.server.session.GlobalSession)6 IOException (java.io.IOException)5 NotSupportYetException (io.seata.common.exception.NotSupportYetException)4 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)4 GlobalTransactionException (io.seata.core.exception.GlobalTransactionException)4 BranchSession (io.seata.server.session.BranchSession)4 TimeoutException (java.util.concurrent.TimeoutException)4 BranchType (io.seata.core.model.BranchType)2 GlobalStatus (io.seata.core.model.GlobalStatus)2 SessionCondition (io.seata.server.session.SessionCondition)2 SessionManager (io.seata.server.session.SessionManager)2 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)2 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2