From f5f1ae5fa2fc694a386e1d9aee002747f5cb360b Mon Sep 17 00:00:00 2001 From: win-luck Date: Fri, 23 Aug 2024 00:27:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[chore]=20=EC=84=A0=EC=B0=A9=EC=88=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EA=B2=B0=EA=B3=BC=20=EC=9D=B4?= =?UTF-8?q?=EA=B4=80=20=EA=B8=B0=EB=8A=A5=20=EB=A1=9C=EA=B9=85=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#143)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/fcfs/scheduler/FcfsScheduler.java | 5 ++++ .../event/fcfs/service/FcfsManageService.java | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java b/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java index 646b0f30..0b32dca2 100644 --- a/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java +++ b/src/main/java/hyundai/softeer/orange/event/fcfs/scheduler/FcfsScheduler.java @@ -6,6 +6,8 @@ import lombok.RequiredArgsConstructor; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -15,6 +17,7 @@ @Component public class FcfsScheduler { + private static final Logger log = LoggerFactory.getLogger(FcfsScheduler.class); // 분산 환경에서 메서드가 여러 번 실행되는 것을 방지하기 위해 분산 락 도입 private final RedissonClient redissonClient; private final FcfsManageService fcfsManageService; @@ -22,6 +25,7 @@ public class FcfsScheduler { // 매일 자정 1분마다 실행되며, 오늘의 선착순 이벤트에 대한 정보를 DB에서 Redis로 이동시킨다. @Scheduled(cron = "0 1 0 * * *") public void registerFcfsEvents() { + log.info("Move the information of FCFS Events from DB to Redis"); RLock lock = redissonClient.getLock(ConstantUtil.DB_TO_REDIS_LOCK); try { // 5분동안 락 점유 @@ -40,6 +44,7 @@ public void registerFcfsEvents() { // 매일 자정마다 실행되며, 선착순 이벤트 당첨자들을 Redis에서 DB로 이동시킨다. @Scheduled(cron = "0 0 0 * * *") public void registerWinners() { + log.info("Move the result of FCFS Events from Redis to DB"); RLock lock = redissonClient.getLock(ConstantUtil.REDIS_TO_DB_LOCK); try { // 5분동안 락 점유 diff --git a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java index 101a7e3f..2f81825f 100644 --- a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java +++ b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java @@ -49,38 +49,44 @@ public void registerFcfsEvents() { // redis에 저장된 모든 선착순 이벤트의 당첨자 정보를 DB로 이관 @Transactional public void registerWinners() { - Set fcfsIds = stringRedisTemplate.keys("*:count"); - if (fcfsIds == null || fcfsIds.isEmpty()) { + Set fcfsKeys = stringRedisTemplate.keys("*:count"); + if (fcfsKeys == null || fcfsKeys.isEmpty()) { + log.info("There are no FCFS events in yesterday"); return; } - for(String fcfsId : fcfsIds) { - String eventId = fcfsId.replace(":count", "").replace("fcfs:", ""); - Set userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(eventId), 0, -1); + // 당첨자 관련 정보 조합하여 Entity 생성 + log.info("keys for FCFS Events: {}", fcfsKeys); + for(String key : fcfsKeys) { + String fcfsEventId = key.replace(":count", "").replace("fcfs:", ""); + Set userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(fcfsEventId), 0, -1); if(userIds == null || userIds.isEmpty()) { + log.info("No winners in FCFS Event {}", fcfsEventId); return; } - FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(eventId)) + FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(fcfsEventId)) .orElseThrow(() -> new FcfsEventException(ErrorCode.FCFS_EVENT_NOT_FOUND)); List users = eventUserRepository.findAllByUserId(userIds.stream().toList()); List winningInfos = users .stream() - .map(user -> FcfsEventWinningInfo.of(event, user, getTimeFromScore(stringRedisTemplate.opsForZSet().score(FcfsUtil.winnerFormatting(eventId), user.getUserId())))) + .map(user -> FcfsEventWinningInfo.of(event, user, getTimeFromScore(stringRedisTemplate.opsForZSet().score(FcfsUtil.winnerFormatting(fcfsEventId), user.getUserId())))) .toList(); + log.info("Winners of FCFS event {} were registered in DB", fcfsEventId); fcfsEventWinningInfoRepository.saveAll(winningInfos); - deleteEventInfo(eventId); + deleteEventInfo(fcfsEventId); } + // PK를 간접적으로 보관하던 eventId 제거 Set eventIds = stringRedisTemplate.keys("*:eventId"); if(eventIds != null && !eventIds.isEmpty()) { for(String eventId : eventIds) { stringRedisTemplate.delete(eventId); } } - log.info("Winners of all FCFS events were registered in DB"); + log.info("Registering winners of FCFS events in DB is completed"); } // 특정 선착순 이벤트의 정보 조회 From 564da2f135b5454fb503c10e7fafb1476dd899e7 Mon Sep 17 00:00:00 2001 From: win-luck Date: Fri, 23 Aug 2024 00:28:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=20=EC=9E=98=EB=AA=BB=EB=90=9C=20?= =?UTF-8?q?=ED=9D=90=EB=A6=84=EC=A0=9C=EC=96=B4=20=ED=91=9C=ED=98=84?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20=EB=8B=B9=EC=B2=A8?= =?UTF-8?q?=EC=9E=90=20=EC=A0=95=EB=B3=B4=20=EC=9D=B4=EA=B4=80=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8D=98=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#143)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../softeer/orange/event/fcfs/service/FcfsManageService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java index 2f81825f..9051cdc5 100644 --- a/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java +++ b/src/main/java/hyundai/softeer/orange/event/fcfs/service/FcfsManageService.java @@ -62,7 +62,7 @@ public void registerWinners() { Set userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(fcfsEventId), 0, -1); if(userIds == null || userIds.isEmpty()) { log.info("No winners in FCFS Event {}", fcfsEventId); - return; + continue; } FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(fcfsEventId))