use of io.seata.core.model.BranchType in project seata by seata.
the class TccActionInterceptor method invoke.
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
if (!RootContext.inGlobalTransaction() || disable || RootContext.inSagaBranch()) {
// not in transaction
return invocation.proceed();
}
Method method = getActionInterfaceMethod(invocation);
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
// try method
if (businessAction != null) {
// save the xid
String xid = RootContext.getXID();
// save the previous branchType
BranchType previousBranchType = RootContext.getBranchType();
// if not TCC, bind TCC branchType
if (BranchType.TCC != previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
}
try {
Object[] methodArgs = invocation.getArguments();
// Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, methodArgs, xid, businessAction, invocation::proceed);
// return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
// MDC remove branchId
MDC.remove(RootContext.MDC_KEY_BRANCH_ID);
}
}
return invocation.proceed();
}
use of io.seata.core.model.BranchType in project seata by seata.
the class ApacheDubboTransactionPropagationFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String xid = RootContext.getXID();
BranchType branchType = RootContext.getBranchType();
String rpcXid = getRpcXid();
String rpcBranchType = RpcContext.getContext().getAttachment(RootContext.KEY_BRANCH_TYPE);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("xid in RootContext[{}] xid in RpcContext[{}]", xid, rpcXid);
}
boolean bind = false;
if (xid != null) {
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid);
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType.name());
} else {
if (rpcXid != null) {
RootContext.bind(rpcXid);
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) {
RootContext.bindBranchType(BranchType.TCC);
}
bind = true;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType);
}
}
}
try {
return invoker.invoke(invocation);
} finally {
if (bind) {
BranchType previousBranchType = RootContext.getBranchType();
String unbindXid = RootContext.unbind();
if (BranchType.TCC == previousBranchType) {
RootContext.unbindBranchType();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("unbind xid [{}] branchType [{}] from RootContext", unbindXid, previousBranchType);
}
if (!rpcXid.equalsIgnoreCase(unbindXid)) {
LOGGER.warn("xid in change during RPC from {} to {},branchType from {} to {}", rpcXid, unbindXid, rpcBranchType != null ? rpcBranchType : "AT", previousBranchType);
if (unbindXid != null) {
RootContext.bind(unbindXid);
LOGGER.warn("bind xid [{}] back to RootContext", unbindXid);
if (BranchType.TCC == previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
LOGGER.warn("bind branchType [{}] back to RootContext", previousBranchType);
}
}
}
}
}
}
use of io.seata.core.model.BranchType in project seata by seata.
the class BranchRegisterRequestCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
BranchRegisterRequest branchRegisterRequest = (BranchRegisterRequest) t;
String xid = branchRegisterRequest.getXid();
BranchType branchType = branchRegisterRequest.getBranchType();
String resourceId = branchRegisterRequest.getResourceId();
String lockKey = branchRegisterRequest.getLockKey();
String applicationData = branchRegisterRequest.getApplicationData();
byte[] lockKeyBytes = null;
if (lockKey != null) {
lockKeyBytes = lockKey.getBytes(UTF8);
}
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 Type
out.writeByte(branchType.ordinal());
// 3. 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);
}
// 4. Lock Key
if (lockKey != null) {
out.writeInt(lockKeyBytes.length);
if (lockKeyBytes.length > 0) {
out.writeBytes(lockKeyBytes);
}
} else {
out.writeInt(0);
}
// 5. applicationData
if (applicationData != null) {
out.writeInt(applicationDataBytes.length);
if (applicationDataBytes.length > 0) {
out.writeBytes(applicationDataBytes);
}
} else {
out.writeInt(0);
}
}
use of io.seata.core.model.BranchType 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.BranchType 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 {
}
});
}
Aggregations