You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/08/06 02:09:47 UTC

[incubator-servicecomb-saga] 01/02: SCB-784 Added the TxAbortEvent when there is exception is thrown in SagaStart annotation

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

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

commit 24b8a598a3abf6978b267c2e0117962d231fcb5c
Author: Willem Jiang <ji...@huawei.com>
AuthorDate: Sun Jul 29 22:38:41 2018 +0800

    SCB-784 Added the TxAbortEvent when there is exception is thrown in SagaStart annotation
---
 .../resources/AExceptionWhenAb_scenario.feature    |  4 ++-
 .../resources/BExceptionWhenAb_scenario.feature    |  4 ++-
 .../resources/CExceptionWhenAbAc_scenario.feature  |  2 ++
 .../resources/CExceptionWhenAbBc_scenario.feature  |  2 ++
 .../org/apache/servicecomb/saga/PackStepdefs.java  |  2 +-
 .../src/test/resources/booking_exception.btm}      | 36 ++++++++--------------
 .../resources/pack_compensation_scenario.feature   |  3 +-
 ...re => pack_post_car_exception_scenario.feature} | 11 ++-----
 .../test/resources/pack_timeout_scenario.feature   |  5 +--
 .../saga/alpha/server/AlphaEventController.java    |  8 +++++
 .../saga/integration/pack/tests/PackIT.java        | 22 ++++++++++---
 .../saga/omega/transaction/SagaStartAspect.java    |  6 +++-
 .../omega/transaction/SagaStartAspectTest.java     |  9 +++++-
 .../saga/demo/pack/booking/BookingController.java  |  7 +++++
 14 files changed, 76 insertions(+), 45 deletions(-)

diff --git a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/AExceptionWhenAb_scenario.feature b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/AExceptionWhenAb_scenario.feature
index 5622006..6778fd1 100644
--- a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/AExceptionWhenAb_scenario.feature
+++ b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/AExceptionWhenAb_scenario.feature
@@ -29,13 +29,15 @@ Feature: Alpha records transaction events
       | serviceb    | TxStartedEvent     |
       | serviceb   | TxEndedEvent   |
       | servicea   | TxAbortedEvent     |
+      | servicea   | TxAbortedEvent     |
       | serviceb | TxCompensatedEvent   |
       | * | SagaEndedEvent   |
 
+
     And servicea success update status
       | service | vstatus |
       | servicea | init |
 
     And serviceb success update status
       | service | vstatus |
-      | serviceb | cancel |
\ No newline at end of file
+      | serviceb | cancel |
diff --git a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature
index d0e295e..e00c6d8 100644
--- a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature
+++ b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature
@@ -29,12 +29,14 @@ Feature: Alpha records transaction events
       | serviceb    | TxStartedEvent     |
       | serviceb   | TxAbortedEvent   |
       | servicea   | TxAbortedEvent     |
+      | servicea    | TxAbortedEvent     |
       | * | SagaEndedEvent   |
 
+
     And servicea success update status
       | service | vstatus |
       | servicea | init |
 
     And serviceb success update status
       | service | vstatus |
-      | serviceb | init |
\ No newline at end of file
+      | serviceb | init |
diff --git a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbAc_scenario.feature b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbAc_scenario.feature
index 9c3bdc9..2a74f55 100644
--- a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbAc_scenario.feature
+++ b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbAc_scenario.feature
@@ -32,9 +32,11 @@ Feature: Alpha records transaction events
       | servicec | TxStartedEvent   |
       | servicec | TxAbortedEvent   |
       | servicea | TxAbortedEvent   |
+      | servicea | TxAbortedEvent   |
       | serviceb | TxCompensatedEvent   |
       | * | SagaEndedEvent   |
 
+
     And servicea success update status
       | service | vstatus |
       | servicea | init |
diff --git a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbBc_scenario.feature b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbBc_scenario.feature
index 065a0cf..e510230 100644
--- a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbBc_scenario.feature
+++ b/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/CExceptionWhenAbBc_scenario.feature
@@ -32,8 +32,10 @@ Feature: Alpha records transaction events
       | servicec | TxAbortedEvent   |
       | serviceb | TxAbortedEvent   |
       | servicea | TxAbortedEvent   |
+      | servicea | TxAbortedEvent     |
       | * | SagaEndedEvent   |
 
+
     And servicea success update status
       | service | vstatus |
       | servicea | init |
diff --git a/acceptance-tests/acceptance-pack-spring-demo/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java b/acceptance-tests/acceptance-pack-spring-demo/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java
index f9af438..5488f0c 100644
--- a/acceptance-tests/acceptance-pack-spring-demo/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java
+++ b/acceptance-tests/acceptance-pack-spring-demo/src/test/java/org/apache/servicecomb/saga/PackStepdefs.java
@@ -145,7 +145,7 @@ public class PackStepdefs implements En {
     List<Map<String, String>> expectedMaps = dataTable.asMaps(String.class, String.class);
     List<Map<String, String>> actualMaps = new ArrayList<>();
 
-    await().atMost(2, SECONDS).until(() -> {
+    await().atMost(5, SECONDS).until(() -> {
       actualMaps.clear();
       Collections.addAll(actualMaps, retrieveDataMaps(address, dataProcessor));
 
diff --git a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/booking_exception.btm
similarity index 51%
copy from acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature
copy to acceptance-tests/acceptance-pack-spring-demo/src/test/resources/booking_exception.btm
index d0e295e..074bb46 100644
--- a/acceptance-tests/acceptance-pack-dubbo-demo/src/test/resources/BExceptionWhenAb_scenario.feature
+++ b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/booking_exception.btm
@@ -13,28 +13,16 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-Feature: Alpha records transaction events
-
-  Scenario: A->B, B.run exception
-    Given ServiceA is up and running
-    And ServiceB is up and running
-    And Alpha is up and running
-
-    When BExceptionWhenAb :: A->B, B.run exception
-
-    Then Alpha records the following events
-      | serviceName  | type             |
-      | servicea | SagaStartedEvent |
-      | servicea     | TxStartedEvent   |
-      | serviceb    | TxStartedEvent     |
-      | serviceb   | TxAbortedEvent   |
-      | servicea   | TxAbortedEvent     |
-      | * | SagaEndedEvent   |
-
-    And servicea success update status
-      | service | vstatus |
-      | servicea | init |
+##############################################################
+# rules to timeout the booking after invoking the services
+#
+###############################################################
 
-    And serviceb success update status
-      | service | vstatus |
-      | serviceb | init |
\ No newline at end of file
+RULE throw exception
+CLASS org.apache.servicecomb.saga.demo.pack.booking.BookingController
+METHOD postCarBooking
+AT ENTRY
+IF TRUE
+DO debug("throw RuntimeException here"),
+   throw new RuntimeException("Cannot access the booking hotel server!")
+ENDRULE
diff --git a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_compensation_scenario.feature b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_compensation_scenario.feature
index 0375f35..931d316 100644
--- a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_compensation_scenario.feature
+++ b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_compensation_scenario.feature
@@ -30,8 +30,9 @@ Feature: Alpha records transaction events
       | car     | TxEndedEvent       |
       | hotel   | TxStartedEvent     |
       | hotel   | TxAbortedEvent     |
+      | booking | TxAbortedEvent     |
       | car     | TxCompensatedEvent |
-      | car     | SagaEndedEvent     |
+     
 
     Then Car Service contains the following booking orders
       | name | amount | confirmed | cancelled |
diff --git a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_post_car_exception_scenario.feature
similarity index 82%
copy from acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature
copy to acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_post_car_exception_scenario.feature
index 78193e9..e93eefe 100644
--- a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature
+++ b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_post_car_exception_scenario.feature
@@ -15,13 +15,13 @@
 
 Feature: Alpha records transaction events
 
-  Scenario: A transaction timeout and will be compensated
+  Scenario: An exception is throw and the first transaction should be compensated
     Given Car Service is up and running
     And Hotel Service is up and running
     And Booking Service is up and running
     And Alpha is up and running
 
-    Given Install the byteman script booking_timeout.btm to Booking Service
+    Given Install the byteman script booking_exception.btm to Booking Service
 
     When User Sean requests to book 1 cars and 1 rooms fail
 
@@ -30,12 +30,8 @@ Feature: Alpha records transaction events
       | booking | SagaStartedEvent   |
       | car     | TxStartedEvent     |
       | car     | TxEndedEvent       |
-      | hotel   | TxStartedEvent     |
-      | hotel   | TxEndedEvent       |
       | booking | TxAbortedEvent     |
-      | hotel   | TxCompensatedEvent |
       | car     | TxCompensatedEvent |
-      | car     | SagaEndedEvent     |
 
     Then Car Service contains the following booking orders
       | name | amount | confirmed | cancelled |
@@ -43,5 +39,4 @@ Feature: Alpha records transaction events
 
     Then Hotel Service contains the following booking orders
       | name | amount | confirmed | cancelled |
-      | Sean | 1      | false     | true      |
-
+    
diff --git a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature
index 78193e9..478d589 100644
--- a/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature
+++ b/acceptance-tests/acceptance-pack-spring-demo/src/test/resources/pack_timeout_scenario.feature
@@ -26,7 +26,7 @@ Feature: Alpha records transaction events
     When User Sean requests to book 1 cars and 1 rooms fail
 
     Then Alpha records the following events
-      | serviceName  | type               |
+      | serviceName  | type          |
       | booking | SagaStartedEvent   |
       | car     | TxStartedEvent     |
       | car     | TxEndedEvent       |
@@ -35,7 +35,8 @@ Feature: Alpha records transaction events
       | booking | TxAbortedEvent     |
       | hotel   | TxCompensatedEvent |
       | car     | TxCompensatedEvent |
-      | car     | SagaEndedEvent     |
+      | car     | SagaEndedEvent   |
+
 
     Then Car Service contains the following booking orders
       | name | amount | confirmed | cancelled |
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java
index aa469c4..8d401de 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/saga/alpha/server/AlphaEventController.java
@@ -17,11 +17,14 @@
 
 package org.apache.servicecomb.saga.alpha.server;
 
+import java.lang.invoke.MethodHandles;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.servicecomb.saga.alpha.core.TxEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.DeleteMapping;
@@ -31,9 +34,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
 
+import sun.rmi.runtime.Log;
+
 @Controller
 @RequestMapping("/")
 class AlphaEventController {
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   private final TxEventEnvelopeRepository eventRepository;
 
@@ -43,10 +49,12 @@ class AlphaEventController {
 
   @GetMapping(value = "/events")
   ResponseEntity<Collection<TxEventVo>> events() {
+    LOG.info("Get the events request");
     Iterable<TxEvent> events = eventRepository.findAll();
 
     List<TxEventVo> eventVos = new LinkedList<>();
     events.forEach(event -> eventVos.add(new TxEventVo(event)));
+    LOG.info("Get the event size " + eventVos.size());
 
     return ResponseEntity.ok(eventVos);
   }
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 9199ff6..d6106ef 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
@@ -171,15 +171,22 @@ public class PackIT {
     assertThat(txAbortedEvent.serviceName(), is(serviceName));
     assertThat(txAbortedEvent.instanceId(), is(txStartedEvent2.instanceId()));
 
-    // TODO: 2018/1/9 compensation shall be done in reverse order
-    TxEvent txCompensatedEvent1 = events.get(5);
+
+    assertThat(events.get(5).type(), is("TxAbortedEvent"));
+    txAbortedEvent = events.get(5);
+    System.out.println(txAbortedEvent);
+    assertThat(txAbortedEvent.localTxId(), is(globalTxId));
+    assertThat(txAbortedEvent.globalTxId(), is(globalTxId));
+    assertThat(txAbortedEvent.parentTxId(), is(nullValue()));
+    
+    TxEvent txCompensatedEvent1 = events.get(6);
     assertThat(txCompensatedEvent1.type(), is("TxCompensatedEvent"));
     assertThat(txCompensatedEvent1.localTxId(), is(txStartedEvent1.localTxId()));
     assertThat(txCompensatedEvent1.parentTxId(), is(globalTxId));
     assertThat(txCompensatedEvent1.serviceName(), is(serviceName));
     assertThat(txCompensatedEvent1.instanceId(), is(txStartedEvent1.instanceId()));
 
-    assertThat(events.get(6).type(), is("SagaEndedEvent"));
+
 
     assertThat(compensatedMessages, contains("Goodbye, " + TRESPASSER));
   }
@@ -289,8 +296,13 @@ public class PackIT {
     assertThat(events.get(6).type(), is("TxAbortedEvent"));
     assertThat(events.get(7).type(), is("TxStartedEvent"));
     assertThat(events.get(8).type(), is("TxAbortedEvent"));
-    assertThat(events.get(9).type(), is("TxCompensatedEvent"));
-    assertThat(events.get(10).type(), is("SagaEndedEvent"));
+    // This event is for the whole saga event
+    assertThat(events.get(9).type(), is("TxAbortedEvent"));
+    assertThat(events.get(10).type(), is("TxCompensatedEvent"));
+    
+    //assertThat(events.get(10).type(), is("SagaEndedEvent"));
+
+    System.out.println(compensatedMessages);
 
     assertThat(compensatedMessages, contains("Goodbye, " + TRESPASSER));
   }
diff --git a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspect.java b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspect.java
index 8722deb..e2cd717 100644
--- a/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspect.java
+++ b/omega/omega-transaction/src/main/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspect.java
@@ -58,7 +58,11 @@ public class SagaStartAspect {
 
       return result;
     } catch (Throwable throwable) {
-      LOG.error("Transaction {} failed.", context.globalTxId());
+      // We don't need to handle the OmegaException here
+      if (!(throwable instanceof OmegaException)) {
+        sagaStartAnnotationProcessor.onError(context.globalTxId(), method.toString(), throwable);
+        LOG.error("Transaction {} failed.", context.globalTxId());
+      }
       throw throwable;
     } finally {
       context.clear();
diff --git a/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspectTest.java b/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspectTest.java
index 10d55ff..4025c1c 100644
--- a/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspectTest.java
+++ b/omega/omega-transaction/src/test/java/org/apache/servicecomb/saga/omega/transaction/SagaStartAspectTest.java
@@ -120,7 +120,7 @@ public class SagaStartAspectTest {
       assertThat(e, is(oops));
     }
 
-    assertThat(messages.size(), is(1));
+    assertThat(messages.size(), is(2));
     TxEvent event = messages.get(0);
 
     assertThat(event.globalTxId(), is(globalTxId));
@@ -128,6 +128,13 @@ public class SagaStartAspectTest {
     assertThat(event.parentTxId(), is(nullValue()));
     assertThat(event.type(), is(EventType.SagaStartedEvent));
 
+    event = messages.get(1);
+    
+    assertThat(event.globalTxId(), is(globalTxId));
+    assertThat(event.localTxId(), is(globalTxId));
+    assertThat(event.parentTxId(), is(nullValue()));
+    assertThat(event.type(), is(EventType.TxAbortedEvent));
+
     assertThat(omegaContext.globalTxId(), is(nullValue()));
     assertThat(omegaContext.localTxId(), is(nullValue()));
   }
diff --git a/saga-demo/saga-spring-demo/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java b/saga-demo/saga-spring-demo/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java
index ffe0fa0..f44abd9 100644
--- a/saga-demo/saga-spring-demo/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java
+++ b/saga-demo/saga-spring-demo/booking/src/main/java/org/apache/servicecomb/saga/demo/pack/booking/BookingController.java
@@ -45,6 +45,8 @@ public class BookingController {
         carServiceUrl + "/order/{name}/{cars}",
         null, String.class, name, cars);
 
+    postCarBooking();
+
     template.postForEntity(
         hotelServiceUrl + "/order/{name}/{rooms}",
         null, String.class, name, rooms);
@@ -54,6 +56,11 @@ public class BookingController {
     return name + " booking " + rooms + " rooms and " + cars + " cars OK";
   }
 
+  // This method is used by the byteman to inject exception here
+  private void postCarBooking() {
+
+  }
+
   // This method is used by the byteman to inject the faults such as the timeout or the crash
   private void postBooking() {