-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 데이터베이스 락을 통해 중복 참여 방지 (#673)
* test: 동시 참여 시 중복 참여 가능한 상황 테스트 * refactor: 공모 상태 업데이트 로직 이동 * refactor: 사용하지 않는 쿼리 제거 * feat: Offering 테이블 비관적 쓰기 락을 통해 중복 참여 방지 * test: 같은 공모 다른 사용자의 동시 참여 경우 테스트 * refactor: 비관적 쓰기 락 -> 낙관적 락을 통해 중복 참여 방지 * refactor: version 필드 추가로 인한 soft delete 쿼리 수정 * refactor: offeringMember 저장 시 offering의 실 저장 데이터 활용하도록 * refactor: 낙관적 락 -> 비관적 쓰기 락 * test: 동시 실행 코드 ConcurrencyExecutor 클래스로 추출
- Loading branch information
Showing
8 changed files
with
261 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
backend/src/test/java/com/zzang/chongdae/global/helper/ConcurrencyExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.zzang.chongdae.global.helper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.function.Supplier; | ||
|
||
public class ConcurrencyExecutor { | ||
|
||
private static ConcurrencyExecutor INSTANCE; | ||
|
||
public static ConcurrencyExecutor getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new ConcurrencyExecutor(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
public void executeWithoutResult(Runnable... tasks) throws InterruptedException { | ||
int executeCount = tasks.length; | ||
ExecutorService executorService = Executors.newFixedThreadPool(executeCount); | ||
CountDownLatch countDownLatch = new CountDownLatch(executeCount); | ||
|
||
for (Runnable task : tasks) { | ||
executorService.submit(() -> { | ||
try { | ||
task.run(); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
countDownLatch.await(); | ||
executorService.shutdown(); | ||
} | ||
|
||
public void executeWithoutResult(int repeatCount, Runnable task) throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(repeatCount); | ||
CountDownLatch countDownLatch = new CountDownLatch(repeatCount); | ||
|
||
for (int i = 0; i < repeatCount; i++) { | ||
executorService.submit(() -> { | ||
try { | ||
task.run(); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
countDownLatch.await(); | ||
executorService.shutdown(); | ||
} | ||
|
||
public final <T> List<T> execute(Supplier<T>... tasks) throws InterruptedException { | ||
int executeCount = tasks.length; | ||
ExecutorService executorService = Executors.newFixedThreadPool(executeCount); | ||
CountDownLatch countDownLatch = new CountDownLatch(executeCount); | ||
|
||
List<Future<T>> result = new ArrayList<>(); | ||
for (Supplier<T> task : tasks) { | ||
Future<T> future = executorService.submit(() -> { | ||
try { | ||
return task.get(); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
result.add(future); | ||
} | ||
|
||
countDownLatch.await(); | ||
executorService.shutdown(); | ||
|
||
return result.stream() | ||
.map(this::getWithoutException) | ||
.toList(); | ||
} | ||
|
||
public <T> List<T> execute(int repeatCount, Supplier<T> task) throws InterruptedException { | ||
ExecutorService executorService = Executors.newFixedThreadPool(repeatCount); | ||
CountDownLatch countDownLatch = new CountDownLatch(repeatCount); | ||
|
||
List<Future<T>> result = new ArrayList<>(); | ||
for (int i = 0; i < repeatCount; i++) { | ||
Future<T> future = executorService.submit(() -> { | ||
try { | ||
return task.get(); | ||
} finally { | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
result.add(future); | ||
} | ||
|
||
countDownLatch.await(); | ||
executorService.shutdown(); | ||
|
||
return result.stream() | ||
.map(this::getWithoutException) | ||
.toList(); | ||
} | ||
|
||
private <T> T getWithoutException(Future<T> future) { | ||
try { | ||
return future.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException("동시 작업 내부 예외 발생: ", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.