Skip to content

Commit

Permalink
Merge 468a6b0 into 697bf60
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-lee-ltk committed Feb 12, 2018
2 parents 697bf60 + 468a6b0 commit 05afaeb
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 5 deletions.
Expand Up @@ -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;
Expand All @@ -32,7 +31,6 @@
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 {
Expand Down
39 changes: 39 additions & 0 deletions omega/omega-transport/omega-transport-servicecomb/pom.xml
@@ -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>
@@ -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);
}
}
@@ -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);
}
}
@@ -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>
@@ -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));
}
}
@@ -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()));
}

}
1 change: 1 addition & 0 deletions omega/omega-transport/pom.xml
Expand Up @@ -33,6 +33,7 @@
<packaging>pom</packaging>
<modules>
<module>omega-transport-resttemplate</module>
<module>omega-transport-servicecomb</module>
</modules>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down

0 comments on commit 05afaeb

Please sign in to comment.