-
Notifications
You must be signed in to change notification settings - Fork 3
[BE] 자동 롤백 기준을 liveness로 변경 #374
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 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c9cf87c
feat: 레디스와 의존성 설치
w0uldy0u 7f5943b
feat: redis 연결 설정
w0uldy0u 64318ba
feat: redis 템플릿 생성
w0uldy0u 727194e
feat: RedisStreamStore 구현
w0uldy0u e6f0d33
chore: redis key 수정 및 ttl 단축
w0uldy0u 9ad0cd0
feat: stage 환경 redis 추가
w0uldy0u f99814d
feat: update 프레임 확인 및 redis append
w0uldy0u 7ebee7e
chore: 불필요한 어노테이션 제거
w0uldy0u 89afedd
Merge branch 'dev' into feat/#356/use_redis_to_save_yjs_update
w0uldy0u 558f681
test: 테스트 코드 작성
w0uldy0u 1f94943
test: 테스트 displayName 작성
w0uldy0u 4f32cbe
chore: liveness readiness 의존성 설정
w0uldy0u da01bd5
chore: healthcheck은 liveness로
w0uldy0u ebf4fe8
feat: Redis append를 별도 스레드로 분리
w0uldy0u b151b3e
Merge branch 'dev' into feat/#356/use_redis_to_save_yjs_update
w0uldy0u ca79f40
Merge branch 'dev' into chore/#366/auto_rollback_using_liveness
w0uldy0u 0fb4fa5
chore: 리뷰 반영
w0uldy0u cca1218
Merge branch 'feat/#356/use_redis_to_save_yjs_update' of https://gith…
w0uldy0u 6eec06c
Merge branch 'feat/#356/use_redis_to_save_yjs_update' into chore/#366…
w0uldy0u fecb16a
Merge branch 'dev' into chore/#366/auto_rollback_using_liveness
w0uldy0u b40ee42
Merge branch 'dev' into chore/#366/auto_rollback_using_liveness
w0uldy0u 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
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
12 changes: 0 additions & 12 deletions
12
backend/api/src/main/java/com/yat2/episode/collaboration/RedisStreamRepository.java
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
backend/api/src/main/java/com/yat2/episode/collaboration/RedisStreamStore.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,41 @@ | ||
| package com.yat2.episode.collaboration; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.redis.connection.stream.MapRecord; | ||
| import org.springframework.data.redis.connection.stream.RecordId; | ||
| import org.springframework.data.redis.connection.stream.StreamRecords; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.data.redis.core.StreamOperations; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RedisStreamStore { | ||
|
|
||
| private final RedisTemplate<String, byte[]> redisBinaryTemplate; | ||
|
|
||
| private static final String FIELD_UPDATE = "u"; | ||
|
|
||
| private String streamKey(UUID roomId) { | ||
| return "collab:room:" + roomId; | ||
| } | ||
|
|
||
| public RecordId appendUpdate(UUID roomId, byte[] update) { | ||
| String key = streamKey(roomId); | ||
|
|
||
| StreamOperations<String, String, byte[]> ops = redisBinaryTemplate.opsForStream(); | ||
|
|
||
| MapRecord<String, String, byte[]> record = | ||
| StreamRecords.newRecord().in(key).ofMap(Map.of(FIELD_UPDATE, update)); | ||
|
|
||
| RecordId id = ops.add(record); | ||
|
|
||
| redisBinaryTemplate.expire(key, Duration.ofDays(2)); | ||
w0uldy0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return id; | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
backend/api/src/main/java/com/yat2/episode/collaboration/YjsProtocolUtil.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,20 @@ | ||
| package com.yat2.episode.collaboration; | ||
|
|
||
| import lombok.experimental.UtilityClass; | ||
|
|
||
| @UtilityClass | ||
| public final class YjsProtocolUtil { | ||
| public static final int MSG_SYNC = 0; | ||
|
|
||
| /* 추후 sync 라우팅을 위함 */ | ||
| public static final int SYNC_STEP_1 = 0; | ||
| public static final int SYNC_STEP_2 = 1; | ||
| public static final int SYNC_UPDATE = 2; | ||
|
|
||
| public static boolean isUpdateFrame(byte[] payload) { | ||
| if (payload == null || payload.length < 3) { | ||
| return false; | ||
| } | ||
| return payload[0] == MSG_SYNC && payload[1] == SYNC_UPDATE; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
backend/api/src/main/java/com/yat2/episode/global/config/RedisConfig.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,31 @@ | ||
| package com.yat2.episode.global.config; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.data.redis.serializer.RedisSerializer; | ||
| import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| public class RedisConfig { | ||
|
|
||
| @Bean | ||
| public RedisTemplate<String, byte[]> redisBinaryTemplate( | ||
| RedisConnectionFactory connectionFactory | ||
| ) { | ||
| RedisTemplate<String, byte[]> template = new RedisTemplate<>(); | ||
| template.setConnectionFactory(connectionFactory); | ||
|
|
||
| template.setKeySerializer(new StringRedisSerializer()); | ||
| template.setHashKeySerializer(new StringRedisSerializer()); | ||
|
|
||
| template.setValueSerializer(RedisSerializer.byteArray()); | ||
| template.setHashValueSerializer(RedisSerializer.byteArray()); | ||
|
|
||
| template.afterPropertiesSet(); | ||
| return template; | ||
| } | ||
| } |
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
176 changes: 176 additions & 0 deletions
176
backend/api/src/test/java/com/yat2/episode/collaboration/CollaborationServiceTest.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,176 @@ | ||
| package com.yat2.episode.collaboration; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.web.socket.BinaryMessage; | ||
| import org.springframework.web.socket.WebSocketSession; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| import com.yat2.episode.global.constant.AttributeKeys; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatCode; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
| import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| @DisplayName("CollaborationService 단위 테스트") | ||
| class CollaborationServiceTest { | ||
|
|
||
| @Mock | ||
| SessionRegistry sessionRegistry; | ||
|
|
||
| @Mock | ||
| RedisStreamStore redisStreamStore; | ||
|
|
||
| CollaborationService service; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| service = new CollaborationService(sessionRegistry, redisStreamStore); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("세션 연결/해제") | ||
| class ConnectionTests { | ||
|
|
||
| @Test | ||
| @DisplayName("연결 시 room에 세션을 등록한다") | ||
| void handleConnect_addsSessionToRoom() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession session = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(session.getAttributes()).thenReturn(attrs); | ||
|
|
||
| service.handleConnect(session); | ||
|
|
||
| verify(sessionRegistry).addSession(roomId, session); | ||
| verifyNoMoreInteractions(sessionRegistry); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("해제 시 room에서 세션을 제거한다") | ||
| void handleDisconnect_removesSessionFromRoom() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession session = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(session.getAttributes()).thenReturn(attrs); | ||
|
|
||
| service.handleDisconnect(session); | ||
|
|
||
| verify(sessionRegistry).removeSession(roomId, session); | ||
| verifyNoMoreInteractions(sessionRegistry); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("메시지 처리") | ||
| class MessageTests { | ||
|
|
||
| @Test | ||
| @DisplayName("항상 브로드캐스트한다") | ||
| void processMessage_alwaysBroadcasts() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession sender = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(sender.getAttributes()).thenReturn(attrs); | ||
|
|
||
| byte[] frame = new byte[]{ 9, 9, 9 }; | ||
| BinaryMessage message = new BinaryMessage(frame); | ||
|
|
||
| service.processMessage(sender, message); | ||
|
|
||
| ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class); | ||
| verify(sessionRegistry).broadcast(eq(roomId), eq(sender), payloadCaptor.capture()); | ||
|
|
||
| assertArrayEquals(frame, payloadCaptor.getValue()); | ||
| verifyNoMoreInteractions(sessionRegistry); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Update 프레임이면 Redis에 저장한다") | ||
| void processMessage_whenUpdateFrame_appendsToRedis() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession sender = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(sender.getAttributes()).thenReturn(attrs); | ||
|
|
||
| byte[] frame = new byte[]{ 0, 2, 1, 2, 3, 4 }; | ||
| BinaryMessage message = new BinaryMessage(frame); | ||
|
|
||
| service.processMessage(sender, message); | ||
|
|
||
| ArgumentCaptor<byte[]> broadcastCaptor = ArgumentCaptor.forClass(byte[].class); | ||
| verify(sessionRegistry).broadcast(eq(roomId), eq(sender), broadcastCaptor.capture()); | ||
| assertArrayEquals(frame, broadcastCaptor.getValue()); | ||
|
|
||
| ArgumentCaptor<byte[]> redisCaptor = ArgumentCaptor.forClass(byte[].class); | ||
| verify(redisStreamStore).appendUpdate(eq(roomId), redisCaptor.capture()); | ||
| assertArrayEquals(frame, redisCaptor.getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Update 프레임이 아니면 Redis에 저장하지 않는다") | ||
| void processMessage_whenNotUpdateFrame_doesNotAppendToRedis() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession sender = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(sender.getAttributes()).thenReturn(attrs); | ||
|
|
||
| byte[] frame = new byte[]{ 0, 1, 9, 9 }; | ||
| BinaryMessage message = new BinaryMessage(frame); | ||
|
|
||
| service.processMessage(sender, message); | ||
|
|
||
| verify(sessionRegistry).broadcast(eq(roomId), eq(sender), any(byte[].class)); | ||
| verifyNoInteractions(redisStreamStore); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Redis 저장 중 예외가 발생해도 처리 흐름이 죽지 않는다") | ||
| void processMessage_whenRedisThrows_doesNotCrash() { | ||
| UUID roomId = UUID.randomUUID(); | ||
| WebSocketSession sender = mock(WebSocketSession.class); | ||
|
|
||
| Map<String, Object> attrs = new HashMap<>(); | ||
| attrs.put(AttributeKeys.MINDMAP_ID, roomId); | ||
| when(sender.getAttributes()).thenReturn(attrs); | ||
|
|
||
| byte[] frame = new byte[]{ 0, 2, 1, 2, 3 }; | ||
| BinaryMessage message = new BinaryMessage(frame); | ||
|
|
||
| doThrow(new RuntimeException("redis down")).when(redisStreamStore) | ||
| .appendUpdate(eq(roomId), any(byte[].class)); | ||
|
|
||
| assertThatCode(() -> service.processMessage(sender, message)).doesNotThrowAnyException(); | ||
|
|
||
| verify(sessionRegistry).broadcast(eq(roomId), eq(sender), any(byte[].class)); | ||
| verify(redisStreamStore).appendUpdate(eq(roomId), any(byte[].class)); | ||
| } | ||
| } | ||
| } |
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.