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/01/13 14:58:04 UTC

[incubator-servicecomb-saga] 13/15: SCB-212 fixed rebase conflict

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 05113e5b3d98229377e54eac8c6db1e851fed500
Author: seanyinx <se...@huawei.com>
AuthorDate: Thu Jan 11 15:18:57 2018 +0800

    SCB-212 fixed rebase conflict
    
    Signed-off-by: seanyinx <se...@huawei.com>
---
 .../saga/alpha/core/TxConsistentService.java          |  5 +++--
 .../transaction/SagaStartAnnotationProcessor.java     |  7 -------
 .../saga/omega/transaction/SagaStartAspect.java       | 17 +++++++++++++----
 .../transaction/SagaStartAnnotationProcessorTest.java | 19 +++++++------------
 .../saga/omega/transaction/SagaStartAspectTest.java   |  5 +++--
 5 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxConsistentService.java b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxConsistentService.java
index 079e559..87cdc60 100644
--- a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxConsistentService.java
+++ b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxConsistentService.java
@@ -20,16 +20,17 @@ package org.apache.servicecomb.saga.alpha.core;
 import static org.apache.servicecomb.saga.alpha.core.EventType.SagaEndedEvent;
 import static org.apache.servicecomb.saga.alpha.core.EventType.TxAbortedEvent;
 import static org.apache.servicecomb.saga.alpha.core.EventType.TxCompensatedEvent;
+import static org.apache.servicecomb.saga.alpha.core.EventType.TxEndedEvent;
 import static org.apache.servicecomb.saga.alpha.core.EventType.TxStartedEvent;
 
 import java.util.Date;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListSet;
 import java.util.function.Consumer;
 
 public class TxConsistentService {
@@ -67,7 +68,7 @@ public class TxConsistentService {
     List<TxEvent> events = eventRepository.findTransactions(event.globalTxId(), TxStartedEvent.name());
     events.forEach(omegaCallback::compensate);
     eventsToCompensate.computeIfAbsent(event.globalTxId(), (v) -> {
-      Set<String> eventSet = new HashSet<>(events.size());
+      Set<String> eventSet = new ConcurrentSkipListSet<>();
       events.forEach(e -> eventSet.add(e.localTxId()));
       return eventSet;
     });
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 0aa8fab..2dad5ae 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,24 +31,17 @@ class SagaStartAnnotationProcessor implements EventAwareInterceptor {
 
   @Override
   public void preIntercept(String parentTxId, String compensationMethod, Object... message) {
-    initializeOmegaContext();
     sender.send(new SagaStartedEvent(omegaContext.globalTxId(), omegaContext.localTxId()));
   }
 
   @Override
   public void postIntercept(String parentTxId, String compensationMethod) {
     sender.send(new SagaEndedEvent(omegaContext.globalTxId(), omegaContext.localTxId()));
-    omegaContext.clear();
   }
 
   @Override
   public void onError(String parentTxId, String compensationMethod, Throwable throwable) {
     String globalTxId = omegaContext.globalTxId();
     sender.send(new TxAbortedEvent(globalTxId, globalTxId, null, compensationMethod, throwable));
-    omegaContext.clear();
-  }
-
-  private void initializeOmegaContext() {
-    omegaContext.setLocalTxId(omegaContext.newGlobalTxId());
   }
 }
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 3fa6322..0951752 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
@@ -48,6 +48,7 @@ public class SagaStartAspect {
 
   @Around("execution(@org.apache.servicecomb.saga.omega.context.annotations.SagaStart * *(..)) && @annotation(sagaStart)")
   Object advise(ProceedingJoinPoint joinPoint, SagaStart sagaStart) throws Throwable {
+    initializeOmegaContext();
     Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
 
     TimeAwareInterceptor interceptor = new TimeAwareInterceptor(sagaStartAnnotationProcessor);
@@ -56,16 +57,24 @@ public class SagaStartAspect {
 
     scheduleTimeoutTask(interceptor, method, sagaStart.timeout());
     try {
-      return joinPoint.proceed();
+      Object result = joinPoint.proceed();
+
+      interceptor.postIntercept(context.globalTxId(), method.toString());
+      LOG.debug("Transaction with context {} has finished.", context);
+
+      return result;
     } catch (Throwable throwable) {
-      LOG.error("Failed to process SagaStart method: {}", method.toString());
+      LOG.error("Transaction {} failed.", context.globalTxId());
       throw throwable;
     } finally {
-      LOG.debug("Transaction with context {} has finished.", context);
-      interceptor.postIntercept(context.globalTxId(), method.toString());
+      context.clear();
     }
   }
 
+  private void initializeOmegaContext() {
+    context.setLocalTxId(context.newGlobalTxId());
+  }
+
   private void scheduleTimeoutTask(
       TimeAwareInterceptor interceptor,
       Method method,
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 e857356..9b8adf5 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
@@ -21,7 +21,6 @@ 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;
@@ -29,6 +28,7 @@ import java.util.UUID;
 
 import org.apache.servicecomb.saga.omega.context.IdGenerator;
 import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.junit.Before;
 import org.junit.Test;
 
 public class SagaStartAnnotationProcessorTest {
@@ -49,15 +49,16 @@ public class SagaStartAnnotationProcessorTest {
   private final SagaStartAnnotationProcessor sagaStartAnnotationProcessor = new SagaStartAnnotationProcessor(context,
       sender);
 
+  @Before
+  public void setUp() throws Exception {
+    context.setGlobalTxId(globalTxId);
+    context.setLocalTxId(globalTxId);
+  }
+
   @Test
   public void sendsSagaStartedEvent() {
-    when(generator.nextId()).thenReturn(globalTxId, localTxId);
-
     sagaStartAnnotationProcessor.preIntercept(null, null);
 
-    assertThat(context.globalTxId(), is(globalTxId));
-    assertThat(context.localTxId(), is(globalTxId));
-
     TxEvent event = messages.get(0);
 
     assertThat(event.globalTxId(), is(globalTxId));
@@ -70,9 +71,6 @@ public class SagaStartAnnotationProcessorTest {
 
   @Test
   public void sendsSagaEndedEvent() {
-    context.setGlobalTxId(globalTxId);
-    context.setLocalTxId(globalTxId);
-
     sagaStartAnnotationProcessor.postIntercept(null, null);
 
     TxEvent event = messages.get(0);
@@ -83,8 +81,5 @@ public class SagaStartAnnotationProcessorTest {
     assertThat(event.compensationMethod().isEmpty(), is(true));
     assertThat(event.type(), is("SagaEndedEvent"));
     assertThat(event.payloads().length, is(0));
-
-    assertThat(context.globalTxId(), is(nullValue()));
-    assertThat(context.localTxId(), is(nullValue()));
   }
 }
\ No newline at end of file
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 5322269..b63b8be 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
@@ -100,12 +100,13 @@ public class SagaStartAspectTest {
       assertThat(e, is(oops));
     }
 
-    TxEvent event = messages.get(1);
+    assertThat(messages.size(), is(1));
+    TxEvent event = messages.get(0);
 
     assertThat(event.globalTxId(), is(globalTxId));
     assertThat(event.localTxId(), is(globalTxId));
     assertThat(event.parentTxId(), is(nullValue()));
-    assertThat(event.type(), is("SagaEndedEvent"));
+    assertThat(event.type(), is("SagaStartedEvent"));
 
     assertThat(omegaContext.globalTxId(), is(nullValue()));
     assertThat(omegaContext.localTxId(), is(nullValue()));

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