mirror of
https://github.com/valitydev/liminator.git
synced 2024-11-06 01:15:21 +00:00
TD-955: Bump proto (add LimitChange object to proto) (#17)
Some checks failed
Deploy Docker Image / build-and-deploy (push) Has been cancelled
Some checks failed
Deploy Docker Image / build-and-deploy (push) Has been cancelled
This commit is contained in:
parent
c7d852d9dc
commit
20314fd894
@ -15,7 +15,7 @@
|
||||
- _LimitNotFound_ - данная ошибка будет даже если какого-то одного лимита из списка
|
||||
переданных нет в БД
|
||||
- _DuplicateOperation_ - данная ошибка будет передана если для связки LimitName+OperationId в
|
||||
БД уже имеется запись в значении HOLD
|
||||
БД уже имеется запись в значении HOLD и флаг skipExistedHoldOps в настройках сервиса установлен в false
|
||||
- _OperationAlreadyInFinalState_ - данная ошибка будет передана если для связки LimitName+OperationId в
|
||||
БД уже имеется запись в значении COMMIT/ROLLBACK
|
||||
- _LimitsValuesReadingException_ - данная ошибка будет передана если при подсчете лимитов произошла
|
||||
|
2
pom.xml
2
pom.xml
@ -173,7 +173,7 @@
|
||||
<dependency>
|
||||
<groupId>dev.vality</groupId>
|
||||
<artifactId>liminator-proto</artifactId>
|
||||
<version>1.4-765e75c</version>
|
||||
<version>1.5-fe957d8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dev.vality.woody</groupId>
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -13,12 +13,12 @@ public class OperationStateHistoryConverterImpl implements OperationStateHistory
|
||||
|
||||
@Override
|
||||
public List<OperationStateHistory> 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;
|
||||
})
|
||||
|
@ -21,7 +21,7 @@ public interface OperationDao extends CommonDao<Operation> {
|
||||
|
||||
List<LimitValue> getCurrentLimitValue(List<String> limitNames, String operationId);
|
||||
|
||||
int commit(List<String> limitNames, String operationId);
|
||||
int commit(String operationId, List<Long> limitIds);
|
||||
|
||||
int rollback(List<String> limitNames, String operationId);
|
||||
int rollback(String operationId, List<Long> limitIds);
|
||||
}
|
||||
|
@ -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<String> limitNames, String operationId) {
|
||||
return updateStateForHoldOperation(limitNames, operationId, OperationState.COMMIT);
|
||||
public int commit(String operationId, List<Long> limitIds) {
|
||||
return updateStateForHoldOperation(operationId, OperationState.COMMIT, limitIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int rollback(List<String> limitNames, String operationId) {
|
||||
return updateStateForHoldOperation(limitNames, operationId, OperationState.ROLLBACK);
|
||||
public int rollback(String operationId, List<Long> limitIds) {
|
||||
return updateStateForHoldOperation(operationId, OperationState.ROLLBACK, limitIds);
|
||||
}
|
||||
|
||||
private int updateStateForHoldOperation(List<String> limitNames, String operationId, OperationState state) {
|
||||
private int updateStateForHoldOperation(String operationId, OperationState state, List<Long> 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();
|
||||
}
|
||||
|
@ -32,14 +32,16 @@ public class FinalizeOperationHandlerImpl implements FinalizeOperationHandler {
|
||||
public void handle(LimitRequest request, OperationState state) throws TException {
|
||||
List<LimitData> limitData = limitDataGettingService.get(request, state.getLiteral());
|
||||
checkExistedHoldOperations(request, limitData, state);
|
||||
|
||||
List<Long> 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<String> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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<LimitRequest, List<LimitR
|
||||
@Transactional
|
||||
@Override
|
||||
public List<LimitResponse> handle(LimitRequest request) throws TException {
|
||||
List<LimitValue> limitValues =
|
||||
operationDao.getCurrentLimitValue(request.getLimitNames(), request.getOperationId());
|
||||
List<String> limitNames = request.getLimitChanges().stream()
|
||||
.map(LimitChange::getLimitName)
|
||||
.toList();
|
||||
List<LimitValue> limitValues = operationDao.getCurrentLimitValue(limitNames, request.getOperationId());
|
||||
return currentLimitValuesToLimitResponseConverter.convert(limitValues);
|
||||
}
|
||||
}
|
||||
|
@ -55,8 +55,13 @@ public class HoldOperationHandlerImpl implements HoldOperationHandler {
|
||||
}
|
||||
|
||||
private List<Operation> convertToOperation(LimitRequest request, Map<String, Long> 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<String, Long> limitNamesMap, String operationId) throws TException {
|
||||
private void checkExistedFinalizeOperations(Map<String, Long> limitNamesMap,
|
||||
String operationId) throws TException {
|
||||
List<Operation> existedFinalizeOperations = operationDao.get(
|
||||
operationId,
|
||||
limitNamesMap.values(),
|
||||
|
@ -34,7 +34,7 @@ public class LiminatorService implements LiminatorServiceSrv.Iface {
|
||||
@Override
|
||||
public List<LimitResponse> 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<>();
|
||||
}
|
||||
|
@ -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<LimitData> get(LimitRequest request, String source) throws TException {
|
||||
List<String> limitNames = request.getLimitNames();
|
||||
List<String> limitNames = request.getLimitChanges().stream()
|
||||
.map(LimitChange::getLimitName)
|
||||
.toList();
|
||||
List<LimitData> limitData = limitDataDao.get(limitNames);
|
||||
if (CollectionUtils.isEmpty(limitData)) {
|
||||
log.error("[{}] Limits not found: {}", source, limitNames);
|
||||
|
@ -78,13 +78,12 @@ public class DaoTests {
|
||||
currentLimitValue.forEach(value -> assertEquals(0, value.getCommitValue()));
|
||||
currentLimitValue.forEach(value -> assertNotEquals(0, value.getHoldValue()));
|
||||
|
||||
List<String> commitLimitNames = limitNamesList.subList(0, 3);
|
||||
List<Long> commitLimitIds = limitIdsList.subList(0, 3);
|
||||
String finalizeOperationName = operationNameTemplate.formatted(1);
|
||||
operationDao.commit(commitLimitNames, finalizeOperationName);
|
||||
|
||||
List<String> rollbackLimitNames = limitNamesList.subList(4, 9);
|
||||
operationDao.rollback(rollbackLimitNames, finalizeOperationName);
|
||||
operationDao.commit(finalizeOperationName, commitLimitIds);
|
||||
|
||||
List<Long> rollbackLimitIds = limitIdsList.subList(4, 9);
|
||||
operationDao.rollback(finalizeOperationName, rollbackLimitIds);
|
||||
|
||||
List<LimitValue> limitValuesAfterChanges = operationDao.getCurrentLimitValue(limitNamesList);
|
||||
List<LimitValue> limitValuesWithCommitData = limitValuesAfterChanges.stream()
|
||||
|
@ -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<LimitResponse> 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);
|
||||
|
||||
|
@ -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));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user