Search in sources :

Example 1 with BranchReportRequest

use of io.seata.core.protocol.transaction.BranchReportRequest in project seata by seata.

the class AbstractTCInboundHandler method handle.

@Override
public BranchReportResponse handle(BranchReportRequest request, final RpcContext rpcContext) {
    BranchReportResponse response = new BranchReportResponse();
    exceptionHandleTemplate(new AbstractCallback<BranchReportRequest, BranchReportResponse>() {

        @Override
        public void execute(BranchReportRequest request, BranchReportResponse response) throws TransactionException {
            try {
                doBranchReport(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("branch report request failed. xid=%s, branchId=%s, msg=%s", request.getXid(), request.getBranchId(), e.getMessage()), e);
            }
        }
    }, request, response);
    return response;
}
Also used : TransactionException(io.seata.core.exception.TransactionException) BranchReportResponse(io.seata.core.protocol.transaction.BranchReportResponse) BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest) StoreException(io.seata.common.exception.StoreException)

Example 2 with BranchReportRequest

use of io.seata.core.protocol.transaction.BranchReportRequest in project seata by seata.

the class BranchReportRequestSerializerTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    BranchReportRequest branchReportRequest = new BranchReportRequest();
    branchReportRequest.setBranchId(1346);
    branchReportRequest.setBranchType(BranchType.TCC);
    branchReportRequest.setApplicationData("acds");
    branchReportRequest.setResourceId("aaa");
    branchReportRequest.setStatus(BranchStatus.PhaseOne_Done);
    branchReportRequest.setXid("abc123");
    byte[] bytes = seataSerializer.serialize(branchReportRequest);
    BranchReportRequest branchReportRequest2 = seataSerializer.deserialize(bytes);
    assertThat(branchReportRequest2.getBranchId()).isEqualTo(branchReportRequest.getBranchId());
    assertThat(branchReportRequest2.getBranchType()).isEqualTo(branchReportRequest.getBranchType());
    assertThat(branchReportRequest2.getApplicationData()).isEqualTo(branchReportRequest.getApplicationData());
    assertThat(branchReportRequest2.getResourceId()).isEqualTo(branchReportRequest.getResourceId());
    assertThat(branchReportRequest2.getStatus()).isEqualTo(branchReportRequest.getStatus());
    assertThat(branchReportRequest2.getXid()).isEqualTo(branchReportRequest.getXid());
}
Also used : BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest) Test(org.junit.jupiter.api.Test)

Example 3 with BranchReportRequest

use of io.seata.core.protocol.transaction.BranchReportRequest in project seata by seata.

the class AbstractResourceManager method branchReport.

/**
 * report branch status
 *
 * @param branchType      the branch type
 * @param xid             the xid
 * @param branchId        the branch id
 * @param status          the status
 * @param applicationData the application data
 * @throws TransactionException  TransactionException
 */
@Override
public void branchReport(BranchType branchType, String xid, long branchId, BranchStatus status, String applicationData) throws TransactionException {
    try {
        BranchReportRequest request = new BranchReportRequest();
        request.setXid(xid);
        request.setBranchId(branchId);
        request.setStatus(status);
        request.setApplicationData(applicationData);
        BranchReportResponse response = (BranchReportResponse) RmNettyRemotingClient.getInstance().sendSyncRequest(request);
        if (response.getResultCode() == ResultCode.Failed) {
            throw new RmTransactionException(response.getTransactionExceptionCode(), String.format("Response[ %s ]", response.getMsg()));
        }
    } catch (TimeoutException toe) {
        throw new RmTransactionException(TransactionExceptionCode.IO, "RPC Timeout", toe);
    } catch (RuntimeException rex) {
        throw new RmTransactionException(TransactionExceptionCode.BranchReportFailed, "Runtime", rex);
    }
}
Also used : RmTransactionException(io.seata.core.exception.RmTransactionException) BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest) BranchReportResponse(io.seata.core.protocol.transaction.BranchReportResponse) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with BranchReportRequest

use of io.seata.core.protocol.transaction.BranchReportRequest 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 BranchReportRequest

use of io.seata.core.protocol.transaction.BranchReportRequest in project seata by seata.

the class BranchReportRequestCodec method decode.

@Override
public <T> void decode(T t, ByteBuffer in) {
    BranchReportRequest branchReportRequest = (BranchReportRequest) t;
    short xidLen = in.getShort();
    if (xidLen > 0) {
        byte[] bs = new byte[xidLen];
        in.get(bs);
        branchReportRequest.setXid(new String(bs, UTF8));
    }
    branchReportRequest.setBranchId(in.getLong());
    branchReportRequest.setStatus(BranchStatus.get(in.get()));
    short len = in.getShort();
    if (len > 0) {
        byte[] bs = new byte[len];
        in.get(bs);
        branchReportRequest.setResourceId(new String(bs, UTF8));
    }
    int iLen = in.getInt();
    if (iLen > 0) {
        byte[] bs = new byte[iLen];
        in.get(bs);
        branchReportRequest.setApplicationData(new String(bs, UTF8));
    }
    branchReportRequest.setBranchType(BranchType.get(in.get()));
}
Also used : BranchReportRequest(io.seata.core.protocol.transaction.BranchReportRequest)

Aggregations

BranchReportRequest (io.seata.core.protocol.transaction.BranchReportRequest)8 Test (org.junit.jupiter.api.Test)3 BranchReportResponse (io.seata.core.protocol.transaction.BranchReportResponse)2 StoreException (io.seata.common.exception.StoreException)1 RmTransactionException (io.seata.core.exception.RmTransactionException)1 TransactionException (io.seata.core.exception.TransactionException)1 BranchStatus (io.seata.core.model.BranchStatus)1 BranchType (io.seata.core.model.BranchType)1 BranchReportRequestProto (io.seata.serializer.protobuf.generated.BranchReportRequestProto)1 TimeoutException (java.util.concurrent.TimeoutException)1