init
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
|
||||
import com.eactive.eai.data.entity.AbstractEntity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
|
||||
|
||||
@Entity
|
||||
@Table(name = "current_commit_id")
|
||||
@org.hibernate.annotations.Table(appliesTo = "current_commit_id", comment = "현재 Commit id를 저장")
|
||||
public class CurrentCommitId extends AbstractEntity<Long> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final Long CURRENT_COMMIT_ID_ID = 1l;
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.PERSIST)
|
||||
@Comment("현재 Commit id")
|
||||
private RollingSuffix rollingSuffix;
|
||||
|
||||
@CreatedDate
|
||||
private LocalDateTime createdDate;
|
||||
|
||||
@LastModifiedDate
|
||||
private LocalDateTime lastModifiedDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import com.eactive.eai.data.jpa.BaseRepository;
|
||||
|
||||
public interface CurrentCommitIdRepository extends BaseRepository<CurrentCommitId, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.eactive.eai.data.jpa.AbstractDataService;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class CurrentCommitIdService extends AbstractDataService<CurrentCommitId, Long, CurrentCommitIdRepository> {
|
||||
|
||||
@Value("#{systemEnvironment['COMMIT_ID'] ?: systemProperties['COMMIT_ID']}")
|
||||
private String systemCommitId;
|
||||
|
||||
public CurrentCommitId currentCommitId() {
|
||||
return repository.getReferenceById(CurrentCommitId.CURRENT_COMMIT_ID_ID);
|
||||
}
|
||||
|
||||
public String getCurrentSuffix() {
|
||||
return currentCommitId().getRollingSuffix().getSuffix();
|
||||
}
|
||||
|
||||
public String getCurrentCommitId() {
|
||||
return currentCommitId().getRollingSuffix().getCommitId();
|
||||
}
|
||||
|
||||
public String getSystemCommitId() {
|
||||
return systemCommitId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.TableGenerator;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
|
||||
import com.eactive.eai.data.entity.AbstractEntity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)
|
||||
|
||||
@Entity
|
||||
@Table(name = "rolling_suffix")
|
||||
@org.hibernate.annotations.Table(appliesTo = "rolling_suffix", comment = "현재 commit id의 suffix를 매핑")
|
||||
public class RollingSuffix extends AbstractEntity<Long> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@TableGenerator(name = "RollingSuffix", table = "TSEAISQ01", initialValue = 1, allocationSize = 1)
|
||||
@GeneratedValue(strategy = GenerationType.TABLE, generator = "RollingSuffix")
|
||||
private Long id;
|
||||
|
||||
private String commitId;
|
||||
|
||||
@Comment("테이블 suffix")
|
||||
private String suffix;
|
||||
|
||||
@CreatedDate
|
||||
private LocalDateTime createdDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
|
||||
import com.eactive.eai.data.jpa.BaseRepository;
|
||||
|
||||
public interface RollingSuffixRepository
|
||||
extends BaseRepository<RollingSuffix, Long>, QuerydslPredicateExecutor<RollingSuffix> {
|
||||
|
||||
Optional<RollingSuffix> findTopByOrderByIdDesc();
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.eactive.eai.data.jpa.AbstractDataService;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class RollingSuffixService extends AbstractDataService<RollingSuffix, Long, RollingSuffixRepository> {
|
||||
|
||||
@Autowired
|
||||
private CurrentCommitIdRepository currentCommitIdRepository;
|
||||
|
||||
public String getCurrentSuffixByCommitId(String currentCommitId) {
|
||||
return repository
|
||||
.findOne(QRollingSuffix.rollingSuffix.commitId.eq(currentCommitId))
|
||||
.map(r -> r.getSuffix())
|
||||
.orElseThrow(() -> new EntityNotFoundException("commit id를 찾을 수 없습니다 : " + currentCommitId));
|
||||
}
|
||||
|
||||
public void insertRollingSuffixAndUpdateCurrentCommitId(String newCommitId) {
|
||||
RollingSuffix newRollingSuffix = createNewRollingSuffix(newCommitId);
|
||||
RollingSuffix savedRollingSuffix = repository.save(newRollingSuffix);
|
||||
updateCurrentCommitId(savedRollingSuffix);
|
||||
}
|
||||
|
||||
private RollingSuffix createNewRollingSuffix(String commitId) {
|
||||
RollingSuffix lastRollingSuffix = repository.findTopByOrderByIdDesc().orElse(new RollingSuffix());
|
||||
String newSuffix = calculateNextSuffix(lastRollingSuffix.getSuffix());
|
||||
|
||||
RollingSuffix newRollingSuffix = new RollingSuffix();
|
||||
newRollingSuffix.setSuffix(newSuffix);
|
||||
newRollingSuffix.setCommitId(commitId);
|
||||
return newRollingSuffix;
|
||||
}
|
||||
|
||||
private void updateCurrentCommitId(RollingSuffix rollingSuffix) {
|
||||
CurrentCommitId currentCommitId = currentCommitIdRepository.findById(CurrentCommitId.CURRENT_COMMIT_ID_ID)
|
||||
.orElseThrow(() -> new RuntimeException("CurrentCommitId not found"));
|
||||
currentCommitId.setRollingSuffix(rollingSuffix);
|
||||
currentCommitIdRepository.save(currentCommitId);
|
||||
}
|
||||
|
||||
private String calculateNextSuffix(String lastSuffix) {
|
||||
int suffixNumber = (lastSuffix == null || lastSuffix.isEmpty()) ? 0 : Integer.parseInt(lastSuffix.replace("_", ""));
|
||||
return String.format("_%02d", (suffixNumber % 5) + 1);
|
||||
}
|
||||
|
||||
public String insertRollingSuffix(String newCommitId) {
|
||||
RollingSuffix newRollingSuffix = createNewRollingSuffix(newCommitId);
|
||||
return repository.save(newRollingSuffix).getSuffix();
|
||||
}
|
||||
|
||||
public void changeCurrentCommitId(String commitId) {
|
||||
RollingSuffix targetRollingSuffix = repository.findOne(QRollingSuffix.rollingSuffix.commitId.eq(commitId))
|
||||
.orElseThrow(() -> new EntityNotFoundException("RollingSuffix not found with commitId: " + commitId));
|
||||
updateCurrentCommitId(targetRollingSuffix);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
import org.hibernate.Interceptor;
|
||||
|
||||
public interface RollingTableInterceptor extends Interceptor {
|
||||
|
||||
@Override
|
||||
default String onPrepareStatement(String sql) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.eactive.kakao.rolling;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TableSuffixResolver {
|
||||
|
||||
public String getCurrentSuffix();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.eactive.kakao.rolling.impl;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.EmptyInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import com.eactive.kakao.rolling.RollingTableInterceptor;
|
||||
import com.eactive.kakao.rolling.TableSuffixResolver;
|
||||
|
||||
public class RollingTableInterceptorImpl extends EmptyInterceptor implements RollingTableInterceptor {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected static final String[] TARGET_TABLE_NAMES = new String[] { "tseaihe01", "tseaihe02", "tseaims02",
|
||||
"tseaifr01", "tseaifr03", "tseaihs04", "tseaihs05", "tseaifr06", "tseaitr07", "tseaitr08", "tseaitr01",
|
||||
"tseaitr02", "tseaitr03", "tseaihs14", "tseaihs15" };
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
private transient TableSuffixResolver tableSuffixResolver;
|
||||
|
||||
@Override
|
||||
public String onPrepareStatement(String sql) {
|
||||
|
||||
if (!StringUtils.containsAny(sql, TARGET_TABLE_NAMES)) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
String currentSuffix = tableSuffixResolver.getCurrentSuffix();
|
||||
for (String tableName : TARGET_TABLE_NAMES) {
|
||||
if (StringUtils.contains(sql, tableName)) {
|
||||
sql = StringUtils.replace(sql, tableName, tableName + currentSuffix);
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.eactive.kakao.rolling.impl;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.eactive.kakao.rolling.CurrentCommitIdService;
|
||||
import com.eactive.kakao.rolling.RollingSuffixService;
|
||||
import com.eactive.kakao.rolling.TableSuffixResolver;
|
||||
|
||||
public class TableSuffixResolverImpl implements TableSuffixResolver {
|
||||
|
||||
private String currentSuffix;
|
||||
|
||||
@Autowired
|
||||
private RollingSuffixService rollingSuffixService;
|
||||
|
||||
@Autowired
|
||||
private CurrentCommitIdService currentCommitIdService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
String systemCommitId = currentCommitIdService.getSystemCommitId();
|
||||
currentSuffix = rollingSuffixService.getCurrentSuffixByCommitId(systemCommitId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentSuffix() {
|
||||
return currentSuffix;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.eactive.kakao.rolling.servlet;
|
||||
|
||||
import com.eactive.eai.adapter.socket2.config.SocketAdapterManager;
|
||||
import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
|
||||
import com.eactive.eai.adapter.socket2.protocol.http.HttpServlet;
|
||||
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpRequest;
|
||||
import com.eactive.eai.adapter.socket2.protocol.http.codec.HttpResponse;
|
||||
import com.eactive.eai.common.util.ApplicationContextProvider;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.kakao.rolling.CurrentCommitIdService;
|
||||
import com.eactive.kakao.rolling.TableSuffixResolver;
|
||||
|
||||
public class RollingServlet extends HttpServlet {
|
||||
static final SocketAdapterManager manager = SocketAdapterManager.getInstance();
|
||||
static Logger logger = SocketLoggerManager.getInstance().getLogger("FileLogger{NET}_HTTP");
|
||||
|
||||
protected void doService(HttpRequest request, HttpResponse response) throws Exception {
|
||||
String command = request.getContext();
|
||||
if (command.lastIndexOf('/') == -1) {
|
||||
command = "";
|
||||
} else {
|
||||
command = command.substring(command.indexOf('/')+1);
|
||||
}
|
||||
String result = "";
|
||||
|
||||
try {
|
||||
if (command == null || "".equals(command) || "commitid".equals(command)) {
|
||||
result = getCommitId();
|
||||
} else if ("suffix".equals(command)) {
|
||||
result = getSuffix();
|
||||
} else {
|
||||
response.setResponseCode(404);
|
||||
return;
|
||||
}
|
||||
} catch(Throwable e) {
|
||||
result = "{ \"error\" : \"" + e.getMessage() + "\"}";
|
||||
}
|
||||
|
||||
response.setContentType("application/json;charset=utf-8");
|
||||
response.setResponseCode(HttpResponse.HTTP_STATUS_SUCCESS);
|
||||
response.appendBody(result);
|
||||
}
|
||||
private String getCommitId() throws Exception {
|
||||
CurrentCommitIdService service = ApplicationContextProvider.getContext().getBean(CurrentCommitIdService.class);
|
||||
String commitId = service.getSystemCommitId();
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("{\"commitId\" : \"").append(commitId).append("\"}");
|
||||
return sb.toString();
|
||||
}
|
||||
private String getSuffix() throws Exception {
|
||||
TableSuffixResolver service = ApplicationContextProvider.getContext().getBean(TableSuffixResolver.class);
|
||||
String suffix = service.getCurrentSuffix();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("{\"suffix\" : \"").append(suffix).append("\"}");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user