261104_03_HOT: 트랜젝션 누수 관련 긴급 조치 2
This commit is contained in:
@@ -14,6 +14,8 @@ public interface DerivedMapper {
|
||||
|
||||
String lockProject(@Param("pkey") String pkey);
|
||||
|
||||
boolean projectExists(@Param("pkey") String pkey);
|
||||
|
||||
List<DerivedDefinitionDto> definitions(@Param("pkey") String pkey);
|
||||
|
||||
DerivedDefinitionDto find(@Param("pkey") String pkey, @Param("qid") String qid);
|
||||
|
||||
@@ -27,6 +27,10 @@ public class DerivedRepository {
|
||||
.lockProject(project.pkey());
|
||||
}
|
||||
|
||||
public boolean projectExists(ProjectRefDto project) {
|
||||
return surveySessionProvider.mapper(project, DerivedMapper.class).projectExists(project.pkey());
|
||||
}
|
||||
|
||||
public List<DerivedDefinitionDto> definitions(ProjectRefDto project) {
|
||||
return surveySessionProvider.mapper(project, DerivedMapper.class)
|
||||
.definitions(project.pkey());
|
||||
|
||||
@@ -6,12 +6,15 @@ import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import com.kri.kart.server.common.enums.ProjectType;
|
||||
import com.kri.kart.server.dto.derived.DerivedCalculationDto;
|
||||
import com.kri.kart.server.dto.derived.DerivedDataDto;
|
||||
import com.kri.kart.server.dto.derived.DerivedDefinitionDto;
|
||||
import com.kri.kart.server.dto.derived.DerivedSaveRequestDto;
|
||||
import com.kri.kart.server.dto.project.ProjectRefDto;
|
||||
import com.kri.kart.server.dto.question.QuestionDto;
|
||||
import com.kri.kart.server.persistence.repository.DerivedRepository;
|
||||
import com.kri.kart.server.service.derived.DerivedComputationService;
|
||||
import com.kri.kart.server.service.derived.DerivedRecalculationGuard;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
@@ -156,23 +159,62 @@ public class DerivedManagementService {
|
||||
if (idkey == null || idkey.isBlank() || idkey.length() > 512) {
|
||||
throw BusinessException.badRequest("응답자 ID를 확인해 주세요.");
|
||||
}
|
||||
return derivedRecalculationGuard.execute(project, () -> transaction(
|
||||
project,
|
||||
() -> {
|
||||
requireProject(project);
|
||||
// Legacy creates an empty DERIVED_DATA row even if there is no eligible
|
||||
// response.
|
||||
return derivedRecalculationGuard.execute(project, idkey, () -> {
|
||||
if (!derivedRepository.projectExists(project)) {
|
||||
throw BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.");
|
||||
}
|
||||
List<DerivedDefinitionDto> definitions = derivedRepository.definitions(project);
|
||||
List<String> derivedIds = derivedIds(definitions);
|
||||
List<QuestionDto> questions = definitions.isEmpty() ? List.of() : questionService.list(project, 1001);
|
||||
List<DerivedDataDto> data = definitions.isEmpty() ? List.of()
|
||||
: derivedRepository.respondentResponses(project, idkey);
|
||||
if (data.size() > 1) {
|
||||
// Legacy joins can return multiple rows for one ID. Preserve their database reread
|
||||
// semantics rather than assuming uniqueness or collapsing results in memory.
|
||||
return transaction(project, () -> {
|
||||
derivedRepository.ensureRespondent(project, idkey);
|
||||
var definitions = derivedRepository.definitions(project);
|
||||
var derivedIds = derivedIds(definitions);
|
||||
int matched = 0;
|
||||
for (var definition : definitions) {
|
||||
matched += calculate(project, definition, idkey, derivedIds);
|
||||
for (DerivedDefinitionDto definition : definitions) {
|
||||
matched += calculate(project, definition, idkey, derivedIds, questions);
|
||||
}
|
||||
return new DerivedCalculationDto(definitions.size(), matched);
|
||||
}));
|
||||
});
|
||||
}
|
||||
definitions.forEach(definition -> validateId(definition.getQid()));
|
||||
var results = derivedComputationService.calculateRespondent(definitions, questions, data, derivedIds);
|
||||
List<RespondentWrite> writes = new ArrayList<>();
|
||||
int matched = 0;
|
||||
for (int variable = 0; variable < definitions.size(); variable++) {
|
||||
List<String> groups = new ArrayList<>();
|
||||
for (var group : results.get(variable)) {
|
||||
groups.add(group.isEmpty() ? null : JsonUtil.toJson(objectMapper, group));
|
||||
if (!group.isEmpty()) {
|
||||
matched++;
|
||||
}
|
||||
}
|
||||
writes.add(new RespondentWrite(definitions.get(variable).getQid(), groups));
|
||||
}
|
||||
int matchedRespondents = matched;
|
||||
return transaction(project, () -> {
|
||||
// Keep the original empty-row contract even when there are no definitions or responses.
|
||||
derivedRepository.ensureRespondent(project, idkey);
|
||||
for (RespondentWrite write : writes) {
|
||||
if (write.groups().isEmpty()) {
|
||||
derivedRepository.clearRespondentData(project, write.qid(), idkey);
|
||||
} else {
|
||||
for (String group : write.groups()) {
|
||||
// JSON_MODIFY updates only this variable on the current row; never replace DATJSON.
|
||||
derivedRepository.saveData(project, write.qid(), idkey, group);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new DerivedCalculationDto(definitions.size(), matchedRespondents);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private record RespondentWrite(String qid, List<String> groups) {}
|
||||
|
||||
/** Stored derived variable IDs; uploaded response columns with these IDs are not imported. */
|
||||
public List<String> derivedIds(ProjectRefDto project) {
|
||||
return derivedIds(derivedRepository.definitions(project));
|
||||
@@ -205,6 +247,12 @@ public class DerivedManagementService {
|
||||
DerivedDefinitionDto definition,
|
||||
String idkey,
|
||||
List<String> derivedIds) {
|
||||
return calculate(project, definition, idkey, derivedIds, null);
|
||||
}
|
||||
|
||||
private int calculate(
|
||||
ProjectRefDto project, DerivedDefinitionDto definition, String idkey,
|
||||
List<String> derivedIds, List<QuestionDto> questions) {
|
||||
validateId(definition.getQid());
|
||||
if (idkey == null) {
|
||||
derivedRepository.clearData(project, definition.getQid());
|
||||
@@ -217,7 +265,8 @@ public class DerivedManagementService {
|
||||
: derivedRepository.respondentResponses(project, idkey);
|
||||
var results =
|
||||
derivedComputationService.calculate(
|
||||
definition, questionService.list(project, idkey == null ? 0 : 1001), data, derivedIds);
|
||||
definition, questions == null ? questionService.list(project, idkey == null ? 0 : 1001) : questions,
|
||||
data, derivedIds);
|
||||
int matched = 0;
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
var group = results.get(i);
|
||||
|
||||
@@ -134,6 +134,37 @@ public class DerivedComputationService {
|
||||
return results;
|
||||
}
|
||||
|
||||
/** Reuse one respondent snapshot, feeding preceding variable results into later conditions. */
|
||||
public List<List<ObjectNode>> calculateRespondent(
|
||||
List<DerivedDefinitionDto> definitions, List<QuestionDto> questions,
|
||||
List<DerivedDataDto> data, Collection<String> derivedIds) {
|
||||
List<DerivedDataDto> working = new ArrayList<>();
|
||||
for (DerivedDataDto row : data) {
|
||||
DerivedDataDto copy = new DerivedDataDto();
|
||||
copy.setIdkey(row.getIdkey());
|
||||
copy.setDatjson(row.getDatjson());
|
||||
copy.setDerivedJson(row.getDerivedJson());
|
||||
working.add(copy);
|
||||
}
|
||||
List<List<ObjectNode>> prepared = new ArrayList<>();
|
||||
for (DerivedDefinitionDto definition : definitions) {
|
||||
List<ObjectNode> groups = calculate(definition, questions, working, derivedIds);
|
||||
prepared.add(groups);
|
||||
for (int row = 0; row < working.size(); row++) {
|
||||
DerivedDataDto response = working.get(row);
|
||||
ObjectNode derived = response.getDerivedJson() == null
|
||||
? objectMapper.createObjectNode() : object(response.getDerivedJson());
|
||||
if (groups.get(row).isEmpty()) {
|
||||
derived.remove(definition.getQid());
|
||||
} else {
|
||||
derived.set(definition.getQid(), groups.get(row));
|
||||
}
|
||||
response.setDerivedJson(JsonUtil.toJson(objectMapper, derived));
|
||||
}
|
||||
}
|
||||
return prepared;
|
||||
}
|
||||
|
||||
private void flatten(ObjectNode grouped, Map<String, String> flat) {
|
||||
grouped
|
||||
.fields()
|
||||
|
||||
@@ -1,56 +1,41 @@
|
||||
package com.kri.kart.server.service.derived;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import com.kri.kart.server.common.enums.ProjectType;
|
||||
import com.kri.kart.server.config.KartDatabaseProperties;
|
||||
import com.kri.kart.server.dto.project.ProjectRefDto;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** Wait before borrowing JDBC connections; database locks still enforce cross-instance safety. */
|
||||
/** Serialize duplicate respondent recalculations before reading their response snapshots. */
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
|
||||
public class DerivedRecalculationGuard {
|
||||
private final ConcurrentHashMap<ProjectRefDto, ProjectGate> projects = new ConcurrentHashMap<>();
|
||||
private final Semaphore kon;
|
||||
private final Semaphore konkiwi;
|
||||
private final ConcurrentHashMap<RespondentKey, RespondentGate> respondents = new ConcurrentHashMap<>();
|
||||
private final long waitNanos;
|
||||
|
||||
@Autowired
|
||||
public DerivedRecalculationGuard(KartDatabaseProperties properties) {
|
||||
this(properties, TimeUnit.SECONDS.toNanos(10));
|
||||
public DerivedRecalculationGuard() {
|
||||
this(TimeUnit.SECONDS.toNanos(10));
|
||||
}
|
||||
|
||||
DerivedRecalculationGuard(KartDatabaseProperties properties, long waitNanos) {
|
||||
// Reserve one connection for access checks and other operations when pool size permits.
|
||||
this.kon = new Semaphore(Math.max(1, properties.getKon().getMaximumPoolSize() - 1), true);
|
||||
this.konkiwi = new Semaphore(Math.max(1, properties.getKonkiwi().getMaximumPoolSize() - 1), true);
|
||||
DerivedRecalculationGuard(long waitNanos) {
|
||||
this.waitNanos = waitNanos;
|
||||
}
|
||||
|
||||
public <T> T execute(ProjectRefDto project, Supplier<T> action) {
|
||||
long deadline = System.nanoTime() + waitNanos;
|
||||
ProjectGate gate = projects.compute(project, (key, current) -> {
|
||||
ProjectGate selected = current == null ? new ProjectGate() : current;
|
||||
public <T> T execute(ProjectRefDto project, String idkey, Supplier<T> action) {
|
||||
RespondentKey respondent = new RespondentKey(project, idkey);
|
||||
RespondentGate gate = respondents.compute(respondent, (key, current) -> {
|
||||
RespondentGate selected = current == null ? new RespondentGate() : current;
|
||||
selected.users++;
|
||||
return selected;
|
||||
});
|
||||
Semaphore source = project.projectType() == ProjectType.KON ? kon : konkiwi;
|
||||
boolean projectAcquired = false;
|
||||
boolean sourceAcquired = false;
|
||||
boolean acquired = false;
|
||||
try {
|
||||
projectAcquired = gate.permit.tryAcquire(remaining(deadline), TimeUnit.NANOSECONDS);
|
||||
if (!projectAcquired) {
|
||||
throw busy();
|
||||
}
|
||||
sourceAcquired = source.tryAcquire(remaining(deadline), TimeUnit.NANOSECONDS);
|
||||
if (!sourceAcquired) {
|
||||
acquired = gate.permit.tryAcquire(waitNanos, TimeUnit.NANOSECONDS);
|
||||
if (!acquired) {
|
||||
throw busy();
|
||||
}
|
||||
return action.get();
|
||||
@@ -58,29 +43,24 @@ public class DerivedRecalculationGuard {
|
||||
Thread.currentThread().interrupt();
|
||||
throw busy();
|
||||
} finally {
|
||||
if (sourceAcquired) {
|
||||
source.release();
|
||||
}
|
||||
if (projectAcquired) {
|
||||
if (acquired) {
|
||||
gate.permit.release();
|
||||
}
|
||||
projects.computeIfPresent(project, (key, current) -> --current.users == 0 ? null : current);
|
||||
respondents.computeIfPresent(respondent, (key, current) -> --current.users == 0 ? null : current);
|
||||
}
|
||||
}
|
||||
|
||||
private long remaining(long deadline) {
|
||||
return Math.max(0, deadline - System.nanoTime());
|
||||
}
|
||||
|
||||
private BusinessException busy() {
|
||||
return BusinessException.of(
|
||||
HttpStatus.SERVICE_UNAVAILABLE, "DERIVED_RECALCULATION_BUSY",
|
||||
"파생변수 재계산 요청이 많습니다. 잠시 후 다시 요청해 주세요.");
|
||||
}
|
||||
|
||||
private static final class ProjectGate {
|
||||
private record RespondentKey(ProjectRefDto project, String idkey) {}
|
||||
|
||||
private static final class RespondentGate {
|
||||
private final Semaphore permit = new Semaphore(1, true);
|
||||
// Accessed only inside ConcurrentHashMap.compute/computeIfPresent for the project.
|
||||
// Accessed only inside ConcurrentHashMap.compute/computeIfPresent for the respondent.
|
||||
private int users;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@
|
||||
<select id="lockProject" databaseId="survey" resultType="string">
|
||||
SELECT PKEY FROM PROJECT WITH(UPDLOCK, HOLDLOCK) WHERE PKEY = #{pkey}
|
||||
</select>
|
||||
<select id="projectExists" databaseId="survey" resultType="boolean">
|
||||
SELECT CASE WHEN EXISTS (SELECT 1 FROM PROJECT WHERE PKEY = #{pkey}) THEN 1 ELSE 0 END
|
||||
</select>
|
||||
<select id="definitions" databaseId="survey" resultType="com.kri.kart.server.dto.derived.DerivedDefinitionDto">
|
||||
SELECT QID, QUESTIONJSON AS question_json, DERIVEDJSON AS derived_json, Qorder AS q_order
|
||||
FROM DERIVED_VARIABLE WHERE PKEY = #{pkey} ORDER BY Qorder
|
||||
|
||||
Reference in New Issue
Block a user