-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] Stock 도메인 Redisson 분산 락 기반 재고 감소 동시성 처리 적용 #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
94d20ff
chore(config) : 테스트 시 Config Server 로드 비활성화를 위해 optional:configserver…
hellonaeunkim 5f29430
test(config): 테스트 yml에 .env import 추가 및 Config/Eureka 비활성화 설정 적용
hellonaeunkim fa67b13
test: Product 도메인 테스트 편의성을 위한 Fixture 클래스 추가
hellonaeunkim 13cfe95
test: Stock 도메인 테스트 편의성을 위한 Fixture 클래스 추가
hellonaeunkim cd1478c
test(stock): StockServiceImpl 재고 감소 통합 테스트 추가
hellonaeunkim 2cf5cf2
feat(stock) : Redisson 의존성 및 Redis 설정 추가
hellonaeunkim 5098711
feat(stock) : RedissonClient 설정 추가
hellonaeunkim 1361009
feat(stock): 재고 감소 락 인터페이스 추가
hellonaeunkim 09b243f
feat(stock): Redisson 분산 락 구현체 추가
hellonaeunkim ab1fff5
test(stock) : 재고 감소 동시 요청 테스트 추가
hellonaeunkim c6343cb
feat(stock): 재고 감소 트랜잭션 분리로 락 해제 순서 개선
hellonaeunkim 07a2889
feat(stock): 락 획득 실패 시 제한적 재시도 적용
hellonaeunkim 0578fca
test(stock) : 테스트용 Redis yml 추가
hellonaeunkim 71ac6e2
chore : 코드 포맷팅(spotlessApply) 적용
hellonaeunkim f9d1498
fix : Docker 환경의 Redis 설정 변수명 일치
hellonaeunkim 2921668
fix : prod 및 test 환경별 Config Server 연결 설정
hellonaeunkim b803d7f
chore : 코드 포맷팅(spotlessApply) 적용
hellonaeunkim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
product/src/main/java/com/hubEleven/stock/application/port/StockLockManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.hubEleven.stock.application.port; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| public interface StockLockManager { | ||
|
|
||
| <T> T executeWithLock(String lockKey, Supplier<T> supplier); | ||
| } |
40 changes: 40 additions & 0 deletions
40
product/src/main/java/com/hubEleven/stock/application/service/StockDecreaseProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.hubEleven.stock.application.service; | ||
|
|
||
| import static com.hubEleven.product.domain.exception.ProductErrorCode.PRODUCT_NOT_FOUND; | ||
| import static com.hubEleven.stock.domain.exception.StockErrorCode.STOCK_NOT_FOUND; | ||
|
|
||
| import com.commonLib.common.exception.GlobalException; | ||
| import com.hubEleven.product.domain.model.Product; | ||
| import com.hubEleven.product.domain.repository.ProductRepository; | ||
| import com.hubEleven.stock.application.dto.StockResult; | ||
| import com.hubEleven.stock.domain.model.Stock; | ||
| import com.hubEleven.stock.domain.repository.StockRepository; | ||
| import com.hubEleven.stock.presentation.dto.request.StockRequests; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class StockDecreaseProcessor { | ||
|
|
||
| private final StockRepository stockRepository; | ||
| private final ProductRepository productRepository; | ||
|
|
||
| @Transactional | ||
| public StockResult decrease(StockRequests.Decrease request) { | ||
| Product product = | ||
| productRepository | ||
| .findByIdNotDeleted(request.productId()) | ||
| .orElseThrow(() -> new GlobalException(PRODUCT_NOT_FOUND)); | ||
|
|
||
| Stock stock = | ||
| stockRepository | ||
| .findByProductId(request.productId()) | ||
| .orElseThrow(() -> new GlobalException(STOCK_NOT_FOUND)); | ||
|
|
||
| stock.decreaseQuantity(request.quantity()); | ||
|
|
||
| return StockResult.from(stock, product.getName()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
product/src/main/java/com/hubEleven/stock/infrastructure/configuration/RedissonConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.hubEleven.stock.infrastructure.configuration; | ||
|
|
||
| import org.redisson.Redisson; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.redisson.config.Config; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class RedissonConfig { | ||
|
|
||
| @Bean(destroyMethod = "shutdown") | ||
| public RedissonClient redissonClient( | ||
| @Value("${spring.data.redis.host:localhost}") String host, | ||
| @Value("${spring.data.redis.port:6379}") int port) { | ||
| Config config = new Config(); | ||
| config.useSingleServer().setAddress("redis://" + host + ":" + port); | ||
| return Redisson.create(config); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
product/src/main/java/com/hubEleven/stock/infrastructure/lock/RedissonStockLockManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.hubEleven.stock.infrastructure.lock; | ||
|
|
||
| import static com.hubEleven.stock.domain.exception.StockErrorCode.STOCK_LOCK_TIMEOUT; | ||
|
|
||
| import com.commonLib.common.exception.GlobalException; | ||
| import com.hubEleven.stock.application.port.StockLockManager; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Supplier; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.redisson.api.RLock; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RedissonStockLockManager implements StockLockManager { | ||
|
|
||
| private static final int MAX_RETRY_COUNT = 3; | ||
| private static final long WAIT_TIME_SECONDS = 3L; | ||
| private static final long LEASE_TIME_SECONDS = 5L; | ||
| private static final long RETRY_BACKOFF_MILLIS = 100L; | ||
|
|
||
| private final RedissonClient redissonClient; | ||
|
|
||
| @Override | ||
| public <T> T executeWithLock(String lockKey, Supplier<T> supplier) { | ||
| RLock lock = redissonClient.getLock(lockKey); | ||
| boolean locked = false; | ||
|
|
||
| try { | ||
| for (int retryCount = 0; retryCount < MAX_RETRY_COUNT; retryCount++) { | ||
| locked = lock.tryLock(WAIT_TIME_SECONDS, LEASE_TIME_SECONDS, TimeUnit.SECONDS); | ||
| if (locked) { | ||
| return supplier.get(); | ||
| } | ||
|
|
||
| Thread.sleep(RETRY_BACKOFF_MILLIS); | ||
| } | ||
|
|
||
| throw new GlobalException(STOCK_LOCK_TIMEOUT); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new GlobalException(STOCK_LOCK_TIMEOUT); | ||
| } finally { | ||
| if (locked && lock.isHeldByCurrentThread()) { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
product/src/test/java/com/hubEleven/product/stock/application/fixtures/ProductFixture.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.hubEleven.product.stock.application.fixtures; | ||
|
|
||
| import com.hubEleven.product.domain.model.Product; | ||
| import java.util.UUID; | ||
|
|
||
| public class ProductFixture { | ||
|
|
||
| // ===== ID ===== | ||
|
|
||
| public static final UUID COMPANY_ID = UUID.randomUUID(); | ||
|
|
||
| public static final UUID HUB_ID = UUID.randomUUID(); | ||
|
|
||
| // ===== Factory Methods ===== | ||
|
|
||
| public static Product createDefault() { | ||
| return Product.create("Default Product", COMPANY_ID, HUB_ID); | ||
| } | ||
|
|
||
| private ProductFixture() {} | ||
| } |
21 changes: 21 additions & 0 deletions
21
product/src/test/java/com/hubEleven/product/stock/application/fixtures/StockFixture.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.hubEleven.product.stock.application.fixtures; | ||
|
|
||
| import com.hubEleven.product.domain.model.Product; | ||
| import com.hubEleven.stock.domain.model.Stock; | ||
| import com.hubEleven.stock.presentation.dto.request.StockRequests; | ||
|
|
||
| public class StockFixture { | ||
|
|
||
| // ===== Factory Methods ===== | ||
|
|
||
| public static Stock createFromProductWithQuantity(Product product, int quantity) { | ||
| return Stock.create( | ||
| product.getProductId(), product.getCompanyId(), product.getHubId(), quantity); | ||
| } | ||
|
|
||
| public static StockRequests.Decrease decreaseRequest(Product product, int quantity) { | ||
| return new StockRequests.Decrease(product.getProductId(), quantity); | ||
| } | ||
|
|
||
| private StockFixture() {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.