Search in sources :

Example 6 with BranchType

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);
}
Also used : BranchType(io.seata.core.model.BranchType) UndoLogDeleteRequest(io.seata.core.protocol.transaction.UndoLogDeleteRequest)

Example 7 with BranchType

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);
    }
}
Also used : AbstractBranchEndRequest(io.seata.core.protocol.transaction.AbstractBranchEndRequest) BranchType(io.seata.core.model.BranchType)

Example 8 with BranchType

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);
                    }
                }
            }
        }
    }
}
Also used : BranchType(io.seata.core.model.BranchType)

Example 9 with BranchType

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);
}
Also used : BranchType(io.seata.core.model.BranchType) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Example 10 with BranchType

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();
    }
}
Also used : BranchType(io.seata.core.model.BranchType) TwoPhaseBusinessAction(io.seata.rm.tcc.api.TwoPhaseBusinessAction) Method(java.lang.reflect.Method)

Aggregations

BranchType (io.seata.core.model.BranchType)10 TwoPhaseBusinessAction (io.seata.rm.tcc.api.TwoPhaseBusinessAction)3 Method (java.lang.reflect.Method)3 BranchStatus (io.seata.core.model.BranchStatus)2 TransactionException (io.seata.core.exception.TransactionException)1 Resource (io.seata.core.model.Resource)1 AbstractBranchEndRequest (io.seata.core.protocol.transaction.AbstractBranchEndRequest)1 BranchRegisterRequest (io.seata.core.protocol.transaction.BranchRegisterRequest)1 BranchReportRequest (io.seata.core.protocol.transaction.BranchReportRequest)1 UndoLogDeleteRequest (io.seata.core.protocol.transaction.UndoLogDeleteRequest)1 ResourceManagerXA (io.seata.rm.datasource.xa.ResourceManagerXA)1 XAResource (javax.transaction.xa.XAResource)1