Skip to content

Commit

Permalink
Merge pull request #68 from f-lab-edu/feature/#65
Browse files Browse the repository at this point in the history
[#65] 변경사항 반영
  • Loading branch information
CEO-Nick authored Jul 25, 2024
2 parents 61376ca + cdab9f1 commit ea76289
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
public class SnowflakeConfig {

@Bean
public Snowflake snowflake0() {
return Snowflake.getInstance(0);
}

@Bean
public Snowflake snowflake1() {
return Snowflake.getInstance(1);
public Snowflake snowflake() {
return new Snowflake();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.outstagram.outstagram.kafka.consumer;

import static com.outstagram.outstagram.common.constant.DBConst.DB_COUNT;
import static com.outstagram.outstagram.common.constant.KafkaConst.NOTIFICATION_GROUPID;
import static com.outstagram.outstagram.common.constant.KafkaConst.SEND_NOTIFICATION;
import static com.outstagram.outstagram.dto.AlarmType.COMMENT;

import com.outstagram.outstagram.dto.CommentDTO;
import com.outstagram.outstagram.dto.NotificationDTO;
import com.outstagram.outstagram.dto.PostDTO;
Expand All @@ -8,20 +13,15 @@
import com.outstagram.outstagram.service.CommentService;
import com.outstagram.outstagram.service.NotificationService;
import com.outstagram.outstagram.service.PostService;
import com.outstagram.outstagram.util.SnowflakeIdGenerator;
import com.outstagram.outstagram.util.Snowflake;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.outstagram.outstagram.common.constant.KafkaConst.NOTIFICATION_GROUPID;
import static com.outstagram.outstagram.common.constant.KafkaConst.SEND_NOTIFICATION;
import static com.outstagram.outstagram.dto.AlarmType.COMMENT;

@Component
@RequiredArgsConstructor
@Slf4j
Expand All @@ -33,7 +33,7 @@ public class NotificationConsumer {

private final CommentService commentService;

private final SnowflakeIdGenerator idGenerator;
private final Snowflake snowflake;

// TODO : toId에게 이메일 보내는걸로 리팩토링
@KafkaListener(topics = SEND_NOTIFICATION, groupId = NOTIFICATION_GROUPID, containerFactory = "notificationKafkaListenerContainerFactory")
Expand Down Expand Up @@ -80,7 +80,7 @@ public void receive(@Payload NotificationDTO notification) {
}

for (NotificationDTO noti : notificationsToSend) {
long notiId = idGenerator.snowflakeIdGenerator(noti.getFromId());
long notiId = snowflake.nextId(noti.getFromId() % DB_COUNT);
noti.setId(notiId);
notificationService.insertNotification(noti);
log.info("Notification[{}] sent: from user {} to user {}, type: {}\n", noti.getId(), noti.getFromId(), noti.getToId(), noti.getAlarmType());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package com.outstagram.outstagram.service;

import static com.outstagram.outstagram.common.constant.CacheConst.IMAGE;

import com.outstagram.outstagram.dto.ImageDTO;
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.mapper.ImageMapper;
import com.outstagram.outstagram.util.SnowflakeIdGenerator;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.outstagram.outstagram.common.constant.CacheConst.IMAGE;

@RequiredArgsConstructor
public abstract class AbstractBaseImageService implements ImageService {

private final ImageMapper imageMapper;

private final SnowflakeIdGenerator idGenerator;

@Transactional
@Override
public void saveImages(List<MultipartFile> imgFiles, Long postId, Long userId) {
public void saveImages(List<MultipartFile> imgFiles, Long postId) {
List<ImageDTO> imageDTOList = new ArrayList<>();

try {
Expand All @@ -39,7 +36,6 @@ public void saveImages(List<MultipartFile> imgFiles, Long postId, Long userId) {

imageDTOList.add(
ImageDTO.builder()
.id(idGenerator.snowflakeIdGenerator(userId))
.postId(postId)
.originalImgName(originName)
.imgUrl(savedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface ImageService {

void saveImages(List<MultipartFile> imgFiles, Long postId, Long userId);
void saveImages(List<MultipartFile> imgFiles, Long postId);

List<ImageDTO> getImageInfos(Long postId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.mapper.ImageMapper;
import com.outstagram.outstagram.util.SnowflakeIdGenerator;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
Expand All @@ -26,8 +24,8 @@ public class LocalImageService extends AbstractBaseImageService {
@Value("com.outstagram.upload.path")
private String uploadPath;

public LocalImageService(ImageMapper imageMapper, SnowflakeIdGenerator idGenerator) {
super(imageMapper, idGenerator);
public LocalImageService(ImageMapper imageMapper) {
super(imageMapper);
}

@PostConstruct
Expand Down
64 changes: 38 additions & 26 deletions src/main/java/com/outstagram/outstagram/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
package com.outstagram.outstagram.service;


import static com.outstagram.outstagram.common.constant.CacheConst.IN_CACHE;
import static com.outstagram.outstagram.common.constant.CacheConst.NOT_FOUND;
import static com.outstagram.outstagram.common.constant.CacheConst.POST;
import static com.outstagram.outstagram.common.constant.DBConst.DB_COUNT;
import static com.outstagram.outstagram.common.constant.KafkaConst.POST_UPSERT_TOPIC;
import static com.outstagram.outstagram.common.constant.KafkaConst.SEND_NOTIFICATION;
import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static com.outstagram.outstagram.common.constant.RedisKeyPrefixConst.FEED;
import static com.outstagram.outstagram.common.constant.RedisKeyPrefixConst.LIKE_COUNT_PREFIX;
import static com.outstagram.outstagram.common.constant.RedisKeyPrefixConst.USER_BOOKMARK_PREFIX;
import static com.outstagram.outstagram.common.constant.RedisKeyPrefixConst.USER_LIKE_PREFIX;
import static com.outstagram.outstagram.dto.AlarmType.COMMENT;
import static com.outstagram.outstagram.dto.AlarmType.LIKE;
import static com.outstagram.outstagram.dto.AlarmType.REPLY;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.outstagram.outstagram.controller.request.CreateCommentReq;
import com.outstagram.outstagram.controller.request.CreatePostReq;
import com.outstagram.outstagram.controller.request.EditCommentReq;
import com.outstagram.outstagram.controller.request.EditPostReq;
import com.outstagram.outstagram.dto.*;
import com.outstagram.outstagram.dto.BookmarkRecordDTO;
import com.outstagram.outstagram.dto.CommentDTO;
import com.outstagram.outstagram.dto.CommentUserDTO;
import com.outstagram.outstagram.dto.ImageDTO;
import com.outstagram.outstagram.dto.LikeCountDTO;
import com.outstagram.outstagram.dto.LikeRecordDTO;
import com.outstagram.outstagram.dto.PostDTO;
import com.outstagram.outstagram.dto.PostDetailsDTO;
import com.outstagram.outstagram.dto.UserDTO;
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.kafka.producer.FeedUpdateProducer;
import com.outstagram.outstagram.kafka.producer.NotificationProducer;
import com.outstagram.outstagram.kafka.producer.PostDeleteProducer;
import com.outstagram.outstagram.kafka.producer.PostProducer;
import com.outstagram.outstagram.mapper.PostMapper;
import com.outstagram.outstagram.util.SnowflakeIdGenerator;
import com.outstagram.outstagram.util.Snowflake;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
Expand All @@ -33,25 +65,11 @@
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.outstagram.outstagram.common.constant.CacheConst.*;
import static com.outstagram.outstagram.common.constant.KafkaConst.POST_UPSERT_TOPIC;
import static com.outstagram.outstagram.common.constant.KafkaConst.SEND_NOTIFICATION;
import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static com.outstagram.outstagram.common.constant.RedisKeyPrefixConst.*;
import static com.outstagram.outstagram.dto.AlarmType.COMMENT;
import static com.outstagram.outstagram.dto.AlarmType.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class PostService {

private final SnowflakeIdGenerator idGenerator;
private final Snowflake snowflake;
private final PostMapper postMapper;
private final ImageService imageService;
private final UserService userService;
Expand Down Expand Up @@ -84,7 +102,7 @@ private static PostDTO validatePostExist(Long postId) {

@Transactional
public void insertPost(CreatePostReq createPostReq, Long userId) {
long postId = idGenerator.snowflakeIdGenerator(userId);
long postId = snowflake.nextId(userId % DB_COUNT);

PostDTO newPost = PostDTO.builder()
.id(postId)
Expand All @@ -98,7 +116,7 @@ public void insertPost(CreatePostReq createPostReq, Long userId) {
postMapper.insertPost(newPost);

// 로컬 디렉토리에 이미지 저장 후, DB에 이미지 정보 저장
imageService.saveImages(createPostReq.getImgFiles(), postId, userId);
imageService.saveImages(createPostReq.getImgFiles(), postId);

// kafka에 메시지 발행 : 팔로워들의 피드목록에 내가 작성한 게시물 ID 넣기
feedUpdateProducer.send("feed", userId, postId);
Expand Down Expand Up @@ -267,8 +285,7 @@ public void editPost(Long postId, EditPostReq editPostReq, Long userId) {

// 추가할 이미지가 있다면 추가하기
if (editPostReq.getImgFiles() != null && !editPostReq.getImgFiles().isEmpty()) {
imageService.saveImages(editPostReq.getImgFiles(),
post.getId(), userId);
imageService.saveImages(editPostReq.getImgFiles(), post.getId());
}

// 수정할 내용이 있다면 수정하기
Expand Down Expand Up @@ -604,11 +621,8 @@ public List<PostDetailsDTO> getBookmarkedPostsPlusOne(Long userId, Long lastId)
public void addComment(CreateCommentReq commentReq, Long postId, Long userId) {
validatePostExist(postId);

long commentId = idGenerator.snowflakeIdGenerator(userId);

// 댓글 객체 생성하기
CommentDTO newComment = CommentDTO.builder()
.id(commentId)
.userId(userId)
.postId(postId)
.parentCommentId(null)
Expand Down Expand Up @@ -636,11 +650,9 @@ public void addComment(CreateCommentReq commentReq, Long postId, Long commentId,
if (comment == null) {
throw new ApiException(ErrorCode.COMMENT_NOT_FOUND);
}
long replyId = idGenerator.snowflakeIdGenerator(userId);

// 대댓글 객체 생성하기
CommentDTO newComment = CommentDTO.builder()
.id(replyId)
.userId(userId)
.postId(postId)
.parentCommentId(commentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.mapper.ImageMapper;
import com.outstagram.outstagram.util.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -26,6 +20,10 @@
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
Expand All @@ -36,8 +34,8 @@ public class S3ImageService extends AbstractBaseImageService {
@Value("${cloud.aws.s3.bucketName}")
private String bucketName;

public S3ImageService(ImageMapper imageMapper, AmazonS3 amazonS3, SnowflakeIdGenerator idGenerator) {
super(imageMapper, idGenerator);
public S3ImageService(ImageMapper imageMapper, AmazonS3 amazonS3) {
super(imageMapper);
this.amazonS3 = amazonS3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@
@Service
@RequiredArgsConstructor
public class UserService {
private final Snowflake snowflake0;
private final Snowflake snowflake1;
private final Snowflake snowflake;
private final UserMapper userMapper;
private final UserProducer userProducer;

private long generateUserId(LocalDateTime now) {
long userId;
if (now.getSecond() % DB_COUNT == 0) {
do {
userId = snowflake0.nextId();
userId = snowflake.nextId(0);
}
while (userId % DB_COUNT != 0);
} else {
do {
userId = snowflake1.nextId();
userId = snowflake.nextId(1);
}
while (userId % DB_COUNT == 0);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/outstagram/outstagram/util/Snowflake.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class Snowflake {

private final long nodeId;
private final long customEpoch;
//private AtomicLong sequence = new AtomicLong(0L);

private volatile long lastTimestamp = -1L;
private volatile long sequence = 0L; // 가시성 보장, 동시성 보장X
Expand Down Expand Up @@ -69,7 +68,7 @@ public static Snowflake getInstance(long nodeId) {
return localInstance;
}

public synchronized long nextId() {
public synchronized long nextId(long nodeId) {
// timestamp 값 구하기
long curTimestamp = timestamp();

Expand Down

This file was deleted.

Loading

0 comments on commit ea76289

Please sign in to comment.