Skip to content

Commit

Permalink
split controller with lock, no lock, update BalanceServiceRedisson, a…
Browse files Browse the repository at this point in the history
…dd controller
  • Loading branch information
yennanliu committed Feb 22, 2024
1 parent f73c88b commit 742f062
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public class BalanceController {
@Autowired
private BalanceService balanceService;

@Autowired
private BalanceServiceRedisson balanceServiceRedisson;

@GetMapping("/")
public ResponseEntity<List<Balance>> getBalanceList(){

Expand All @@ -39,13 +36,6 @@ public ResponseEntity<Balance> getDepartmentByUserId(@PathVariable("userId") Int
return new ResponseEntity<>(balance, HttpStatus.OK);
}

@GetMapping("/v1/{userId}")
public ResponseEntity<Balance> getDepartmentByUserIdWithLock(@PathVariable("userId") Integer userId){

Balance balance = balanceServiceRedisson.getBalanceById(userId);
return new ResponseEntity<>(balance, HttpStatus.OK);
}

// @PostMapping("/update")
// public ResponseEntity<ApiResponse> updateDepartment(@RequestBody DepartmentDto departmentDto) {
//
Expand All @@ -63,17 +53,16 @@ public ResponseEntity<ApiResponse> addBalance(@RequestBody AddBalanceDto addBala
@PostMapping("/deduct")
public ResponseEntity<ApiResponse> deductBalance(@RequestBody DeductBalanceDto deductBalanceDto){

//balanceService.deductBalance(deductBalanceDto);
balanceServiceRedisson.deductBalance(deductBalanceDto);
balanceService.deductBalance(deductBalanceDto);
return new ResponseEntity<>(new ApiResponse(true, "Balance has been deducted !!!"), HttpStatus.CREATED);
}

@PostMapping("/transfer")
public ResponseEntity<ApiResponse> transfer(@RequestBody DeductBalanceDto deductBalanceDto){

//balanceServiceRedisson.transferRedis(deductBalanceDto);
balanceServiceRedisson.transferMysql(deductBalanceDto);
return new ResponseEntity<>(new ApiResponse(true, "Balance has been transferred !!!"), HttpStatus.CREATED);
}
// @PostMapping("/transfer")
// public ResponseEntity<ApiResponse> transfer(@RequestBody DeductBalanceDto deductBalanceDto){
//
// //balanceServiceRedisson.transferRedis(deductBalanceDto);
// balanceService.transferMysql(deductBalanceDto);
// return new ResponseEntity<>(new ApiResponse(true, "Balance has been transferred !!!"), HttpStatus.CREATED);
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.yen.springBankApp.controller;

import com.yen.springBankApp.common.ApiResponse;
import com.yen.springBankApp.model.dto.Balance.DeductBalanceDto;
import com.yen.springBankApp.service.BalanceServiceRedisson;
import lombok.extern.slf4j.Slf4j;
import com.yen.springBankApp.model.Balance;
import com.yen.springBankApp.model.dto.Balance.AddBalanceDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.yen.springBankApp.service.BalanceService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/balance_lock")
public class BalanceControllerLock {

@Autowired
private BalanceServiceRedisson balanceServiceRedisson;

@GetMapping("/")
public ResponseEntity<List<Balance>> getBalanceList(){

List<Balance> balanceList = balanceServiceRedisson.getBalances();
return new ResponseEntity<>(balanceList, HttpStatus.OK);
}

@GetMapping("/{userId}")
public ResponseEntity<Balance> getDepartmentByUserIdWithLock(@PathVariable("userId") Integer userId){

Balance balance = balanceServiceRedisson.getBalanceById(userId);
return new ResponseEntity<>(balance, HttpStatus.OK);
}

// @PostMapping("/update")
// public ResponseEntity<ApiResponse> updateDepartment(@RequestBody DepartmentDto departmentDto) {
//
// departmentService.updateDepartment(departmentDto);
// return new ResponseEntity<ApiResponse>(new ApiResponse(true, "Department has been updated"), HttpStatus.OK);
// }

@PostMapping("/add")
public ResponseEntity<ApiResponse> addBalance(@RequestBody AddBalanceDto addBalanceDto){

balanceServiceRedisson.addBalance(addBalanceDto);
return new ResponseEntity<>(new ApiResponse(true, "Balance has been added"), HttpStatus.CREATED);
}

@PostMapping("/deduct")
public ResponseEntity<ApiResponse> deductBalance(@RequestBody DeductBalanceDto deductBalanceDto){

balanceServiceRedisson.deductBalance(deductBalanceDto);
return new ResponseEntity<>(new ApiResponse(true, "Balance has been deducted !!!"), HttpStatus.CREATED);
}

@PostMapping("/transfer")
public ResponseEntity<ApiResponse> transfer(@RequestBody DeductBalanceDto deductBalanceDto){

//balanceServiceRedisson.transferRedis(deductBalanceDto);
balanceServiceRedisson.transferMysql(deductBalanceDto);
return new ResponseEntity<>(new ApiResponse(true, "Balance has been transferred !!!"), HttpStatus.CREATED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ public BalanceServiceRedisson(RedissonClient redissonClient) {

public List<Balance> getBalances() {

return balanceRepository.findAll();
System.out.println("(BalanceServiceRedisson) getBalances");
// lock
this.rwLock.readLock().lock();
try{
return balanceRepository.findAll();
}catch (Exception e){
e.printStackTrace();
}finally {
this.rwLock.readLock().unlock();
}
return null; // return default val ?
}

public Balance getBalanceById(Integer id) {
Expand Down Expand Up @@ -68,30 +78,49 @@ public Balance getBalanceById(Integer id) {

public Balance getBalanceByUserId(Integer userId) {

List<Balance> balanceList = balanceRepository.findAll();
return balanceList.stream().filter(x -> {return x.getUserId().equals(userId);}
).collect(Collectors.toList()).get(0);
// lock
this.rwLock.readLock().lock();
try{
List<Balance> balanceList = balanceRepository.findAll();
return balanceList.stream().filter(x -> {return x.getUserId().equals(userId);}
).collect(Collectors.toList()).get(0);
}catch (Exception e){
e.printStackTrace();
}finally {
// unlock
this.rwLock.readLock().unlock();
}
return null;
}

public void addBalance(AddBalanceDto addBalanceDto) {

Balance balance = new Balance();
balance.setUserId(addBalanceDto.getUserId());
balance.setBalance(addBalanceDto.getAmount());
balance.setCreateTime(new Date());
balance.setUpdateTime(new Date());
balanceRepository.save(balance);
// lock
this.rwLock.readLock().lock();
try{
Balance balance = new Balance();
balance.setUserId(addBalanceDto.getUserId());
balance.setBalance(addBalanceDto.getAmount());
balance.setCreateTime(new Date());
balance.setUpdateTime(new Date());
balanceRepository.save(balance);
}catch (Exception e){
e.printStackTrace();
}finally {
// unlock
this.rwLock.readLock().unlock();
}
}

public void topUpBalance(){

log.info(">>> (BalanceServiceRedisson) topUpBalance start ...");

// get lock
RLock lock = redissonClient.getLock("lock");
// // get lock
// RLock lock = redissonClient.getLock("lock");
// // lock
// lock.lock();

// lock
lock.lock();
this.rwLock.writeLock().lock();

try{
// V2 : Mysql
Expand All @@ -107,21 +136,24 @@ public void topUpBalance(){
e.printStackTrace();
}finally {
// unlock
lock.unlock();
this.rwLock.writeLock().unlock();
}
}

public void deductBalance(DeductBalanceDto deductBalanceDto) {

log.info(">>> (BalanceServiceRedisson) deductBalance start ...");

// get lock
//RLock lock = redissonClient.getLock("lock");
RReadWriteLock rwLock = redissonClient.getReadWriteLock("rwLock");
// // get lock
// //RLock lock = redissonClient.getLock("lock");
// RReadWriteLock rwLock = redissonClient.getReadWriteLock("rwLock");
//
// // lock
// //lock.lock();
// rwLock.writeLock();

// lock
//lock.lock();
rwLock.writeLock();
this.rwLock.writeLock().lock();

try{
// sleep 10 sec
Expand Down Expand Up @@ -149,7 +181,8 @@ public void deductBalance(DeductBalanceDto deductBalanceDto) {
}finally {
// unlock
//lock.unlock();
rwLock.writeLock().unlock();
//rwLock.writeLock().unlock();
this.rwLock.writeLock().unlock();
}
}

Expand Down

0 comments on commit 742f062

Please sign in to comment.