use of io.seata.core.model.BranchType in project seata by seata.
the class UndoLogDeleteRequestCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
UndoLogDeleteRequest undoLogDeleteRequest = (UndoLogDeleteRequest) t;
short saveDays = undoLogDeleteRequest.getSaveDays();
BranchType branchType = undoLogDeleteRequest.getBranchType();
String resourceId = undoLogDeleteRequest.getResourceId();
// 1. Branch Type
out.writeByte((byte) branchType.ordinal());
// 2. 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);
}
// 3.save days
out.writeShort(saveDays);
}
use of io.seata.core.model.BranchType in project seata by seata.
the class AbstractBranchEndRequestCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
AbstractBranchEndRequest abstractBranchEndRequest = (AbstractBranchEndRequest) t;
String xid = abstractBranchEndRequest.getXid();
long branchId = abstractBranchEndRequest.getBranchId();
BranchType branchType = abstractBranchEndRequest.getBranchType();
String resourceId = abstractBranchEndRequest.getResourceId();
String applicationData = abstractBranchEndRequest.getApplicationData();
// 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 Type
out.writeByte(branchType.ordinal());
// 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
byte[] applicationDataBytes = null;
if (applicationData != null) {
applicationDataBytes = applicationData.getBytes(UTF8);
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 AlibabaDubboTransactionPropagationFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (!DubboConstants.ALIBABADUBBO) {
return invoker.invoke(invocation);
}
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 framework by wcnnkh.
the class TccActionInterceptor method intercept.
@Override
public Object intercept(MethodInvoker invoker, Object[] args) throws Throwable {
if (!RootContext.inGlobalTransaction() || disable || RootContext.inSagaBranch()) {
// not in transaction
return invoker.invoke(args);
}
Method method = getActionInterfaceMethod(invoker);
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 {
// Handler the TCC Aspect
Map<String, Object> ret = actionInterceptorHandler.proceed(method, args, xid, businessAction, () -> {
return invoker.invoke(args);
});
// return the final result
return ret.get(Constants.TCC_METHOD_RESULT);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
}
}
return invoker.invoke(args);
}
use of io.seata.core.model.BranchType in project jboot by yangfuhai.
the class TccActionInterceptor method intercept.
@Override
public void intercept(Invocation inv) {
if (!JbootSeataManager.me().isEnable()) {
inv.invoke();
return;
}
if (!RootContext.inGlobalTransaction()) {
// not in transaction
inv.invoke();
return;
}
Method method = inv.getMethod();
TwoPhaseBusinessAction businessAction = method.getAnnotation(TwoPhaseBusinessAction.class);
// try method
if (businessAction != null) {
// save the xid
String xid = RootContext.getXID();
// clear the context
BranchType previousBranchType = RootContext.getBranchType();
if (BranchType.TCC != previousBranchType) {
RootContext.bindBranchType(BranchType.TCC);
}
try {
Object[] methodArgs = inv.getArgs();
// Handler the TCC Aspect
actionInterceptorHandler.proceed(method, methodArgs, xid, businessAction, inv);
} finally {
// if not TCC, unbind branchType
if (BranchType.TCC != previousBranchType) {
RootContext.unbindBranchType();
}
}
} else {
inv.invoke();
}
}
Aggregations