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/02/13 07:50:33 UTC
[incubator-servicecomb-saga] branch master updated: SCB-243 able to
intercept old java chassis requests (#136)
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
The following commit(s) were added to refs/heads/master by this push:
new da07faa SCB-243 able to intercept old java chassis requests (#136)
da07faa is described below
commit da07faa00be97a6bd0793044f609e2890a36eaa5
Author: Eric Lee <da...@huawei.com>
AuthorDate: Tue Feb 13 15:50:31 2018 +0800
SCB-243 able to intercept old java chassis requests (#136)
* SCB-243 able to intercept old java chassis requests
Signed-off-by: Eric Lee <da...@huawei.com>
* SCB-243 add license header
Signed-off-by: Eric Lee <da...@huawei.com>
---
.../saga/omega/context}/UniqueIdGeneratorTest.java | 6 +-
.../{ => omega-transport-servicecomb}/pom.xml | 44 ++----------
.../transport/servicecomb/SagaConsumerHandler.java | 59 ++++++++++++++++
.../transport/servicecomb/SagaProviderHandler.java | 56 ++++++++++++++++
.../src/main/resources/config/cse.handler.xml | 23 +++++++
.../servicecomb/SagaConsumerHandlerTest.java | 78 ++++++++++++++++++++++
.../servicecomb/SagaProviderHandlerTest.java | 78 ++++++++++++++++++++++
omega/omega-transport/pom.xml | 1 +
pom.xml | 2 +-
9 files changed, 305 insertions(+), 42 deletions(-)
diff --git a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java b/omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
similarity index 91%
rename from omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java
rename to omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
index 88ffac2..bcedcfa 100644
--- a/omega/omega-transport/omega-transport-resttemplate/src/test/java/org/apache/servicecomb/saga/omega/transport/resttemplate/UniqueIdGeneratorTest.java
+++ b/omega/omega-context/src/test/java/org/apache/servicecomb/saga/omega/context/UniqueIdGeneratorTest.java
@@ -6,17 +6,16 @@
* (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
+ * 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.transport.resttemplate;
+package org.apache.servicecomb.saga.omega.context;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@@ -32,7 +31,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
-import org.apache.servicecomb.saga.omega.context.UniqueIdGenerator;
import org.junit.Test;
public class UniqueIdGeneratorTest {
diff --git a/omega/omega-transport/pom.xml b/omega/omega-transport/omega-transport-servicecomb/pom.xml
similarity index 54%
copy from omega/omega-transport/pom.xml
copy to omega/omega-transport/omega-transport-servicecomb/pom.xml
index 4cbe189..2798f7d 100644
--- a/omega/omega-transport/pom.xml
+++ b/omega/omega-transport/omega-transport-servicecomb/pom.xml
@@ -14,56 +14,26 @@
~ 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.
- ~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
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>omega</artifactId>
+ <artifactId>omega-transport</artifactId>
<groupId>org.apache.servicecomb.saga</groupId>
<version>0.0.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <artifactId>omega-transport</artifactId>
- <name>Saga::Omega::Transport</name>
-
- <packaging>pom</packaging>
- <modules>
- <module>omega-transport-resttemplate</module>
- </modules>
+ <artifactId>omega-transport-servicecomb</artifactId>
<dependencies>
<dependency>
- <groupId>org.apache.servicecomb.saga</groupId>
- <artifactId>omega-context</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.servicecomb.saga</groupId>
- <artifactId>omega-transaction</artifactId>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-all</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.tomakehurst</groupId>
- <artifactId>wiremock-standalone</artifactId>
- </dependency>
- <dependency>
- <groupId>com.github.seanyinx</groupId>
- <artifactId>unit-scaffolding</artifactId>
+ <groupId>io.servicecomb</groupId>
+ <artifactId>java-chassis-core</artifactId>
+ <version>${java.chassis.version}</version>
</dependency>
</dependencies>
-</project>
+
+</project>
\ No newline at end of file
diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java
new file mode 100644
index 0000000..8fd296f
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java
@@ -0,0 +1,59 @@
+/*
+ * 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.transport.servicecomb;
+
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+import java.lang.invoke.MethodHandles;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import io.servicecomb.core.Handler;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaConsumerHandler implements Handler {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ private final OmegaContext omegaContext;
+
+ @Autowired
+ public SagaConsumerHandler(OmegaContext omegaContext) {
+ this.omegaContext = omegaContext;
+ }
+
+ @Override
+ public void handle(Invocation invocation, AsyncResponse asyncResponse) throws Exception {
+ if (omegaContext.globalTxId() != null) {
+ invocation.getContext().put(GLOBAL_TX_ID_KEY, omegaContext.globalTxId());
+ invocation.getContext().put(LOCAL_TX_ID_KEY, omegaContext.localTxId());
+
+ LOG.debug("Added {} {} and {} {} to request header",
+ GLOBAL_TX_ID_KEY,
+ omegaContext.globalTxId(),
+ LOCAL_TX_ID_KEY,
+ omegaContext.localTxId());
+ }
+
+ invocation.next(asyncResponse);
+ }
+}
diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
new file mode 100644
index 0000000..d639db5
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
@@ -0,0 +1,56 @@
+/*
+ * 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.transport.servicecomb;
+
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+
+import java.lang.invoke.MethodHandles;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import io.servicecomb.core.Handler;
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaProviderHandler implements Handler {
+
+ private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ private final OmegaContext omegaContext;
+
+ @Autowired
+ public SagaProviderHandler(OmegaContext omegaContext) {
+ this.omegaContext = omegaContext;
+ }
+
+ @Override
+ public void handle(Invocation invocation, AsyncResponse asyncResponse) throws Exception {
+ String globalTxId = invocation.getContext().get(GLOBAL_TX_ID_KEY);
+ if (globalTxId == null) {
+ LOG.debug("no such header: {}", GLOBAL_TX_ID_KEY);
+ } else {
+ omegaContext.setGlobalTxId(globalTxId);
+ omegaContext.setLocalTxId(invocation.getContext().get(LOCAL_TX_ID_KEY));
+ }
+
+ invocation.next(asyncResponse);
+ }
+}
diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml b/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
new file mode 100644
index 0000000..9515815
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
@@ -0,0 +1,23 @@
+<!--
+ ~ 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.
+ -->
+
+<config>
+ <handler id="saga-consumer"
+ class="org.apache.servicecomb.saga.omega.transport.servicecomb.SagaConsumerHandler" />
+ <handler id="saga-provider"
+ class="org.apache.servicecomb.saga.omega.transport.servicecomb.SagaProviderHandler" />
+</config>
\ No newline at end of file
diff --git a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
new file mode 100644
index 0000000..d23ad2b
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.transport.servicecomb;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+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;
+
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaConsumerHandlerTest {
+
+ private static final String globalTxId = UUID.randomUUID().toString();
+ private static final String localTxId = UUID.randomUUID().toString();
+ @SuppressWarnings("unchecked")
+ private final IdGenerator<String> idGenerator = mock(IdGenerator.class);
+
+ private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+ private final Invocation invocation = mock(Invocation.class);
+ private final AsyncResponse asyncResponse = mock(AsyncResponse.class);
+
+ private final SagaConsumerHandler handler = new SagaConsumerHandler(omegaContext);
+
+ @Before
+ public void setUp() {
+ when(idGenerator.nextId()).thenReturn(globalTxId, localTxId);
+ }
+
+ @Test
+ public void keepHeaderUnchangedIfContextAbsent() throws Exception {
+ Map<String, String> context = new HashMap<>();
+ when(invocation.getContext()).thenReturn(context);
+
+ handler.handle(invocation, asyncResponse);
+
+ assertThat(invocation.getContext().isEmpty(), is(true));
+ }
+
+ @Test
+ public void interceptTransactionIdInHeaderIfContextPresent() throws Exception {
+ omegaContext.setGlobalTxId(globalTxId);
+ omegaContext.setLocalTxId(localTxId);
+
+ Map<String, String> context = new HashMap<>();
+ when(invocation.getContext()).thenReturn(context);
+
+ handler.handle(invocation, asyncResponse);
+
+ assertThat(invocation.getContext().get(OmegaContext.GLOBAL_TX_ID_KEY), is(globalTxId));
+ assertThat(invocation.getContext().get(OmegaContext.LOCAL_TX_ID_KEY), is(localTxId));
+ }
+}
diff --git a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
new file mode 100644
index 0000000..a9b7e37
--- /dev/null
+++ b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.transport.servicecomb;
+
+import static java.util.Collections.emptyMap;
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.GLOBAL_TX_ID_KEY;
+import static org.apache.servicecomb.saga.omega.context.OmegaContext.LOCAL_TX_ID_KEY;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.junit.Before;
+import org.junit.Test;
+
+import io.servicecomb.core.Invocation;
+import io.servicecomb.swagger.invocation.AsyncResponse;
+
+public class SagaProviderHandlerTest {
+
+ private static final String globalTxId = UUID.randomUUID().toString();
+ private static final String localTxId = UUID.randomUUID().toString();
+ private final OmegaContext omegaContext = new OmegaContext(() -> "ignored");
+ private final Invocation invocation = mock(Invocation.class);
+ private final AsyncResponse asyncResponse = mock(AsyncResponse.class);
+
+ private final SagaProviderHandler handler = new SagaProviderHandler(omegaContext);
+
+ @Before
+ public void setUp() {
+ omegaContext.clear();
+ }
+
+ @Test
+ public void setUpOmegaContextInTransactionRequest() throws Exception {
+ Map<String, String> context = new HashMap<>();
+ context.put(GLOBAL_TX_ID_KEY, globalTxId);
+ context.put(LOCAL_TX_ID_KEY, localTxId);
+ when(invocation.getContext()).thenReturn(context);
+
+ handler.handle(invocation, asyncResponse);
+
+ assertThat(omegaContext.globalTxId(), is(globalTxId));
+ assertThat(omegaContext.localTxId(), is(localTxId));
+ }
+
+ @Test
+ public void doNothingInNonTransactionRequest() throws Exception {
+ when(invocation.getContext()).thenReturn(emptyMap());
+
+ handler.handle(invocation, asyncResponse);
+
+ assertThat(omegaContext.globalTxId(), is(nullValue()));
+ assertThat(omegaContext.localTxId(), is(nullValue()));
+ }
+
+}
diff --git a/omega/omega-transport/pom.xml b/omega/omega-transport/pom.xml
index 4cbe189..352926a 100644
--- a/omega/omega-transport/pom.xml
+++ b/omega/omega-transport/pom.xml
@@ -33,6 +33,7 @@
<packaging>pom</packaging>
<modules>
<module>omega-transport-resttemplate</module>
+ <module>omega-transport-servicecomb</module>
</modules>
<dependencies>
diff --git a/pom.xml b/pom.xml
index f263683..85e9405 100755
--- a/pom.xml
+++ b/pom.xml
@@ -54,7 +54,7 @@
<log4j.version>2.6.2</log4j.version>
<spring.boot.version>1.4.5.RELEASE</spring.boot.version>
<spring.cloud.version>Camden.SR6</spring.cloud.version>
- <java.chassis.version>0.4.0</java.chassis.version>
+ <java.chassis.version>0.5.0</java.chassis.version>
<spring.version>4.3.7.RELEASE</spring.version>
<akka.version>2.5.6</akka.version>
<rat.version>0.12</rat.version>
--
To stop receiving notification emails like this one, please contact
ningjiang@apache.org.