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