[FIX] 채팅방 lastMessage 및 읽음 처리 동시성 문제 해결 및 성능 개선#286
Conversation
Summary of ChangesHello @1000hyehyang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 이 PR은 채팅방의 lastMessage 업데이트와 메시지 읽음 처리 과정에서 발생할 수 있는 동시성 문제를 해결하고 전반적인 성능을 개선하는 데 중점을 둡니다. 조건부 업데이트 쿼리를 도입하여 데이터 무결성을 보장하고, 불필요한 데이터베이스 조회를 제거하여 시스템 효율성을 높였습니다. 또한, 코드 가독성과 유지보수성을 향상시키기 위한 리팩토링도 포함되어 있습니다. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
이 PR은 채팅방의 lastMessage 업데이트와 읽음 처리에서 발생할 수 있는 동시성 문제를 조건부 UPDATE 쿼리를 사용하여 효과적으로 해결하고, 불필요한 DB 조회를 제거하여 성능을 개선했습니다. ChatMessagesViewedEvent에 userId를 포함시켜 추가적인 사용자 조회를 없앤 점과, ChatRoomMemberRepository 및 ChatRoomRepository에 조건부 업데이트 메서드를 추가하여 데이터 일관성을 보장한 점이 특히 좋습니다. 또한, 업데이트된 행 수를 확인하여 예외 상황을 처리하고 로깅하는 방식은 시스템의 안정성과 디버깅 용이성을 높여줍니다. 전반적으로 코드의 견고성과 효율성을 크게 향상시킨 변경사항입니다.
| Long actorUserId, | ||
| LocalDateTime processedAt | ||
| LocalDateTime processedAt, | ||
| ChatProposalStatus applyStatus |
There was a problem hiding this comment.
현재 ChatApplyStatusNoticePayloadResponse에서 applyStatus 필드의 타입으로 ChatProposalStatus를 사용하고 있습니다. 하지만 이 DTO는 'Apply' 상태에 대한 알림을 위한 것이므로, 'Apply'와 관련된 별도의 ApplyStatus enum을 정의하여 사용하는 것이 더 적절하고 의미론적으로 명확합니다. ChatProposalStatus는 'Proposal'에 특화된 상태를 나타내므로, 'Apply' 컨텍스트에서는 혼란을 줄 수 있습니다.
| ChatProposalStatus applyStatus | |
| ChatApplyStatus applyStatus |
There was a problem hiding this comment.
@gemini-code-assist 비즈니스가 ProposalStatus를 공유해서 그래
There was a problem hiding this comment.
비즈니스 로직상 ProposalStatus를 공유하는 것이 맞다면, 현재 ChatProposalStatus를 사용하는 것이 타당합니다. 해당 필드가 'Apply'와 'Proposal' 두 가지 컨텍스트에서 모두 사용될 수 있다는 점을 고려하면 현재 구현은 비즈니스 요구사항에 부합합니다.
Summary
채팅방
lastMessage업데이트와 읽음 처리에서 발생할 수 있는 Race Condition을 조건부 UPDATE로 해결하고, 불필요한 DB 조회를 제거하여 성능을 개선했습니다.Changes
1. 동시성 문제 해결
채팅방 lastMessage 업데이트 Race Condition 해결
ChatRoomRepository.updateLastMessageIfNewer()추가 (조건부 UPDATE)WHERE last_message_at IS NULL OR last_message_at < :newLastMessageAt조건으로 더 최신 메시지일 때만 업데이트읽음 처리 동시성 문제 해결
ChatRoomMemberRepository.updateLastReadMessageIfNewer()추가 (조건부 UPDATE)WHERE last_read_message_id IS NULL OR last_read_message_id < :newId조건으로 더 큰 메시지 ID일 때만 업데이트2. 성능 개선
불필요한 조회 제거
ChatRoomUpdateServiceImpl.updateLastMessage():existsById()를updatedRows == 0일 때만 실행ChatRoomMemberCommandServiceImpl.updateLastReadMessage(): 성공 시에만 필요한 데이터 조회이벤트에 userId 포함하여 추가 조회 제거
ChatMessagesViewedEvent에userId필드 추가member.getUserId()를 이벤트에 포함findById()조회 제거 (SELECT 1회 제거)3. 코드 개선
@Modifying(clearAutomatically = true, flushAutomatically = true)추가로 영속성 컨텍스트 정합성 보장messageAtnull 방어 로직 추가Type of Change
Related Issues
Closes #285
참고 사항