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

#575 [feat] 분산락 컴포넌트 구현 #577

Merged
merged 3 commits into from
Nov 13, 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
@@ -1,4 +1,4 @@
package com.mile.moim.service.lock;
package com.mile.common.lock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mile.moim.service.lock;
package com.mile.common.lock;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mile.common.lock;

import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.MileException;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
@Component
public class DistributedLock {

private final RedissonClient redissonClient;

public void getLock(final String key) {
final RLock lock = redissonClient.getLock(key);
try {
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

public void afterLock(final String key) {
final RLock lock = redissonClient.getLock(key);
lock.unlock();
}

public void checkAvailability(final Boolean available) {
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION);
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package com.mile.moim.service.lock;
package com.mile.common.lock;

import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.MileException;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Aspect
@RequiredArgsConstructor
@Component
public class MoimNameRequestAspect {

private final RedissonClient redissonClient;
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK : ";
private final DistributedLock distributedLock;
private final static String MOIM_NAME_LOCK = "MOIM_NAME_LOCK";
private final AopForTransaction aopForTransaction;

@Pointcut("@annotation(com.mile.moim.service.lock.AtomicValidateUniqueMoimName)")
@Pointcut("@annotation(com.mile.common.lock.AtomicValidateUniqueMoimName)")
public void uniqueMoimNameCut() {
}

@Around("uniqueMoimNameCut()")
public Object validateUniqueName(final ProceedingJoinPoint joinPoint) throws Throwable {
final RLock lock = redissonClient.getLock(MOIM_NAME_LOCK);
try {
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS));
distributedLock.getLock(MOIM_NAME_LOCK);
return aopForTransaction.proceed(joinPoint);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
distributedLock.afterLock(MOIM_NAME_LOCK);
}
}

public void checkAvailability(final Boolean available) {
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mile.moim.domain.Moim;
import com.mile.moim.repository.MoimRepository;
import com.mile.moim.service.dto.response.MoimInfoResponse;
import com.mile.moim.service.lock.AtomicValidateUniqueMoimName;
import com.mile.common.lock.AtomicValidateUniqueMoimName;
import com.mile.user.domain.User;
import com.mile.common.utils.DateUtil;
import com.mile.writername.domain.WriterName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.mile.moim.service.dto.response.TemporaryPostExistResponse;
import com.mile.moim.service.dto.response.TopicListResponse;
import com.mile.moim.service.dto.response.WriterNameConflictCheckResponse;
import com.mile.moim.service.lock.AtomicValidateUniqueMoimName;
import com.mile.common.lock.AtomicValidateUniqueMoimName;
import com.mile.moim.service.popular.MoimPopularInfoService;
import com.mile.post.domain.Post;
import com.mile.post.service.PostRetriever;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.mile.moim.service.popular;

import com.mile.common.lock.DistributedLock;
import com.mile.curious.repository.dto.PostAndCuriousCountInLastWeek;
import com.mile.curious.service.CuriousRetriever;
import com.mile.moim.domain.Moim;
import com.mile.moim.domain.popular.MoimCuriousPost;
import com.mile.moim.domain.popular.MoimCuriousWriter;
import com.mile.moim.domain.popular.MoimPopularInfo;
import com.mile.moim.repository.MoimPopularInfoRepository;
import com.mile.moim.service.lock.AtomicMoimPopulerInfo;
import com.mile.writername.domain.WriterName;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
Expand All @@ -25,6 +25,7 @@ public class MoimPopularInfoRegister {

private final MoimPopularInfoRepository moimPopularInfoRepository;
private final CuriousRetriever curiousRetriever;
private final DistributedLock distributedLock;


private Set<MoimCuriousPost> getMoimCuriousPost(final List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek) {
Expand All @@ -46,6 +47,8 @@ private Set<MoimCuriousWriter> getMoimCuriousWriter(final List<PostAndCuriousCou

@CachePut(value = "moimPopularInfo", key = "#moim.id")
public MoimPopularInfo setMostPopularInfoOfMoim(final Moim moim) {
distributedLock.getLock("MOIM_POPULAR_LOCK");

List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek = curiousRetriever.findMostCuriousPostsInLastWeek(moim);

Set<MoimCuriousPost> moimCuriousPosts = getMoimCuriousPost(mostCuriousPostsInLastWeek);
Expand All @@ -54,7 +57,11 @@ public MoimPopularInfo setMostPopularInfoOfMoim(final Moim moim) {

MoimPopularInfo moimPopularInfo = MoimPopularInfo.of(moim.getId(), moimCuriousPosts, moimCuriousWriters);

return moimPopularInfoRepository.saveAndFlush(moimPopularInfo);
moimPopularInfoRepository.saveAndFlush(moimPopularInfo);

distributedLock.afterLock("MOIM_POPULAR_LOCK");

return moimPopularInfo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mile.moim.domain.Moim;
import com.mile.moim.domain.popular.MoimPopularInfo;
import com.mile.moim.repository.MoimPopularInfoRepository;
import com.mile.moim.service.lock.AtomicMoimPopulerInfo;
import com.mile.slack.module.SendMessageModule;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,7 +21,6 @@ public class MoimPopularInfoService {


@Cacheable(value = "moimPopularInfo", key = "#moim.id")
@AtomicMoimPopulerInfo
public MoimPopularInfo getMoimPopularInfo(final Moim moim) {
return moimPopularInfoRepository.findByMoimId(moim.getId()).orElseGet(
() -> moimPopularInfoRegister.setMostPopularInfoOfMoim(moim)
Expand Down
Loading