Search in sources :

Example 1 with BranchCommitRequest

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

the class AbstractCore method branchCommit.

@Override
public BranchStatus branchCommit(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
    try {
        BranchCommitRequest request = new BranchCommitRequest();
        request.setXid(branchSession.getXid());
        request.setBranchId(branchSession.getBranchId());
        request.setResourceId(branchSession.getResourceId());
        request.setApplicationData(branchSession.getApplicationData());
        request.setBranchType(branchSession.getBranchType());
        return branchCommitSend(request, globalSession, branchSession);
    } catch (IOException | TimeoutException e) {
        throw new BranchTransactionException(FailedToSendBranchCommitRequest, String.format("Send branch commit failed, xid = %s branchId = %s", branchSession.getXid(), branchSession.getBranchId()), e);
    }
}
Also used : IOException(java.io.IOException) FailedToSendBranchCommitRequest(io.seata.core.exception.TransactionExceptionCode.FailedToSendBranchCommitRequest) BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) BranchTransactionException(io.seata.core.exception.BranchTransactionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with BranchCommitRequest

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

the class BranchCommitRequestSerializerTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    BranchCommitRequest branchCommitRequest = new BranchCommitRequest();
    branchCommitRequest.setApplicationData("abc");
    branchCommitRequest.setBranchId(123);
    branchCommitRequest.setBranchType(BranchType.AT);
    branchCommitRequest.setResourceId("t");
    branchCommitRequest.setXid("a3");
    byte[] bytes = seataSerializer.serialize(branchCommitRequest);
    BranchCommitRequest branchCommitReques2 = seataSerializer.deserialize(bytes);
    assertThat(branchCommitReques2.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData());
    assertThat(branchCommitReques2.getBranchType()).isEqualTo(branchCommitRequest.getBranchType());
    assertThat(branchCommitReques2.getBranchId()).isEqualTo(branchCommitRequest.getBranchId());
    assertThat(branchCommitReques2.getResourceId()).isEqualTo(branchCommitRequest.getResourceId());
    assertThat(branchCommitReques2.getXid()).isEqualTo(branchCommitRequest.getXid());
}
Also used : BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) Test(org.junit.jupiter.api.Test)

Example 3 with BranchCommitRequest

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

the class ProtocolV1Client method main.

// can test tps
public static void main(String[] args) {
    ProtocolV1Client client = new ProtocolV1Client();
    client.connect("127.0.0.1", 8811, 500);
    Map<String, String> head = new HashMap<>();
    head.put("tracerId", "xxadadadada");
    head.put("token", "adadadad");
    BranchCommitRequest body = new BranchCommitRequest();
    body.setBranchId(12345L);
    body.setApplicationData("application");
    body.setBranchType(BranchType.AT);
    body.setResourceId("resource-1234");
    body.setXid("xid-1234");
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    // no queue
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("client-", false));
    for (int i = 0; i < threads; i++) {
        service1.execute(() -> {
            while (true) {
                try {
                    Future future = client.sendRpc(head, body);
                    RpcMessage resp = (RpcMessage) future.get(200, TimeUnit.MILLISECONDS);
                    if (resp != null) {
                        cnt.incrementAndGet();
                    }
                } catch (Exception e) {
                // ignore
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NamedThreadFactory(io.seata.common.thread.NamedThreadFactory) BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) RpcMessage(io.seata.core.protocol.RpcMessage) AtomicLong(java.util.concurrent.atomic.AtomicLong) Future(java.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 4 with BranchCommitRequest

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

the class HessianSerializerTest method testBranchCommitRequest.

@Test
public void testBranchCommitRequest() {
    BranchCommitRequest branchCommitRequest = new BranchCommitRequest();
    branchCommitRequest.setBranchType(BranchType.AT);
    branchCommitRequest.setXid("xid");
    branchCommitRequest.setResourceId("resourceId");
    branchCommitRequest.setBranchId(20190809);
    branchCommitRequest.setApplicationData("app");
    byte[] bytes = hessianCodec.serialize(branchCommitRequest);
    BranchCommitRequest t = hessianCodec.deserialize(bytes);
    assertThat(t.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode());
    assertThat(t.getBranchType()).isEqualTo(branchCommitRequest.getBranchType());
    assertThat(t.getXid()).isEqualTo(branchCommitRequest.getXid());
    assertThat(t.getResourceId()).isEqualTo(branchCommitRequest.getResourceId());
    assertThat(t.getBranchId()).isEqualTo(branchCommitRequest.getBranchId());
    assertThat(t.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData());
}
Also used : BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) Test(org.junit.jupiter.api.Test)

Example 5 with BranchCommitRequest

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

the class BranchCommitRequestConvertorTest method convert2Proto.

@Test
public void convert2Proto() {
    BranchCommitRequest branchCommitRequest = new BranchCommitRequest();
    branchCommitRequest.setBranchType(BranchType.AT);
    branchCommitRequest.setXid("xid");
    branchCommitRequest.setResourceId("resourceId");
    branchCommitRequest.setBranchId(123);
    branchCommitRequest.setApplicationData("app");
    BranchCommitRequestConvertor branchCommitRequestConvertor = new BranchCommitRequestConvertor();
    BranchCommitRequestProto proto = branchCommitRequestConvertor.convert2Proto(branchCommitRequest);
    BranchCommitRequest realRequest = branchCommitRequestConvertor.convert2Model(proto);
    assertThat(realRequest.getTypeCode()).isEqualTo(branchCommitRequest.getTypeCode());
    assertThat(realRequest.getBranchType()).isEqualTo(branchCommitRequest.getBranchType());
    assertThat(realRequest.getXid()).isEqualTo(branchCommitRequest.getXid());
    assertThat(realRequest.getResourceId()).isEqualTo(branchCommitRequest.getResourceId());
    assertThat(realRequest.getBranchId()).isEqualTo(branchCommitRequest.getBranchId());
    assertThat(realRequest.getApplicationData()).isEqualTo(branchCommitRequest.getApplicationData());
}
Also used : BranchCommitRequestProto(io.seata.serializer.protobuf.generated.BranchCommitRequestProto) BranchCommitRequest(io.seata.core.protocol.transaction.BranchCommitRequest) Test(org.junit.jupiter.api.Test)

Aggregations

BranchCommitRequest (io.seata.core.protocol.transaction.BranchCommitRequest)12 Test (org.junit.jupiter.api.Test)7 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)2 BranchTransactionException (io.seata.core.exception.BranchTransactionException)2 RpcMessage (io.seata.core.protocol.RpcMessage)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Future (java.util.concurrent.Future)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 TimeoutException (java.util.concurrent.TimeoutException)2 ChannelFuture (io.netty.channel.ChannelFuture)1 TransactionException (io.seata.core.exception.TransactionException)1 FailedToSendBranchCommitRequest (io.seata.core.exception.TransactionExceptionCode.FailedToSendBranchCommitRequest)1 BranchCommitResponse (io.seata.core.protocol.transaction.BranchCommitResponse)1 BranchCommitRequestProto (io.seata.serializer.protobuf.generated.BranchCommitRequestProto)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1