Search in sources :

Example 1 with GlobalCommitRequest

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

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

Example 3 with GlobalCommitRequest

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

the class GlobalCommitRequestCodecTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    GlobalCommitRequest globalCommitRequest = new GlobalCommitRequest();
    globalCommitRequest.setExtraData("aaaa");
    globalCommitRequest.setXid("adf");
    byte[] bytes = seataSerializer.serialize(globalCommitRequest);
    GlobalCommitRequest globalCommitRequest2 = seataSerializer.deserialize(bytes);
    assertThat(globalCommitRequest2.getExtraData()).isEqualTo(globalCommitRequest.getExtraData());
    assertThat(globalCommitRequest2.getXid()).isEqualTo(globalCommitRequest.getXid());
}
Also used : GlobalCommitRequest(io.seata.core.protocol.transaction.GlobalCommitRequest) Test(org.junit.jupiter.api.Test)

Example 4 with GlobalCommitRequest

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

the class DefaultTransactionManager method commit.

@Override
public GlobalStatus commit(String xid) throws TransactionException {
    GlobalCommitRequest globalCommit = new GlobalCommitRequest();
    globalCommit.setXid(xid);
    GlobalCommitResponse response = (GlobalCommitResponse) syncCall(globalCommit);
    return response.getGlobalStatus();
}
Also used : GlobalCommitRequest(io.seata.core.protocol.transaction.GlobalCommitRequest) GlobalCommitResponse(io.seata.core.protocol.transaction.GlobalCommitResponse)

Example 5 with GlobalCommitRequest

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

the class GlobalCommitRequestConvertor method convert2Model.

@Override
public GlobalCommitRequest convert2Model(GlobalCommitRequestProto globalCommitRequestProto) {
    GlobalCommitRequest branchCommitRequest = new GlobalCommitRequest();
    branchCommitRequest.setExtraData(globalCommitRequestProto.getAbstractGlobalEndRequest().getExtraData());
    branchCommitRequest.setXid(globalCommitRequestProto.getAbstractGlobalEndRequest().getXid());
    return branchCommitRequest;
}
Also used : GlobalCommitRequest(io.seata.core.protocol.transaction.GlobalCommitRequest)

Aggregations

GlobalCommitRequest (io.seata.core.protocol.transaction.GlobalCommitRequest)6 GlobalCommitResponse (io.seata.core.protocol.transaction.GlobalCommitResponse)3 Test (org.junit.jupiter.api.Test)3 StoreException (io.seata.common.exception.StoreException)1 TransactionException (io.seata.core.exception.TransactionException)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 GlobalCommitRequestProto (io.seata.serializer.protobuf.generated.GlobalCommitRequestProto)1 MockServerMessageSender (io.seata.server.coordinator.DefaultCoordinatorTest.MockServerMessageSender)1 HashMap (java.util.HashMap)1