Skip to content

Commit

Permalink
158 (dromara#319)
Browse files Browse the repository at this point in the history
* 1. Fix the error

* 1. Initial Xa submission. 158

* 1. Implementation of the basic framework

* 1. Basic definition.

* 1. Dealt with xid

* 1. 今天又完成了一部分..158.

* I don’t have much time today, I just wrote a little bit

* 1. Optimize part of the code. 158

* 1. The basic can be realized by xa.

* 1. Adjust the code.

* 1. 增加timer

* 1. 日志相关的处理.

* 1. 日志相关的处理.

* 1. 准备处理,恢复日志.158

* 1. 增加XaRepository,

* 1. 处理dubbo xa事务的实现逻辑.158

* 1. rpc 初长成.

* 1. 准备修改mysql xa bug.158

* 1.测试部分处理一下.158

* 1. 处理与dubbo事务相关.158

* 1. 处理maven依赖.158

* 1. 处理xa同一个数据源的问题.158

* 1. 修复相关代码.158

* 1. 修复相关代码.158

* 1. checkstyle.158

Co-authored-by: sixh <[email protected]>
  • Loading branch information
prFor and sixh authored Jun 28, 2021
1 parent cbfbc84 commit 80a46ff
Show file tree
Hide file tree
Showing 106 changed files with 9,274 additions and 97 deletions.
7 changes: 7 additions & 0 deletions hmily-annotation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>hmily-annotation</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.dromara.hmily.common.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* NetUtils .
*
* @author sixh chenbin
*/
public class NetUtils {

private static volatile String localAddress;

/**
* Gets local ip.
*
* @return the local ip
*/
public static String getLocalIp() {
if (localAddress == null) {
synchronized (NetUtils.class) {
if (localAddress == null) {
try {
localAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
localAddress = "0.0.0.0";
}
}
}
}
return localAddress;
}
}
4 changes: 4 additions & 0 deletions hmily-config/hmily-config-zookeeper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class AbstractHmilyTransactionAspect {
/**
* this is point cut with {@linkplain HmilyTCC }.
*/
@Pointcut("@annotation(org.dromara.hmily.annotation.HmilyTCC) || @annotation(org.dromara.hmily.annotation.HmilyTAC)")
@Pointcut("@annotation(org.dromara.hmily.annotation.HmilyTCC) || @annotation(org.dromara.hmily.annotation.HmilyTAC) || @annotation(org.dromara.hmily.annotation.HmilyXA)")
public void hmilyInterceptor() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,40 @@
*/
@Data
public class HmilyTransactionContext {

/**
* transId.
*/
private Long transId;

/**
* participant id.
*/
private Long participantId;

/**
* participant ref id.
*/
private Long participantRefId;

/**
* this hmily action. {@linkplain HmilyActionEnum}
*/
private int action;

/**
* 事务参与的角色. {@linkplain HmilyRoleEnum}
*/
private int role;

/**
* transType.
*/
private String transType;

//以下为xa相关的参数.
/**
* xa相关的参数定义.
*/
private XaParticipant xaParticipant;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.dromara.hmily.core.context;

import lombok.Data;

import javax.transaction.xa.XAResource;

/**
* XaParticipant .
* 1、处理相关的参与者相关的数据Rpc的传传递.
*
* @author sixh chenbin
*/
@Data
public class XaParticipant {

/**
* 事务ID.
*/
private String globalId;

private String branchId;

/**
* flag.
* @see XAResource
*/
private int flag;

private String cmd;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.annotation.HmilyXA;
import org.dromara.hmily.annotation.TransTypeEnum;
import org.dromara.hmily.core.context.HmilyTransactionContext;
import org.dromara.hmily.core.mediator.LocalParameterLoader;
Expand All @@ -37,32 +39,42 @@
* @author zhaojun
*/
public class HmilyGlobalInterceptor implements HmilyTransactionInterceptor {

private static RpcParameterLoader parameterLoader;

private static final EnumMap<TransTypeEnum, HmilyTransactionHandlerRegistry> REGISTRY = new EnumMap<>(TransTypeEnum.class);

static {
parameterLoader = Optional.ofNullable(ExtensionLoaderFactory.load(RpcParameterLoader.class)).orElse(new LocalParameterLoader());
}

static {
REGISTRY.put(TransTypeEnum.TCC, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "tcc"));
REGISTRY.put(TransTypeEnum.TAC, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "tac"));
REGISTRY.put(TransTypeEnum.XA, ExtensionLoaderFactory.load(HmilyTransactionHandlerRegistry.class, "xa"));
}

@Override
public Object invoke(final ProceedingJoinPoint pjp) throws Throwable {
HmilyTransactionContext context = parameterLoader.load();
return invokeWithinTransaction(context, pjp);
}

private Object invokeWithinTransaction(final HmilyTransactionContext context, final ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
return getRegistry(signature.getMethod()).select(context).handleTransaction(point, context);
}

private HmilyTransactionHandlerRegistry getRegistry(final Method method) {
return null != method.getAnnotation(HmilyTCC.class) ? REGISTRY.get(TransTypeEnum.TCC) : REGISTRY.get(TransTypeEnum.TAC);
if (method.isAnnotationPresent(HmilyTCC.class)) {
return REGISTRY.get(TransTypeEnum.TCC);
} else if (method.isAnnotationPresent(HmilyTAC.class)) {
return REGISTRY.get(TransTypeEnum.TAC);
} else if (method.isAnnotationPresent(HmilyXA.class)) {
return REGISTRY.get(TransTypeEnum.XA);
} else {
return REGISTRY.get(TransTypeEnum.TAC);
}
// return null != method.getAnnotation(HmilyTCC.class) ? REGISTRY.get(TransTypeEnum.TCC) : REGISTRY.get(TransTypeEnum.TAC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed 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.
Expand All @@ -19,6 +19,7 @@
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;

import org.dromara.hmily.common.constant.CommonConstant;
import org.dromara.hmily.common.utils.GsonUtils;
import org.dromara.hmily.common.utils.StringUtils;
Expand All @@ -30,9 +31,9 @@
* @author xiaoyu(Myth)
*/
public class RpcMediator {

private static final RpcMediator INSTANCE = new RpcMediator();

/**
* Gets instance.
*
Expand All @@ -41,7 +42,7 @@ public class RpcMediator {
public static RpcMediator getInstance() {
return INSTANCE;
}

/**
* Transmit.
*
Expand All @@ -53,22 +54,47 @@ public void transmit(final RpcTransmit rpcTransmit, final HmilyTransactionContex
rpcTransmit.transmit(CommonConstant.HMILY_TRANSACTION_CONTEXT, GsonUtils.getInstance().toJson(context));
}
}


/**
* Transmit.
*
* @param <T> the type parameter
* @param rpcTransmit the rpc transmit
* @param context the context
*/
public <T> void transmit(final RpcTransmit rpcTransmit, final T context) {
if (Objects.nonNull(context)) {
rpcTransmit.transmit(CommonConstant.HMILY_TRANSACTION_CONTEXT, GsonUtils.getInstance().toJson(context));
}
}

/**
* Acquire hmily transaction context.
*
* @param rpcAcquire the rpc acquire
* @return the hmily transaction context
*/
public HmilyTransactionContext acquire(final RpcAcquire rpcAcquire) {
HmilyTransactionContext hmilyTransactionContext = null;
return acquire(rpcAcquire, HmilyTransactionContext.class);
}

/**
* Acquire hmily transaction context.
*
* @param <T> the type parameter
* @param rpcAcquire the rpc acquire
* @param clazz the clazz
* @return the hmily transaction context
*/
public <T> T acquire(final RpcAcquire rpcAcquire, final Class<T> clazz) {
T hmilyTransactionContext = null;
final String context = rpcAcquire.acquire(CommonConstant.HMILY_TRANSACTION_CONTEXT);
if (StringUtils.isNoneBlank(context)) {
hmilyTransactionContext = GsonUtils.getInstance().fromJson(context, HmilyTransactionContext.class);
hmilyTransactionContext = GsonUtils.getInstance().fromJson(context, clazz);
}
return hmilyTransactionContext;
}

/**
* Gets and set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* The interface Rpc parameter loader.
*/
public interface RpcParameterLoader {

/**
* Load hmily transaction context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.dromara.hmily.core.repository;

import lombok.Getter;
import lombok.Setter;
import org.dromara.hmily.common.enums.HmilyActionEnum;
import org.dromara.hmily.config.api.ConfigEnv;
Expand Down Expand Up @@ -44,6 +45,7 @@ public final class HmilyRepositoryFacade {
private final HmilyConfig hmilyConfig = ConfigEnv.getInstance().getConfig(HmilyConfig.class);

@Setter
@Getter
private HmilyRepository hmilyRepository;

private HmilyRepositoryFacade() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* The interface Hmily transaction handler registry.
*/
public interface HmilyTransactionHandlerRegistry {

/**
* Get hmily transaction handler.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public interface AccountService {
* @param accountDTO the account dto
* @return the boolean
*/
@Hmily
boolean testPayment(AccountDTO accountDTO);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.dromara.hmily.demo.tac.dubbo.inventory.DubboHmilyInventoryApplication</mainClass>
<mainClass>org.dromara.hmily.demo.tac.dubbo.org.dromara.hmily.xa.demo.dubbo.inventory.DubboHmilyInventoryApplication</mainClass>
<executable>true</executable>
</configuration>
</plugin>
Expand Down
Loading

0 comments on commit 80a46ff

Please sign in to comment.