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

[BE] 레코드 락 위한 인덱스 명시(#796) #797

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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 @@ -10,6 +10,12 @@
import java.time.LocalDateTime;

@Entity
@Table(name = "automatic_matching",
indexes = {
@Index(name = "idx_room_id", columnList = "room_id"),
@Index(name = "idx_room_status", columnList = "status")
}
)
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
import java.time.LocalDateTime;

@Entity
@Table(name = "automatic_update",
indexes = {
@Index(name = "idx_room_id", columnList = "room_id"),
@Index(name = "idx_room_status", columnList = "status")
}
)
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/java/config/TestAsyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

Expand All @@ -12,6 +12,6 @@ public class TestAsyncConfig {

@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
return new SimpleAsyncTaskExecutor();
}
}
17 changes: 17 additions & 0 deletions backend/src/test/java/config/TestSyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

@TestConfiguration
@EnableAsync
public class TestSyncConfig {

@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;

@ServiceTest
Expand All @@ -59,16 +60,19 @@ class AutomaticMatchingExecutorTest {
@MockBean
private PullRequestProvider pullRequestProvider;

@Autowired
private RoomMatchInfoRepository roomMatchInfoRepository;

@SpyBean
MatchingExecutor matchingExecutor;

private Room room;
private Room emptyParticipantRoom;
private Member pororo;
private Member ash;
private Member joysun;
private Member movin;
private Member ten;
private Member cho;
@Autowired
private RoomMatchInfoRepository roomMatchInfoRepository;

@BeforeEach
void setUp() {
Expand All @@ -79,9 +83,9 @@ void setUp() {
ten = memberRepository.save(MemberFixture.MEMBER_TENTEN());
cho = memberRepository.save(MemberFixture.MEMBER_CHOCO());

room = roomRepository.save(RoomFixture.ROOM_DOMAIN(pororo, LocalDateTime.now().plusSeconds(3)));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(),true));
emptyParticipantRoom = roomRepository.save(RoomFixture.ROOM_DOMAIN(ash, LocalDateTime.now().plusSeconds(3)));
room = roomRepository.save(RoomFixture.ROOM_DOMAIN(pororo, LocalDateTime.now()
.plusSeconds(3)));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(), true));

participationRepository.save(new Participation(room, pororo, MemberRole.BOTH, room.getMatchingSize()));
participationRepository.save(new Participation(room, ash, MemberRole.BOTH, room.getMatchingSize()));
Expand Down Expand Up @@ -121,17 +125,14 @@ private PullRequestInfo getPullRequestInfo(Member pororo, Member ash, Member joy
@Test
@DisplayName("동시에 10개의 자동 매칭을 실행해도 PESSIMISTIC_WRITE 락을 통해 동시성을 제어할 수 있다.")
void startMatchingWithLock() throws InterruptedException {
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(room.getId(), LocalDateTime.now().plusDays(1)));
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(room.getId(), LocalDateTime.now()
.plusDays(1)));

int threadCount = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);
AtomicInteger successCount = new AtomicInteger(0);

when(pullRequestProvider.getUntilDeadline(any(), any())).thenAnswer(ignore -> {
successCount.incrementAndGet();
return getPullRequestInfo(pororo, ash, joysun, movin, ten, cho);
});
when(pullRequestProvider.getUntilDeadline(any(), any())).thenReturn(getPullRequestInfo(pororo, ash, joysun, movin, ten, cho));

for (int i = 0; i < threadCount; i++) {
executorService.execute(() -> {
Expand All @@ -144,7 +145,6 @@ void startMatchingWithLock() throws InterruptedException {
}

latch.await();

assertThat(successCount.get()).isEqualTo(1);
Mockito.verify(matchingExecutor,Mockito.times(1)).match(anyLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
Expand All @@ -52,6 +52,9 @@ class AutomaticUpdateExecutorTest {
@MockBean
private MatchResultRepository matchResultRepository;

@SpyBean
UpdateExecutor updateExecutor;

private Room room;

@BeforeEach
Expand All @@ -65,17 +68,15 @@ void setUp() {
@Test
@DisplayName("동시에 10개의 자동 업데이트를 실행해도 PESSIMISTIC_WRITE 락을 통해 동시성을 제어할 수 있다.")
void startMatchingWithLock() throws InterruptedException {
AutomaticUpdate automaticUpdate = automaticUpdateRepository.save(new AutomaticUpdate(room.getId(), LocalDateTime.now().plusDays(1)));
AutomaticUpdate automaticUpdate = automaticUpdateRepository.save(new AutomaticUpdate(room.getId(), LocalDateTime.now()
.plusDays(1)));

int threadCount = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);
AtomicInteger successCount = new AtomicInteger(0);

when(matchResultRepository.findAllByRoomIdAndReviewStatus(anyLong(), any(ReviewStatus.class))).thenAnswer(ignore -> {
successCount.incrementAndGet();
return Collections.singletonList(new MatchResult(room.getId(), MemberFixture.MEMBER_PORORO(), MemberFixture.MEMBER_MOVIN(), ""));
});
when(matchResultRepository.findAllByRoomIdAndReviewStatus(anyLong(), any(ReviewStatus.class)))
.thenReturn(Collections.singletonList(new MatchResult(room.getId(), MemberFixture.MEMBER_PORORO(), MemberFixture.MEMBER_MOVIN(), "")));

for (int i = 0; i < threadCount; i++) {
executorService.execute(() -> {
Expand All @@ -88,7 +89,6 @@ void startMatchingWithLock() throws InterruptedException {
}

latch.await();

assertThat(successCount.get()).isEqualTo(1);
Mockito.verify(updateExecutor,Mockito.times(1)).update(anyLong());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package corea.scheduler.service;

import config.ServiceTest;
import config.TestAsyncConfig;
import config.TestSyncConfig;
import corea.alarm.domain.AlarmActionType;
import corea.alarm.domain.ServerToUserAlarm;
import corea.alarm.repository.ServerToUserAlarmRepository;
Expand Down Expand Up @@ -42,7 +42,7 @@
import static org.mockito.Mockito.when;

@ServiceTest
@Import(TestAsyncConfig.class)
@Import(TestSyncConfig.class)
class MatchingExecutorTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package corea.scheduler.service;

import config.ServiceTest;
import config.TestAsyncConfig;
import config.TestSyncConfig;
import corea.fixture.MemberFixture;
import corea.fixture.RoomFixture;
import corea.member.domain.Member;
Expand All @@ -22,7 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@ServiceTest
@Import(TestAsyncConfig.class)
@Import(TestSyncConfig.class)
class UpdateExecutorTest {

@Autowired
Expand Down
Loading