Search in sources :

Example 1 with BranchType

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

Example 2 with BranchType

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

Example 3 with BranchType

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

Example 4 with BranchType

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

Example 5 with BranchType

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

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