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();
}
}
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;
}
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());
}
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();
}
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;
}
Aggregations