Repository navigation
[refactor] @Async 풀 분리, 거부정책, MDC 전파로 비동기 작업 격리 #167
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
5 commits
Select commit
Hold shift + click to select a range
370e96d
refactor: @Async 풀을 성격별로 분리하고 거부정책 적용
gihhyeon 82e12d7
feat: async 스레드로 MDC(traceId) 전파
gihhyeon 5210663
refactor: async 풀 이름을 상수로 추출
gihhyeon 800ee09
refactor: external 풀도 유실 시 로그를 남기도록 수정
gihhyeon c8f0d7f
style: public, private 가독성을 위한 공백 추가
gihhyeon 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
58 changes: 46 additions & 12 deletions
58
src/main/java/com/matzip/common/config/AsyncConfiguration.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,30 +1,64 @@ | ||
| package com.matzip.common.config; | ||
|
|
||
| import org.springframework.boot.task.ThreadPoolTaskExecutorBuilder; | ||
| import com.matzip.common.logging.MdcTaskDecorator; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.task.TaskExecutor; | ||
| import org.springframework.core.task.TaskDecorator; | ||
| import org.springframework.scheduling.annotation.AsyncConfigurer; | ||
| import org.springframework.scheduling.annotation.EnableAsync; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
|
||
| import java.util.concurrent.Executor; | ||
|
|
||
| import java.util.concurrent.RejectedExecutionHandler;@Slf4j | ||
| @EnableAsync | ||
| @Configuration | ||
| public class AsyncConfiguration implements AsyncConfigurer { | ||
|
|
||
| public static final String GENERAL_EXECUTOR = "generalExecutor"; | ||
| public static final String EXTERNAL_EXECUTOR = "externalExecutor"; | ||
|
|
||
| private static final int SHUTDOWN_AWAIT_SECONDS = 15; | ||
|
|
||
| @Override | ||
| public Executor getAsyncExecutor() { | ||
| return asyncTaskExecutor(); | ||
| return generalExecutor(); | ||
| } | ||
|
|
||
| @Bean(GENERAL_EXECUTOR) | ||
| public ThreadPoolTaskExecutor generalExecutor() { | ||
| // Hikari 기본 커넥션 풀(10)을 웹 요청과 나눠 쓰도록 max 4로 제한 | ||
| return buildExecutor("async-", 3, 4, 200, loggingDiscardPolicy(GENERAL_EXECUTOR)); | ||
| } | ||
|
|
||
| @Bean(EXTERNAL_EXECUTOR) | ||
| public ThreadPoolTaskExecutor externalExecutor() { | ||
| return buildExecutor("ext-", 1, 2, 30, loggingDiscardPolicy(EXTERNAL_EXECUTOR)); | ||
| } | ||
|
|
||
| private ThreadPoolTaskExecutor buildExecutor(String threadNamePrefix, int corePoolSize, int maxPoolSize, | ||
| int queueCapacity, RejectedExecutionHandler rejectedHandler) { | ||
| ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
| executor.setCorePoolSize(corePoolSize); | ||
| executor.setMaxPoolSize(maxPoolSize); | ||
| executor.setQueueCapacity(queueCapacity); | ||
| executor.setThreadNamePrefix(threadNamePrefix); | ||
| executor.setRejectedExecutionHandler(rejectedHandler); | ||
| executor.setTaskDecorator(mdcTaskDecorator()); | ||
| executor.setWaitForTasksToCompleteOnShutdown(true); | ||
| executor.setAwaitTerminationSeconds(SHUTDOWN_AWAIT_SECONDS); | ||
| executor.initialize(); | ||
| return executor; | ||
| } | ||
|
|
||
| private TaskDecorator mdcTaskDecorator() { | ||
| return new MdcTaskDecorator(); | ||
| } | ||
|
|
||
| @Bean | ||
| public TaskExecutor asyncTaskExecutor() { | ||
| return new ThreadPoolTaskExecutorBuilder() | ||
| .corePoolSize(10) | ||
| .maxPoolSize(50) | ||
| .queueCapacity(100) | ||
| .threadNamePrefix("Async-") | ||
| .build(); | ||
| // ViewCountService가 REQUIRES_NEW라 CallerRunsPolicy는 커넥션 데드락 위험이 있어 log & discard 사용 | ||
| private RejectedExecutionHandler loggingDiscardPolicy(String poolName) { | ||
| return (rejected, executor) -> | ||
| log.warn("[비동기 작업 유실] {}} 풀 포화로 작업을 버립니다. activeThreads={}, queueSize={}", | ||
| poolName, executor.getActiveCount(), executor.getQueue().size()); | ||
| } | ||
| } | ||
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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/matzip/common/logging/MdcTaskDecorator.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,26 @@ | ||
| package com.matzip.common.logging; | ||
|
|
||
| import org.slf4j.MDC; | ||
| import org.springframework.core.task.TaskDecorator; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class MdcTaskDecorator implements TaskDecorator { | ||
|
|
||
| @Override | ||
| public Runnable decorate(Runnable runnable) { | ||
| Map<String, String> parentContext = MDC.getCopyOfContextMap(); | ||
| return () -> { | ||
| if (parentContext != null) { | ||
| MDC.setContextMap(parentContext); | ||
| } else { | ||
| MDC.clear(); | ||
| } | ||
| try { | ||
| runnable.run(); | ||
| } finally { | ||
| MDC.clear(); // 풀 스레드 재사용 시 이전 요청 컨텍스트가 남지 않도록 정리 | ||
| } | ||
| }; | ||
| } | ||
| } |
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
37 changes: 37 additions & 0 deletions
37
src/test/java/com/matzip/common/config/AsyncConfigurationTest.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,37 @@ | ||
| package com.matzip.common.config; | ||
|
|
||
| import com.matzip.AbstractMatzipApplicationTest; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class AsyncConfigurationTest extends AbstractMatzipApplicationTest { | ||
|
|
||
| @Autowired | ||
| @Qualifier("generalExecutor") | ||
| private ThreadPoolTaskExecutor generalExecutor; | ||
|
|
||
| @Autowired | ||
| @Qualifier("externalExecutor") | ||
| private ThreadPoolTaskExecutor externalExecutor; | ||
|
|
||
| @Test | ||
| void 내부작업_풀과_외부IO_풀이_분리되어_등록된다() { | ||
| assertThat(generalExecutor).isNotSameAs(externalExecutor); | ||
| } | ||
|
|
||
| @Test | ||
| void generalExecutor는_Hikari_커넥션을_고려한_크기로_구성된다() { | ||
| assertThat(generalExecutor.getCorePoolSize()).isEqualTo(3); | ||
| assertThat(generalExecutor.getMaxPoolSize()).isEqualTo(4); | ||
| } | ||
|
|
||
| @Test | ||
| void externalExecutor는_저빈도_외부IO용_작은_크기로_구성된다() { | ||
| assertThat(externalExecutor.getCorePoolSize()).isEqualTo(1); | ||
| assertThat(externalExecutor.getMaxPoolSize()).isEqualTo(2); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/matzip/common/logging/MdcTaskDecoratorTest.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,68 @@ | ||
| package com.matzip.common.logging; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.MDC; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class MdcTaskDecoratorTest { | ||
|
|
||
| private static final String TRACE_ID = "traceId"; | ||
|
|
||
| private final MdcTaskDecorator decorator = new MdcTaskDecorator(); | ||
|
|
||
| @AfterEach | ||
| void clearMdc() { | ||
| MDC.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| void 부모_스레드의_MDC를_실행_스레드로_복사한다() throws Exception { | ||
| MDC.put(TRACE_ID, "abc123"); // 제출(부모) 스레드에서 데코레이트 | ||
| AtomicReference<String> seenDuringRun = new AtomicReference<>(); | ||
| Runnable decorated = decorator.decorate(() -> seenDuringRun.set(MDC.get(TRACE_ID))); | ||
|
|
||
| // 다른 스레드에서 실행 → ThreadLocal이라 데코레이터 없이는 전파되지 않음 | ||
| runAndJoin(new Thread(decorated)); | ||
|
|
||
| assertThat(seenDuringRun.get()).isEqualTo("abc123"); | ||
| } | ||
|
|
||
| @Test | ||
| void 실행_후_스레드의_MDC를_정리한다() throws Exception { | ||
| MDC.put(TRACE_ID, "abc123"); | ||
| Runnable decorated = decorator.decorate(() -> {}); | ||
|
|
||
| // 실행이 끝난 뒤 워커 스레드에 값이 남으면 재사용 시 다음 작업을 오염시킨다 | ||
| AtomicReference<String> afterRun = new AtomicReference<>("SENTINEL"); | ||
| runAndJoin(new Thread(() -> { | ||
| decorated.run(); | ||
| afterRun.set(MDC.get(TRACE_ID)); | ||
| })); | ||
|
|
||
| assertThat(afterRun.get()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void 부모_MDC가_없으면_워커에_남은_잔여값을_지운다() throws Exception { | ||
| MDC.clear(); // 부모(제출) 스레드에 컨텍스트 없음 | ||
| AtomicReference<String> seenDuringRun = new AtomicReference<>("SENTINEL"); | ||
| Runnable decorated = decorator.decorate(() -> seenDuringRun.set(MDC.get(TRACE_ID))); | ||
|
|
||
| // 재사용된 워커 스레드에 이전 작업의 값이 남아있는 상황을 모사 | ||
| runAndJoin(new Thread(() -> { | ||
| MDC.put(TRACE_ID, "STALE"); | ||
| decorated.run(); | ||
| })); | ||
|
|
||
| assertThat(seenDuringRun.get()).isNull(); | ||
| } | ||
|
|
||
| private void runAndJoin(Thread worker) throws InterruptedException { | ||
| worker.start(); | ||
| worker.join(); | ||
| } | ||
| } |
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.
참고용) 스프링 빈으로 등록하는 경우에는 자동으로
initialize()가 호출되긴 해