|
|
|
|
@@ -13,6 +13,7 @@ import com.kri.contact.server.common.enums.ListHeaderGrade;
|
|
|
|
|
import com.kri.contact.server.common.enums.ListHeaderType;
|
|
|
|
|
import com.kri.contact.server.common.enums.ListIsUse;
|
|
|
|
|
import com.kri.contact.server.common.enums.ResultCodeType;
|
|
|
|
|
import com.kri.contact.server.config.ListEventHistoryProperties;
|
|
|
|
|
import com.kri.contact.server.dto.list.ListEventHistFromSurveyDto;
|
|
|
|
|
import com.kri.contact.server.dto.list.ListEventHistInternalMetaDto;
|
|
|
|
|
import com.kri.contact.server.dto.list.ListEventHistInsertRequestDto;
|
|
|
|
|
@@ -36,6 +37,7 @@ import com.kri.contact.server.persistence.support.ListEventHistDeleteSupport;
|
|
|
|
|
import com.kri.contact.server.persistence.support.ListItemDeleteSupport;
|
|
|
|
|
import com.kri.contact.server.persistence.support.ListItemDeleteSupport.FileToDelete;
|
|
|
|
|
import com.kri.contact.server.persistence.repository.ListEventHistRepository;
|
|
|
|
|
import com.kri.contact.server.persistence.repository.ListEventLatestRepository;
|
|
|
|
|
import com.kri.contact.server.persistence.repository.ListHeaderRepository;
|
|
|
|
|
import com.kri.contact.server.persistence.repository.ListRepository;
|
|
|
|
|
import com.kri.contact.server.persistence.repository.ProjectSurveyRepository;
|
|
|
|
|
@@ -51,9 +53,14 @@ import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.transaction.support.TransactionSynchronization;
|
|
|
|
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@@ -70,6 +77,8 @@ public class ListUpdateService {
|
|
|
|
|
private final ListUpdateJdbcSupport listUpdateJdbcSupport;
|
|
|
|
|
private final ListRepository listRepository;
|
|
|
|
|
private final ListEventHistRepository listEventHistRepository;
|
|
|
|
|
private final ListEventLatestRepository listEventLatestRepository;
|
|
|
|
|
private final ListEventHistoryProperties listEventHistoryProperties;
|
|
|
|
|
private final ListEventHistDeleteSupport listEventHistDeleteSupport;
|
|
|
|
|
private final ListItemDeleteSupport listItemDeleteSupport;
|
|
|
|
|
private final ProjectSurveyRepository projectSurveyRepository;
|
|
|
|
|
@@ -78,6 +87,9 @@ public class ListUpdateService {
|
|
|
|
|
private final ListCallHistService listCallHistService;
|
|
|
|
|
private final ReserveService reserveService;
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
// 같은 설문과 SID의 요청만 직렬화한다. 잠금 대기자도 참조 수에 포함해 객체가 교체되지 않게 한다.
|
|
|
|
|
private final ConcurrentHashMap<SurveyHistoryKey, SurveyHistoryLock> surveyHistoryLocks =
|
|
|
|
|
new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public int updateList(
|
|
|
|
|
@@ -195,12 +207,15 @@ public class ListUpdateService {
|
|
|
|
|
String regUser,
|
|
|
|
|
LocalDateTime regDt
|
|
|
|
|
) {
|
|
|
|
|
// COMPLETE가 먼저 커밋했다면 뒤따르는 START가 이를 확인할 수 있도록 조회 전에 잠근다.
|
|
|
|
|
lockSurveyHistoryUntilTransactionCompletion(request);
|
|
|
|
|
|
|
|
|
|
TPrjctSurveyEntity survey = validateProjectSurvey(request);
|
|
|
|
|
validateEventHistoryTarget(survey.getPrjctIdx(), request.getSid());
|
|
|
|
|
applySurveySnapshot(request, survey);
|
|
|
|
|
|
|
|
|
|
if (request.getAction() == ListEventHistorySurveyType.START
|
|
|
|
|
&& listEventHistRepository.existsByPrjctIdxAndPrjctSurveyIdxAndSidAndEventTyAndResultCdNot(
|
|
|
|
|
&& listEventLatestRepository.existsByPrjctIdxAndPrjctSurveyIdxAndSidAndEventTyAndResultCdNot(
|
|
|
|
|
survey.getPrjctIdx(),
|
|
|
|
|
request.getPrjctSurveyIdx(),
|
|
|
|
|
request.getSid(),
|
|
|
|
|
@@ -219,6 +234,67 @@ public class ListUpdateService {
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void lockSurveyHistoryUntilTransactionCompletion(ListEventHistFromSurveyDto request) {
|
|
|
|
|
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
|
|
|
|
|
throw new IllegalStateException("설문 이력 처리는 활성 트랜잭션 동기화가 필요합니다.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SurveyHistoryKey key = new SurveyHistoryKey(request.getPrjctSurveyIdx(), request.getSid());
|
|
|
|
|
// acquire 전에 참조를 등록해야 마지막 보유자가 잠금 객체를 제거하는 경쟁을 막을 수 있다.
|
|
|
|
|
SurveyHistoryLock lock = surveyHistoryLocks.compute(key, (ignored, current) -> {
|
|
|
|
|
SurveyHistoryLock selected = current == null ? new SurveyHistoryLock() : current;
|
|
|
|
|
selected.users++;
|
|
|
|
|
return selected;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 각 요청은 같은 설문과 SID의 선행 트랜잭션을 설정된 시간까지만 기다린다.
|
|
|
|
|
long timeoutSeconds = listEventHistoryProperties.getSurveyLockWaitTimeoutSeconds();
|
|
|
|
|
if (!lock.semaphore.tryAcquire(timeoutSeconds, TimeUnit.SECONDS)) {
|
|
|
|
|
releaseSurveyHistoryLockReference(key, lock);
|
|
|
|
|
throw new IllegalStateException("설문 이력 잠금 대기 시간이 " + timeoutSeconds + "초를 초과했습니다.");
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
// 허가를 얻지 못했으므로 semaphore.release() 없이 참조만 정리한다.
|
|
|
|
|
releaseSurveyHistoryLockReference(key, lock);
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
throw new IllegalStateException("설문 이력 잠금 대기 중 요청이 중단되었습니다.", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// @Transactional의 실제 커밋은 메서드 반환 후에 일어나므로 완료 콜백에서 잠금을 푼다.
|
|
|
|
|
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
|
|
|
|
@Override
|
|
|
|
|
public void afterCompletion(int status) {
|
|
|
|
|
lock.semaphore.release();
|
|
|
|
|
releaseSurveyHistoryLockReference(key, lock);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
// 콜백 등록에 실패하면 트랜잭션 완료 시 해제할 수 없으므로 즉시 정리한다.
|
|
|
|
|
lock.semaphore.release();
|
|
|
|
|
releaseSurveyHistoryLockReference(key, lock);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void releaseSurveyHistoryLockReference(SurveyHistoryKey key, SurveyHistoryLock lock) {
|
|
|
|
|
// 대기자와 보유자가 모두 사라진 키만 제거해 잠금 맵이 계속 커지지 않게 한다.
|
|
|
|
|
surveyHistoryLocks.compute(key, (ignored, current) -> {
|
|
|
|
|
if (current != lock) {
|
|
|
|
|
throw new IllegalStateException("설문 이력 잠금 참조가 예상과 다르게 변경되었습니다.");
|
|
|
|
|
}
|
|
|
|
|
return --current.users == 0 ? null : current;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private record SurveyHistoryKey(Long prjctSurveyIdx, String sid) {}
|
|
|
|
|
|
|
|
|
|
private static final class SurveyHistoryLock {
|
|
|
|
|
private final Semaphore semaphore = new Semaphore(1, true);
|
|
|
|
|
private int users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateListUpdateRequest(ListUpdateRequestDto request){
|
|
|
|
|
if(
|
|
|
|
|
CollectionUtil.isEmpty(request.getUpdateContents()) &&
|
|
|
|
|
|