Skip to content

Commit 031636d

Browse files
committed
fixed bug.
1 parent 040939d commit 031636d

7 files changed

Lines changed: 28 additions & 69 deletions

File tree

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/DTXContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public interface DTXContext {
5555
List<TransactionUnit> transactionUnits() throws TransactionException;
5656

5757
/**
58-
* 获取事务组属性
58+
* 获取事务组ID
5959
*
60-
* @return GroupProps
60+
* @return groupId
6161
*/
62-
GroupProps groupProps() throws TransactionException;
62+
String getGroupId();
6363

6464
/**
6565
* 获取事务组状态

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/DefaultDTXContext.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public class DefaultDTXContext implements DTXContext {
4343
@Override
4444
public void join(TransactionUnit transactionUnit) throws TransactionException {
4545
try {
46-
// idempotent processing
47-
if (fastStorage.containsTransactionUnit(groupId, transactionUnit)) {
48-
return;
49-
}
5046
fastStorage.saveTransactionUnitToGroup(groupId, transactionUnit);
5147
} catch (FastStorageException e) {
5248
throw new TransactionException("attempts to join the non-existent transaction group.");
@@ -72,12 +68,8 @@ public List<TransactionUnit> transactionUnits() throws TransactionException {
7268
}
7369

7470
@Override
75-
public GroupProps groupProps() throws TransactionException {
76-
try {
77-
return fastStorage.getGroupProps(groupId);
78-
} catch (FastStorageException e) {
79-
throw new TransactionException(e);
80-
}
71+
public String getGroupId() {
72+
return this.groupId;
8173
}
8274

8375
@Override

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/DefaultDTXContextRegistry.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public DefaultDTXContextRegistry(FastStorage fastStorage) {
4141
@Override
4242
public DTXContext create(String groupId) throws TransactionException {
4343
try {
44-
GroupProps groupProps = new GroupProps();
45-
groupProps.setGroupId(groupId);
46-
groupProps.setCreateTimeMillis(System.currentTimeMillis());
47-
fastStorage.initGroup(groupProps);
44+
fastStorage.initGroup(groupId);
4845
} catch (FastStorageException e) {
4946
// idempotent processing
5047
if (e.getCode() != FastStorageException.EX_CODE_REPEAT_GROUP) {
@@ -57,9 +54,9 @@ public DTXContext create(String groupId) throws TransactionException {
5754
@Override
5855
public DTXContext get(String groupId) throws TransactionException {
5956
// test has group
60-
if (!fastStorage.containsGroup(groupId)) {
61-
throw new TransactionException("non this transaction group: " + groupId);
62-
}
57+
// if (!fastStorage.containsGroup(groupId)) {
58+
// throw new TransactionException("non this transaction group: " + groupId);
59+
// }
6360
return new DefaultDTXContext(groupId, fastStorage);
6461
}
6562

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/SimpleTransactionManager.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void begin(String groupId) throws TransactionException {
7575

7676
@Override
7777
public void join(DTXContext dtxContext, String unitId, String unitType, String modId, int userState) throws TransactionException {
78-
log.debug("unit:{} joined group:{}", unitId, dtxContext.groupProps().getGroupId());
78+
log.debug("unit:{} joined group:{}", unitId, dtxContext.getGroupId());
7979
//手动回滚时设置状态为回滚状态 0
8080
if (userState == 0) {
8181
dtxContext.resetTransactionState(0);
@@ -120,11 +120,11 @@ public int transactionStateFromFastStorage(String groupId) {
120120
private void notifyTransaction(DTXContext dtxContext, int transactionState) throws TransactionException {
121121
for (TransactionUnit transUnit : dtxContext.transactionUnits()) {
122122
NotifyUnitParams notifyUnitParams = new NotifyUnitParams();
123-
notifyUnitParams.setGroupId(dtxContext.groupProps().getGroupId());
123+
notifyUnitParams.setGroupId(dtxContext.getGroupId());
124124
notifyUnitParams.setUnitId(transUnit.getUnitId());
125125
notifyUnitParams.setUnitType(transUnit.getUnitType());
126126
notifyUnitParams.setState(transactionState);
127-
txLogger.info(dtxContext.groupProps().getGroupId(),
127+
txLogger.info(dtxContext.getGroupId(),
128128
notifyUnitParams.getUnitId(), Transactions.TAG_TRANSACTION, "notify %s's unit: %s",
129129
transUnit.getModId(), transUnit.getUnitId());
130130
try {
@@ -145,8 +145,7 @@ private void notifyTransaction(DTXContext dtxContext, int transactionState) thro
145145
List<Object> params = Arrays.asList(notifyUnitParams, transUnit.getModId());
146146
rpcExceptionHandler.handleNotifyUnitMessageException(params, e);
147147
} finally {
148-
txLogger.info(dtxContext.groupProps().getGroupId(),
149-
notifyUnitParams.getUnitId(), Transactions.TAG_TRANSACTION, "notify unit over");
148+
txLogger.transactionInfo(dtxContext.getGroupId(), notifyUnitParams.getUnitId(), "notify unit over");
150149
}
151150
}
152151
}

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/storage/FastStorage.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,14 @@ public interface FastStorage {
3131

3232

3333
/*-----------------------DTX group------------------------------*/
34-
3534
/**
3635
* init DTX group.
3736
* note: group info should clean by self 10 seconds after DTX time.
3837
*
39-
* @param groupProps groupProps
40-
* @throws FastStorageException fastStorageException
41-
*/
42-
void initGroup(GroupProps groupProps) throws FastStorageException;
43-
44-
/**
45-
* the group props.
46-
*
4738
* @param groupId groupId
48-
* @return group props
49-
* @throws FastStorageException FastStorageException
50-
*/
51-
GroupProps getGroupProps(String groupId) throws FastStorageException;
52-
53-
/**
54-
* DTX group has unit
55-
*
56-
* @param groupId groupId
57-
* @param transactionUnit transactionUnit
58-
* @return bool
39+
* @throws FastStorageException fastStorageException
5940
*/
60-
boolean containsTransactionUnit(String groupId, TransactionUnit transactionUnit);
41+
void initGroup(String groupId) throws FastStorageException;
6142

6243
/**
6344
* has group

txlcn-tm/src/main/java/com/codingapi/txlcn/tm/core/storage/redis/RedisStorage.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.codingapi.txlcn.tm.cluster.TMProperties;
2222
import com.codingapi.txlcn.tm.config.TxManagerConfig;
2323
import com.codingapi.txlcn.tm.core.storage.FastStorage;
24-
import com.codingapi.txlcn.tm.core.storage.GroupProps;
2524
import com.codingapi.txlcn.tm.core.storage.LockValue;
2625
import com.codingapi.txlcn.tm.core.storage.TransactionUnit;
2726
import com.google.common.collect.Sets;
@@ -71,21 +70,9 @@ public RedisStorage(RedisTemplate<String, Object> redisTemplate, StringRedisTemp
7170
}
7271

7372
@Override
74-
public void initGroup(GroupProps groupProps) {
75-
redisTemplate.opsForHash().put(REDIS_GROUP_PREFIX + groupProps.getGroupId(), "root", groupProps);
76-
redisTemplate.expire(REDIS_GROUP_PREFIX + groupProps.getGroupId(),
77-
managerConfig.getDtxTime() + 10000, TimeUnit.MILLISECONDS);
78-
}
79-
80-
@Override
81-
public GroupProps getGroupProps(String groupId) throws FastStorageException {
82-
return (GroupProps) redisTemplate.opsForHash().get(REDIS_GROUP_PREFIX + groupId, "root");
83-
}
84-
85-
@Override
86-
public boolean containsTransactionUnit(String groupId, TransactionUnit transactionUnit) {
87-
Object unit = redisTemplate.opsForHash().get(REDIS_GROUP_PREFIX + groupId, transactionUnit.getUnitId());
88-
return Objects.nonNull(unit);
73+
public void initGroup(String groupId) {
74+
redisTemplate.opsForHash().put(REDIS_GROUP_PREFIX + groupId, "root", "");
75+
redisTemplate.expire(REDIS_GROUP_PREFIX + groupId, managerConfig.getDtxTime() + 10000, TimeUnit.MILLISECONDS);
8976
}
9077

9178
@Override
@@ -198,13 +185,13 @@ public void removeToken(String token) {
198185
@Override
199186
public void saveTMProperties(TMProperties tmProperties) {
200187
Objects.requireNonNull(tmProperties);
201-
redisTemplate.opsForHash().put(REDIS_TM_LIST,
202-
tmProperties.getHost() + ":" + tmProperties.getTransactionPort(), tmProperties.getHttpPort());
188+
stringRedisTemplate.opsForHash().put(REDIS_TM_LIST,
189+
tmProperties.getHost() + ":" + tmProperties.getTransactionPort(), String.valueOf(tmProperties.getHttpPort()));
203190
}
204191

205192
@Override
206193
public List<TMProperties> findTMProperties() {
207-
return redisTemplate.opsForHash().entries(REDIS_TM_LIST).entrySet().stream()
194+
return stringRedisTemplate.opsForHash().entries(REDIS_TM_LIST).entrySet().stream()
208195
.map(entry -> {
209196
String[] args = ApplicationInformation.splitAddress(entry.getKey().toString());
210197
TMProperties tmProperties = new TMProperties();
@@ -251,12 +238,15 @@ public int acquireOrRefreshMachineId(int machineId, long machineMaxSize, long ti
251238
int curId = Math.toIntExact(
252239
Objects.requireNonNull(
253240
stringRedisTemplate.opsForValue().increment(REDIS_MACHINE_ID_MAP_PREFIX + "cur_id", 1)));
241+
if (curId > machineMaxSize) {
242+
stringRedisTemplate.opsForValue().set(REDIS_MACHINE_ID_MAP_PREFIX + "cur_id", "0");
243+
curId = 0;
244+
}
254245
if (Optional
255-
.ofNullable(stringRedisTemplate.hasKey(REDIS_MACHINE_ID_MAP_PREFIX + ((curId) &= machineMaxSize)))
246+
.ofNullable(stringRedisTemplate.hasKey(REDIS_MACHINE_ID_MAP_PREFIX + curId))
256247
.orElse(true)) {
257248
continue;
258249
}
259-
stringRedisTemplate.opsForValue().set(REDIS_MACHINE_ID_MAP_PREFIX + "cur_id", String.valueOf(curId));
260250
stringRedisTemplate.opsForValue().set(REDIS_MACHINE_ID_MAP_PREFIX + curId, "", timeout, TimeUnit.MILLISECONDS);
261251
return curId;
262252
}

txlcn-tm/src/main/resources/application-ujued.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22
#tx-lcn.logger.enabled=true
3-
logging.level.com.codingapi.txlcn=DEBUG
3+
#logging.level.com.codingapi.txlcn=DEBUG
44

55
spring.datasource.password=123456
66

77

88

99
tx-lcn.message.netty.attr-delay-time=10000
1010

11-
tx-lcn.logger.enabled=true
11+
tx-lcn.logger.enabled=false
1212
#tx-lcn.logger.driver-class-name=com.mysql.jdbc.Driver
1313
#tx-lcn.logger.jdbc-url=jdbc:mysql://127.0.0.1:3306/tx_logger?characterEncoding=UTF-8
1414
#tx-lcn.logger.username=root

0 commit comments

Comments
 (0)