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 2017/12/25 10:36:27 UTC

[incubator-servicecomb-saga] 03/14: SCB-96 omega context to save tx id

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 7fb580f389d53791fa71cefe43739043b96d5785
Author: seanyinx <se...@huawei.com>
AuthorDate: Fri Dec 22 17:09:14 2017 +0800

    SCB-96 omega context to save tx id
    
    Signed-off-by: seanyinx <se...@huawei.com>
---
 omega/{ => omega-context}/pom.xml                  | 31 +++++------
 .../saga/omega/context/OmegaContext.java           | 31 +++++++++++
 .../saga/omega/context/OmegaContextTest.java       | 64 ++++++++++++++++++++++
 omega/omega-transaction/pom.xml                    |  4 ++
 omega/pom.xml                                      |  6 ++
 5 files changed, 118 insertions(+), 18 deletions(-)

diff --git a/omega/pom.xml b/omega/omega-context/pom.xml
similarity index 70%
copy from omega/pom.xml
copy to omega/omega-context/pom.xml
index fd96f21..5bba67c 100644
--- a/omega/pom.xml
+++ b/omega/omega-context/pom.xml
@@ -20,28 +20,23 @@
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <parent>
-    <artifactId>saga</artifactId>
+    <artifactId>omega</artifactId>
     <groupId>io.servicecomb.saga</groupId>
     <version>0.0.3-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
 
-  <artifactId>omega</artifactId>
-  <packaging>pom</packaging>
-  <modules>
-    <module>omega-transaction</module>
-  </modules>
+  <artifactId>omega-context</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.github.seanyinx</groupId>
+      <artifactId>unit-scaffolding</artifactId>
+    </dependency>
 
-  <dependencyManagement>
-    <dependencies>
-      <dependency>
-        <groupId>org.springframework.boot</groupId>
-        <artifactId>spring-boot-dependencies</artifactId>
-        <version>${spring.boot.version}</version>
-        <type>pom</type>
-        <scope>import</scope>
-      </dependency>
-    </dependencies>
-  </dependencyManagement>
+  </dependencies>
 
-</project>
\ No newline at end of file
+</project>
diff --git a/omega/omega-context/src/main/java/io/servicecomb/saga/omega/context/OmegaContext.java b/omega/omega-context/src/main/java/io/servicecomb/saga/omega/context/OmegaContext.java
new file mode 100644
index 0000000..2c33e41
--- /dev/null
+++ b/omega/omega-context/src/main/java/io/servicecomb/saga/omega/context/OmegaContext.java
@@ -0,0 +1,31 @@
+/*
+ * 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 io.servicecomb.saga.omega.context;
+
+public class OmegaContext {
+  private final ThreadLocal<Long> transactionId = new ThreadLocal<>();
+
+
+  public void setTxId(long txId) {
+    transactionId.set(txId);
+  }
+
+  public long txId() {
+    return transactionId.get();
+  }
+}
diff --git a/omega/omega-context/src/test/java/io/servicecomb/saga/omega/context/OmegaContextTest.java b/omega/omega-context/src/test/java/io/servicecomb/saga/omega/context/OmegaContextTest.java
new file mode 100644
index 0000000..82837d1
--- /dev/null
+++ b/omega/omega-context/src/test/java/io/servicecomb/saga/omega/context/OmegaContextTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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 io.servicecomb.saga.omega.context;
+
+import static com.seanyinx.github.unit.scaffolding.Randomness.nextId;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.*;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CyclicBarrier;
+
+import org.junit.Test;
+
+public class OmegaContextTest {
+
+  private final OmegaContext omegaContext = new OmegaContext();
+
+  @Test
+  public void eachThreadGetsDifferentId() throws Exception {
+    CyclicBarrier barrier = new CyclicBarrier(2);
+
+    Runnable runnable = exceptionalRunnable(() -> {
+      long txId = nextId();
+      omegaContext.setTxId(txId);
+      barrier.await();
+
+      assertThat(omegaContext.txId(), is(txId));
+    });
+
+    CompletableFuture<Void> future1 = CompletableFuture.runAsync(runnable);
+    CompletableFuture<Void> future2 = CompletableFuture.runAsync(runnable);
+
+    CompletableFuture.allOf(future1, future2).join();
+  }
+
+  private Runnable exceptionalRunnable(ExceptionalRunnable runnable) {
+    return () -> {
+      try {
+        runnable.run();
+      } catch (Exception e) {
+        fail(e.getMessage());
+      }
+    };
+  }
+
+  interface ExceptionalRunnable {
+    void run() throws Exception;
+  }
+}
diff --git a/omega/omega-transaction/pom.xml b/omega/omega-transaction/pom.xml
index 569e07f..1de2909 100644
--- a/omega/omega-transaction/pom.xml
+++ b/omega/omega-transaction/pom.xml
@@ -37,6 +37,10 @@
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
+    <dependency>
+      <groupId>io.servicecomb.saga</groupId>
+      <artifactId>omega-context</artifactId>
+    </dependency>
 
     <dependency>
       <groupId>junit</groupId>
diff --git a/omega/pom.xml b/omega/pom.xml
index fd96f21..069f907 100644
--- a/omega/pom.xml
+++ b/omega/pom.xml
@@ -30,6 +30,7 @@
   <packaging>pom</packaging>
   <modules>
     <module>omega-transaction</module>
+    <module>omega-context</module>
   </modules>
 
   <dependencyManagement>
@@ -41,6 +42,11 @@
         <type>pom</type>
         <scope>import</scope>
       </dependency>
+      <dependency>
+        <groupId>io.servicecomb.saga</groupId>
+        <artifactId>omega-context</artifactId>
+        <version>0.0.3-SNAPSHOT</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 

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