Skip to content

Commit af06347

Browse files
committed
修复已知bug
1 parent e422f01 commit af06347

6 files changed

Lines changed: 153 additions & 72 deletions

File tree

tx-client/src/main/java/com/codingapi/tx/aop/service/impl/TxStartTransactionServerImpl.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,50 +83,56 @@ public Object execute(ProceedingJoinPoint point,final TxTransactionInfo info) th
8383
new Thread(new HookRunnable() {
8484
@Override
8585
public void run0() {
86-
if(task.isAwait()) {
8786

88-
int rs = txManagerService.closeTransactionGroup(groupId, resState);
87+
while (!task.isAwait() && !Thread.currentThread().interrupted()) {
88+
try {
89+
Thread.sleep(1);
90+
} catch (InterruptedException e) {
91+
e.printStackTrace();
92+
}
93+
}
8994

90-
int lastState = rs==-1?0:resState;
95+
int rs = txManagerService.closeTransactionGroup(groupId, resState);
9196

92-
int executeConnectionError = 0;
97+
int lastState = rs==-1?0:resState;
9398

94-
//控制本地事务的数据提交
95-
final TxTask waitTask = TaskGroupManager.getInstance().getTask(groupId, type);
96-
if(waitTask!=null){
97-
waitTask.setState(lastState);
98-
waitTask.signalTask();
99+
int executeConnectionError = 0;
99100

100-
while (!waitTask.isRemove()){
101-
try {
102-
Thread.sleep(1);
103-
} catch (InterruptedException e) {
104-
e.printStackTrace();
105-
}
106-
}
101+
//控制本地事务的数据提交
102+
final TxTask waitTask = TaskGroupManager.getInstance().getTask(groupId, type);
103+
if(waitTask!=null){
104+
waitTask.setState(lastState);
105+
waitTask.signalTask();
107106

108-
if(waitTask.getState()== TaskState.connectionError.getCode()){
109-
//本地执行失败.
110-
executeConnectionError = 1;
111-
112-
lastState = 0;
107+
while (!waitTask.isRemove()){
108+
try {
109+
Thread.sleep(1);
110+
} catch (InterruptedException e) {
111+
e.printStackTrace();
113112
}
114113
}
115114

115+
if(waitTask.getState()== TaskState.connectionError.getCode()){
116+
//本地执行失败.
117+
executeConnectionError = 1;
116118

117-
if (compensateLocal == null) {
118-
long end = System.currentTimeMillis();
119-
long time = end - start;
120-
if (executeConnectionError == 1||(lastState == 1 && rs == 0)) {
121-
//记录补偿日志
122-
txManagerService.sendCompensateMsg(groupId, time, info,executeConnectionError);
123-
}
119+
lastState = 0;
124120
}
121+
}
125122

126-
task.setState(lastState);
127-
task.signalTask();
123+
if (compensateLocal == null) {
124+
long end = System.currentTimeMillis();
125+
long time = end - start;
126+
if (executeConnectionError == 1||(lastState == 1 && rs == 0)) {
127+
//记录补偿日志
128+
txManagerService.sendCompensateMsg(groupId, time, info,executeConnectionError);
129+
}
128130
}
131+
132+
task.setState(lastState);
133+
task.signalTask();
129134
}
135+
130136
}).start();
131137

132138
task.awaitTask();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3+
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
4+
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
5+
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
6+
* Vestibulum commodo. Ut rhoncus gravida arcu.
7+
*/
8+
9+
package com.codingapi.tx.framework.utils;
10+
11+
import java.net.InetAddress;
12+
import java.net.UnknownHostException;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
15+
16+
/**
17+
* create by lorne on 2017/12/12
18+
*/
19+
public class IpAddressUtils {
20+
21+
22+
public static boolean isIpAddress(String ipAddress){
23+
String ipAddressRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}:([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5][0-9][0-3][0-5])";
24+
Pattern ipAddressPattern = Pattern.compile(ipAddressRegex);
25+
Matcher matcher = ipAddressPattern.matcher(ipAddress);
26+
return matcher.matches();
27+
}
28+
29+
public static boolean isIpFormat(String ipAddress){
30+
return ipAddress.contains(":");
31+
}
32+
33+
public static String getIpByDomain(String domain){
34+
InetAddress ip= null;
35+
try {
36+
ip = InetAddress.getByName(domain);
37+
} catch (UnknownHostException e) {
38+
return null;
39+
}
40+
return ip.getHostAddress();
41+
}
42+
43+
44+
public static boolean isIp(String ipString) {
45+
String ipRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
46+
Pattern pattern = Pattern.compile(ipRegex);
47+
Matcher matcher = pattern.matcher(ipString);
48+
return matcher.matches();
49+
}
50+
51+
52+
}

tx-client/src/main/java/com/codingapi/tx/netty/service/impl/NettyControlServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.codingapi.tx.netty.service.MQTxManagerService;
88
import com.codingapi.tx.netty.service.NettyControlService;
99
import com.codingapi.tx.netty.service.NettyService;
10-
import com.codingapi.tx.netty.utils.IpAddressUtils;
10+
import com.codingapi.tx.framework.utils.IpAddressUtils;
1111
import com.lorne.core.framework.utils.task.ConditionUtils;
1212
import com.lorne.core.framework.utils.task.IBack;
1313
import com.lorne.core.framework.utils.task.Task;

tx-client/src/main/java/com/codingapi/tx/netty/utils/IpAddressUtils.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codingapi.tm.framework.utils;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* create by lorne on 2017/12/12
10+
*/
11+
public class IpAddressUtils {
12+
13+
14+
public static boolean isIpAddress(String ipAddress){
15+
String ipAddressRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}:([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-5][0-9][0-3][0-5])";
16+
Pattern ipAddressPattern = Pattern.compile(ipAddressRegex);
17+
Matcher matcher = ipAddressPattern.matcher(ipAddress);
18+
return matcher.matches();
19+
}
20+
21+
public static boolean isIpFormat(String ipAddress){
22+
return ipAddress.contains(":");
23+
}
24+
25+
public static String getIpByDomain(String domain){
26+
InetAddress ip= null;
27+
try {
28+
ip = InetAddress.getByName(domain);
29+
} catch (UnknownHostException e) {
30+
return null;
31+
}
32+
return ip.getHostAddress();
33+
}
34+
35+
36+
public static boolean isIp(String ipString) {
37+
String ipRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
38+
Pattern pattern = Pattern.compile(ipRegex);
39+
Matcher matcher = pattern.matcher(ipString);
40+
return matcher.matches();
41+
}
42+
43+
44+
45+
46+
}

tx-manager/src/main/java/com/codingapi/tm/manager/service/impl/MicroServiceImpl.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.codingapi.tm.Constants;
44
import com.codingapi.tm.config.ConfigReader;
5+
import com.codingapi.tm.framework.utils.IpAddressUtils;
56
import com.codingapi.tm.framework.utils.SocketManager;
67
import com.codingapi.tm.manager.service.MicroService;
78
import com.codingapi.tm.model.TxServer;
@@ -18,8 +19,6 @@
1819

1920
import java.util.ArrayList;
2021
import java.util.List;
21-
import java.util.regex.Matcher;
22-
import java.util.regex.Pattern;
2322

2423
/**
2524
* create by lorne on 2017/11/11
@@ -43,19 +42,10 @@ public class MicroServiceImpl implements MicroService {
4342
private EurekaClient eurekaClient;
4443

4544

46-
47-
4845
/** logger */
4946
private static final Logger logger = LoggerFactory.getLogger(MicroServiceImpl.class);
5047

5148

52-
private boolean isIp(String ipAddress) {
53-
String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
54-
Pattern pattern = Pattern.compile(ip);
55-
Matcher matcher = pattern.matcher(ipAddress);
56-
return matcher.matches();
57-
}
58-
5949

6050
public List<InstanceInfo> getConfigServiceInstances() {
6151
Application application = eurekaClient.getApplication(tmKey);
@@ -70,10 +60,7 @@ public TxState getState() {
7060
TxState state = new TxState();
7161

7262
//String ipAddress = EurekaServerContextHolder.getInstance().getServerContext().getApplicationInfoManager().getEurekaInstanceConfig().getIpAddress();
73-
String ipAddress = discoveryClient.getLocalServiceInstance().getHost();
74-
if(!isIp(ipAddress)){
75-
ipAddress = "127.0.0.1";
76-
}
63+
String ipAddress = getIp(discoveryClient.getLocalServiceInstance().getHost());
7764
state.setIp(ipAddress);
7865
state.setPort(Constants.socketPort);
7966
state.setMaxConnection(SocketManager.getInstance().getMaxConnection());
@@ -90,18 +77,28 @@ public TxState getState() {
9077
}
9178

9279

80+
private String getIp(String ipAddress){
81+
if(!IpAddressUtils.isIp(ipAddress)){
82+
83+
ipAddress = IpAddressUtils.getIpByDomain(ipAddress);
84+
85+
if(ipAddress==null||!IpAddressUtils.isIp(ipAddress)) {
86+
ipAddress = "127.0.0.1";
87+
}
88+
}
89+
return ipAddress;
90+
}
91+
9392
private List<String> getServices(){
9493
List<String> urls = new ArrayList<>();
9594
List<InstanceInfo> instanceInfos =getConfigServiceInstances();
9695
for (InstanceInfo instanceInfo : instanceInfos) {
9796
String url = instanceInfo.getHomePageUrl();
9897
String address = instanceInfo.getIPAddr();
99-
if (isIp(address)) {
100-
urls.add(url);
101-
}else{
102-
url = url.replace(address,"127.0.0.1");
103-
urls.add(url);
104-
}
98+
String ipAddress = getIp(address);
99+
100+
url = url.replace(address,ipAddress);
101+
urls.add(url);
105102
}
106103
return urls;
107104
}

0 commit comments

Comments
 (0)