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());
}
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 {
}
});
}
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;
}
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();
}
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");
}
Aggregations