Search in sources :

Example 6 with GlobalBeginRequest

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

the class DefaultCoordinatorMetricsTest method test.

@Test
public void test() throws IOException, TransactionException, InterruptedException {
    SessionHolder.init(null);
    DefaultCoordinator coordinator = new DefaultCoordinator(new MockServerMessageSender());
    coordinator.init();
    try {
        MetricsManager.get().init();
        // start a transaction
        GlobalBeginRequest request = new GlobalBeginRequest();
        request.setTransactionName("test_transaction");
        GlobalBeginResponse response = new GlobalBeginResponse();
        coordinator.doGlobalBegin(request, response, new RpcContext());
        Map<String, Measurement> measurements = new HashMap<>();
        MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
        Assertions.assertEquals(1, measurements.size());
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
        // commit this transaction
        GlobalCommitRequest commitRequest = new GlobalCommitRequest();
        commitRequest.setXid(response.getXid());
        coordinator.doGlobalCommit(commitRequest, new GlobalCommitResponse(), new RpcContext());
        // we need sleep for a short while because default canBeCommittedAsync() is true
        Thread.sleep(200);
        measurements.clear();
        MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
        Assertions.assertEquals(9, measurements.size());
        Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=committed)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=committed)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=committed)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);
        // start another new transaction
        request = new GlobalBeginRequest();
        request.setTransactionName("test_transaction_2");
        response = new GlobalBeginResponse();
        coordinator.doGlobalBegin(request, response, new RpcContext());
        // rollback this transaction
        GlobalRollbackRequest rollbackRequest = new GlobalRollbackRequest();
        rollbackRequest.setXid(response.getXid());
        coordinator.doGlobalRollback(rollbackRequest, new GlobalRollbackResponse(), new RpcContext());
        Thread.sleep(200);
        measurements.clear();
        MetricsManager.get().getRegistry().measure().forEach(measurement -> measurements.put(measurement.getId().toString(), measurement));
        Assertions.assertEquals(17, measurements.size());
        Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=active)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=committed)").getValue(), 0);
        Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=committed)").getValue(), 0);
        Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=committed)").getValue(), 0);
        Assertions.assertEquals(0, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=committed)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=counter,role=tc,status=rollbacked)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=summary,role=tc,statistic=total,status=rollbacked)").getValue(), 0);
        Assertions.assertEquals(1, measurements.get("seata.transaction(applicationId=null,group=null,meter=timer,role=tc,statistic=count,status=rollbacked)").getValue(), 0);
    } finally {
        coordinator.destroy();
        SessionHolder.destroy();
    }
}
Also used : MockServerMessageSender(io.seata.server.coordinator.DefaultCoordinatorTest.MockServerMessageSender) Measurement(io.seata.metrics.Measurement) GlobalCommitRequest(io.seata.core.protocol.transaction.GlobalCommitRequest) HashMap(java.util.HashMap) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) GlobalRollbackResponse(io.seata.core.protocol.transaction.GlobalRollbackResponse) RpcContext(io.seata.core.rpc.RpcContext) GlobalRollbackRequest(io.seata.core.protocol.transaction.GlobalRollbackRequest) GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest) GlobalBeginResponse(io.seata.core.protocol.transaction.GlobalBeginResponse) Test(org.junit.jupiter.api.Test)

Example 7 with GlobalBeginRequest

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

the class GlobalBeginRequestConvertorTest method convert2Proto.

@Test
public void convert2Proto() {
    GlobalBeginRequest globalBeginRequest = new GlobalBeginRequest();
    globalBeginRequest.setTimeout(3000);
    globalBeginRequest.setTransactionName("taa");
    GlobalBeginRequestConvertor convertor = new GlobalBeginRequestConvertor();
    GlobalBeginRequestProto proto = convertor.convert2Proto(globalBeginRequest);
    GlobalBeginRequest real = convertor.convert2Model(proto);
    assertThat(real.getTypeCode()).isEqualTo(globalBeginRequest.getTypeCode());
    assertThat(real.getTimeout()).isEqualTo(globalBeginRequest.getTimeout());
    assertThat(real.getTransactionName()).isEqualTo(globalBeginRequest.getTransactionName());
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest) GlobalBeginRequestProto(io.seata.serializer.protobuf.generated.GlobalBeginRequestProto) Test(org.junit.jupiter.api.Test)

Example 8 with GlobalBeginRequest

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

the class MergeMessageConvertorTest method buildGlobalBeginRequest.

private GlobalBeginRequest buildGlobalBeginRequest() {
    final GlobalBeginRequest globalBeginRequest = new GlobalBeginRequest();
    globalBeginRequest.setTransactionName("xx");
    globalBeginRequest.setTimeout(3000);
    return globalBeginRequest;
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest)

Example 9 with GlobalBeginRequest

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

the class GlobalBeginRequestCodec method decode.

@Override
public <T> void decode(T t, ByteBuffer in) {
    GlobalBeginRequest globalBeginRequest = (GlobalBeginRequest) t;
    globalBeginRequest.setTimeout(in.getInt());
    short len = in.getShort();
    if (len > 0) {
        byte[] bs = new byte[len];
        in.get(bs);
        globalBeginRequest.setTransactionName(new String(bs, UTF8));
    }
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest)

Example 10 with GlobalBeginRequest

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

the class GlobalBeginRequestCodec method encode.

@Override
public <T> void encode(T t, ByteBuf out) {
    GlobalBeginRequest globalBeginRequest = (GlobalBeginRequest) t;
    int timeout = globalBeginRequest.getTimeout();
    String transactionName = globalBeginRequest.getTransactionName();
    out.writeInt(timeout);
    if (transactionName != null) {
        byte[] bs = transactionName.getBytes(UTF8);
        out.writeShort((short) bs.length);
        if (bs.length > 0) {
            out.writeBytes(bs);
        }
    } else {
        out.writeShort((short) 0);
    }
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest)

Aggregations

GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)14 Test (org.junit.jupiter.api.Test)6 GlobalBeginResponse (io.seata.core.protocol.transaction.GlobalBeginResponse)3 AbstractMessage (io.seata.core.protocol.AbstractMessage)2 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)2 ArrayList (java.util.ArrayList)2 StoreException (io.seata.common.exception.StoreException)1 TmTransactionException (io.seata.core.exception.TmTransactionException)1 TransactionException (io.seata.core.exception.TransactionException)1 GlobalCommitRequest (io.seata.core.protocol.transaction.GlobalCommitRequest)1 GlobalCommitResponse (io.seata.core.protocol.transaction.GlobalCommitResponse)1 GlobalRollbackRequest (io.seata.core.protocol.transaction.GlobalRollbackRequest)1 GlobalRollbackResponse (io.seata.core.protocol.transaction.GlobalRollbackResponse)1 RpcContext (io.seata.core.rpc.RpcContext)1 Measurement (io.seata.metrics.Measurement)1 GlobalBeginRequestProto (io.seata.serializer.protobuf.generated.GlobalBeginRequestProto)1 MergedWarpMessageProto (io.seata.serializer.protobuf.generated.MergedWarpMessageProto)1 MockServerMessageSender (io.seata.server.coordinator.DefaultCoordinatorTest.MockServerMessageSender)1 HashMap (java.util.HashMap)1