Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
337 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
|
||
<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-transport</artifactId> | ||
<groupId>org.apache.servicecomb.saga</groupId> | ||
<version>0.0.3-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>omega-transport-servicecomb</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>java-chassis-core</artifactId> | ||
<version>${java.chassis.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
59 changes: 59 additions & 0 deletions
59
...ain/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ain/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
omega/omega-transport/omega-transport-servicecomb/src/main/resources/config/cse.handler.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
78 changes: 78 additions & 0 deletions
78
...java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters