From 20314fd894b0b89b60050b6671a5d23b21550281 Mon Sep 17 00:00:00 2001 From: Baikov Dmitrii <44803026+D-Baykov@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:53:07 +0300 Subject: [PATCH] TD-955: Bump proto (add LimitChange object to proto) (#17) --- README.md | 2 +- pom.xml | 2 +- .../converter/OperationConverter.java | 2 +- .../impl/OperationConverterImpl.java | 4 ++-- .../OperationStateHistoryConverterImpl.java | 8 ++++---- .../empayre/liminator/dao/OperationDao.java | 4 ++-- .../liminator/dao/impl/OperationDaoImpl.java | 18 ++++++----------- .../impl/FinalizeOperationHandlerImpl.java | 20 ++++++++++--------- .../handler/impl/GetLimitsValuesHandler.java | 7 +++++-- .../impl/HoldOperationHandlerImpl.java | 12 ++++++++--- .../liminator/service/LiminatorService.java | 2 +- .../service/LimitDataGettingService.java | 5 ++++- .../com/empayre/liminator/dao/DaoTests.java | 9 ++++----- .../service/LiminatorServiceTest.java | 10 ++++------ .../TransactionVisibilityTest.java | 4 ++-- 15 files changed, 57 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 7448bd1..a4cd677 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - _LimitNotFound_ - данная ошибка будет даже если какого-то одного лимита из списка переданных нет в БД - _DuplicateOperation_ - данная ошибка будет передана если для связки LimitName+OperationId в - БД уже имеется запись в значении HOLD + БД уже имеется запись в значении HOLD и флаг skipExistedHoldOps в настройках сервиса установлен в false - _OperationAlreadyInFinalState_ - данная ошибка будет передана если для связки LimitName+OperationId в БД уже имеется запись в значении COMMIT/ROLLBACK - _LimitsValuesReadingException_ - данная ошибка будет передана если при подсчете лимитов произошла diff --git a/pom.xml b/pom.xml index e4b968d..d98c965 100644 --- a/pom.xml +++ b/pom.xml @@ -173,7 +173,7 @@ dev.vality liminator-proto - 1.4-765e75c + 1.5-fe957d8 dev.vality.woody diff --git a/src/main/java/com/empayre/liminator/converter/OperationConverter.java b/src/main/java/com/empayre/liminator/converter/OperationConverter.java index 73e66f5..c2e2353 100644 --- a/src/main/java/com/empayre/liminator/converter/OperationConverter.java +++ b/src/main/java/com/empayre/liminator/converter/OperationConverter.java @@ -5,5 +5,5 @@ import dev.vality.liminator.LimitRequest; public interface OperationConverter { - Operation convert(LimitRequest request, Long limitId); + Operation convert(LimitRequest request, Long limitId, Long limitValue); } diff --git a/src/main/java/com/empayre/liminator/converter/impl/OperationConverterImpl.java b/src/main/java/com/empayre/liminator/converter/impl/OperationConverterImpl.java index 8d2753e..e60f494 100644 --- a/src/main/java/com/empayre/liminator/converter/impl/OperationConverterImpl.java +++ b/src/main/java/com/empayre/liminator/converter/impl/OperationConverterImpl.java @@ -12,11 +12,11 @@ import java.time.LocalDateTime; public class OperationConverterImpl implements OperationConverter { @Override - public Operation convert(LimitRequest request, Long limitId) { + public Operation convert(LimitRequest request, Long limitId, Long limitValue) { Operation operation = new Operation(); operation.setLimitId(limitId); operation.setOperationId(request.getOperationId()); - operation.setOperationValue(request.getValue()); + operation.setOperationValue(limitValue); operation.setCreatedAt(LocalDateTime.now()); operation.setState(OperationState.HOLD); return operation; diff --git a/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java b/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java index 35a6d4e..ab1455f 100644 --- a/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java +++ b/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java @@ -13,12 +13,12 @@ public class OperationStateHistoryConverterImpl implements OperationStateHistory @Override public List convert(LimitRequest request, OperationState state) { - return request.getLimitNames().stream() - .map(limitName -> { + return request.getLimitChanges().stream() + .map(change -> { OperationStateHistory history = new OperationStateHistory(); history.setOperationId(request.getOperationId()); - history.setLimitName(limitName); - history.setOperationValue(request.getValue()); + history.setLimitName(change.getLimitName()); + history.setOperationValue(change.getValue()); history.setState(state); return history; }) diff --git a/src/main/java/com/empayre/liminator/dao/OperationDao.java b/src/main/java/com/empayre/liminator/dao/OperationDao.java index aa86aa5..4f718e9 100644 --- a/src/main/java/com/empayre/liminator/dao/OperationDao.java +++ b/src/main/java/com/empayre/liminator/dao/OperationDao.java @@ -21,7 +21,7 @@ public interface OperationDao extends CommonDao { List getCurrentLimitValue(List limitNames, String operationId); - int commit(List limitNames, String operationId); + int commit(String operationId, List limitIds); - int rollback(List limitNames, String operationId); + int rollback(String operationId, List limitIds); } diff --git a/src/main/java/com/empayre/liminator/dao/impl/OperationDaoImpl.java b/src/main/java/com/empayre/liminator/dao/impl/OperationDaoImpl.java index 7bbb279..2cde9f7 100644 --- a/src/main/java/com/empayre/liminator/dao/impl/OperationDaoImpl.java +++ b/src/main/java/com/empayre/liminator/dao/impl/OperationDaoImpl.java @@ -13,7 +13,6 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; -import static com.empayre.liminator.domain.Tables.LIMIT_DATA; import static com.empayre.liminator.domain.Tables.OPERATION; import static org.jooq.impl.DSL.raw; import static org.jooq.impl.DSL.val; @@ -80,26 +79,21 @@ public class OperationDaoImpl implements OperationDao { } @Override - public int commit(List limitNames, String operationId) { - return updateStateForHoldOperation(limitNames, operationId, OperationState.COMMIT); + public int commit(String operationId, List limitIds) { + return updateStateForHoldOperation(operationId, OperationState.COMMIT, limitIds); } @Override - public int rollback(List limitNames, String operationId) { - return updateStateForHoldOperation(limitNames, operationId, OperationState.ROLLBACK); + public int rollback(String operationId, List limitIds) { + return updateStateForHoldOperation(operationId, OperationState.ROLLBACK, limitIds); } - private int updateStateForHoldOperation(List limitNames, String operationId, OperationState state) { + private int updateStateForHoldOperation(String operationId, OperationState state, List limitIds) { return dslContext .update(OPERATION) .set(OPERATION.STATE, state) .where(OPERATION.OPERATION_ID.eq(operationId)) - .and(OPERATION.LIMIT_ID.in( - dslContext - .select(LIMIT_DATA.ID) - .from(LIMIT_DATA) - .where(LIMIT_DATA.NAME.in(limitNames)) - )) + .and(OPERATION.LIMIT_ID.in(limitIds)) .and(OPERATION.STATE.eq(OperationState.HOLD)) .execute(); } diff --git a/src/main/java/com/empayre/liminator/handler/impl/FinalizeOperationHandlerImpl.java b/src/main/java/com/empayre/liminator/handler/impl/FinalizeOperationHandlerImpl.java index 3fafe61..2efec9b 100644 --- a/src/main/java/com/empayre/liminator/handler/impl/FinalizeOperationHandlerImpl.java +++ b/src/main/java/com/empayre/liminator/handler/impl/FinalizeOperationHandlerImpl.java @@ -32,14 +32,16 @@ public class FinalizeOperationHandlerImpl implements FinalizeOperationHandler { public void handle(LimitRequest request, OperationState state) throws TException { List limitData = limitDataGettingService.get(request, state.getLiteral()); checkExistedHoldOperations(request, limitData, state); - + List limitIds = limitData.stream() + .map(LimitData::getId) + .toList(); int updatedRowsCount = switch (state) { - case COMMIT -> operationDao.commit(request.getLimitNames(), request.getOperationId()); - case ROLLBACK -> operationDao.rollback(request.getLimitNames(), request.getOperationId()); + case COMMIT -> operationDao.commit(request.getOperationId(), limitIds); + case ROLLBACK -> operationDao.rollback(request.getOperationId(), limitIds); default -> throw new TException(); }; - checkUpdatedOperstionsConsistency(request, state, updatedRowsCount); + checkUpdatedOperationsConsistency(request, state, updatedRowsCount); limitOperationsLoggingService.writeOperations(request, state); } @@ -65,14 +67,14 @@ public class FinalizeOperationHandlerImpl implements FinalizeOperationHandler { } } - private void checkUpdatedOperstionsConsistency(LimitRequest request, + private void checkUpdatedOperationsConsistency(LimitRequest request, OperationState state, int updatedRowsCount) throws TException { - List limitNames = request.getLimitNames(); - if (updatedRowsCount != limitNames.size()) { + int changesSize = request.getLimitChanges().size(); + if (updatedRowsCount != changesSize) { log.error("[{}] Count of updated rows ({}) is not equal to the expected count of updated operations " + - "(rollback size: {})", - state.getLiteral(), updatedRowsCount, limitNames.size(), request); + "(rollback size: {}, request: {})", + state.getLiteral(), updatedRowsCount, changesSize, request); throw new OperationNotFound(); } } diff --git a/src/main/java/com/empayre/liminator/handler/impl/GetLimitsValuesHandler.java b/src/main/java/com/empayre/liminator/handler/impl/GetLimitsValuesHandler.java index 2c07909..d930722 100644 --- a/src/main/java/com/empayre/liminator/handler/impl/GetLimitsValuesHandler.java +++ b/src/main/java/com/empayre/liminator/handler/impl/GetLimitsValuesHandler.java @@ -3,6 +3,7 @@ package com.empayre.liminator.handler.impl; import com.empayre.liminator.dao.OperationDao; import com.empayre.liminator.handler.Handler; import com.empayre.liminator.model.LimitValue; +import dev.vality.liminator.LimitChange; import dev.vality.liminator.LimitRequest; import dev.vality.liminator.LimitResponse; import lombok.RequiredArgsConstructor; @@ -25,8 +26,10 @@ public class GetLimitsValuesHandler implements Handler handle(LimitRequest request) throws TException { - List limitValues = - operationDao.getCurrentLimitValue(request.getLimitNames(), request.getOperationId()); + List limitNames = request.getLimitChanges().stream() + .map(LimitChange::getLimitName) + .toList(); + List limitValues = operationDao.getCurrentLimitValue(limitNames, request.getOperationId()); return currentLimitValuesToLimitResponseConverter.convert(limitValues); } } diff --git a/src/main/java/com/empayre/liminator/handler/impl/HoldOperationHandlerImpl.java b/src/main/java/com/empayre/liminator/handler/impl/HoldOperationHandlerImpl.java index dea95b5..4c45e6c 100644 --- a/src/main/java/com/empayre/liminator/handler/impl/HoldOperationHandlerImpl.java +++ b/src/main/java/com/empayre/liminator/handler/impl/HoldOperationHandlerImpl.java @@ -55,8 +55,13 @@ public class HoldOperationHandlerImpl implements HoldOperationHandler { } private List convertToOperation(LimitRequest request, Map limitNamesMap) { - return request.getLimitNames().stream() - .map(limitName -> operationConverter.convert(request, limitNamesMap.get(limitName))) + return request.getLimitChanges().stream() + .map(change -> operationConverter.convert( + request, + limitNamesMap.get(change.getLimitName()), + change.getValue() + ) + ) .toList(); } @@ -69,7 +74,8 @@ public class HoldOperationHandlerImpl implements HoldOperationHandler { } } - private void checkExistedFinalizeOperations(Map limitNamesMap, String operationId) throws TException { + private void checkExistedFinalizeOperations(Map limitNamesMap, + String operationId) throws TException { List existedFinalizeOperations = operationDao.get( operationId, limitNamesMap.values(), diff --git a/src/main/java/com/empayre/liminator/service/LiminatorService.java b/src/main/java/com/empayre/liminator/service/LiminatorService.java index 73036d4..79aa233 100644 --- a/src/main/java/com/empayre/liminator/service/LiminatorService.java +++ b/src/main/java/com/empayre/liminator/service/LiminatorService.java @@ -34,7 +34,7 @@ public class LiminatorService implements LiminatorServiceSrv.Iface { @Override public List hold(LimitRequest request) throws LimitNotFound, DuplicateOperation, OperationAlreadyInFinalState, TException { - if (request == null || CollectionUtils.isEmpty(request.getLimitNames())) { + if (request == null || CollectionUtils.isEmpty(request.getLimitChanges())) { log.warn("[HOLD] LimitRequest or LimitNames is empty. Request: {}", request); return new ArrayList<>(); } diff --git a/src/main/java/com/empayre/liminator/service/LimitDataGettingService.java b/src/main/java/com/empayre/liminator/service/LimitDataGettingService.java index 8718b9f..8bba43d 100644 --- a/src/main/java/com/empayre/liminator/service/LimitDataGettingService.java +++ b/src/main/java/com/empayre/liminator/service/LimitDataGettingService.java @@ -2,6 +2,7 @@ package com.empayre.liminator.service; import com.empayre.liminator.dao.LimitDataDao; import com.empayre.liminator.domain.tables.pojos.LimitData; +import dev.vality.liminator.LimitChange; import dev.vality.liminator.LimitNotFound; import dev.vality.liminator.LimitRequest; import lombok.RequiredArgsConstructor; @@ -20,7 +21,9 @@ public class LimitDataGettingService { private final LimitDataDao limitDataDao; public List get(LimitRequest request, String source) throws TException { - List limitNames = request.getLimitNames(); + List limitNames = request.getLimitChanges().stream() + .map(LimitChange::getLimitName) + .toList(); List limitData = limitDataDao.get(limitNames); if (CollectionUtils.isEmpty(limitData)) { log.error("[{}] Limits not found: {}", source, limitNames); diff --git a/src/test/java/com/empayre/liminator/dao/DaoTests.java b/src/test/java/com/empayre/liminator/dao/DaoTests.java index 32f6195..41e2a99 100644 --- a/src/test/java/com/empayre/liminator/dao/DaoTests.java +++ b/src/test/java/com/empayre/liminator/dao/DaoTests.java @@ -78,13 +78,12 @@ public class DaoTests { currentLimitValue.forEach(value -> assertEquals(0, value.getCommitValue())); currentLimitValue.forEach(value -> assertNotEquals(0, value.getHoldValue())); - List commitLimitNames = limitNamesList.subList(0, 3); + List commitLimitIds = limitIdsList.subList(0, 3); String finalizeOperationName = operationNameTemplate.formatted(1); - operationDao.commit(commitLimitNames, finalizeOperationName); - - List rollbackLimitNames = limitNamesList.subList(4, 9); - operationDao.rollback(rollbackLimitNames, finalizeOperationName); + operationDao.commit(finalizeOperationName, commitLimitIds); + List rollbackLimitIds = limitIdsList.subList(4, 9); + operationDao.rollback(finalizeOperationName, rollbackLimitIds); List limitValuesAfterChanges = operationDao.getCurrentLimitValue(limitNamesList); List limitValuesWithCommitData = limitValuesAfterChanges.stream() diff --git a/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java b/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java index d7a9d59..4854476 100644 --- a/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java +++ b/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java @@ -2,6 +2,7 @@ package com.empayre.liminator.service; import com.empayre.liminator.config.PostgresqlSpringBootITest; import dev.vality.liminator.CreateLimitRequest; +import dev.vality.liminator.LimitChange; import dev.vality.liminator.LimitRequest; import dev.vality.liminator.LimitResponse; import org.apache.thrift.TException; @@ -40,8 +41,7 @@ public class LiminatorServiceTest { String operationId = "OpHold"; LimitRequest holdRequest = new LimitRequest() .setOperationId(operationId) - .setLimitNames(List.of(limitName)) - .setValue(500L); + .setLimitChanges(List.of(new LimitChange(limitName, 500L))); List holdResponse = liminatorService.hold(holdRequest); assertEquals(1, holdResponse.size()); LimitResponse response = holdResponse.get(0); @@ -60,8 +60,7 @@ public class LiminatorServiceTest { String operationId = "OpComit"; LimitRequest holdRequest = new LimitRequest() .setOperationId(operationId) - .setLimitNames(List.of(limitName)) - .setValue(500L); + .setLimitChanges(List.of(new LimitChange(limitName, 500L))); liminatorService.hold(holdRequest); liminatorService.commit(holdRequest); @@ -82,8 +81,7 @@ public class LiminatorServiceTest { String operationId = "Op-112"; LimitRequest holdRequest = new LimitRequest() .setOperationId(operationId) - .setLimitNames(List.of(limitName)) - .setValue(500L); + .setLimitChanges(List.of(new LimitChange(limitName, 500L))); liminatorService.hold(holdRequest); liminatorService.rollback(holdRequest); diff --git a/src/test/java/com/empayre/liminator/transaction/TransactionVisibilityTest.java b/src/test/java/com/empayre/liminator/transaction/TransactionVisibilityTest.java index 5d3a045..ce136a0 100644 --- a/src/test/java/com/empayre/liminator/transaction/TransactionVisibilityTest.java +++ b/src/test/java/com/empayre/liminator/transaction/TransactionVisibilityTest.java @@ -3,6 +3,7 @@ package com.empayre.liminator.transaction; import com.empayre.liminator.dao.OperationStateHistoryDao; import com.empayre.liminator.service.LiminatorService; import dev.vality.liminator.CreateLimitRequest; +import dev.vality.liminator.LimitChange; import dev.vality.liminator.LimitRequest; import dev.vality.liminator.LimitResponse; import org.apache.thrift.TException; @@ -42,8 +43,7 @@ public class TransactionVisibilityTest extends AbstractIntegrationTestWithEmbedd String operationId = "OpComit123"; LimitRequest holdRequest = new LimitRequest() .setOperationId(operationId) - .setLimitNames(List.of(limitName)) - .setValue(500L); + .setLimitChanges(List.of(new LimitChange(limitName, 500L))); assertThrows(RuntimeException.class, () -> liminatorService.hold(holdRequest));