-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Added FoodRepositoryFacade #34
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/com/f_lab/la_planete/facade/FoodRepositoryFacade.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,47 @@ | ||
package com.f_lab.la_planete.facade; | ||
|
||
import com.f_lab.la_planete.domain.Food; | ||
import com.f_lab.la_planete.repository.FoodRepository; | ||
import jakarta.persistence.LockTimeoutException; | ||
import jakarta.persistence.PessimisticLockException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class FoodRepositoryFacade { | ||
|
||
private static final int MAX_RETRY = 3; | ||
|
||
private final FoodRepository foodRepository; | ||
|
||
public void save(Food food) { | ||
foodRepository.save(food); | ||
} | ||
|
||
public Food findFoodWithLockAndRetry(Long foodId) { | ||
int attempts = 0; | ||
|
||
while (attempts < MAX_RETRY) { | ||
try { | ||
Food food = foodRepository.findFoodByFoodIdWithPessimisticLock(foodId); | ||
|
||
if (food != null) | ||
return food; | ||
|
||
} catch (PessimisticLockException | LockTimeoutException e) { | ||
log.warn("시도 횟수={}, 다시 id={} 에 해당되는 food의 락을 얻기를 시도합니다", attempts, foodId); | ||
} catch (Exception e) { | ||
log.error("Error Occurred at FoodLockFacade.findFoodWithLockAndRetry", e); | ||
throw e; | ||
} | ||
|
||
attempts++; | ||
} | ||
|
||
throw new RuntimeException("현재 너무 많은 요청을 처리하고 있습니다. 다시 시도해주세요"); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/test/java/com/f_lab/la_planete/facade/FoodRepositoryFacadeTest.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,82 @@ | ||
package com.f_lab.la_planete.facade; | ||
|
||
import com.f_lab.la_planete.domain.Food; | ||
import com.f_lab.la_planete.repository.FoodRepository; | ||
import jakarta.persistence.LockTimeoutException; | ||
import jakarta.persistence.PessimisticLockException; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
class FoodRepositoryFacadeTest { | ||
|
||
@InjectMocks | ||
FoodRepositoryFacade foodRepositoryFacade; | ||
@Mock | ||
FoodRepository foodRepository; | ||
|
||
@Test | ||
@DisplayName("락 없이 첫 시도에 성공") | ||
void test_find_food_lock_and_retry_success() { | ||
// given | ||
Long foodId = 1L; | ||
Food expectedFood = createFood(foodId); | ||
|
||
// when | ||
when(foodRepository.findFoodByFoodIdWithPessimisticLock(anyLong())).thenReturn(expectedFood); | ||
Food foundFood = foodRepositoryFacade.findFoodWithLockAndRetry(foodId); | ||
|
||
// then | ||
assertThat(foundFood.getId()).isEqualTo(foodId); | ||
} | ||
|
||
@Test | ||
@DisplayName("첫 번째 시도는 실패하고 두 번째 시도에 성공") | ||
void test_find_food_lock_and_retry_fail_on_first_then_success() { | ||
// given | ||
Long foodId = 1L; | ||
Food expectedFood = createFood(foodId); | ||
|
||
// when | ||
when(foodRepository.findFoodByFoodIdWithPessimisticLock(anyLong())) | ||
.thenThrow(new PessimisticLockException()) | ||
.thenReturn(expectedFood); | ||
|
||
Food foundFood = foodRepositoryFacade.findFoodWithLockAndRetry(foodId); | ||
|
||
// then | ||
assertThat(foundFood.getId()).isEqualTo(foodId); | ||
} | ||
|
||
@Test | ||
@DisplayName("락 타임아웃으로 최대 재시도 후 실패") | ||
void test_find_food_lock_and_retry_fail() { | ||
// given | ||
Long foodId = 1L; | ||
|
||
// when | ||
when(foodRepository.findFoodByFoodIdWithPessimisticLock(anyLong())) | ||
.thenThrow(new PessimisticLockException()) | ||
.thenThrow(new LockTimeoutException()) | ||
.thenThrow(new LockTimeoutException()); | ||
|
||
assertThatThrownBy(() -> foodRepositoryFacade.findFoodWithLockAndRetry(foodId)) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessage("현재 너무 많은 요청을 처리하고 있습니다. 다시 시도해주세요"); | ||
} | ||
|
||
|
||
private Food createFood(Long foodId) { | ||
return Food.builder() | ||
.id(foodId) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Facade 외에 AOP를 사용하여 재시도 구현시 장단점을 비교 해보는 것을 추천드립니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우선 기존의 facade를 대신하여 AOP를 사용하면
장점
단점
정도로 생각되어집니다.