-
Notifications
You must be signed in to change notification settings - Fork 0
[FIX] Redis 메모리 부족에 따른 Consumer 관련 데이터 삭제 해결 #98
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
8 commits
Select commit
Hold shift + click to select a range
235b5e7
feat: bound Redis stream retention
moonwhistle e671d99
fix: recover missing Redis consumer groups
moonwhistle 81838d4
chore: preserve PEL monitoring stack traces
moonwhistle 54d9cc8
infra: harden Redis stream deployment
moonwhistle e3bf221
fix: align service image ports
moonwhistle a647c2b
fix: restart consumer after fatal stream errors
moonwhistle b65de10
feat: monitor Redis stream retention risk
moonwhistle 208de4d
security: require Redis authentication in production
moonwhistle 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
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
25 changes: 25 additions & 0 deletions
25
.../coinflow-consumer-app/src/main/java/com/coinflow/config/ConsumerApplicationShutdown.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,25 @@ | ||
| package com.coinflow.config; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class ConsumerApplicationShutdown { | ||
|
|
||
| private final ConfigurableApplicationContext applicationContext; | ||
| private final AtomicBoolean shutdownRequested = new AtomicBoolean(false); | ||
|
|
||
| public void request() { | ||
| if (!shutdownRequested.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
|
|
||
| log.error("Closing consumer application after a fatal Redis Stream subscription error"); | ||
| applicationContext.close(); | ||
| } | ||
| } |
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
89 changes: 89 additions & 0 deletions
89
...nd/coinflow-consumer-app/src/main/java/com/coinflow/config/RedisConsumerGroupManager.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,89 @@ | ||
| package com.coinflow.config; | ||
|
|
||
| import com.coinflow.config.properties.TickConsumerProperties; | ||
| import java.nio.charset.StandardCharsets; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.redis.connection.stream.ReadOffset; | ||
| import org.springframework.data.redis.core.RedisCallback; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class RedisConsumerGroupManager { | ||
|
|
||
| private static final String ERROR_BUSY_GROUP = "BUSYGROUP"; | ||
| private static final String ERROR_NO_GROUP = "NOGROUP"; | ||
|
|
||
| private final RedisTemplate<String, String> redisTemplate; | ||
| private final TickConsumerProperties properties; | ||
| private final ConsumerApplicationShutdown applicationShutdown; | ||
|
|
||
| public void ensureConsumerGroup() { | ||
| try { | ||
| redisTemplate.execute((RedisCallback<String>) connection -> | ||
| connection.streamCommands().xGroupCreate( | ||
| raw(properties.streamKey()), | ||
| properties.group(), | ||
| ReadOffset.latest(), | ||
| true)); | ||
| log.info("Created Redis consumer group. stream={}, group={}", | ||
| properties.streamKey(), properties.group()); | ||
| } catch (RuntimeException e) { | ||
| if (containsError(e, ERROR_BUSY_GROUP)) { | ||
| log.info("Redis consumer group already exists. stream={}, group={}", | ||
| properties.streamKey(), properties.group()); | ||
| return; | ||
| } | ||
| throw new IllegalStateException( | ||
| "Failed to initialize Redis consumer group. stream=" + properties.streamKey() | ||
| + ", group=" + properties.group(), | ||
| e); | ||
| } | ||
| } | ||
|
|
||
| public void handleSubscriptionError(Throwable error) { | ||
| if (!isNoGroup(error)) { | ||
| log.error("Redis Stream subscription failed. stream={}, group={}", | ||
| properties.streamKey(), properties.group(), error); | ||
| applicationShutdown.request(); | ||
| return; | ||
| } | ||
|
|
||
| log.warn("Redis consumer group is missing. Recreating group without cancelling subscription. " | ||
| + "stream={}, group={}", | ||
| properties.streamKey(), properties.group()); | ||
| try { | ||
| ensureConsumerGroup(); | ||
| } catch (RuntimeException recoveryError) { | ||
| log.error("Failed to recover missing Redis consumer group. stream={}, group={}", | ||
| properties.streamKey(), properties.group(), recoveryError); | ||
| } | ||
| } | ||
|
|
||
| public boolean shouldCancelSubscription(Throwable error) { | ||
| return !isNoGroup(error); | ||
| } | ||
|
|
||
| boolean isNoGroup(Throwable error) { | ||
| return containsError(error, ERROR_NO_GROUP); | ||
| } | ||
|
|
||
| private static boolean containsError(Throwable error, String code) { | ||
| Throwable current = error; | ||
| while (current != null) { | ||
| String message = current.getMessage(); | ||
| if (message != null && message.contains(code)) { | ||
| return true; | ||
| } | ||
| current = current.getCause(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static byte[] raw(String value) { | ||
| return value.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
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
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.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: moonwhistle/CoinFlow
Length of output: 50376
🏁 Script executed:
Repository: moonwhistle/CoinFlow
Length of output: 50376
NOGROUP 복구에 별도의 시작 offset 정책을 적용하세요.
NOGROUP복구도ReadOffset.latest()를 사용합니다. Stream이 유지된 상태에서 group만 삭제되면, 복구 시점 이전의 레코드가 유실됩니다.startup과 복구의 offset을 분리하세요. 유실 방지가 정책이면 복구에
ReadOffset.from("0-0")또는 저장된 checkpoint를 사용하세요.RedisConsumerGroupManagerTest에서 복구 offset이"$"가 아닌지 검증하세요.TickProcessService의 중복 방지는 1분 만료와 100,000개 제한이 있는 인메모리 Caffeine 캐시에 의존합니다.0-0재처리를 선택하면 이 캐시만으로는 중복 처리를 보장할 수 없으므로 저장된 checkpoint 또는 영속적 멱등성도 설계에 포함하세요.🤖 Prompt for AI Agents