Skip to content

Commit c1aae3a

Browse files
committed
id, notify group, rpc
1 parent 76a5cba commit c1aae3a

9 files changed

Lines changed: 43 additions & 20 deletions

File tree

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/id/DefaultIdGen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public synchronized String nextId() {
4949

5050
long seqWithMachine = (machineId << machineOffset & Long.MAX_VALUE) | seq;
5151

52-
return Long.toHexString(curTime - START_TIME) + Long.toHexString(seqWithMachine);
52+
return Long.toHexString((curTime - START_TIME) ^ 9527) + Long.toHexString(seqWithMachine ^ 9527);
5353
}
5454

5555
private long tilNextMillis() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codingapi.txlcn.common.util.id;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.JUnit4;
6+
7+
/**
8+
* Description:
9+
* Date: 19-2-22 下午3:35
10+
*
11+
* @author ujued
12+
*/
13+
@RunWith(JUnit4.class)
14+
public class IdTest {
15+
16+
@Test
17+
public void testDefaultIdGen() {
18+
IdGenInit.applyDefaultIdGen(12, 1);
19+
20+
for (int i = 0; i < 10; i++) {
21+
System.out.println(RandomUtils.randomKey());
22+
}
23+
}
24+
}

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/AbstractTxLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void trace(String groupId, String unitId, String tag, String content, Obj
5656
if (logDbProperties.isEnabled() && !logDbProperties.isOnlyError()) {
5757
saveTxLog(groupId, unitId, tag, content, args);
5858
}
59-
LOG.debug(content, args);
59+
LOG.debug(content + " @group(" + groupId + ")", args);
6060
}
6161

6262
@Override
@@ -67,7 +67,7 @@ public void error(String groupId, String unitId, String tag, String content, Obj
6767
if (logDbProperties.isEnabled() && logDbProperties.isOnlyError()) {
6868
saveTxLog(groupId, unitId, tag, content, args);
6969
}
70-
LOG.error(content, args);
70+
LOG.error(content + " @group(" + groupId + ")", args);
7171
}
7272

7373
private void saveTxLog(String groupId, String unitId, String tag, String content, Object... args) {

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/core/template/TransactionControlTemplate.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void createGroup(String groupId, String unitId, TransactionInfo transacti
8484
try {
8585
// 日志
8686
txLogger.txTrace(groupId, unitId,
87-
"create group > {} > groupId: {xid}, unitId: {uid}", transactionType);
87+
"create group > transaction type: {}", transactionType);
8888
// 创建事务组消息
8989
reliableMessenger.createGroup(groupId);
9090
// 缓存发起方切面信息
@@ -111,11 +111,11 @@ public void createGroup(String groupId, String unitId, TransactionInfo transacti
111111
public void joinGroup(String groupId, String unitId, String transactionType, TransactionInfo transactionInfo)
112112
throws TransactionException {
113113
try {
114-
txLogger.txTrace(groupId, unitId, "join group > {} > groupId: {xid}, unitId: {uid}", transactionType);
114+
txLogger.txTrace(groupId, unitId, "join group > transaction type: {}", transactionType);
115115

116116
reliableMessenger.joinGroup(groupId, unitId, transactionType, DTXLocalContext.transactionState(globalContext.dtxState(groupId)));
117117

118-
txLogger.txTrace(groupId, unitId, "{xid} join group message over.");
118+
txLogger.txTrace(groupId, unitId, "join group message over.");
119119

120120
// 异步检测
121121
dtxChecking.startDelayCheckingAsync(groupId, unitId, transactionType);
@@ -127,7 +127,7 @@ public void joinGroup(String groupId, String unitId, String transactionType, Tra
127127
} catch (LcnBusinessException e) {
128128
dtxExceptionHandler.handleJoinGroupBusinessException(Arrays.asList(groupId, unitId, transactionType), e);
129129
}
130-
txLogger.txTrace(groupId, unitId, "join logic group over");
130+
txLogger.txTrace(groupId, unitId, "join group logic over");
131131
}
132132

133133
/**
@@ -141,11 +141,11 @@ public void joinGroup(String groupId, String unitId, String transactionType, Tra
141141
public void notifyGroup(String groupId, String unitId, String transactionType, int state) {
142142
try {
143143
txLogger.txTrace(
144-
groupId, unitId, "notify group > {} > groupId: {xid}, unitId: {uid}, state: {}.", transactionType, state);
144+
groupId, unitId, "notify group > transaction type: {}, state: {}.", transactionType, state);
145145
if (globalContext.isDTXTimeout()) {
146146
throw new LcnBusinessException("dtx timeout.");
147147
}
148-
reliableMessenger.notifyGroup(groupId, state);
148+
state = reliableMessenger.notifyGroup(groupId, state);
149149
transactionCleanTemplate.clean(groupId, unitId, transactionType, state);
150150
} catch (TransactionClearException e) {
151151
txLogger.trace(groupId, unitId, Transactions.TE, "clean transaction fail.");

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/txmsg/LoopMessenger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void releaseLocks(Set<String> lockIdList) throws RpcException {
6666
}
6767

6868
@Override
69-
public void notifyGroup(String groupId, int transactionState) throws RpcException, LcnBusinessException {
69+
public int notifyGroup(String groupId, int transactionState) throws RpcException, LcnBusinessException {
7070
NotifyGroupParams notifyGroupParams = new NotifyGroupParams();
7171
notifyGroupParams.setGroupId(groupId);
7272
notifyGroupParams.setState(transactionState);
@@ -76,6 +76,7 @@ public void notifyGroup(String groupId, int transactionState) throws RpcExceptio
7676
if (!MessageUtils.statusOk(messageDto)) {
7777
throw new LcnBusinessException(messageDto.loadBean(Throwable.class));
7878
}
79+
return messageDto.loadBean(Integer.class);
7980
}
8081

8182
@Override

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/txmsg/ReliableMessenger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ public interface ReliableMessenger {
5353
*
5454
* @param groupId groupId
5555
* @param transactionState 分布式事务状态
56+
* @return dtx state
5657
* @throws RpcException Non TM
5758
* @throws LcnBusinessException TM Business Err
5859
*/
59-
void notifyGroup(String groupId, int transactionState) throws RpcException, LcnBusinessException;
60+
int notifyGroup(String groupId, int transactionState) throws RpcException, LcnBusinessException;
6061

6162
/**
6263
* 加入事务组

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/txmsg/transaction/CreateGroupExecuteService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Serializable execute(TransactionCmd transactionCmd) throws TxManagerExcep
5151
} catch (TransactionException e) {
5252
throw new TxManagerException(e);
5353
}
54-
txLogger.txTrace(transactionCmd.getGroupId(), null, "created group:{}", transactionCmd.getGroupId());
54+
txLogger.txTrace(transactionCmd.getGroupId(), null, "created group");
5555
return null;
5656
}
5757
}

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/txmsg/transaction/NotifyGroupExecuteService.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.codingapi.txlcn.common.exception.TransactionException;
1919
import com.codingapi.txlcn.common.exception.TxManagerException;
20-
import com.codingapi.txlcn.common.exception.UserRollbackException;
2120
import com.codingapi.txlcn.logger.TxLogger;
2221
import com.codingapi.txlcn.tm.core.DTXContext;
2322
import com.codingapi.txlcn.tm.core.DTXContextRegistry;
@@ -60,10 +59,8 @@ public Serializable execute(TransactionCmd transactionCmd) throws TxManagerExcep
6059
int commitState = notifyGroupParams.getState();
6160
// 获取事务状态(当手动回滚时会先设置状态)
6261
int transactionState = transactionManager.transactionStateFromFastStorage(transactionCmd.getGroupId());
63-
boolean hasThrow = false;
6462
if (transactionState == 0) {
6563
commitState = 0;
66-
hasThrow = true;
6764
}
6865

6966
// 系统日志
@@ -75,16 +72,16 @@ public Serializable execute(TransactionCmd transactionCmd) throws TxManagerExcep
7572
} else if (commitState == 0) {
7673
transactionManager.rollback(dtxContext);
7774
}
78-
if (hasThrow) {
79-
throw new UserRollbackException("user mandatory rollback");
75+
if (transactionState == 0) {
76+
txLogger.txTrace(transactionCmd.getGroupId(), "", "mandatory rollback for user.");
8077
}
78+
return transactionState;
8179
} catch (TransactionException e) {
8280
throw new TxManagerException(e);
8381
} finally {
8482
transactionManager.close(transactionCmd.getGroupId());
8583
// 系统日志
8684
txLogger.txTrace(transactionCmd.getGroupId(), "", "notify group successfully.");
8785
}
88-
return null;
8986
}
9087
}

txlcn-txmsg/src/main/java/com/codingapi/txlcn/txmsg/RpcConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ public class RpcConfig {
4545
/**
4646
* 断线重连次数
4747
*/
48-
private int reconnectCount = 5;
48+
private int reconnectCount = 8;
4949

5050
/**
5151
* 重连延迟时间(ms)
5252
*/
53-
private long reconnectDelay = 10000;
53+
private long reconnectDelay = 6000;
5454

5555
}

0 commit comments

Comments
 (0)