You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by se...@apache.org on 2018/01/10 01:00:26 UTC

[incubator-servicecomb-saga] branch master updated (3c166ad -> b93e795)

This is an automated email from the ASF dual-hosted git repository.

seanyinx pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git.


    from 3c166ad  SCB-193 Clean up the project name
     new 541360c  SCB-173 mark start of saga
     new 9eed9e6  SCB-173 add saga ended event
     new ad172d7  SCB-173 update id generate strategy of SagaStart
     new b93e795  SCB-173 move context init from request to preInterceptor

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../integration/pack/tests/GreetingController.java |   3 +-
 .../saga/integration/pack/tests/PackIT.java        | 152 ++++++++++++---------
 .../integration/pack/tests/TxEventEnvelope.java    |   4 +
 .../pack/tests/TxEventEnvelopeRepository.java      |   4 +
 .../connector/grpc/GrpcClientMessageSender.java    |   4 +-
 .../saga/omega/context/annotations/SagaStart.java} |   7 +-
 .../spring/CompensableMethodCheckingCallback.java  |   7 +-
 .../{MessageSender.java => SagaEndedEvent.java}    |   9 +-
 ...dler.java => SagaStartAnnotationProcessor.java} |  23 ++--
 .../{MessageSender.java => SagaStartedEvent.java}  |  10 +-
 .../saga/omega/transaction/TransactionAspect.java  |  39 +++++-
 .../SagaStartAnnotationProcessorTest.java          |  89 ++++++++++++
 .../TransactionClientHttpRequestInterceptor.java   |  24 +---
 ...ransactionClientHttpRequestInterceptorTest.java |  13 +-
 14 files changed, 258 insertions(+), 130 deletions(-)
 copy omega/{omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/annotations/OmegaContextAware.java => omega-context/src/main/java/org/apache/servicecomb/saga/omega/context/annotations/SagaStart.java} (84%)
 copy omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/{MessageSender.java => SagaEndedEvent.java} (85%)
 copy omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/{CompensationMessageHandler.java => SagaStartAnnotationProcessor.java} (67%)
 copy omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/{MessageSender.java => SagaStartedEvent.java} (78%)
 create mode 100644 omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java

-- 
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].

[incubator-servicecomb-saga] 03/04: SCB-173 update id generate strategy of SagaStart

Posted by se...@apache.org.
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 <da...@huawei.com>
AuthorDate: Tue Jan 9 16:34:43 2018 +0800

    SCB-173 update id generate strategy of SagaStart
    
    Signed-off-by: Eric Lee <da...@huawei.com>
---
 .../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
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.

[incubator-servicecomb-saga] 01/04: SCB-173 mark start of saga

Posted by se...@apache.org.
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 541360c4361f69cde406672fc552a196fc21ace6
Author: Eric Lee <da...@huawei.com>
AuthorDate: Mon Jan 8 20:48:09 2018 +0800

    SCB-173 mark start of saga
    
    Signed-off-by: Eric Lee <da...@huawei.com>
---
 .../integration/pack/tests/GreetingController.java |  2 +
 .../saga/integration/pack/tests/PackIT.java        | 91 +++++++++++-----------
 .../integration/pack/tests/TxEventEnvelope.java    |  4 +
 .../pack/tests/TxEventEnvelopeRepository.java      |  4 +
 .../connector/grpc/GrpcClientMessageSender.java    |  4 +-
 .../saga/omega/context/annotations/SagaStart.java  | 13 ++--
 .../transaction/SagaStartAnnotationProcessor.java  | 20 +++--
 .../saga/omega/transaction/SagaStartedEvent.java   | 12 +--
 .../saga/omega/transaction/TransactionAspect.java  | 27 ++++++-
 .../SagaStartAnnotationProcessorTest.java          | 71 +++++++++++++++++
 10 files changed, 179 insertions(+), 69 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 12356ee..9d8d2e8 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
@@ -17,6 +17,7 @@
 
 package org.apache.servicecomb.saga.integration.pack.tests;
 
+import org.apache.servicecomb.saga.omega.context.annotations.SagaStart;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
@@ -39,6 +40,7 @@ public class GreetingController {
   }
 
 
+  @SagaStart
   @GetMapping("/greet")
   ResponseEntity<String> greet(@RequestParam String name) {
     String greetings = greetingService.greet(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 66454b7..de9e48f 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
@@ -25,7 +25,6 @@ import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
-import static org.springframework.http.HttpMethod.GET;
 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
 import static org.springframework.http.HttpStatus.OK;
 
@@ -41,8 +40,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
 import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
 import org.springframework.http.ResponseEntity;
 import org.springframework.test.context.junit4.SpringRunner;
 
@@ -72,90 +69,92 @@ public class PackIT {
 
   @Test
   public void updatesTxStateToAlpha() throws Exception {
-    HttpHeaders headers = new HttpHeaders();
-
-    headers.set(OmegaContext.GLOBAL_TX_ID_KEY, globalTxId);
-
-    ResponseEntity<String> entity = restTemplate.exchange("/greet?name={name}",
-        GET,
-        new HttpEntity<>(headers),
+    ResponseEntity<String> entity = restTemplate.getForEntity("/greet?name={name}",
         String.class,
         "mike");
 
     assertThat(entity.getStatusCode(), is(OK));
     assertThat(entity.getBody(), is("Greetings, mike; Bonjour, mike"));
 
+    List<String> distinctGlobalTxIds = repository.findDistinctGlobalTxId();
+    assertThat(distinctGlobalTxIds.size(), is(1));
+
+    String globalTxId = distinctGlobalTxIds.get(0);
     List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId);
 
-    assertThat(envelopes.size(), is(4));
-    assertThat(envelopes.get(0).type(), is("TxStartedEvent"));
+    assertThat(envelopes.size(), is(5));
+    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("TxEndedEvent"));
+    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("TxStartedEvent"));
-    assertThat(envelopes.get(2).localTxId(), is(notNullValue()));
-    assertThat(envelopes.get(2).parentTxId(), is(envelopes.get(0).localTxId()));
+    assertThat(envelopes.get(2).type(), is("TxEndedEvent"));
+    assertThat(envelopes.get(2).localTxId(), is(envelopes.get(0).localTxId()));
+    assertThat(envelopes.get(2).parentTxId(), is(nullValue()));
     assertThat(envelopes.get(2).serviceName(), is(serviceName));
-    assertThat(envelopes.get(2).instanceId(), is(notNullValue()));
+    assertThat(envelopes.get(2).instanceId(), is(envelopes.get(0).instanceId()));
 
-    assertThat(envelopes.get(3).type(), is("TxEndedEvent"));
-    assertThat(envelopes.get(3).localTxId(), is(envelopes.get(2).localTxId()));
+    assertThat(envelopes.get(3).type(), is("TxStartedEvent"));
+    assertThat(envelopes.get(3).localTxId(), is(notNullValue()));
     assertThat(envelopes.get(3).parentTxId(), is(envelopes.get(0).localTxId()));
     assertThat(envelopes.get(3).serviceName(), is(serviceName));
-    assertThat(envelopes.get(3).instanceId(), is(envelopes.get(2).instanceId()));
+    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(0).localTxId()));
+    assertThat(envelopes.get(4).serviceName(), is(serviceName));
+    assertThat(envelopes.get(4).instanceId(), is(envelopes.get(3).instanceId()));
 
     assertThat(compensatedMessages.isEmpty(), is(true));
   }
 
   @Test
   public void compensatesFailedGlobalTransaction() throws Exception {
-    HttpHeaders headers = new HttpHeaders();
-
-    headers.set(OmegaContext.GLOBAL_TX_ID_KEY, globalTxId);
-
-    ResponseEntity<String> entity = restTemplate.exchange("/greet?name={name}",
-        GET,
-        new HttpEntity<>(headers),
+    ResponseEntity<String> entity = restTemplate.getForEntity("/greet?name={name}",
         String.class,
         TRESPASSER);
 
     assertThat(entity.getStatusCode(), is(INTERNAL_SERVER_ERROR));
 
-    await().atMost(2, SECONDS).until(() -> repository.count() == 6);
+    await().atMost(2, SECONDS).until(() -> repository.count() == 7);
 
-    List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId);
-    assertThat(envelopes.size(), is(6));
+    List<String> distinctGlobalTxIds = repository.findDistinctGlobalTxId();
+    assertThat(distinctGlobalTxIds.size(), is(1));
 
-    assertThat(envelopes.get(0).type(), is("TxStartedEvent"));
-    assertThat(envelopes.get(1).type(), is("TxEndedEvent"));
-    assertThat(envelopes.get(2).type(), is("TxStartedEvent"));
+    String globalTxId = distinctGlobalTxIds.get(0);
+    List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId);
+    assertThat(envelopes.size(), is(7));
 
-    assertThat(envelopes.get(3).type(), is("TxAbortedEvent"));
-    assertThat(envelopes.get(3).localTxId(), is(envelopes.get(2).localTxId()));
-    assertThat(envelopes.get(3).parentTxId(), is(envelopes.get(0).localTxId()));
-    assertThat(envelopes.get(3).serviceName(), is(serviceName));
-    assertThat(envelopes.get(3).instanceId(), is(envelopes.get(2).instanceId()));
+    assertThat(envelopes.get(0).type(), is("SagaStartedEvent"));
+    assertThat(envelopes.get(1).type(), is("TxStartedEvent"));
+    assertThat(envelopes.get(2).type(), is("TxEndedEvent"));
+    assertThat(envelopes.get(3).type(), is("TxStartedEvent"));
 
-    assertThat(envelopes.get(4).type(), is("TxCompensatedEvent"));
-    assertThat(envelopes.get(4).localTxId(), is(envelopes.get(0).localTxId()));
-    assertThat(envelopes.get(4).parentTxId(), is(nullValue()));
+    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(0).instanceId()));
+    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(2).localTxId()));
-    assertThat(envelopes.get(5).parentTxId(), is(envelopes.get(0).localTxId()));
+    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(2).instanceId()));
+    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()));
 
     assertThat(compensatedMessages, contains(
         "Goodbye, " + TRESPASSER,
diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelope.java b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelope.java
index d99c2b6..0087dd7 100644
--- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelope.java
+++ b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelope.java
@@ -50,6 +50,10 @@ class TxEventEnvelope {
     return instanceId;
   }
 
+  String globalTxId() {
+    return globalTxId;
+  }
+
   String localTxId() {
     return localTxId;
   }
diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
index 7e4b7e9..5400d7c 100644
--- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
+++ b/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
@@ -19,8 +19,12 @@ package org.apache.servicecomb.saga.integration.pack.tests;
 
 import java.util.List;
 
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.CrudRepository;
 
 interface TxEventEnvelopeRepository extends CrudRepository<TxEventEnvelope, Long> {
   List<TxEventEnvelope> findByGlobalTxIdOrderByCreationTime(String globalTxId);
+
+  @Query("SELECT DISTINCT(e.globalTxId) from TxEventEnvelope e")
+  List<String> findDistinctGlobalTxId();
 }
diff --git a/omega/omega-connector/omega-connector-grpc/src/main/java/org/apache/servicecomb/saga/omega/connector/grpc/GrpcClientMessageSender.java b/omega/omega-connector/omega-connector-grpc/src/main/java/org/apache/servicecomb/saga/omega/connector/grpc/GrpcClientMessageSender.java
index b2e837c..59fbce1 100644
--- a/omega/omega-connector/omega-connector-grpc/src/main/java/org/apache/servicecomb/saga/omega/connector/grpc/GrpcClientMessageSender.java
+++ b/omega/omega-connector/omega-connector-grpc/src/main/java/org/apache/servicecomb/saga/omega/connector/grpc/GrpcClientMessageSender.java
@@ -84,13 +84,11 @@ public class GrpcClientMessageSender implements MessageSender {
         .setTimestamp(event.timestamp())
         .setGlobalTxId(event.globalTxId())
         .setLocalTxId(event.localTxId())
+        .setParentTxId(event.parentTxId() == null ? "" : event.parentTxId())
         .setType(event.type())
         .setCompensationMethod(event.compensationMethod())
         .setPayloads(payloads);
 
-    if (event.parentTxId() != null) {
-      builder.setParentTxId(event.parentTxId());
-    }
     return builder.build();
   }
 
diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java b/omega/omega-context/src/main/java/org/apache/servicecomb/saga/omega/context/annotations/SagaStart.java
similarity index 71%
copy from integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
copy to omega/omega-context/src/main/java/org/apache/servicecomb/saga/omega/context/annotations/SagaStart.java
index 7e4b7e9..435d72f 100644
--- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
+++ b/omega/omega-context/src/main/java/org/apache/servicecomb/saga/omega/context/annotations/SagaStart.java
@@ -15,12 +15,15 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.saga.integration.pack.tests;
+package org.apache.servicecomb.saga.omega.context.annotations;
 
-import java.util.List;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
-import org.springframework.data.repository.CrudRepository;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
 
-interface TxEventEnvelopeRepository extends CrudRepository<TxEventEnvelope, Long> {
-  List<TxEventEnvelope> findByGlobalTxIdOrderByCreationTime(String globalTxId);
+@Retention(RUNTIME)
+@Target(METHOD)
+public @interface SagaStart {
 }
diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java
similarity index 60%
copy from integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
copy to omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java
index 7e4b7e9..66fadda 100644
--- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java
@@ -15,12 +15,22 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.saga.integration.pack.tests;
+package org.apache.servicecomb.saga.omega.transaction;
 
-import java.util.List;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
 
-import org.springframework.data.repository.CrudRepository;
+public class SagaStartAnnotationProcessor {
 
-interface TxEventEnvelopeRepository extends CrudRepository<TxEventEnvelope, Long> {
-  List<TxEventEnvelope> findByGlobalTxIdOrderByCreationTime(String globalTxId);
+  private final OmegaContext omegaContext;
+
+  private final MessageSender sender;
+
+  SagaStartAnnotationProcessor(OmegaContext omegaContext, MessageSender sender) {
+    this.omegaContext = omegaContext;
+    this.sender = sender;
+  }
+
+  void intercept() {
+    sender.send(new SagaStartedEvent(omegaContext.newGlobalTxId(), omegaContext.newLocalTxId()));
+  }
 }
diff --git a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartedEvent.java
similarity index 72%
copy from integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
copy to omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartedEvent.java
index 7e4b7e9..0985f59 100644
--- a/integration-tests/pack-tests/src/test/java/org/apache/servicecomb/saga/integration/pack/tests/TxEventEnvelopeRepository.java
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartedEvent.java
@@ -15,12 +15,12 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.saga.integration.pack.tests;
+package org.apache.servicecomb.saga.omega.transaction;
 
-import java.util.List;
+public class SagaStartedEvent extends TxEvent {
 
-import org.springframework.data.repository.CrudRepository;
-
-interface TxEventEnvelopeRepository extends CrudRepository<TxEventEnvelope, Long> {
-  List<TxEventEnvelope> findByGlobalTxIdOrderByCreationTime(String globalTxId);
+  public SagaStartedEvent(String globalTxId, String localTxId) {
+    // use "" instead of null as compensationMethod requires not null in sql
+    super(globalTxId, localTxId, null, "");
+  }
 }
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 2b68da5..3ad52a8 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
@@ -20,6 +20,8 @@ package org.apache.servicecomb.saga.omega.transaction;
 import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Method;
 
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
@@ -27,22 +29,24 @@ import org.aspectj.lang.reflect.MethodSignature;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import org.apache.servicecomb.saga.omega.context.OmegaContext;
-import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
-
 @Aspect
 public class TransactionAspect {
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
   private final PreTransactionInterceptor preTransactionInterceptor;
   private final PostTransactionInterceptor postTransactionInterceptor;
   private final FailedTransactionInterceptor failedTransactionInterceptor;
+
+  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)")
@@ -65,6 +69,21 @@ public class TransactionAspect {
     }
   }
 
+  @Around("execution(@org.apache.servicecomb.saga.omega.context.annotations.SagaStart * *(..))")
+  Object advise(ProceedingJoinPoint joinPoint) throws Throwable {
+    Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
+
+    LOG.debug("Initializing global tx id before execution of method {}", method.toString());
+    sagaStartAnnotationProcessor.intercept();
+
+    try {
+      return joinPoint.proceed();
+    } catch (Throwable throwable) {
+      LOG.error("Failed to process SagaStart method: {}", method.toString());
+      throw throwable;
+    }
+  }
+
   private String compensationMethodSignature(ProceedingJoinPoint joinPoint, Compensable compensable, Method method)
       throws NoSuchMethodException {
 
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
new file mode 100644
index 0000000..6ef5aff
--- /dev/null
+++ b/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessorTest.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.saga.omega.transaction;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.servicecomb.saga.omega.context.IdGenerator;
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.junit.Test;
+
+public class SagaStartAnnotationProcessorTest {
+
+  private final List<TxEvent> messages = new ArrayList<>();
+
+  private final MessageSender sender = messages::add;
+
+  private final String globalTxId = UUID.randomUUID().toString();
+
+  private final String localTxId = UUID.randomUUID().toString();
+
+
+  private final IdGenerator generator = mock(IdGenerator.class);
+
+  @SuppressWarnings("unchecked")
+  private final OmegaContext context = new OmegaContext(generator);
+
+  private final SagaStartAnnotationProcessor sagaStartAnnotationProcessor = new SagaStartAnnotationProcessor(context,
+      sender);
+
+  @Test
+  public void sendsSagaStartedEvent() {
+    when(generator.nextId()).thenReturn(globalTxId, localTxId);
+
+    sagaStartAnnotationProcessor.intercept();
+
+    assertThat(context.globalTxId(), is(globalTxId));
+    assertThat(context.localTxId(), is(localTxId));
+
+    TxEvent event = messages.get(0);
+
+    assertThat(event.globalTxId(), is(globalTxId));
+    assertThat(event.localTxId(), is(localTxId));
+    assertThat(event.parentTxId(), is(nullValue()));
+    assertThat(event.compensationMethod().isEmpty(), is(true));
+    assertThat(event.type(), is("SagaStartedEvent"));
+    assertThat(event.parentTxId(), is(nullValue()));
+  }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.

[incubator-servicecomb-saga] 02/04: SCB-173 add saga ended event

Posted by se...@apache.org.
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 9eed9e64bd2072f7c6b1db3eff1b68af628207a8
Author: Eric Lee <da...@huawei.com>
AuthorDate: Tue Jan 9 10:48:14 2018 +0800

    SCB-173 add saga ended event
    
    Signed-off-by: Eric Lee <da...@huawei.com>
---
 .../saga/integration/pack/tests/PackIT.java        | 22 +++++++++++++++-------
 ...nnotationProcessor.java => SagaEndedEvent.java} | 17 +++--------------
 .../transaction/SagaStartAnnotationProcessor.java  |  6 +++++-
 .../saga/omega/transaction/TransactionAspect.java  |  5 ++++-
 .../SagaStartAnnotationProcessorTest.java          | 20 +++++++++++++++++++-
 5 files changed, 46 insertions(+), 24 deletions(-)

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 de9e48f..7776699 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
@@ -82,7 +82,7 @@ public class PackIT {
     String globalTxId = distinctGlobalTxIds.get(0);
     List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId);
 
-    assertThat(envelopes.size(), is(5));
+    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()));
@@ -96,23 +96,29 @@ public class PackIT {
     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(0).localTxId()));
+    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(0).instanceId()));
+    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(0).localTxId()));
+    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(0).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()));
+
     assertThat(compensatedMessages.isEmpty(), is(true));
   }
 
@@ -124,14 +130,14 @@ public class PackIT {
 
     assertThat(entity.getStatusCode(), is(INTERNAL_SERVER_ERROR));
 
-    await().atMost(2, SECONDS).until(() -> repository.count() == 7);
+    await().atMost(2, SECONDS).until(() -> repository.count() == 8);
 
     List<String> distinctGlobalTxIds = repository.findDistinctGlobalTxId();
     assertThat(distinctGlobalTxIds.size(), is(1));
 
     String globalTxId = distinctGlobalTxIds.get(0);
     List<TxEventEnvelope> envelopes = repository.findByGlobalTxIdOrderByCreationTime(globalTxId);
-    assertThat(envelopes.size(), is(7));
+    assertThat(envelopes.size(), is(8));
 
     assertThat(envelopes.get(0).type(), is("SagaStartedEvent"));
     assertThat(envelopes.get(1).type(), is("TxStartedEvent"));
@@ -156,6 +162,8 @@ public class PackIT {
     assertThat(envelopes.get(6).serviceName(), is(serviceName));
     assertThat(envelopes.get(6).instanceId(), is(envelopes.get(3).instanceId()));
 
+    assertThat(envelopes.get(7).type(), is("SagaEndedEvent"));
+
     assertThat(compensatedMessages, contains(
         "Goodbye, " + TRESPASSER,
         "My bad, please take the window instead, " + TRESPASSER));
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/SagaEndedEvent.java
similarity index 65%
copy from omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAnnotationProcessor.java
copy to omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaEndedEvent.java
index 66fadda..3048d36 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/SagaEndedEvent.java
@@ -17,20 +17,9 @@
 
 package org.apache.servicecomb.saga.omega.transaction;
 
-import org.apache.servicecomb.saga.omega.context.OmegaContext;
+public class SagaEndedEvent extends TxEvent {
 
-public class SagaStartAnnotationProcessor {
-
-  private final OmegaContext omegaContext;
-
-  private final MessageSender sender;
-
-  SagaStartAnnotationProcessor(OmegaContext omegaContext, MessageSender sender) {
-    this.omegaContext = omegaContext;
-    this.sender = sender;
-  }
-
-  void intercept() {
-    sender.send(new SagaStartedEvent(omegaContext.newGlobalTxId(), omegaContext.newLocalTxId()));
+  public SagaEndedEvent(String globalTxId, String localTxId) {
+    super(globalTxId, localTxId, null, "");
   }
 }
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 66fadda..acd6c92 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
@@ -30,7 +30,11 @@ public class SagaStartAnnotationProcessor {
     this.sender = sender;
   }
 
-  void intercept() {
+  void preIntercept() {
     sender.send(new SagaStartedEvent(omegaContext.newGlobalTxId(), omegaContext.newLocalTxId()));
   }
+
+  void postIntercept() {
+    sender.send(new SagaEndedEvent(omegaContext.globalTxId(), omegaContext.localTxId()));
+  }
 }
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 3ad52a8..4ef2144 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
@@ -74,13 +74,16 @@ public class TransactionAspect {
     Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
 
     LOG.debug("Initializing global tx id before execution of method {}", method.toString());
-    sagaStartAnnotationProcessor.intercept();
+    sagaStartAnnotationProcessor.preIntercept();
 
     try {
       return joinPoint.proceed();
     } catch (Throwable throwable) {
       LOG.error("Failed to process SagaStart method: {}", method.toString());
       throw throwable;
+    } finally {
+      LOG.debug("Transaction {} has finished.", context.globalTxId());
+      sagaStartAnnotationProcessor.postIntercept();
     }
   }
 
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 6ef5aff..4eca86a 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
@@ -54,7 +54,7 @@ public class SagaStartAnnotationProcessorTest {
   public void sendsSagaStartedEvent() {
     when(generator.nextId()).thenReturn(globalTxId, localTxId);
 
-    sagaStartAnnotationProcessor.intercept();
+    sagaStartAnnotationProcessor.preIntercept();
 
     assertThat(context.globalTxId(), is(globalTxId));
     assertThat(context.localTxId(), is(localTxId));
@@ -66,6 +66,24 @@ public class SagaStartAnnotationProcessorTest {
     assertThat(event.parentTxId(), is(nullValue()));
     assertThat(event.compensationMethod().isEmpty(), is(true));
     assertThat(event.type(), is("SagaStartedEvent"));
+    assertThat(event.payloads().length, is(0));
+  }
+
+  @Test
+  public void sendsSagaEndedEvent() {
+    context.clear();
+    context.setGlobalTxId(globalTxId);
+    context.setLocalTxId(localTxId);
+
+    sagaStartAnnotationProcessor.postIntercept();
+
+    TxEvent event = messages.get(0);
+
+    assertThat(event.globalTxId(), is(globalTxId));
+    assertThat(event.localTxId(), is(localTxId));
     assertThat(event.parentTxId(), is(nullValue()));
+    assertThat(event.compensationMethod().isEmpty(), is(true));
+    assertThat(event.type(), is("SagaEndedEvent"));
+    assertThat(event.payloads().length, is(0));
   }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.

[incubator-servicecomb-saga] 04/04: SCB-173 move context init from request to preInterceptor

Posted by se...@apache.org.
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 b93e79527eead2a6c3e06c36cc27d2ab65003458
Author: Eric Lee <da...@huawei.com>
AuthorDate: Tue Jan 9 21:51:36 2018 +0800

    SCB-173 move context init from request to preInterceptor
    
    Signed-off-by: Eric Lee <da...@huawei.com>
---
 .../spring/CompensableMethodCheckingCallback.java  |  7 +++---
 .../transaction/SagaStartAnnotationProcessor.java  |  9 +------
 .../saga/omega/transaction/TransactionAspect.java  | 13 ++++++++++
 .../SagaStartAnnotationProcessorTest.java          |  4 +--
 .../TransactionClientHttpRequestInterceptor.java   | 29 +++-------------------
 ...ransactionClientHttpRequestInterceptorTest.java | 14 +++--------
 6 files changed, 26 insertions(+), 50 deletions(-)

diff --git a/omega/omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java b/omega/omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java
index 2793109..ac6615c 100644
--- a/omega/omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java
+++ b/omega/omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/CompensableMethodCheckingCallback.java
@@ -21,13 +21,12 @@ import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Method;
 
 import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.apache.servicecomb.saga.omega.transaction.OmegaException;
+import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.ReflectionUtils.MethodCallback;
 
-import org.apache.servicecomb.saga.omega.transaction.OmegaException;
-import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable;
-
 class CompensableMethodCheckingCallback implements MethodCallback {
   private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
@@ -40,7 +39,7 @@ class CompensableMethodCheckingCallback implements MethodCallback {
   }
 
   @Override
-  public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
+  public void doWith(Method method) throws IllegalArgumentException {
     if (!method.isAnnotationPresent(Compensable.class)) {
       return;
     }
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 b058570..e92ebb1 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,7 +31,7 @@ public class SagaStartAnnotationProcessor {
   }
 
   void preIntercept() {
-    String globalTxId = initChildContext();
+    String globalTxId = omegaContext.newGlobalTxId();
     // reuse the globalTxId as localTxId to differ localTxId in SagaStartedEvent and the first TxStartedEvent
     sender.send(new SagaStartedEvent(globalTxId, globalTxId));
   }
@@ -40,11 +40,4 @@ public class SagaStartAnnotationProcessor {
     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 fca7305..bf13e3a 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
@@ -95,6 +95,9 @@ public class TransactionAspect {
   }
 
   private void preIntercept(ProceedingJoinPoint joinPoint, String signature) {
+    // context without a parent should be the first TxStartedEvent
+    initFirstOmegaContext();
+
     preTransactionInterceptor.intercept(
         context.globalTxId(),
         context.localTxId(),
@@ -119,4 +122,14 @@ public class TransactionAspect {
         signature,
         throwable);
   }
+
+  private void initFirstOmegaContext() {
+    if (context.parentTxId() != null) {
+      return;
+    }
+    if (context.localTxId() == null) {
+      context.newLocalTxId();
+    }
+    context.setParentTxId(context.globalTxId());
+  }
 }
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 64735f0..8913b7f 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
@@ -56,8 +56,8 @@ public class SagaStartAnnotationProcessorTest {
     sagaStartAnnotationProcessor.preIntercept();
 
     assertThat(context.globalTxId(), is(globalTxId));
-    assertThat(context.localTxId(), is(localTxId));
-    assertThat(context.parentTxId(), is(globalTxId));
+    assertThat(context.localTxId(), is(nullValue()));
+    assertThat(context.parentTxId(), is(nullValue()));
 
     TxEvent event = messages.get(0);
 
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 6d7c428..5e69598 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,11 +19,8 @@
 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;
@@ -31,8 +28,6 @@ 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) {
@@ -43,26 +38,10 @@ class TransactionClientHttpRequestInterceptor implements ClientHttpRequestInterc
   public ClientHttpResponse intercept(HttpRequest request, byte[] body,
       ClientHttpRequestExecution execution) throws IOException {
 
-    request.getHeaders().add(OmegaContext.GLOBAL_TX_ID_KEY, globalTxId());
-    request.getHeaders().add(OmegaContext.LOCAL_TX_ID_KEY, localTxId());
-    return execution.execute(request, body);
-  }
-
-  private String globalTxId() {
-    String globalTxId = omegaContext.globalTxId();
-
-    if (globalTxId == null) {
-      LOG.error("Global tx id should not be null.");
+    if (omegaContext.globalTxId() != null) {
+      request.getHeaders().add(OmegaContext.GLOBAL_TX_ID_KEY, omegaContext.globalTxId());
+      request.getHeaders().add(OmegaContext.LOCAL_TX_ID_KEY, omegaContext.localTxId());
     }
-    return globalTxId;
-  }
-
-  private String localTxId() {
-    String localTxId = omegaContext.localTxId();
-
-    if (localTxId == null) {
-      LOG.error("Local tx id should not be null of global tx {}.", omegaContext.globalTxId());
-    }
-    return localTxId;
+    return execution.execute(request, body);
   }
 }
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 371c0de..14e288e 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,7 +20,6 @@ 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;
@@ -66,22 +65,18 @@ public class TransactionClientHttpRequestInterceptorTest {
   }
 
   @Test
-  public void transactionIdInHeaderIsNullIfNonExists() throws IOException {
+  public void keepHeaderUnchangedIfContextAbsent() 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(nullValue()));
-    assertThat(request.getHeaders().get(OmegaContext.LOCAL_TX_ID_KEY), contains(nullValue()));
-
-    assertThat(omegaContext.globalTxId(), is(nullValue()));
-    assertThat(omegaContext.localTxId(), is(nullValue()));
+    assertThat(request.getHeaders().isEmpty(), is(true));
   }
 
   @Test
-  public void sameTransactionIdInHeaderIfAlreadyExists() throws IOException {
+  public void interceptTransactionIdInHeaderIfContextPresent() throws IOException {
     omegaContext.setGlobalTxId(globalTxId);
     omegaContext.setLocalTxId(localTxId);
 
@@ -93,8 +88,5 @@ public class TransactionClientHttpRequestInterceptorTest {
 
     assertThat(request.getHeaders().get(OmegaContext.GLOBAL_TX_ID_KEY), contains(globalTxId));
     assertThat(request.getHeaders().get(OmegaContext.LOCAL_TX_ID_KEY), contains(localTxId));
-
-    assertThat(omegaContext.globalTxId(), is(globalTxId));
-    assertThat(omegaContext.localTxId(), is(localTxId));
   }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.