-
Notifications
You must be signed in to change notification settings - Fork 3
[BE] 동시편집 Redis 연동 #373
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
[BE] 동시편집 Redis 연동 #373
Changes from all commits
Commits
Show all changes
16 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 ebf4fe8
feat: Redis append를 별도 스레드로 분리
w0uldy0u b151b3e
Merge branch 'dev' into feat/#356/use_redis_to_save_yjs_update
w0uldy0u 0fb4fa5
chore: 리뷰 반영
w0uldy0u cca1218
Merge branch 'feat/#356/use_redis_to_save_yjs_update' of https://gith…
w0uldy0u 133768d
Merge branch 'dev' into feat/#356/use_redis_to_save_yjs_update
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
Some comments aren't visible on the classic Files Changed page.
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
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; | ||
| } | ||
w0uldy0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
...end/api/src/main/java/com/yat2/episode/collaboration/config/CollaborationAsyncConfig.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,46 @@ | ||
| package com.yat2.episode.collaboration.config; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
|
||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.RejectedExecutionHandler; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| public class CollaborationAsyncConfig { | ||
|
|
||
| @Bean(name = "redisExecutor") | ||
| public Executor redisExecutor() { | ||
| ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor(); | ||
| exec.setThreadNamePrefix("redis-append-"); | ||
| exec.setCorePoolSize(1); | ||
| exec.setMaxPoolSize(2); | ||
| exec.setQueueCapacity(10_000); | ||
| exec.setKeepAliveSeconds(30); | ||
w0uldy0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exec.setAllowCoreThreadTimeOut(true); | ||
|
|
||
| exec.setRejectedExecutionHandler(dropAndLogError()); | ||
w0uldy0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exec.initialize(); | ||
| return exec; | ||
| } | ||
|
|
||
| private RejectedExecutionHandler dropAndLogError() { | ||
| AtomicLong dropped = new AtomicLong(); | ||
| AtomicLong lastLogMs = new AtomicLong(0); | ||
| //TODO: Update가 drop 될 경우 Yjs Runner에게 알려 Sync 프로토콜로 복구 | ||
| return (r, executor) -> { | ||
| long n = dropped.incrementAndGet(); | ||
|
|
||
| long now = System.currentTimeMillis(); | ||
| long prev = lastLogMs.get(); | ||
| if (now - prev >= 1000 && lastLogMs.compareAndSet(prev, now)) { | ||
| int qSize = executor.getQueue().size(); | ||
| log.error("Redis queue full. Dropping tasks. dropped={}, queueSize={}", n, qSize); | ||
| } | ||
| }; | ||
| } | ||
w0uldy0u marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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
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.