-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 바이너리 업로드를 트랜잭션 밖으로 분리 #255
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/codeit/team5/mopl/binarycontent/dto/UploadedBinaryContent.java
This file contains hidden or 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,7 @@ | ||
| package com.codeit.team5.mopl.binarycontent.dto; | ||
|
|
||
| public record UploadedBinaryContent( | ||
| String key, | ||
| String url | ||
| ) { | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/codeit/team5/mopl/binarycontent/event/BinaryContentDeleteEvent.java
This file contains hidden or 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,9 @@ | ||
| package com.codeit.team5.mopl.binarycontent.event; | ||
|
|
||
| import com.codeit.team5.mopl.binarycontent.dto.UploadedBinaryContent; | ||
|
|
||
| // 업로드 후 후속 작업 실패 시, 업로드된 스토리지 객체를 비동기로 삭제(롤백)하기 위한 이벤트 | ||
| public record BinaryContentDeleteEvent( | ||
| UploadedBinaryContent uploaded | ||
| ) { | ||
| } |
21 changes: 21 additions & 0 deletions
21
...a/com/codeit/team5/mopl/binarycontent/eventlistener/BinaryContentDeleteEventListener.java
This file contains hidden or 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,21 @@ | ||
| package com.codeit.team5.mopl.binarycontent.eventlistener; | ||
|
|
||
| import com.codeit.team5.mopl.binarycontent.event.BinaryContentDeleteEvent; | ||
| import com.codeit.team5.mopl.binarycontent.service.BinaryContentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class BinaryContentDeleteEventListener { | ||
|
|
||
| private final BinaryContentService binaryContentService; | ||
|
|
||
| @Async("binaryContentTaskExecutor") | ||
| @EventListener | ||
| public void handle(BinaryContentDeleteEvent event) { | ||
| binaryContentService.deleteQuietly(event.uploaded()); | ||
| } | ||
| } |
28 changes: 22 additions & 6 deletions
28
src/main/java/com/codeit/team5/mopl/binarycontent/service/BinaryContentService.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,30 +1,46 @@ | ||
| package com.codeit.team5.mopl.binarycontent.service; | ||
|
|
||
| import com.codeit.team5.mopl.binarycontent.dto.UploadedBinaryContent; | ||
| import com.codeit.team5.mopl.binarycontent.entity.BinaryContent; | ||
| import com.codeit.team5.mopl.binarycontent.repository.BinaryContentRepository; | ||
| import com.codeit.team5.mopl.binarycontent.storage.BinaryContentStorage; | ||
| import com.codeit.team5.mopl.binarycontent.storage.GeneratedKey; | ||
| import com.codeit.team5.mopl.binarycontent.storage.StorageDirectory; | ||
| import com.codeit.team5.mopl.binarycontent.storage.StorageKeyFactory; | ||
| import com.codeit.team5.mopl.global.dto.FileRequest; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class BinaryContentService { | ||
|
|
||
| private final BinaryContentStorage binaryContentStorage; | ||
| private final StorageKeyFactory storageKeyFactory; | ||
| private final BinaryContentRepository binaryContentRepository; | ||
|
|
||
| @Transactional | ||
| public BinaryContent upload(StorageDirectory directory, UUID ownerId, FileRequest image) { | ||
| GeneratedKey generated = storageKeyFactory.generate(directory, ownerId, image.filename()); | ||
| public UploadedBinaryContent uploadToStorage(StorageDirectory directory, FileRequest image) { | ||
| GeneratedKey generated = storageKeyFactory.generate(directory, image.filename()); | ||
| binaryContentStorage.store(generated.key(), image.bytes(), generated.contentType()); | ||
| return binaryContentRepository.save( | ||
| BinaryContent.completed(binaryContentStorage.toUrl(generated.key()))); | ||
| return new UploadedBinaryContent(generated.key(), binaryContentStorage.toUrl(generated.key())); | ||
| } | ||
|
|
||
| @Transactional | ||
| public BinaryContent saveCompleted(UploadedBinaryContent uploaded) { | ||
| return binaryContentRepository.save(BinaryContent.completed(uploaded.url())); | ||
| } | ||
|
|
||
| public void deleteQuietly(UploadedBinaryContent uploaded) { | ||
| if (uploaded == null) { | ||
| return; | ||
| } | ||
| try { | ||
| binaryContentStorage.delete(uploaded.key()); | ||
| } catch (RuntimeException e) { | ||
| log.warn("업로드 보상 삭제 실패: key={}", uploaded.key(), e); | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/codeit/team5/mopl/binarycontent/service/UploadWithRollback.java
This file contains hidden or 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,41 @@ | ||
| package com.codeit.team5.mopl.binarycontent.service; | ||
|
|
||
| import com.codeit.team5.mopl.binarycontent.dto.UploadedBinaryContent; | ||
| import com.codeit.team5.mopl.binarycontent.event.BinaryContentDeleteEvent; | ||
| import com.codeit.team5.mopl.binarycontent.storage.StorageDirectory; | ||
| import com.codeit.team5.mopl.global.dto.FileRequest; | ||
| import java.util.function.Function; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.ApplicationEventPublisher; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * 스토리지 업로드(트랜잭션 밖)를 수행한 뒤 후속 작업을 실행하고, | ||
| * 후속 작업이 실패하면 업로드한 객체를 비동기로 삭제해 롤백한다. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class UploadWithRollback { | ||
|
|
||
| private final BinaryContentService binaryContentService; | ||
| private final ApplicationEventPublisher eventPublisher; | ||
|
|
||
| public <T> T execute( | ||
| StorageDirectory directory, | ||
| FileRequest image, | ||
| Function<UploadedBinaryContent, T> persist | ||
| ) { | ||
| UploadedBinaryContent uploaded = null; | ||
| try { | ||
| if (image != null) { | ||
| uploaded = binaryContentService.uploadToStorage(directory, image); | ||
| } | ||
| return persist.apply(uploaded); | ||
| } catch (RuntimeException e) { | ||
| if (uploaded != null) { | ||
| eventPublisher.publishEvent(new BinaryContentDeleteEvent(uploaded)); | ||
| } | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
18 changes: 17 additions & 1 deletion
18
src/main/java/com/codeit/team5/mopl/binarycontent/storage/s3/S3StorageProperties.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,29 @@ | ||
| package com.codeit.team5.mopl.binarycontent.storage.s3; | ||
|
|
||
| import java.time.Duration; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "mopl.storage.s3") | ||
| public record S3StorageProperties( | ||
| String accessKey, | ||
| String secretKey, | ||
| String bucketName, | ||
| String region | ||
| String region, | ||
| Duration apiCallTimeout, | ||
| Duration apiCallAttemptTimeout | ||
| ) { | ||
|
|
||
| public S3StorageProperties { | ||
| if (apiCallTimeout == null) { | ||
| apiCallTimeout = Duration.ofSeconds(10); | ||
| } | ||
| if (apiCallAttemptTimeout == null) { | ||
| apiCallAttemptTimeout = Duration.ofSeconds(5); | ||
| } | ||
| if (apiCallAttemptTimeout.compareTo(apiCallTimeout) > 0) { | ||
| throw new IllegalArgumentException( | ||
| "apiCallAttemptTimeout(" + apiCallAttemptTimeout | ||
| + ")은 apiCallTimeout(" + apiCallTimeout + ")보다 클 수 없습니다."); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/codeit/team5/mopl/content/facade/ContentFacade.java
This file contains hidden or 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,30 @@ | ||
| package com.codeit.team5.mopl.content.facade; | ||
|
|
||
| import com.codeit.team5.mopl.binarycontent.service.UploadWithRollback; | ||
| import com.codeit.team5.mopl.binarycontent.storage.StorageDirectory; | ||
| import com.codeit.team5.mopl.content.dto.request.ContentCreateRequest; | ||
| import com.codeit.team5.mopl.content.dto.request.ContentUpdateRequest; | ||
| import com.codeit.team5.mopl.content.dto.response.ContentResponse; | ||
| import com.codeit.team5.mopl.content.service.ContentService; | ||
| import com.codeit.team5.mopl.global.dto.FileRequest; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class ContentFacade { | ||
|
|
||
| private final ContentService contentService; | ||
| private final UploadWithRollback uploadWithRollback; | ||
|
|
||
| public ContentResponse create(ContentCreateRequest request, FileRequest image) { | ||
| return uploadWithRollback.execute(StorageDirectory.THUMBNAIL, image, | ||
| uploaded -> contentService.create(request, uploaded)); | ||
| } | ||
|
|
||
| public ContentResponse update(UUID contentId, ContentUpdateRequest request, FileRequest image) { | ||
| return uploadWithRollback.execute(StorageDirectory.THUMBNAIL, image, | ||
| uploaded -> contentService.update(contentId, request, uploaded)); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.