Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dependencies/bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@
<artifactId>servicestage</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ServiceComb Dtm extension -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>dtm</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ServiceComb: swagger -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
Expand Down
14 changes: 10 additions & 4 deletions distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-local</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-schema-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-schema-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-service-center</artifactId>
Expand Down Expand Up @@ -253,6 +253,12 @@
<artifactId>servicestage</artifactId>
</dependency>

<!-- ServiceComb dtm extension -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>dtm</artifactId>
</dependency>

<!-- swagger -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
Expand Down
51 changes: 51 additions & 0 deletions huawei-cloud/dtm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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>huawei-cloud</artifactId>
<groupId>org.apache.servicecomb</groupId>
<version>2.1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dtm</artifactId>
<name>Java Chassis::Huawei Cloud::Dtm</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.huaweicloud.dtm;

import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;

public class DtmConfig {
private static final String DTM_CONTEXT_CLASS_NAME_KEY = "servicecomb.dtm.className";

private static final String DTM_CONTEXT_DEFAULT_CLASS_NAME = "com.huawei.middleware.dtm.client.context.DTMContext";

public static final String DTM_EXPORT_METHOD = "getContextData";

public static final String DTM_IMPORT_METHOD = "setContextData";

public static final String DTM_TRACE_ID_KEY = "X-Dtm-Trace-Id-Key";

public static final DtmConfig INSTANCE = new DtmConfig();

private DynamicStringProperty contextClassNameProperty = DynamicPropertyFactory.getInstance()
.getStringProperty(DTM_CONTEXT_CLASS_NAME_KEY, DTM_CONTEXT_DEFAULT_CLASS_NAME);

private DtmConfig() {
}

public String getDtmContextClassName() {
return contextClassNameProperty.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.huaweicloud.dtm;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.servicecomb.core.Handler;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.definition.MicroserviceMeta;
import org.apache.servicecomb.swagger.invocation.AsyncResponse;
import org.apache.servicecomb.swagger.invocation.InvocationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DtmConsumerHandler implements Handler {
private static final Logger LOG = LoggerFactory.getLogger(DtmConsumerHandler.class);

private Method dtmContextExMethod;

@Override
public void init(MicroserviceMeta microserviceMeta, InvocationType invocationType) {
String className = DtmConfig.INSTANCE.getDtmContextClassName();
try {
Class<?> clazz = Class.forName(className);
dtmContextExMethod = clazz.getMethod(DtmConfig.DTM_EXPORT_METHOD);
} catch (Throwable e) {
// ignore just warn
LOG.warn("Failed to init method {}#{}", className, DtmConfig.DTM_EXPORT_METHOD, e);
}
}

@Override
@SuppressWarnings("unchecked")
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
try {
if (dtmContextExMethod != null) {
Object context = dtmContextExMethod.invoke(null);
if (context instanceof Map) {
invocation.getContext().putAll((Map<? extends String, ? extends String>) context);
}
}
} catch (Throwable e) {
LOG.warn("Failed to execute method DTMContext#{}, please check", DtmConfig.DTM_EXPORT_METHOD, e);
}
invocation.next(asyncResp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.huaweicloud.dtm;

import static org.apache.servicecomb.huaweicloud.dtm.DtmConfig.DTM_TRACE_ID_KEY;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.servicecomb.core.Const;
import org.apache.servicecomb.core.Handler;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.definition.MicroserviceMeta;
import org.apache.servicecomb.swagger.invocation.AsyncResponse;
import org.apache.servicecomb.swagger.invocation.InvocationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DtmProviderHandler implements Handler {
private static final Logger LOG = LoggerFactory.getLogger(DtmProviderHandler.class);

private Method dtmContextImMethod;

@Override
public void init(MicroserviceMeta microserviceMeta, InvocationType invocationType) {
String className = DtmConfig.INSTANCE.getDtmContextClassName();
try {
Class<?> clazz = Class.forName(className);
dtmContextImMethod = clazz.getMethod(DtmConfig.DTM_IMPORT_METHOD, Map.class);
} catch (Throwable e) {
// ignore just warn
LOG.warn("Failed to init method {}#{}", className, DtmConfig.DTM_IMPORT_METHOD, e);
}
}

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
try {
if (dtmContextImMethod != null) {
String traceId = invocation.getContext().get(Const.TRACE_ID_NAME);
invocation.getContext().put(DTM_TRACE_ID_KEY, traceId);
dtmContextImMethod.invoke(null, invocation.getContext());
}
} catch (Throwable e) {
LOG.warn("Failed to execute method DTMContext#{}, please check", DtmConfig.DTM_IMPORT_METHOD, e);
}
invocation.next(asyncResp);
}
}
23 changes: 23 additions & 0 deletions huawei-cloud/dtm/src/main/resources/config/cse.handler.xml
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="dtm-consumer"
class="org.apache.servicecomb.huaweicloud.dtm.DtmConsumerHandler"/>
<handler id="dtm-provider"
class="org.apache.servicecomb.huaweicloud.dtm.DtmProviderHandler"/>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 com.huawei.middleware.dtm.client.context;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.CollectionUtils;

public class DTMContext {
public static final String GLOBAL_TX_ID_KEY = "dtm-global-tx-key";

public static final String TRACE_ID_KEY = "X-Dtm-Trace-Id-Key";

public static String GLOBAL_TX_ID = "";

public static String TRACE_ID = "";

public static Map<String, String> getContextData() {
HashMap<String, String> context = new HashMap<>();
context.put(GLOBAL_TX_ID_KEY, GLOBAL_TX_ID);
return context;
}

public static void setContextData(Map<String, String> context) {
if (CollectionUtils.isEmpty(context)) {
return;
}
GLOBAL_TX_ID = context.getOrDefault(GLOBAL_TX_ID_KEY, "");
TRACE_ID = context.getOrDefault(TRACE_ID_KEY, "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.huaweicloud.dtm;

import java.util.HashMap;
import java.util.Map;

import org.apache.servicecomb.core.Invocation;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;

import com.huawei.middleware.dtm.client.context.DTMContext;

import mockit.Expectations;
import mockit.Mocked;

public class TestConsumerHandler {

@BeforeClass
public static void init() {
DTMContext.TRACE_ID = "";
DTMContext.GLOBAL_TX_ID = "";
}

@Test
public void testHandler(@Mocked Invocation invocation) throws Exception {
DtmConsumerHandler consumerHandler = new DtmConsumerHandler();
consumerHandler.init(null, null);
Map<String, String> context = new HashMap<>();
new Expectations() {{
invocation.getContext();
result = context;
}};
String expectTxId = "dtm-tx-id";
String expectTraceId = "dtm-trace-id";

Assert.assertEquals(0, context.size());
DTMContext.GLOBAL_TX_ID = expectTxId;
DTMContext.TRACE_ID = expectTraceId;
consumerHandler.handle(invocation, null);
Assert.assertEquals(1, context.size());
Assert.assertEquals(expectTxId, context.get(DTMContext.GLOBAL_TX_ID_KEY));
}
}
Loading