Skip to content

Conversation

@sgh002400
Copy link
Contributor

@sgh002400 sgh002400 commented May 17, 2025

변경사항

  • 자동매매의 성능 개선

상세내용

  • 프로메테우스, 그라파나 모니터링 적용
  • 레디스 락 적용 후 비관적 락과 성능 비교
  • 이벤트 기반 비동기 처리하도록 개선
  • 스레드 풀 및 커넥션 풀 조절

연관 이슈

sgh002400 and others added 4 commits May 7, 2025 21:31
* feat: Redisson 라이브러리를 활용하여 분산락 적용
* refactor: 양방향 연관관계 제거
* feat: 이벤트 기반 병렬 처리로 개선 및 스레드풀 커넥션풀 튜닝
@sgh002400 sgh002400 requested a review from Copilot May 17, 2025 06:26
@sgh002400 sgh002400 self-assigned this May 17, 2025
@sgh002400 sgh002400 changed the title Feat/#23 enhance auto trading 자동매매 성능 개선 May 17, 2025
@sgh002400 sgh002400 merged commit 0fb51cf into dev May 17, 2025
1 check passed
@sgh002400 sgh002400 deleted the feat/#23-enhance-auto-trading branch May 17, 2025 06:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Enhances the auto-trading module’s throughput and reliability by replacing DB locks with Redis‐based distributed locks, introducing event-driven order processing, and tuning async/threadpool configurations.

  • Replace pessimistic “FOR UPDATE” locks with a custom @DistributedLock mechanism
  • Publish auto-trade operations as async Spring events instead of inline try/catch loops
  • Tune thread-pool sizes and add Prometheus/Grafana monitoring integrations

Reviewed Changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/main/java/com/tradin/module/users/account/implement/AccountProcessor.java Simplify account + USDT balance creation flow
src/main/java/com/tradin/module/users/account/domain/Account.java Remove old balance/subscription/position collections
src/main/java/com/tradin/module/strategy/subscription/service/SubscriptionService.java Drop DB-level “for update” lock in validation
src/main/java/com/tradin/module/strategy/subscription/implement/SubscriptionProcessor.java Remove direct account–subscription link
src/main/java/com/tradin/module/strategy/subscription/domain/repository/impl/SubscriptionQueryRepositoryImpl.java Refactor subscription lookup query
src/main/java/com/tradin/module/strategy/strategy/service/StrategyService.java Add same-position guard and event publishing
src/main/java/com/tradin/module/futures/position/implement/FuturesPositionReader.java Rename query method, remove pessimistic lock
src/main/java/com/tradin/module/futures/position/implement/FuturesPositionProcessor.java Drop unused entity associations, inject EM
src/main/java/com/tradin/module/futures/position/domain/repository/impl/FuturesPositionQueryRepositoryImpl.java Rename method, remove lock-mode usage
src/main/java/com/tradin/module/futures/position/domain/FuturesPositionQueryRepository.java Update interface to match new method
src/main/java/com/tradin/module/futures/order/implement/FuturesOrderProcessor.java Introduce @DistributedLock on order APIs
src/main/java/com/tradin/module/futures/order/event/AutoTradeEventHandler.java New async handler for AutoTradeEvent
src/main/java/com/tradin/module/futures/order/event/AutoTradeEvent.java Define application event payload
src/main/java/com/tradin/common/utils/CustomSpringELParser.java Utility for evaluating SpEL in lock keys
src/main/java/com/tradin/common/exception/ExceptionType.java Add lock‐related and same-position exceptions
src/main/java/com/tradin/common/config/RedissonConfiguration.java Bean for RedissonClient using application props
src/main/java/com/tradin/common/config/AsyncConfiguration.java Define tradeExecutor thread-pool bean
src/main/java/com/tradin/common/aop/DistributedLockAop.java Aspect for @DistributedLock support
src/main/java/com/tradin/common/aop/AopForTransaction.java Wraps joinpoints in new transactions
src/main/java/com/tradin/common/annotation/DistributedLock.java Annotation definition for distributed lock
Comments suppressed due to low confidence (1)

src/main/java/com/tradin/module/strategy/subscription/service/SubscriptionService.java:124

  • [nitpick] By removing the DB-level pessimistic lock (forUpdate) here, this check can race under high concurrency. Consider applying a transactional lock or the same distributed-lock mechanism to prevent duplicate subscriptions.
futuresPositionReader.findOpenFuturesPositionByAccountAndCoinType(accountId, coinType)

* 1. 기존 포지션 조회 (신규 주문인 경우 SKIP) 2. 반대 매매 주문 3. 포지션 청산 (시장가) 4. USDT 수익 정산
* 기존 포지션 정리 - 1. 기존 포지션 조회 (신규 주문인 경우 SKIP) 2. 반대 매매 주문 3. 포지션 청산 (시장가) 4. USDT 수익 정산
*/
@DistributedLock(
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closeExistPosition method is annotated with fallbackMethod = "handleLockFailure", but its signature only takes (Strategy, Account) while handleLockFailure expects (Strategy, Account, Position). This mismatch will cause the fallback to not be found at runtime; adjust the fallback signature or define a separate fallback for closeExistPosition.

Copilot uses AI. Check for mistakes.

private final FuturesOrderProcessor futuresOrderProcessor;

@Async
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @Async annotation uses the default executor. To leverage the configured tradeExecutor pool and avoid unbounded thread creation, specify @Async("tradeExecutor").

Suggested change
@Async
@Async("tradeExecutor")

Copilot uses AI. Check for mistakes.
Comment on lines +20 to 21
private final EntityManager entityManager;

Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EntityManager field is injected but never used in this class. Consider removing the field and its import to clean up unused dependencies.

Suggested change
private final EntityManager entityManager;

Copilot uses AI. Check for mistakes.
executor.setMaxPoolSize(30); //최대 쓰레드 풀 사이즈
executor.setQueueCapacity(50); //쓰레드가 모두 사용중인 경우 큐에 쌓여있는 요청의 최대 갯수
executor.setThreadNamePrefix("TRADIN-ASYNC-"); //쓰레드 이름 접두사
executor.setCorePoolSize(16);
Copy link

Copilot AI May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tradeExecutor bean only sets corePoolSize and threadNamePrefix. It’s recommended to also configure maxPoolSize and queueCapacity to prevent potential queue backlogs or thread saturation.

Suggested change
executor.setCorePoolSize(16);
executor.setCorePoolSize(16);
executor.setMaxPoolSize(32); // Allow the pool to grow under high load
executor.setQueueCapacity(100); // Limit the task queue size

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] 오토 트레이딩 성능 개선

2 participants