This is an automated email from the ASF dual-hosted git repository. seanyinx pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git
commit ad172d7b0a56c556cbad8f354b04b1e8c3b06504 Author: Eric Lee <[email protected]> AuthorDate: Tue Jan 9 16:34:43 2018 +0800 SCB-173 update id generate strategy of SagaStart Signed-off-by: Eric Lee <[email protected]> --- .../integration/pack/tests/GreetingController.java | 1 - .../saga/integration/pack/tests/PackIT.java | 125 ++++++++++++--------- .../transaction/SagaStartAnnotationProcessor.java | 14 ++- .../saga/omega/transaction/TransactionAspect.java | 4 +- .../SagaStartAnnotationProcessorTest.java | 6 +- .../TransactionClientHttpRequestInterceptor.java | 9 +- ...ransactionClientHttpRequestInterceptorTest.java | 11 +- 7 files changed, 99 insertions(+), 71 deletions(-) diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/GreetingController.java b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/GreetingController.java index 9d8d2e8..3c7c095 100644 --- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/GreetingController.java +++ b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/GreetingController.java @@ -39,7 +39,6 @@ public class GreetingController { this.restTemplate = restTemplate; } - @SagaStart @GetMapping("/greet") ResponseEntity<String> greet(@RequestParam String name) { diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/PackIT.java b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/PackIT.java index 7776699..32d4254 100644 --- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/PackIT.java +++ b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/PackIT.java @@ -83,41 +83,48 @@ public class PackIT { List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId); assertThat(envelopes.size(), is(6)); - assertThat(envelopes.get(0).type(), is("SagaStartedEvent")); - assertThat(envelopes.get(0).localTxId(), is(notNullValue())); - assertThat(envelopes.get(0).parentTxId(), is(nullValue())); - assertThat(envelopes.get(0).serviceName(), is(serviceName)); - assertThat(envelopes.get(0).instanceId(), is(notNullValue())); - - assertThat(envelopes.get(1).type(), is("TxStartedEvent")); - assertThat(envelopes.get(1).localTxId(), is(envelopes.get(0).localTxId())); - assertThat(envelopes.get(1).parentTxId(), is(nullValue())); - assertThat(envelopes.get(1).serviceName(), is(serviceName)); - assertThat(envelopes.get(1).instanceId(), is(envelopes.get(0).instanceId())); - assertThat(envelopes.get(2).type(), is("TxEndedEvent")); - assertThat(envelopes.get(2).localTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(2).parentTxId(), is(nullValue())); - assertThat(envelopes.get(2).serviceName(), is(serviceName)); - assertThat(envelopes.get(2).instanceId(), is(envelopes.get(1).instanceId())); - - assertThat(envelopes.get(3).type(), is("TxStartedEvent")); - assertThat(envelopes.get(3).localTxId(), is(notNullValue())); - assertThat(envelopes.get(3).parentTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(3).serviceName(), is(serviceName)); - assertThat(envelopes.get(3).instanceId(), is(notNullValue())); - - assertThat(envelopes.get(4).type(), is("TxEndedEvent")); - assertThat(envelopes.get(4).localTxId(), is(envelopes.get(3).localTxId())); - assertThat(envelopes.get(4).parentTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(4).serviceName(), is(serviceName)); - assertThat(envelopes.get(4).instanceId(), is(envelopes.get(3).instanceId())); - - assertThat(envelopes.get(5).type(), is("SagaEndedEvent")); - assertThat(envelopes.get(5).localTxId(), is(envelopes.get(0).localTxId())); - assertThat(envelopes.get(5).parentTxId(), is(nullValue())); - assertThat(envelopes.get(5).serviceName(), is(serviceName)); - assertThat(envelopes.get(5).instanceId(), is(notNullValue())); + TxEventEnvelope sagaStartedEventEnvelope = envelopes.get(0); + assertThat(sagaStartedEventEnvelope.type(), is("SagaStartedEvent")); + assertThat(sagaStartedEventEnvelope.localTxId(), is(notNullValue())); + assertThat(sagaStartedEventEnvelope.parentTxId(), is(nullValue())); + assertThat(sagaStartedEventEnvelope.serviceName(), is(serviceName)); + assertThat(sagaStartedEventEnvelope.instanceId(), is(notNullValue())); + + TxEventEnvelope txStartedEventEnvelope1 = envelopes.get(1); + assertThat(txStartedEventEnvelope1.type(), is("TxStartedEvent")); + assertThat(txStartedEventEnvelope1.localTxId(), is(notNullValue())); + assertThat(txStartedEventEnvelope1.parentTxId(), is(sagaStartedEventEnvelope.localTxId())); + assertThat(txStartedEventEnvelope1.serviceName(), is(serviceName)); + assertThat(txStartedEventEnvelope1.instanceId(), is(sagaStartedEventEnvelope.instanceId())); + + TxEventEnvelope txEndedEventEnvelope1 = envelopes.get(2); + assertThat(txEndedEventEnvelope1.type(), is("TxEndedEvent")); + assertThat(txEndedEventEnvelope1.localTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txEndedEventEnvelope1.parentTxId(), is(sagaStartedEventEnvelope.localTxId())); + assertThat(txEndedEventEnvelope1.serviceName(), is(serviceName)); + assertThat(txEndedEventEnvelope1.instanceId(), is(txStartedEventEnvelope1.instanceId())); + + TxEventEnvelope txStartedEventEnvelope2 = envelopes.get(3); + assertThat(txStartedEventEnvelope2.type(), is("TxStartedEvent")); + assertThat(txStartedEventEnvelope2.localTxId(), is(notNullValue())); + assertThat(txStartedEventEnvelope2.parentTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txStartedEventEnvelope2.serviceName(), is(serviceName)); + assertThat(txStartedEventEnvelope2.instanceId(), is(notNullValue())); + + TxEventEnvelope txEndedEventEnvelope2 = envelopes.get(4); + assertThat(txEndedEventEnvelope2.type(), is("TxEndedEvent")); + assertThat(txEndedEventEnvelope2.localTxId(), is(txStartedEventEnvelope2.localTxId())); + assertThat(txEndedEventEnvelope2.parentTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txEndedEventEnvelope2.serviceName(), is(serviceName)); + assertThat(txEndedEventEnvelope2.instanceId(), is(txStartedEventEnvelope2.instanceId())); + + TxEventEnvelope sagaEndedEventEnvelope = envelopes.get(5); + assertThat(sagaEndedEventEnvelope.type(), is("SagaEndedEvent")); + assertThat(sagaEndedEventEnvelope.localTxId(), is(sagaStartedEventEnvelope.localTxId())); + assertThat(sagaEndedEventEnvelope.parentTxId(), is(nullValue())); + assertThat(sagaEndedEventEnvelope.serviceName(), is(serviceName)); + assertThat(sagaEndedEventEnvelope.instanceId(), is(notNullValue())); assertThat(compensatedMessages.isEmpty(), is(true)); } @@ -139,28 +146,36 @@ public class PackIT { List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId); assertThat(envelopes.size(), is(8)); - assertThat(envelopes.get(0).type(), is("SagaStartedEvent")); - assertThat(envelopes.get(1).type(), is("TxStartedEvent")); + TxEventEnvelope sagaStartedEventEnvelope = envelopes.get(0); + assertThat(sagaStartedEventEnvelope.type(), is("SagaStartedEvent")); + + TxEventEnvelope txStartedEventEnvelope1 = envelopes.get(1); + assertThat(txStartedEventEnvelope1.type(), is("TxStartedEvent")); assertThat(envelopes.get(2).type(), is("TxEndedEvent")); - assertThat(envelopes.get(3).type(), is("TxStartedEvent")); - - assertThat(envelopes.get(4).type(), is("TxAbortedEvent")); - assertThat(envelopes.get(4).localTxId(), is(envelopes.get(3).localTxId())); - assertThat(envelopes.get(4).parentTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(4).serviceName(), is(serviceName)); - assertThat(envelopes.get(4).instanceId(), is(envelopes.get(3).instanceId())); - - assertThat(envelopes.get(5).type(), is("TxCompensatedEvent")); - assertThat(envelopes.get(5).localTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(5).parentTxId(), is(nullValue())); - assertThat(envelopes.get(5).serviceName(), is(serviceName)); - assertThat(envelopes.get(5).instanceId(), is(envelopes.get(1).instanceId())); - - assertThat(envelopes.get(6).type(), is("TxCompensatedEvent")); - assertThat(envelopes.get(6).localTxId(), is(envelopes.get(3).localTxId())); - assertThat(envelopes.get(6).parentTxId(), is(envelopes.get(1).localTxId())); - assertThat(envelopes.get(6).serviceName(), is(serviceName)); - assertThat(envelopes.get(6).instanceId(), is(envelopes.get(3).instanceId())); + + TxEventEnvelope txStartedEventEnvelope2 = envelopes.get(3); + assertThat(txStartedEventEnvelope2.type(), is("TxStartedEvent")); + + TxEventEnvelope txAbortedEventEnvelope = envelopes.get(4); + assertThat(txAbortedEventEnvelope.type(), is("TxAbortedEvent")); + assertThat(txAbortedEventEnvelope.localTxId(), is(txStartedEventEnvelope2.localTxId())); + assertThat(txAbortedEventEnvelope.parentTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txAbortedEventEnvelope.serviceName(), is(serviceName)); + assertThat(txAbortedEventEnvelope.instanceId(), is(txStartedEventEnvelope2.instanceId())); + + TxEventEnvelope txCompensatedEventEnvelope1 = envelopes.get(5); + assertThat(txCompensatedEventEnvelope1.type(), is("TxCompensatedEvent")); + assertThat(txCompensatedEventEnvelope1.localTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txCompensatedEventEnvelope1.parentTxId(), is(sagaStartedEventEnvelope.localTxId())); + assertThat(txCompensatedEventEnvelope1.serviceName(), is(serviceName)); + assertThat(txCompensatedEventEnvelope1.instanceId(), is(txStartedEventEnvelope1.instanceId())); + + TxEventEnvelope txCompensatedEventEnvelope2 = envelopes.get(6); + assertThat(txCompensatedEventEnvelope2.type(), is("TxCompensatedEvent")); + assertThat(txCompensatedEventEnvelope2.localTxId(), is(txStartedEventEnvelope2.localTxId())); + assertThat(txCompensatedEventEnvelope2.parentTxId(), is(txStartedEventEnvelope1.localTxId())); + assertThat(txCompensatedEventEnvelope2.serviceName(), is(serviceName)); + assertThat(txCompensatedEventEnvelope2.instanceId(), is(txStartedEventEnvelope2.instanceId())); assertThat(envelopes.get(7).type(), is("SagaEndedEvent")); diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java index acd6c92..b058570 100644 --- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java +++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java @@ -31,10 +31,20 @@ public class SagaStartAnnotationProcessor { } void preIntercept() { - sender.send(new SagaStartedEvent(omegaContext.newGlobalTxId(), omegaContext.newLocalTxId())); + String globalTxId = initChildContext(); + // reuse the globalTxId as localTxId to differ localTxId in SagaStartedEvent and the first TxStartedEvent + sender.send(new SagaStartedEvent(globalTxId, globalTxId)); } void postIntercept() { - sender.send(new SagaEndedEvent(omegaContext.globalTxId(), omegaContext.localTxId())); + String globalTxId = omegaContext.globalTxId(); + sender.send(new SagaEndedEvent(globalTxId, globalTxId)); + } + + private String initChildContext() { + String globalTxId = omegaContext.newGlobalTxId(); + omegaContext.newLocalTxId(); + omegaContext.setParentTxId(globalTxId); + return globalTxId; } } diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/TransactionAspect.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/TransactionAspect.java index 4ef2144..fca7305 100644 --- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/TransactionAspect.java +++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/TransactionAspect.java @@ -39,14 +39,12 @@ public class TransactionAspect { private final SagaStartAnnotationProcessor sagaStartAnnotationProcessor; private final OmegaContext context; - public TransactionAspect(MessageSender sender, - OmegaContext context) { + public TransactionAspect(MessageSender sender, OmegaContext context) { this.context = context; this.preTransactionInterceptor = new PreTransactionInterceptor(sender); this.postTransactionInterceptor = new PostTransactionInterceptor(sender); this.failedTransactionInterceptor = new FailedTransactionInterceptor(sender); this.sagaStartAnnotationProcessor = new SagaStartAnnotationProcessor(this.context, sender); - } @Around("execution(@org.apache.servicecomb.saga.omega.transaction.annotations.Compensable * *(..)) && @annotation(compensable)") diff --git a/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java b/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java index 4eca86a..64735f0 100644 --- a/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java +++ b/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java @@ -41,7 +41,6 @@ public class SagaStartAnnotationProcessorTest { private final String localTxId = UUID.randomUUID().toString(); - private final IdGenerator generator = mock(IdGenerator.class); @SuppressWarnings("unchecked") @@ -58,11 +57,12 @@ public class SagaStartAnnotationProcessorTest { assertThat(context.globalTxId(), is(globalTxId)); assertThat(context.localTxId(), is(localTxId)); + assertThat(context.parentTxId(), is(globalTxId)); TxEvent event = messages.get(0); assertThat(event.globalTxId(), is(globalTxId)); - assertThat(event.localTxId(), is(localTxId)); + assertThat(event.localTxId(), is(globalTxId)); assertThat(event.parentTxId(), is(nullValue())); assertThat(event.compensationMethod().isEmpty(), is(true)); assertThat(event.type(), is("SagaStartedEvent")); @@ -80,7 +80,7 @@ public class SagaStartAnnotationProcessorTest { TxEvent event = messages.get(0); assertThat(event.globalTxId(), is(globalTxId)); - assertThat(event.localTxId(), is(localTxId)); + assertThat(event.localTxId(), is(globalTxId)); assertThat(event.parentTxId(), is(nullValue())); assertThat(event.compensationMethod().isEmpty(), is(true)); assertThat(event.type(), is("SagaEndedEvent")); diff --git a/omega/omega-transport/omega-transport-resttemplate/src/main/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java b/omega/omega-transport/omega-transport-resttemplate/src/main/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java index 5b4ceda..6d7c428 100644 --- a/omega/omega-transport/omega-transport-resttemplate/src/main/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java +++ b/omega/omega-transport/omega-transport-resttemplate/src/main/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptor.java @@ -19,8 +19,11 @@ package org.apache.servicecomb.saga.omega.transport.resttemplate; import java.io.IOException; +import java.lang.invoke.MethodHandles; import org.apache.servicecomb.saga.omega.context.OmegaContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; @@ -28,6 +31,8 @@ import org.springframework.http.client.ClientHttpResponse; class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final OmegaContext omegaContext; TransactionClientHttpRequestInterceptor(OmegaContext omegaContext) { @@ -47,7 +52,7 @@ class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterc String globalTxId = omegaContext.globalTxId(); if (globalTxId == null) { - return omegaContext.newGlobalTxId(); + LOG.error("Global tx id should not be null."); } return globalTxId; } @@ -56,7 +61,7 @@ class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterc String localTxId = omegaContext.localTxId(); if (localTxId == null) { - return omegaContext.newLocalTxId(); + LOG.error("Local tx id should not be null of global tx {}.", omegaContext.globalTxId()); } return localTxId; } diff --git a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java b/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java index 82e74cf..371c0de 100644 --- a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java +++ b/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/TransactionClientHttpRequestInterceptorTest.java @@ -20,6 +20,7 @@ package org.apache.servicecomb.saga.omega.transport.resttemplate; import static com.seanyinx.github.unit.scaffolding.Randomness.uniquify; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; @@ -65,18 +66,18 @@ public class TransactionClientHttpRequestInterceptorTest { } @Test - public void newTransactionIdInHeaderIfNonExists() throws IOException { + public void transactionIdInHeaderIsNullIfNonExists() throws IOException { when(request.getHeaders()).thenReturn(new HttpHeaders()); when(execution.execute(request, null)).thenReturn(response); clientHttpRequestInterceptor.intercept(request, null, execution); - assertThat(request.getHeaders().get(OmegaContext.GLOBAL_TX_ID_KEY), contains(globalTxId)); - assertThat(request.getHeaders().get(OmegaContext.LOCAL_TX_ID_KEY), contains(localTxId)); + assertThat(request.getHeaders().get(OmegaContext.GLOBAL_TX_ID_KEY), contains(nullValue())); + assertThat(request.getHeaders().get(OmegaContext.LOCAL_TX_ID_KEY), contains(nullValue())); - assertThat(omegaContext.globalTxId(), is(globalTxId)); - assertThat(omegaContext.localTxId(), is(localTxId)); + assertThat(omegaContext.globalTxId(), is(nullValue())); + assertThat(omegaContext.localTxId(), is(nullValue())); } @Test -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
