Search in sources :

Example 1 with GlobalCommitResponse

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

the class GlobalCommitResponseSerializerTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    GlobalCommitResponse globalCommitResponse = new GlobalCommitResponse();
    globalCommitResponse.setGlobalStatus(GlobalStatus.AsyncCommitting);
    globalCommitResponse.setMsg("aaa");
    globalCommitResponse.setResultCode(ResultCode.Failed);
    globalCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.GlobalTransactionStatusInvalid);
    byte[] bytes = seataSerializer.serialize(globalCommitResponse);
    GlobalCommitResponse globalCommitResponse2 = seataSerializer.deserialize(bytes);
    assertThat(globalCommitResponse2.getGlobalStatus()).isEqualTo(globalCommitResponse.getGlobalStatus());
    assertThat(globalCommitResponse2.getMsg()).isEqualTo(globalCommitResponse.getMsg());
    assertThat(globalCommitResponse2.getResultCode()).isEqualTo(globalCommitResponse.getResultCode());
    assertThat(globalCommitResponse2.getTransactionExceptionCode()).isEqualTo(globalCommitResponse.getTransactionExceptionCode());
}
Also used : GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) Test(org.junit.jupiter.api.Test)

Example 2 with GlobalCommitResponse

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

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

the class MergeResultMessageConvertorTest method convert2Proto.

@Test
public void convert2Proto() {
    MergeResultMessage mergeResultMessage = new MergeResultMessage();
    AbstractResultMessage[] msgs = new AbstractResultMessage[1];
    final GlobalCommitResponse globalCommitResponse = new GlobalCommitResponse();
    globalCommitResponse.setGlobalStatus(GlobalStatus.AsyncCommitting);
    globalCommitResponse.setMsg("msg");
    globalCommitResponse.setResultCode(ResultCode.Failed);
    globalCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchRegisterFailed);
    msgs[0] = globalCommitResponse;
    mergeResultMessage.setMsgs(msgs);
    MergeResultMessageConvertor convertor = new MergeResultMessageConvertor();
    MergedResultMessageProto proto = convertor.convert2Proto(mergeResultMessage);
    MergeResultMessage real = convertor.convert2Model(proto);
    GlobalCommitResponse realObj = (GlobalCommitResponse) real.getMsgs()[0];
    assertThat((realObj.getTypeCode())).isEqualTo(globalCommitResponse.getTypeCode());
    assertThat((realObj.getMsg())).isEqualTo(globalCommitResponse.getMsg());
    assertThat((realObj.getResultCode())).isEqualTo(globalCommitResponse.getResultCode());
    assertThat((realObj.getTransactionExceptionCode())).isEqualTo(globalCommitResponse.getTransactionExceptionCode());
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) MergedResultMessageProto(io.seata.serializer.protobuf.generated.MergedResultMessageProto) Test(org.junit.jupiter.api.Test)

Example 4 with GlobalCommitResponse

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

the class GlobalCommitResponseConvertorTest method convert2Proto.

@Test
public void convert2Proto() {
    GlobalCommitResponse globalCommitResponse = new GlobalCommitResponse();
    globalCommitResponse.setGlobalStatus(GlobalStatus.AsyncCommitting);
    globalCommitResponse.setMsg("msg");
    globalCommitResponse.setResultCode(ResultCode.Failed);
    globalCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchRegisterFailed);
    GlobalCommitResponseConvertor convertor = new GlobalCommitResponseConvertor();
    GlobalCommitResponseProto proto = convertor.convert2Proto(globalCommitResponse);
    GlobalCommitResponse real = convertor.convert2Model(proto);
    assertThat((real.getTypeCode())).isEqualTo(globalCommitResponse.getTypeCode());
    assertThat((real.getMsg())).isEqualTo(globalCommitResponse.getMsg());
    assertThat((real.getResultCode())).isEqualTo(globalCommitResponse.getResultCode());
    assertThat((real.getTransactionExceptionCode())).isEqualTo(globalCommitResponse.getTransactionExceptionCode());
}
Also used : GlobalCommitResponseProto(io.seata.serializer.protobuf.generated.GlobalCommitResponseProto) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) Test(org.junit.jupiter.api.Test)

Example 5 with GlobalCommitResponse

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

the class AbstractTCInboundHandler method handle.

@Override
public GlobalCommitResponse handle(GlobalCommitRequest request, final RpcContext rpcContext) {
    GlobalCommitResponse response = new GlobalCommitResponse();
    response.setGlobalStatus(GlobalStatus.Committing);
    exceptionHandleTemplate(new AbstractCallback<GlobalCommitRequest, GlobalCommitResponse>() {

        @Override
        public void execute(GlobalCommitRequest request, GlobalCommitResponse response) throws TransactionException {
            try {
                doGlobalCommit(request, response, rpcContext);
            } catch (StoreException e) {
                throw new TransactionException(TransactionExceptionCode.FailedStore, String.format("global commit request failed. xid=%s, msg=%s", request.getXid(), e.getMessage()), e);
            }
        }

        @Override
        public void onTransactionException(GlobalCommitRequest request, GlobalCommitResponse response, TransactionException tex) {
            super.onTransactionException(request, response, tex);
            checkTransactionStatus(request, response);
        }

        @Override
        public void onException(GlobalCommitRequest request, GlobalCommitResponse response, Exception rex) {
            super.onException(request, response, rex);
            checkTransactionStatus(request, response);
        }
    }, request, response);
    return response;
}
Also used : GlobalCommitRequest(io.seata.core.protocol.transaction.GlobalCommitRequest) TransactionException(io.seata.core.exception.TransactionException) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse) TransactionException(io.seata.core.exception.TransactionException) StoreException(io.seata.common.exception.StoreException) StoreException(io.seata.common.exception.StoreException)

Aggregations

GlobalCommitResponse (io.seata.core.protocol.transaction.GlobalCommitResponse)8 Test (org.junit.jupiter.api.Test)5 GlobalCommitRequest (io.seata.core.protocol.transaction.GlobalCommitRequest)3 StoreException (io.seata.common.exception.StoreException)1 TransactionException (io.seata.core.exception.TransactionException)1 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)1 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)1 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)1 GlobalBeginResponse (io.seata.core.protocol.transaction.GlobalBeginResponse)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 AbstractGlobalEndResponseProto (io.seata.serializer.protobuf.generated.AbstractGlobalEndResponseProto)1 AbstractTransactionResponseProto (io.seata.serializer.protobuf.generated.AbstractTransactionResponseProto)1 GlobalCommitResponseProto (io.seata.serializer.protobuf.generated.GlobalCommitResponseProto)1 MergedResultMessageProto (io.seata.serializer.protobuf.generated.MergedResultMessageProto)1 MockServerMessageSender (io.seata.server.coordinator.DefaultCoordinatorTest.MockServerMessageSender)1 HashMap (java.util.HashMap)1