-
Notifications
You must be signed in to change notification settings - Fork 0
[HSC-424] user-log admin dispatch outbox 저장 및 재처리 경로 구성 #280
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
2 commits
Select commit
Hold shift + click to select a range
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
30 changes: 28 additions & 2 deletions
30
...java/site/holliverse/customer/application/usecase/log/AdminLogFeatureDispatchService.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,23 +1,49 @@ | ||
| package site.holliverse.customer.application.usecase.log; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Service; | ||
| import site.holliverse.customer.integration.external.AdminLogFeaturesClient; | ||
| import site.holliverse.customer.persistence.entity.UserLogAdminDispatchOutbox; | ||
|
|
||
| /** | ||
| * admin log-feature 별도 executor로 분리 | ||
| */ | ||
| @Slf4j | ||
| @Service | ||
| @Profile("customer") | ||
| @RequiredArgsConstructor | ||
| public class AdminLogFeatureDispatchService { | ||
|
|
||
| private final AdminLogFeaturesClient adminLogFeaturesClient; | ||
| private final UserLogAdminDispatchOutboxStateService stateService; | ||
|
|
||
| @Async("adminLogFeatureTaskExecutor") | ||
| public void dispatch(Long memberId, UserLogEventName eventName, String timestamp) { | ||
| adminLogFeaturesClient.sendLogFeature(memberId, eventName, timestamp); | ||
| public void dispatch(Long eventId) { | ||
| stateService.get(eventId).ifPresentOrElse( | ||
| this::dispatchClaimedRow, | ||
| () -> log.warn("[UserLog][Outbox] claimed row not found eventId={}", eventId) | ||
| ); | ||
| } | ||
|
|
||
| private void dispatchClaimedRow(UserLogAdminDispatchOutbox row) { | ||
| try { | ||
| var result = adminLogFeaturesClient.sendLogFeature( | ||
| row.getMemberId(), | ||
| UserLogEventName.from(row.getEventName()), | ||
| row.getEventTimestamp().toString() | ||
| ); | ||
|
|
||
| if (result.success()) { | ||
| stateService.markAcked(row.getEventId()); | ||
| return; | ||
| } | ||
|
|
||
| stateService.markRetry(row.getEventId(), result.errorMessage()); | ||
| } catch (Exception e) { | ||
| stateService.markRetry(row.getEventId(), e.getClass().getSimpleName() + ":" + e.getMessage()); | ||
| } | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
...a/site/holliverse/customer/application/usecase/log/UserLogAdminDispatchOutboxService.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,74 @@ | ||
| package site.holliverse.customer.application.usecase.log; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.core.task.TaskRejectedException; | ||
| import org.springframework.dao.DataIntegrityViolationException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import site.holliverse.customer.persistence.entity.UserLogAdminDispatchOutbox; | ||
| import site.holliverse.customer.persistence.entity.UserLogDispatchStatus; | ||
| import site.holliverse.customer.persistence.repository.UserLogAdminDispatchOutboxRepository; | ||
| import site.holliverse.customer.web.dto.log.UserLogRequest; | ||
| import site.holliverse.shared.monitoring.CustomerMetrics; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @Profile("customer") | ||
| @RequiredArgsConstructor | ||
| public class UserLogAdminDispatchOutboxService { | ||
|
|
||
| private final UserLogAdminDispatchOutboxRepository repository; | ||
| private final UserLogAdminDispatchOutboxStateService stateService; | ||
| private final AdminLogFeatureDispatchService dispatchService; | ||
| private final ObjectMapper objectMapper; | ||
| private final CustomerMetrics customerMetrics; | ||
|
|
||
| @Transactional | ||
| public void enqueue(Long eventId, Long memberId, UserLogEventName eventName, UserLogRequest request) { | ||
| try { | ||
| UserLogPayload payload = new UserLogPayload( | ||
| eventId, | ||
| request.timestamp(), | ||
| request.event(), | ||
| eventName.value(), | ||
| memberId, | ||
| request.eventProperties() | ||
| ); | ||
|
|
||
| repository.saveAndFlush(UserLogAdminDispatchOutbox.builder() | ||
| .eventId(eventId) | ||
| .memberId(memberId) | ||
| .eventName(eventName.value()) | ||
| .eventType(request.event()) | ||
| .eventTimestamp(Instant.parse(request.timestamp())) | ||
| .payload(objectMapper.valueToTree(payload)) | ||
| .status(UserLogDispatchStatus.READY) | ||
| .attemptCount(0) | ||
| .build()); | ||
| customerMetrics.recordAdminLogFeatureOutbox("stored"); | ||
| } catch (DataIntegrityViolationException e) { | ||
| customerMetrics.recordAdminLogFeatureOutbox("duplicate"); | ||
| } catch (Exception e) { | ||
| customerMetrics.recordAdminLogFeatureOutbox("store_error"); | ||
| } | ||
| } | ||
|
|
||
| public void dispatchReadyBatch(int batchSize) { | ||
| List<Long> claimedIds = stateService.claimReadyBatch(batchSize); | ||
| for (Long eventId : claimedIds) { | ||
| try { | ||
| dispatchService.dispatch(eventId); | ||
| customerMetrics.recordAdminLogFeatureDispatch("enqueued"); | ||
| } catch (TaskRejectedException e) { | ||
| customerMetrics.recordAdminLogFeatureDispatch("rejected"); | ||
| stateService.markRetry(eventId, "dispatch_rejected: " + e.getClass().getSimpleName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
...e/holliverse/customer/application/usecase/log/UserLogAdminDispatchOutboxStateService.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,71 @@ | ||
| package site.holliverse.customer.application.usecase.log; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import site.holliverse.customer.persistence.entity.UserLogAdminDispatchOutbox; | ||
| import site.holliverse.customer.persistence.repository.UserLogAdminDispatchOutboxRepository; | ||
| import site.holliverse.shared.monitoring.CustomerMetrics; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @Service | ||
| @Profile("customer") | ||
| @RequiredArgsConstructor | ||
| public class UserLogAdminDispatchOutboxStateService { | ||
|
|
||
| private final UserLogAdminDispatchOutboxRepository repository; | ||
| private final CustomerMetrics customerMetrics; | ||
|
|
||
| @Value("${app.userlog.admin-dispatch.retry-delay-ms:10000}") | ||
| private long retryDelayMs; | ||
|
|
||
| @Value("${app.userlog.admin-dispatch.max-attempts:5}") | ||
| private int maxAttempts; | ||
|
|
||
| @Transactional | ||
| public List<Long> claimReadyBatch(int batchSize) { | ||
| List<Long> ids = repository.findReadyEventIdsForUpdate(batchSize); | ||
| if (ids.isEmpty()) { | ||
| return ids; | ||
| } | ||
|
|
||
| List<UserLogAdminDispatchOutbox> rows = repository.findAllById(ids); | ||
| rows.forEach(UserLogAdminDispatchOutbox::markProcessing); | ||
| repository.saveAll(rows); | ||
|
Comment on lines
+33
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return ids; | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public Optional<UserLogAdminDispatchOutbox> get(Long eventId) { | ||
| return repository.findById(eventId); | ||
| } | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markAcked(Long eventId) { | ||
| repository.findById(eventId).ifPresent(row -> { | ||
| row.markAcked(); | ||
| repository.save(row); | ||
| customerMetrics.recordAdminLogFeatureOutbox("acked"); | ||
| }); | ||
| } | ||
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void markRetry(Long eventId, String errorMessage) { | ||
| repository.findById(eventId).ifPresent(row -> { | ||
| if (row.getAttemptCount() + 1 >= maxAttempts) { | ||
| row.markDead(errorMessage); | ||
| customerMetrics.recordAdminLogFeatureOutbox("dead"); | ||
| } else { | ||
| row.markRetry(errorMessage, Instant.now().plusMillis(retryDelayMs)); | ||
| customerMetrics.recordAdminLogFeatureOutbox("retry"); | ||
| } | ||
| repository.save(row); | ||
| }); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
.../java/site/holliverse/customer/application/usecase/log/UserLogAdminDispatchScheduler.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,23 @@ | ||
| package site.holliverse.customer.application.usecase.log; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @Profile("customer") | ||
| @RequiredArgsConstructor | ||
| public class UserLogAdminDispatchScheduler { | ||
|
|
||
| private final UserLogAdminDispatchOutboxService outboxService; | ||
|
|
||
| @Value("${app.userlog.admin-dispatch.batch-size:100}") | ||
| private int batchSize; | ||
|
|
||
| @Scheduled(fixedDelayString = "${app.userlog.admin-dispatch.fixed-delay-ms:1000}") | ||
| public void dispatch() { | ||
| outboxService.dispatchReadyBatch(batchSize); | ||
| } | ||
| } |
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
88 changes: 88 additions & 0 deletions
88
src/main/java/site/holliverse/customer/persistence/entity/UserLogAdminDispatchOutbox.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,88 @@ | ||
| package site.holliverse.customer.persistence.entity; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.hibernate.annotations.CreationTimestamp; | ||
| import org.hibernate.annotations.JdbcTypeCode; | ||
| import org.hibernate.annotations.UpdateTimestamp; | ||
| import org.hibernate.type.SqlTypes; | ||
| import site.holliverse.shared.persistence.BaseEntity; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| @Entity | ||
| @Table(name = "user_log_admin_dispatch_outbox") | ||
| @Getter | ||
| @Builder | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class UserLogAdminDispatchOutbox extends BaseEntity { | ||
|
|
||
| @Id | ||
| @Column(name = "event_id", nullable = false) | ||
| private Long eventId; | ||
|
|
||
| @Column(name = "member_id", nullable = false) | ||
| private Long memberId; | ||
|
|
||
| @Column(name = "event_name", nullable = false, length = 200) | ||
| private String eventName; | ||
|
|
||
| @Column(name = "event_type", nullable = false, length = 100) | ||
| private String eventType; | ||
|
|
||
| @Column(name = "event_timestamp", nullable = false) | ||
| private Instant eventTimestamp; | ||
|
|
||
| @JdbcTypeCode(SqlTypes.JSON) | ||
| @Column(name = "payload", nullable = false, columnDefinition = "jsonb") | ||
| private JsonNode payload; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(name = "status", nullable = false, length = 20) | ||
| private UserLogDispatchStatus status; | ||
|
|
||
| @Column(name = "attempt_count", nullable = false) | ||
| private int attemptCount; | ||
|
|
||
| @Column(name = "next_retry_at") | ||
| private Instant nextRetryAt; | ||
|
|
||
| @Column(name = "last_error", columnDefinition = "TEXT") | ||
| private String lastError; | ||
|
|
||
| public void markProcessing() { | ||
| this.status = UserLogDispatchStatus.PROCESSING; | ||
| this.lastError = null; | ||
| } | ||
|
|
||
| public void markAcked() { | ||
| this.status = UserLogDispatchStatus.ACKED; | ||
| this.nextRetryAt = null; | ||
| this.lastError = null; | ||
| } | ||
|
|
||
| public void markRetry(String errorMessage, Instant nextRetryAt) { | ||
| this.status = UserLogDispatchStatus.RETRY; | ||
| this.attemptCount += 1; | ||
| this.nextRetryAt = nextRetryAt; | ||
| this.lastError = errorMessage; | ||
| } | ||
|
|
||
| public void markDead(String errorMessage) { | ||
| this.status = UserLogDispatchStatus.DEAD; | ||
| this.attemptCount += 1; | ||
| this.nextRetryAt = null; | ||
| this.lastError = errorMessage; | ||
| } | ||
| } |
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.
모든 예외(
Exception)를 catch하면서 메트릭만 기록하고 에러 로그를 남기지 않고 있습니다. DB 연결 오류나 직렬화 실패 등 예기치 못한 장애 발생 시 원인 파악이 매우 어려울 수 있습니다. 최소한 에러 메시지와 스택 트레이스를 로깅하도록 개선이 필요합니다.