-
Notifications
You must be signed in to change notification settings - Fork 0
fix: 재고 차감 시나리오 예외 처리 #12
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
24 commits
Select commit
Hold shift + click to select a range
ccb0bd0
feat: Consumer 재시도 전략 ExponentialBackOffWithJitter 클래스 구현 및 적용 (#11)
sangyunpark99 6bf4a3a
test: Consumer 재시도 전략 ExponentialBackOffWithJitter 클래스 테스트 코드 구현 (#11)
sangyunpark99 87ad012
feat: Consumer 재시도 전략 ExponentialBackOffWithJitter 최대 시도 횟수 적용 (#11)
sangyunpark99 53174f2
test: Consumer 재시도 전략 ExponentialBackOffWithJitter 최대 시도 횟수 적용 test 구…
sangyunpark99 843919f
refactor: global 폴더로 ExponentialBackOffWithJitter 클래스 이동 (#11)
sangyunpark99 f1086ca
refactor: global 폴더로 ExponentialBackOffWithJitter 클래스 이동 (#11)
sangyunpark99 7172fd7
refactor: global 폴더로 ExponentialBackOffWithJitter 클래스 이동 (#11)
sangyunpark99 b03ddff
feat: Redis에 상품 key가 존재하는지 확인하는 메서드 구현 (#11)
sangyunpark99 f71e9da
feat: Redis에 상품 재고가 존재하는지 확인하는 메서드 추가 (#11)
sangyunpark99 d372ce5
test: Redis에 상품 재고가 존재하는지 확인하는 메서드로 인한 테스트 코드 수정 (#11)
sangyunpark99 60ebcd2
fix: Kafka Consumer Redis 복구방식 개선 (#11)
sangyunpark99 62463fd
chore: 불필요한 kafka-test impl 제거하기 (#11)
sangyunpark99 9714cdf
refactor: 사용하지 않는 StockRedisRepository import문 제거 (#11)
sangyunpark99 c2a6d22
test: kafka Producer Event 발행 순서 테스트 구현 (#11)
sangyunpark99 b9f4eb6
feat: producer 실패시 해주는 Redis 재고 복구 로직 제거 (#11)
sangyunpark99 8159c1e
test: 사용하지 않는 stockRedisRepoistory 주입 제거 (#11)
sangyunpark99 e86b1ca
test: 결과 비교 횟수 수정 (#11)
sangyunpark99 de3bac8
test: setIfAbsent 메서드 구현 (#11)
sangyunpark99 ba6e14f
feat: consumer에서 진행되던 재고 중복 확인 및 저장 로직 제거 (#11)
sangyunpark99 a8ce10f
test: java.util.Collections를 import로 변경 (#11)
sangyunpark99 bfea5ba
test: 사용하지 않는 stockRedisRepository 필드 변수 삭제 (#11)
sangyunpark99 e7f5674
test: testContainer Kafka 설정 변경 (#11)
sangyunpark99 fe1b5cc
feat: 이미 처리된 주문 로직 service단에서 검증 (#11)
sangyunpark99 1ce1566
test: 이미 처리된 주문 로직 service단에서 검증에 따른 테스트 코드 변경 (#11)
sangyunpark99 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
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
44 changes: 44 additions & 0 deletions
44
product/src/main/java/com/sangyunpark/product/global/ExponentialBackOffWithJitter.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,44 @@ | ||
| package com.sangyunpark.product.global; | ||
|
|
||
| import java.util.Random; | ||
| import org.springframework.util.backoff.BackOff; | ||
| import org.springframework.util.backoff.BackOffExecution; | ||
|
|
||
| public class ExponentialBackOffWithJitter implements BackOff { | ||
|
|
||
| private final double multiplier; | ||
| private final long maxElapsedTime; | ||
| private final int maxAttempts; | ||
|
|
||
| private long currentInterval; | ||
| private int attemptCount; | ||
| private long elapsedTime; | ||
| private final Random random = new Random(); | ||
|
|
||
| public ExponentialBackOffWithJitter(long initialInterval, double multiplier, long maxElapsedTime, int maxAttempts) { | ||
| this.maxAttempts= maxAttempts; | ||
| this.multiplier = multiplier; | ||
| this.maxElapsedTime = maxElapsedTime; | ||
| this.currentInterval = initialInterval; | ||
| this.elapsedTime = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public BackOffExecution start() { | ||
| return () -> { | ||
| if (elapsedTime >= maxElapsedTime || attemptCount >= maxAttempts) { | ||
| return BackOffExecution.STOP; | ||
| } | ||
|
|
||
| long next = currentInterval; | ||
| long jittered = (next > 0) ? random.nextLong(next + 1) : 0; | ||
|
|
||
| currentInterval = Math.min((long) (currentInterval * multiplier), Long.MAX_VALUE / 2); | ||
|
|
||
| elapsedTime += jittered; | ||
| attemptCount++; | ||
|
|
||
| return jittered; | ||
| }; | ||
| } | ||
| } |
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: 7 additions & 1 deletion
8
product/src/main/java/com/sangyunpark/product/infrastructure/kafka/dlt/StockDLTListener.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 |
|---|---|---|
| @@ -1,16 +1,22 @@ | ||
| package com.sangyunpark.product.infrastructure.kafka.dlt; | ||
|
|
||
| import com.sangyunpark.product.application.event.StockDeductedEvent; | ||
| import com.sangyunpark.product.infrastructure.redis.StockRedisRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class StockDLTListener { | ||
|
|
||
| private final StockRedisRepository stockRedisRepository; | ||
|
|
||
| @KafkaListener(topics = "stock.deducted.DLT", groupId = "product-service-dlt") | ||
| public void listenStockDLT(final StockDeductedEvent event) { | ||
| log.error("DLT 수신 - event: {}", event); | ||
| log.error("Dead Letter 수신 - 재고 복구 진행. event: {}", event); | ||
| stockRedisRepository.increase(event.productId(), event.quantity()); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
나중에 admin( 판매자가 사용하는 화면에서 재고 관리할때 영향을 주지 않게 설계해야함)