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:28 UTC

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

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>.