-
Notifications
You must be signed in to change notification settings - Fork 0
refactor/24, 25 - 트랜잭션 아웃박스 패턴 적용 및 스케줄러 구현 #32
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
6 commits
Select commit
Hold shift + click to select a range
87c5b3c
refactor: 트랜잭션 아웃박스 패턴 적용 및 스케줄러 구현
ji-circle ea2d512
fix: 인프라 의존성 제거
ji-circle 46e92e3
fix: 영구 실패 이벤트의 무한 재시도 방지 메커니즘 추가, CompletableFuture 기반 비동기 처리, 로그 메시…
ji-circle e878bfd
fix: FAILED 상태 -> PUBLISHED로 역전 방지, DLT 준비
ji-circle 8f7f9c7
fix: 재시도 설정 수정, fail-fast
ji-circle f290957
fix: 로그 내용 수정
ji-circle 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
98 changes: 98 additions & 0 deletions
98
src/main/java/com/michelet/inventory/application/InventoryOutboxHelper.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,98 @@ | ||
| package com.michelet.inventory.application; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.michelet.inventory.domain.model.InventoryOutbox; | ||
| import com.michelet.inventory.domain.model.OutboxStatus; | ||
| import com.michelet.inventory.domain.repository.InventoryOutboxRepository; | ||
| import jakarta.annotation.PostConstruct; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class InventoryOutboxHelper { | ||
|
|
||
| private final InventoryOutboxRepository outboxRepository; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| @Value("${inventory.outbox.max-retries:3}") | ||
| private int maxRetries; | ||
|
|
||
| // 시작 시점에 maxRetries 값 검증 | ||
| @PostConstruct | ||
| public void validateMaxRetries() { | ||
| if (maxRetries < 1) { | ||
| throw new IllegalStateException("inventory.outbox.max-retries 설정 오류: 반드시 1 이상이어야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| // MANDATORY로 변경하여 부모 트랜잭션이 없으면 즉각 실패하도록 원자성 강제 | ||
| @Transactional(propagation = Propagation.MANDATORY) | ||
| public void append(String aggregateType, String aggregateId, String eventType, Object payloadObj) { | ||
| if (aggregateType == null || aggregateType.isBlank()) { | ||
| throw new IllegalArgumentException("aggregateType은 필수입니다."); | ||
| } | ||
| if (aggregateId == null || aggregateId.isBlank()) { | ||
| throw new IllegalArgumentException("aggregateId는 필수입니다."); | ||
| } | ||
| if (eventType == null || eventType.isBlank()) { | ||
| throw new IllegalArgumentException("eventType은 필수입니다."); | ||
| } | ||
| if (payloadObj == null) { | ||
| throw new IllegalArgumentException("payloadObj는 필수입니다."); | ||
| } | ||
|
|
||
| try { | ||
| String payloadJson = objectMapper.writeValueAsString(payloadObj); | ||
| InventoryOutbox outbox = InventoryOutbox.builder() | ||
| .aggregateType(aggregateType) | ||
| .aggregateId(aggregateId) | ||
| .eventType(eventType) | ||
| .payload(payloadJson) | ||
| .build(); | ||
| outboxRepository.save(outbox); | ||
| log.info("[Inventory Outbox] 이벤트 적재 요청: type={}, id={}", eventType, aggregateId); | ||
| } catch (JsonProcessingException e) { | ||
| log.error("Outbox 페이로드 직렬화 실패. aggregateId={}, eventType={}", aggregateId, eventType, e); | ||
| throw new RuntimeException("Outbox 이벤트 생성 중 오류가 발생했습니다.", e); | ||
| } | ||
| } | ||
|
|
||
| // 2단계 스케줄러에서 상태 업데이트 시 사용할 독립 트랜잭션 메서드 | ||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markAsPublished(UUID outboxId) { | ||
| outboxRepository.findById(outboxId).ifPresentOrElse( | ||
| outbox -> { | ||
| if (outbox.getStatus() != OutboxStatus.INIT) { | ||
| return; | ||
| } | ||
| outbox.markAsPublished(); | ||
| outboxRepository.save(outbox); | ||
| }, | ||
| () -> log.warn("[Inventory Outbox] 상태 변경 대상이 없습니다. id={}", outboxId) | ||
| ); | ||
| } | ||
|
|
||
| // 비동기 실패 시 재시도 횟수 및 상태 관리 로직 | ||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void handleFailure(UUID outboxId) { | ||
| outboxRepository.findById(outboxId).ifPresent(outbox -> { | ||
| if (outbox.getStatus() != OutboxStatus.INIT) { | ||
| return; | ||
| } | ||
| outbox.incrementRetryCount(); | ||
| if (outbox.getRetryCount() >= maxRetries) { // 3번 이상 실패 시 영구 실패 처리 | ||
| outbox.markAsFailed(); | ||
| log.error("[CRITICAL] Outbox 발행 영구 실패. 수동 확인 요망! id={}", outboxId); | ||
| } | ||
| outboxRepository.save(outbox); | ||
| }); | ||
| } | ||
| } | ||
141 changes: 141 additions & 0 deletions
141
src/main/java/com/michelet/inventory/application/InventoryOutboxScheduler.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,141 @@ | ||
| package com.michelet.inventory.application; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.michelet.inventory.application.dto.DailyStockResetEvent; | ||
| import com.michelet.inventory.application.dto.ProductCreatedEvent; | ||
| import com.michelet.inventory.application.dto.ProductStatusChangedEvent; | ||
| import com.michelet.inventory.application.dto.ProductUpdatedEvent; | ||
| import com.michelet.inventory.application.dto.StockReservedEvent; | ||
| import com.michelet.inventory.application.dto.StockRestoredEvent; | ||
| import com.michelet.inventory.domain.model.InventoryOutbox; | ||
| import com.michelet.inventory.domain.model.OutboxStatus; | ||
| import com.michelet.inventory.domain.repository.InventoryOutboxRepository; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.orm.ObjectOptimisticLockingFailureException; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class InventoryOutboxScheduler { | ||
|
|
||
| private final InventoryOutboxRepository outboxRepository; | ||
| private final InventoryOutboxHelper outboxHelper; | ||
| private final KafkaTemplate<String, Object> kafkaTemplate; | ||
|
|
||
| // JSON 문자열을 객체로 복원하기 위한 매퍼 주입 | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| // 문자열 상수 추출 | ||
| private static final String EVENT_PRODUCT_CREATED = "PRODUCT_CREATED"; | ||
| private static final String EVENT_PRODUCT_UPDATED = "PRODUCT_UPDATED"; | ||
| private static final String EVENT_STATUS_CHANGED = "PRODUCT_STATUS_CHANGED"; | ||
| private static final String EVENT_STOCK_RESERVED = "STOCK_RESERVED"; | ||
| private static final String EVENT_STOCK_RESTORED = "STOCK_RESTORED"; | ||
| private static final String EVENT_DAILY_RESET = "DAILY_STOCK_RESET"; | ||
|
|
||
| @Value("${inventory.kafka.topic.product-created:product.created}") | ||
| private String topicProductCreated; | ||
| @Value("${inventory.kafka.topic.product-updated:product.updated}") | ||
| private String topicProductUpdated; | ||
| @Value("${inventory.kafka.topic.status-changed:product.status-changed}") | ||
| private String topicStatusChanged; | ||
| @Value("${inventory.kafka.topic.reserved:stock.reserved}") | ||
| private String topicStockReserved; | ||
| @Value("${inventory.kafka.topic.restored:stock.restored}") | ||
| private String topicStockRestored; | ||
| @Value("${inventory.kafka.topic.daily-reset:stock.daily-reset}") | ||
| private String topicDailyReset; | ||
|
|
||
| @Scheduled(fixedDelay = 5000) | ||
| public void processOutboxEvents() { | ||
| // 1. OOM 방지 및 순서 보장을 위해 Top N 배치 조회 | ||
| List<InventoryOutbox> pendingEvents = outboxRepository.findTop50ByStatusOrderByCreatedAtAsc(OutboxStatus.INIT); | ||
| if (pendingEvents.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| log.info("[Inventory Outbox Scheduler] {}개의 미발행 이벤트를 찾아 Kafka 전송을 시도합니다.", pendingEvents.size()); | ||
|
|
||
| for (InventoryOutbox event : pendingEvents) { | ||
| try { | ||
| String topic = resolveTopic(event.getEventType()); | ||
|
|
||
| // String(JSON)을 다시 원본 Event 객체로 복원 | ||
| Object originalEventObject = deserializePayload(event.getEventType(), event.getPayload()); | ||
|
|
||
| // 블로킹(.get) 제거 -> 비동기 발송 콜백(.whenComplete) 적용 | ||
| kafkaTemplate.send(topic, event.getAggregateId(), originalEventObject) | ||
| .whenComplete((result, ex) -> { | ||
| if (ex == null) { | ||
| try { | ||
| outboxHelper.markAsPublished(event.getId()); | ||
| log.info("[Inventory Outbox Scheduler] 이벤트 발행 성공! Outbox ID: {}", event.getId()); | ||
| } catch (ObjectOptimisticLockingFailureException oole) { | ||
| log.info("[Inventory Outbox Scheduler] 낙관적 락 방어 (동시성 경합). Outbox ID: {}", | ||
| event.getId()); | ||
| } catch (Exception updateEx) { | ||
| log.error("[Inventory Outbox Scheduler] DB 상태 업데이트 실패. Outbox ID: {}", event.getId(), | ||
| updateEx); | ||
| } | ||
| } else { | ||
| log.error("[Inventory Outbox Scheduler] 카프카 이벤트 발행 실패. Outbox ID: {}", event.getId(), ex); | ||
| safeHandleFailure(event.getId()); | ||
| } | ||
| }); | ||
|
|
||
| } catch (Exception e) { | ||
| // 역직렬화 실패, 토픽 변환 실패 등 무한 에러 유발 시 | ||
| log.error("[Inventory Outbox Scheduler] 이벤트 전송 준비 중 예외 발생. Outbox ID: {}", event.getId(), e); | ||
| safeHandleFailure(event.getId()); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| // 재시도 횟수 처리 및 상태 변경을 돕는 실패 처리 메서드 | ||
| private void safeHandleFailure(UUID eventId) { | ||
| try { | ||
| outboxHelper.handleFailure(eventId); | ||
| } catch (ObjectOptimisticLockingFailureException oole) { | ||
| log.info("[Inventory Outbox Scheduler] 실패 마킹 중 낙관적 락 방어. Outbox ID: {}", eventId); | ||
| } catch (Exception e) { | ||
| log.error("[Inventory Outbox Scheduler] 실패 상태 업데이트 중 예외 발생. Outbox ID: {}", eventId, e); | ||
| } | ||
| } | ||
|
|
||
| // 이벤트 타입에 따른 발행 토픽 라우팅 | ||
| // JSON 문자열을 원래 DTO 클래스로 변환 | ||
| private Object deserializePayload(String eventType, String jsonPayload) throws Exception { | ||
| return switch (eventType) { | ||
| case EVENT_PRODUCT_CREATED -> objectMapper.readValue(jsonPayload, ProductCreatedEvent.class); | ||
| case EVENT_PRODUCT_UPDATED -> objectMapper.readValue(jsonPayload, ProductUpdatedEvent.class); | ||
| case EVENT_STATUS_CHANGED -> objectMapper.readValue(jsonPayload, ProductStatusChangedEvent.class); | ||
| case EVENT_STOCK_RESERVED -> objectMapper.readValue(jsonPayload, StockReservedEvent.class); | ||
| case EVENT_STOCK_RESTORED -> objectMapper.readValue(jsonPayload, StockRestoredEvent.class); | ||
| case EVENT_DAILY_RESET -> objectMapper.readValue(jsonPayload, DailyStockResetEvent.class); | ||
| // 매핑 안 된 이벤트를 String으로 보내면 직렬화 에러 발생! 예외를 던져서 스케줄러 재시도 루프로 넘김 | ||
| default -> { | ||
| log.warn("등록되지 않은 알 수 없는 이벤트 타입입니다: {}", eventType); | ||
| throw new IllegalArgumentException("Unknown event type: " + eventType); | ||
| } | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private String resolveTopic(String eventType) { | ||
| return switch (eventType) { | ||
| case EVENT_PRODUCT_CREATED -> topicProductCreated; | ||
| case EVENT_PRODUCT_UPDATED -> topicProductUpdated; | ||
| case EVENT_STATUS_CHANGED -> topicStatusChanged; | ||
| case EVENT_STOCK_RESERVED -> topicStockReserved; | ||
| case EVENT_STOCK_RESTORED -> topicStockRestored; | ||
| case EVENT_DAILY_RESET -> topicDailyReset; | ||
| default -> "inventory.unknown.event"; | ||
| }; | ||
| } | ||
| } | ||
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.