Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAC模式读已提交的隔离级别bug修复 #364

Merged
merged 3 commits into from
Aug 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public class HmilyConfig extends AbstractConfig {
* tac sqlRevert.
*/
private String sqlRevert = "default";

/**
* global lock retry interval(unit: ms).
*/
private int lockRetryInterval = 10;

/**
* global lock retry times.
*/
private int lockRetryTimes = 30;

@Override
public String prefix() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.dromara.hmily.demo.tac.dubbo.order.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* The enum transaction enum.
*
* @author zhangzhi
*/
@RequiredArgsConstructor
@Getter
public enum ReadCommittedTransactionEnum {

TRANSACTION_READ_WRITE(1, "读已提交隔离级别的事务, 包括更新、查询操作"),

TRANSACTION_READ_ONLY(2, "读已提交隔离级别的事务, 只有查询操作");

private final int code;

private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dromara.hmily.demo.tac.dubbo.order.service;

import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;

/**
* The interface Payment service.
Expand Down Expand Up @@ -97,6 +98,7 @@ public interface PaymentService {
* 订单支付.
*
* @param order 订单实体
* @param readCommittedTransactionEnum 事务类型
*/
String makePaymentWithReadCommitted(Order order);
String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum readCommittedTransactionEnum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package org.dromara.hmily.demo.tac.dubbo.order.service.impl;

import org.dromara.hmily.common.utils.IdWorkerUtils;
import org.dromara.hmily.demo.common.account.api.AccountService;
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.tac.dubbo.order.service.OrderService;
import org.dromara.hmily.demo.tac.dubbo.order.service.PaymentService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -165,7 +165,23 @@ private Order saveOrder(Integer count, BigDecimal amount) {
public String orderPayWithReadCommitted(Integer count, BigDecimal amount) {
Order order = saveOrder(count, amount);
long start = System.currentTimeMillis();
paymentService.makePaymentWithReadCommitted(order);
// 开启一个事务
new Thread(() -> {
try {
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.TRANSACTION_READ_WRITE);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}, "global trans2").start();
try {
// 确保第一个事务先执行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 开启另一个事务
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY);

System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
return "success";
}
Expand Down
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 @@ -27,19 +27,21 @@
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.tac.dubbo.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.tac.dubbo.order.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
* PaymentServiceImpl.
* @author xiaoyu
*/
@Service
public class PaymentServiceImpl implements PaymentService {

private static final Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class);

private final OrderMapper orderMapper;
Expand All @@ -56,7 +58,7 @@ public PaymentServiceImpl(final OrderMapper orderMapper,
this.accountService = accountService;
this.inventoryService = inventoryService;
}

@Override
@HmilyTAC
public void makePayment(final Order order) {
Expand All @@ -66,7 +68,7 @@ public void makePayment(final Order order) {
//进入扣减库存操作
inventoryService.decrease(buildInventoryDTO(order));
}

@Override
public void testMakePayment(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
Expand All @@ -87,7 +89,7 @@ public void makePaymentWithNested(final Order order) {
//扣除用户余额
accountService.paymentWithNested(buildAccountNestedDTO(order));
}

@Override
@HmilyTAC
public void makePaymentWithNestedException(final Order order) {
Expand All @@ -99,7 +101,7 @@ public void makePaymentWithNestedException(final Order order) {
//扣除用户余额
accountService.paymentWithNestedException(buildAccountNestedDTO(order));
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithTryException(final Order order) {
Expand All @@ -109,7 +111,7 @@ public String mockPaymentInventoryWithTryException(final Order order) {
inventoryService.mockWithTryException(buildInventoryDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithTryTimeout(final Order order) {
Expand All @@ -119,23 +121,23 @@ public String mockPaymentInventoryWithTryTimeout(final Order order) {
inventoryService.mockWithTryTimeout(buildInventoryDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentAccountWithTryException(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
accountService.mockTryPaymentException(buildAccountDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentAccountWithTryTimeout(final Order order) {
updateOrderStatus(order, OrderStatusEnum.PAYING);
accountService.mockTryPaymentTimeout(buildAccountDTO(order));
return "success";
}

@Override
@HmilyTAC
public String mockPaymentInventoryWithConfirmTimeout(final Order order) {
Expand All @@ -151,13 +153,24 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {

@Override
@HmilyTAC
public String makePaymentWithReadCommitted(Order order) {
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
if (ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY.equals(transactionEnum)) {
accountService.findByUserId(order.getUserId());
return "success";
}
updateOrderStatus(order, OrderStatusEnum.PAY_SUCCESS);
//扣除用户余额
accountService.payment(buildAccountDTO(order));
//查询账户信息, 读已提交, 此时该事务未结束, 获取全局锁失败, 将会回滚
//查询账户信息, 读已提交隔离级别, 但是在统一全局事务中, 所以可见
accountService.findByUserId(order.getUserId());
//进入扣减库存操作
try {
// 延时第一个事务, 确保第一个事务还没结束, 第二个事务执行查询
Thread.sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
}
inventoryService.decrease(buildInventoryDTO(order));
return "success";
}
Expand All @@ -166,14 +179,14 @@ private void updateOrderStatus(final Order order, final OrderStatusEnum orderSta
order.setStatus(orderStatus.getCode());
orderMapper.update(order);
}

private AccountDTO buildAccountDTO(final Order order) {
AccountDTO accountDTO = new AccountDTO();
accountDTO.setAmount(order.getTotalAmount());
accountDTO.setUserId(order.getUserId());
return accountDTO;
}

private AccountNestedDTO buildAccountNestedDTO(final Order order) {
AccountNestedDTO nestedDTO = new AccountNestedDTO();
nestedDTO.setAmount(order.getTotalAmount());
Expand All @@ -182,7 +195,7 @@ private AccountNestedDTO buildAccountNestedDTO(final Order order) {
nestedDTO.setCount(order.getCount());
return nestedDTO;
}

private InventoryDTO buildInventoryDTO(final Order order) {
InventoryDTO inventoryDTO = new InventoryDTO();
inventoryDTO.setCount(order.getCount());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.dromara.hmily.demo.springcloud.order.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* The enum transaction enum.
*
* @author zhangzhi
*/
@RequiredArgsConstructor
@Getter
public enum ReadCommittedTransactionEnum {

TRANSACTION_READ_WRITE(1, "读已提交隔离级别的事务, 包括更新、查询操作"),

TRANSACTION_READ_ONLY(2, "读已提交隔离级别的事务, 只有查询操作");

private final int code;

private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;

/**
* PaymentService.
Expand Down Expand Up @@ -100,6 +101,7 @@ public interface PaymentService {
* 订单支付.
*
* @param order 订单实体
* @param readCommittedTransactionEnum 读已提交事务类型
*/
String makePaymentWithReadCommitted(Order order);
String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum readCommittedTransactionEnum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.dromara.hmily.demo.common.order.entity.Order;
import org.dromara.hmily.demo.common.order.enums.OrderStatusEnum;
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.springcloud.order.service.OrderService;
import org.dromara.hmily.demo.springcloud.order.service.PaymentService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -133,7 +134,23 @@ public void updateOrderStatus(Order order) {
public String orderPayWithReadCommitted(Integer count, BigDecimal amount) {
Order order = saveOrder(count, amount);
long start = System.currentTimeMillis();
paymentService.makePaymentWithReadCommitted(order);
// 开启一个事务
new Thread(() -> {
try {
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.TRANSACTION_READ_WRITE);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}, "global trans2").start();
try {
// 确保第一个事务先执行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 开启另一个事务
paymentService.makePaymentWithReadCommitted(order, ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY);

System.out.println("切面耗时:" + (System.currentTimeMillis() - start));
return "success";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.dromara.hmily.demo.springcloud.order.service.impl;

import org.dromara.hmily.annotation.HmilyTAC;
import org.dromara.hmily.annotation.HmilyTCC;
import org.dromara.hmily.common.exception.HmilyRuntimeException;
import org.dromara.hmily.demo.common.account.dto.AccountDTO;
import org.dromara.hmily.demo.common.account.dto.AccountNestedDTO;
Expand All @@ -27,6 +26,7 @@
import org.dromara.hmily.demo.common.order.mapper.OrderMapper;
import org.dromara.hmily.demo.springcloud.order.client.AccountClient;
import org.dromara.hmily.demo.springcloud.order.client.InventoryClient;
import org.dromara.hmily.demo.springcloud.order.enums.ReadCommittedTransactionEnum;
import org.dromara.hmily.demo.springcloud.order.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -154,13 +154,24 @@ public String mockPaymentInventoryWithConfirmTimeout(final Order order) {

@Override
@HmilyTAC
public String makePaymentWithReadCommitted(Order order) {
public String makePaymentWithReadCommitted(Order order, ReadCommittedTransactionEnum transactionEnum) {
//第二个事务查询相同账户信息, 获取不到全局锁, 会进行回滚
if (ReadCommittedTransactionEnum.TRANSACTION_READ_ONLY.equals(transactionEnum)) {
accountClient.findByUserId(order.getUserId());
return "success";
}
updateOrderStatus(order, OrderStatusEnum.PAY_SUCCESS);
//扣除用户余额
accountClient.payment(buildAccountDTO(order));
//查询账户信息, 读已提交, 此时该事务未结束, 获取全局锁失败, 将会回滚
//查询账户信息, 读已提交隔离级别, 但是在统一全局事务中, 所以可见
accountClient.findByUserId(order.getUserId());
//进入扣减库存操作
try {
// 延时第一个事务, 确保第一个事务还没结束, 第二个事务执行查询
Thread.sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
}
inventoryClient.decrease(buildInventoryDTO(order));
return "success";
}
Expand Down Expand Up @@ -192,4 +203,5 @@ private AccountNestedDTO buildAccountNestedDTO(Order order) {
nestedDTO.setCount(order.getCount());
return nestedDTO;
}

}
Loading
Loading