Search in sources :

Example 1 with BranchRegisterRequest

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

the class TmNettyClientTest method testSendMsgWithResponse.

@Test
public void testSendMsgWithResponse() throws Exception {
    workingThreads.submit(new Runnable() {

        @Override
        public void run() {
            NettyRemotingServer nettyRemotingServer = new NettyRemotingServer(workingThreads);
            nettyRemotingServer.setHandler(new DefaultCoordinator(nettyRemotingServer));
            UUIDGenerator.init(1L);
            nettyRemotingServer.init();
        }
    });
    Thread.sleep(3000);
    String applicationId = "app 1";
    String transactionServiceGroup = "my_test_tx_group";
    TmNettyRemotingClient tmNettyRemotingClient = TmNettyRemotingClient.getInstance(applicationId, transactionServiceGroup);
    tmNettyRemotingClient.init();
    Method doConnectMethod = TmNettyRemotingClient.class.getDeclaredMethod("doConnect", String.class);
    doConnectMethod.setAccessible(true);
    String serverAddress = "0.0.0.0:8091";
    Channel channel = (Channel) doConnectMethod.invoke(tmNettyRemotingClient, serverAddress);
    Assertions.assertNotNull(channel);
    BranchRegisterRequest request = new BranchRegisterRequest();
    request.setXid("127.0.0.1:8091:1249853");
    request.setLockKey("lock key testSendMsgWithResponse");
    request.setResourceId("resoutceId1");
    BranchRegisterResponse branchRegisterResponse = (BranchRegisterResponse) tmNettyRemotingClient.sendSyncRequest(request);
    Assertions.assertNotNull(branchRegisterResponse);
    Assertions.assertEquals(ResultCode.Failed, branchRegisterResponse.getResultCode());
    Assertions.assertEquals("RuntimeException[SessionManager is NOT init!]", branchRegisterResponse.getMsg());
}
Also used : BranchRegisterResponse(io.seata.core.protocol.transaction.BranchRegisterResponse) BranchRegisterRequest(io.seata.core.protocol.transaction.BranchRegisterRequest) Channel(io.netty.channel.Channel) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 2 with BranchRegisterRequest

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

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

the class AbstractTCInboundHandler method handle.

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

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

Example 4 with BranchRegisterRequest

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

the class BranchRegisterRequestSerializerTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    BranchRegisterRequest branchRegisterRequest = new BranchRegisterRequest();
    branchRegisterRequest.setBranchType(BranchType.AT);
    branchRegisterRequest.setApplicationData("abc");
    branchRegisterRequest.setLockKey("a:1,b:2");
    branchRegisterRequest.setResourceId("124");
    branchRegisterRequest.setXid("abc134");
    byte[] bytes = seataSerializer.serialize(branchRegisterRequest);
    BranchRegisterRequest branchRegisterRequest2 = seataSerializer.deserialize(bytes);
    assertThat(branchRegisterRequest2.getBranchType()).isEqualTo(branchRegisterRequest.getBranchType());
    assertThat(branchRegisterRequest2.getApplicationData()).isEqualTo(branchRegisterRequest.getApplicationData());
    assertThat(branchRegisterRequest2.getLockKey()).isEqualTo(branchRegisterRequest.getLockKey());
    assertThat(branchRegisterRequest2.getResourceId()).isEqualTo(branchRegisterRequest.getResourceId());
    assertThat(branchRegisterRequest2.getXid()).isEqualTo(branchRegisterRequest.getXid());
}
Also used : BranchRegisterRequest(io.seata.core.protocol.transaction.BranchRegisterRequest) Test(org.junit.jupiter.api.Test)

Example 5 with BranchRegisterRequest

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

the class BranchRegisterRequestCodec method decode.

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

Aggregations

BranchRegisterRequest (io.seata.core.protocol.transaction.BranchRegisterRequest)9 Test (org.junit.jupiter.api.Test)4 BranchRegisterResponse (io.seata.core.protocol.transaction.BranchRegisterResponse)3 Channel (io.netty.channel.Channel)1 StoreException (io.seata.common.exception.StoreException)1 RmTransactionException (io.seata.core.exception.RmTransactionException)1 TransactionException (io.seata.core.exception.TransactionException)1 BranchType (io.seata.core.model.BranchType)1 BranchRegisterRequestProto (io.seata.serializer.protobuf.generated.BranchRegisterRequestProto)1 DefaultCoordinator (io.seata.server.coordinator.DefaultCoordinator)1 Method (java.lang.reflect.Method)1 TimeoutException (java.util.concurrent.TimeoutException)1