Skip to content
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

[fix] 스케줄러 의도대로 동작하지 않는 버그 수정 (#143) #144

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,13 +17,15 @@
@Component
public class FcfsScheduler {

private static final Logger log = LoggerFactory.getLogger(FcfsScheduler.class);
// 분산 환경에서 메서드가 여러 번 실행되는 것을 방지하기 위해 분산 락 도입
private final RedissonClient redissonClient;
private final FcfsManageService fcfsManageService;

// 매일 자정 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분동안 락 점유
Expand All @@ -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분동안 락 점유
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,44 @@ public void registerFcfsEvents() {
// redis에 저장된 모든 선착순 이벤트의 당첨자 정보를 DB로 이관
@Transactional
public void registerWinners() {
Set<String> fcfsIds = stringRedisTemplate.keys("*:count");
if (fcfsIds == null || fcfsIds.isEmpty()) {
Set<String> 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<String> 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<String> userIds = stringRedisTemplate.opsForZSet().range(FcfsUtil.winnerFormatting(fcfsEventId), 0, -1);
if(userIds == null || userIds.isEmpty()) {
return;
log.info("No winners in FCFS Event {}", fcfsEventId);
continue;
}
Comment on lines -57 to 66
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

기존 return;으로 인해 운이 좋지 않게도 앞 순번의 선착순 이벤트의 당첨자가 없는 경우 메서드 자체가 종료되어 버리기에 뒤에 서있던 다른 선착순 이벤트의 결과 정보가 DB로 이동하지 못하고 붕 떠버리고 있었습니다.

continue로 바꾸어 뒷 순번 이벤트 결과도 저장할 수 있도록 고친 결과 정상 동작합니다.

Copy link
Collaborator

Choose a reason for hiding this comment

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

시연 이전에 예외를 찾을 수 있어서 다행이네요. 생각보다 심각한 버그일 수 있었는데 수정해서 다행입니다.


FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(eventId))
FcfsEvent event = fcfsEventRepository.findById(Long.parseLong(fcfsEventId))
.orElseThrow(() -> new FcfsEventException(ErrorCode.FCFS_EVENT_NOT_FOUND));

List<EventUser> users = eventUserRepository.findAllByUserId(userIds.stream().toList());
List<FcfsEventWinningInfo> 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()))))
Comment on lines -64 to +74
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

그 외 eventId라는 표현이 모호하다고 생각하여 fcfsEventId로 표현을 명확하게 다듬었습니다.

.toList();

log.info("Winners of FCFS event {} were registered in DB", fcfsEventId);
fcfsEventWinningInfoRepository.saveAll(winningInfos);
deleteEventInfo(eventId);
deleteEventInfo(fcfsEventId);
}

// PK를 간접적으로 보관하던 eventId 제거
Set<String> 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");
}

// 특정 선착순 이벤트의 정보 조회
Expand Down
Loading