-
Notifications
You must be signed in to change notification settings - Fork 1
[DEBUG] 모임 삭제 시 채팅방/GCS/Redis 정리 보장 개선 #596
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
16 commits
Select commit
Hold shift + click to select a range
b480f76
improve: 모임 삭제 시 이벤트를 발행하도록 변경
Dimo-2562 22851ee
feat: PartyDeletedEvent 리스너 구현
Dimo-2562 d904049
feat: 실제 채팅 관련 삭제 로직 구현
Dimo-2562 08eb713
test: 모임 삭제 통합 테스트 강화 - 채팅 삭제 검증 로직 추가
Dimo-2562 0034609
refactor: 채팅 삭제 로직을 bulk update로 변경
Dimo-2562 132f875
feat: GCS 삭제 보장을 위해 Domain 추가
Dimo-2562 b43edf6
feat: 채팅방 삭제 시 ObjectStorage file을 Outbox 테이블에 넣도록 변경
Dimo-2562 95e1854
feat: GCS 삭제 프로세서 클래스 구현
Dimo-2562 ae36500
feat: ObjectStorage 삭제 스케쥴러 구현
Dimo-2562 9338fdd
chore: 테스트에서는 스케쥴러 안 돌도록 변경
Dimo-2562 68b34ea
test: GCS 삭제 관련 통합 테스트 작성
Dimo-2562 62c9b0f
refactor: Redis 삭제 실패는 TTL이 있으므로 정상 작동 흐름을 방해하지 않도록 변경
Dimo-2562 4673942
refactor: 불필요하면 추상화 레벨 제거
Dimo-2562 64bfbac
refactor: 불필요한 sql 쿼리 제거
Dimo-2562 6e9f3f7
refactor: redis 삭제는 after-commit으로 처리하도록 변경
Dimo-2562 d4a9575
feat: GCS 삭제 스케쥴러가 동시에 도는 상황을 고려하여 DB Claim으로 멱등성 보장
Dimo-2562 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
9 changes: 9 additions & 0 deletions
9
src/main/java/umc/cockple/demo/domain/chat/events/ChatRoomRedisCleanupEvent.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,9 @@ | ||
| package umc.cockple.demo.domain.chat.events; | ||
|
|
||
| public record ChatRoomRedisCleanupEvent( | ||
| Long chatRoomId | ||
| ) { | ||
| public static ChatRoomRedisCleanupEvent of(Long chatRoomId) { | ||
| return new ChatRoomRedisCleanupEvent(chatRoomId); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/umc/cockple/demo/domain/chat/events/ChatRoomRedisCleanupListener.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,39 @@ | ||
| package umc.cockple.demo.domain.chat.events; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
| import umc.cockple.demo.domain.chat.service.websocket.ChatListSubscriptionService; | ||
| import umc.cockple.demo.domain.chat.service.websocket.ChatRoomListCacheService; | ||
| import umc.cockple.demo.domain.chat.service.websocket.RedisSubscriptionService; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class ChatRoomRedisCleanupListener { | ||
|
|
||
| private final ChatRoomListCacheService chatRoomListCacheService; | ||
| private final RedisSubscriptionService redisSubscriptionService; | ||
| private final ChatListSubscriptionService chatListSubscriptionService; | ||
|
|
||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| @Async | ||
| public void handleChatRoomRedisCleanup(ChatRoomRedisCleanupEvent event) { | ||
| Long chatRoomId = event.chatRoomId(); | ||
| log.info("[채팅방 Redis 정리 시작] - chatRoomId: {}", chatRoomId); | ||
|
|
||
| try { | ||
| chatRoomListCacheService.evictLastMessage(chatRoomId); | ||
| } catch (Exception e) { | ||
| log.warn("[채팅방 Redis 정리] 마지막 메시지 캐시 best-effort 삭제 실패 - chatRoomId: {}", chatRoomId, e); | ||
| } | ||
|
|
||
| redisSubscriptionService.tryClearRoomSubscribers(chatRoomId); | ||
| chatListSubscriptionService.tryClearChatListSubscribers(chatRoomId); | ||
|
|
||
| log.info("[채팅방 Redis 정리 완료] - chatRoomId: {}", chatRoomId); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/umc/cockple/demo/domain/chat/events/PartyDeletedChatCleanupListener.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,22 @@ | ||
| package umc.cockple.demo.domain.chat.events; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.stereotype.Component; | ||
| import umc.cockple.demo.domain.chat.service.ChatRoomService; | ||
| import umc.cockple.demo.domain.party.events.PartyDeletedEvent; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class PartyDeletedChatCleanupListener { | ||
|
|
||
| private final ChatRoomService chatRoomService; | ||
|
|
||
| @EventListener | ||
| public void handlePartyDeleted(PartyDeletedEvent event) { | ||
| log.info("모임 삭제 이벤트 처리 - partyId: {}, deletedBy: {}", event.partyId(), event.deletedByMemberId()); | ||
| chatRoomService.deletePartyChatRoom(event.partyId()); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/umc/cockple/demo/domain/chat/repository/ChatFileRepository.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,7 +1,25 @@ | ||
| package umc.cockple.demo.domain.chat.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Modifying; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import umc.cockple.demo.domain.chat.domain.ChatMessageFile; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ChatFileRepository extends JpaRepository<ChatMessageFile, Long> { | ||
|
|
||
| @Query(""" | ||
| SELECT cmf.fileKey FROM ChatMessageFile cmf | ||
| WHERE cmf.chatMessage.chatRoom.id = :chatRoomId | ||
| """) | ||
| List<String> findObjectKeysByChatRoomId(@Param("chatRoomId") Long chatRoomId); | ||
|
|
||
| @Modifying | ||
| @Query(""" | ||
| DELETE FROM ChatMessageFile cmf | ||
| WHERE cmf.chatMessage.chatRoom.id = :chatRoomId | ||
| """) | ||
| int deleteByChatRoomId(@Param("chatRoomId") Long chatRoomId); | ||
| } |
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
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
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
93 changes: 93 additions & 0 deletions
93
src/main/java/umc/cockple/demo/domain/file/domain/ObjectStorageDeleteOutbox.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,93 @@ | ||
| package umc.cockple.demo.domain.file.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import umc.cockple.demo.domain.file.enums.ObjectStorageDeleteSourceType; | ||
| import umc.cockple.demo.domain.file.enums.ObjectStorageDeleteStatus; | ||
| import umc.cockple.demo.global.common.BaseEntity; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "object_storage_delete_outbox") | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class ObjectStorageDeleteOutbox extends BaseEntity { | ||
|
|
||
| private static final int LAST_ERROR_MAX_LENGTH = 2000; | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "object_key", nullable = false, length = 512) | ||
| private String objectKey; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(name = "source_type", nullable = false, length = 50) | ||
| private ObjectStorageDeleteSourceType sourceType; | ||
|
|
||
| @Column(name = "source_id") | ||
| private Long sourceId; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false, length = 20) | ||
| private ObjectStorageDeleteStatus status; | ||
|
|
||
| @Column(name = "retry_count", nullable = false) | ||
| private int retryCount; | ||
|
|
||
| @Column(name = "last_error", length = LAST_ERROR_MAX_LENGTH) | ||
| private String lastError; | ||
|
|
||
| @Column(name = "last_attempted_at") | ||
| private LocalDateTime lastAttemptedAt; | ||
|
|
||
| @Column(name = "claim_token", length = 36) | ||
| private String claimToken; | ||
|
|
||
| public static ObjectStorageDeleteOutbox pending(String objectKey, ObjectStorageDeleteSourceType sourceType, Long sourceId) { | ||
| return ObjectStorageDeleteOutbox.builder() | ||
| .objectKey(objectKey) | ||
| .sourceType(sourceType) | ||
| .sourceId(sourceId) | ||
| .status(ObjectStorageDeleteStatus.PENDING) | ||
| .retryCount(0) | ||
| .build(); | ||
| } | ||
|
|
||
| public void markDone() { | ||
| this.status = ObjectStorageDeleteStatus.DONE; | ||
| this.lastError = null; | ||
| this.lastAttemptedAt = LocalDateTime.now(); | ||
| this.claimToken = null; | ||
| } | ||
|
|
||
| public void markFailed(String errorMessage) { | ||
| this.status = ObjectStorageDeleteStatus.FAILED; | ||
| this.retryCount++; | ||
| this.lastError = truncate(errorMessage); | ||
| this.lastAttemptedAt = LocalDateTime.now(); | ||
| this.claimToken = null; | ||
| } | ||
|
|
||
| private String truncate(String value) { | ||
| if (value == null || value.length() <= LAST_ERROR_MAX_LENGTH) { | ||
| return value; | ||
| } | ||
| return value.substring(0, LAST_ERROR_MAX_LENGTH); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/umc/cockple/demo/domain/file/enums/ObjectStorageDeleteSourceType.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,5 @@ | ||
| package umc.cockple.demo.domain.file.enums; | ||
|
|
||
| public enum ObjectStorageDeleteSourceType { | ||
| PARTY_CHAT_ROOM | ||
| } |
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.