FINERACT-1724-COB-hardlocked-loans-and-inline-cob-issue
This commit is contained in:
committed by
Adam Saghy
parent
03fd2ac7ce
commit
d704d3d027
+2
@@ -55,4 +55,6 @@ public interface LoanAccountLockRepository extends JpaRepository<LoanAccountLock
|
||||
""")
|
||||
@Modifying(flushAutomatically = true)
|
||||
void removeLockByOwner();
|
||||
|
||||
List<LoanAccountLock> findAllByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner);
|
||||
}
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.fineract.cob.exceptions;
|
||||
|
||||
public class LoanLockCannotBeAppliedException extends Exception {
|
||||
|
||||
public LoanLockCannotBeAppliedException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
+36
-6
@@ -18,6 +18,8 @@
|
||||
*/
|
||||
package org.apache.fineract.cob.loan;
|
||||
|
||||
import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRES_NEW;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -29,6 +31,7 @@ import org.apache.fineract.cob.common.CustomJobParameterResolver;
|
||||
import org.apache.fineract.cob.data.LoanCOBParameter;
|
||||
import org.apache.fineract.cob.domain.LoanAccountLock;
|
||||
import org.apache.fineract.cob.domain.LockOwner;
|
||||
import org.apache.fineract.cob.exceptions.LoanLockCannotBeAppliedException;
|
||||
import org.apache.fineract.infrastructure.core.config.FineractProperties;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
@@ -36,19 +39,26 @@ import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
import org.springframework.batch.core.step.tasklet.Tasklet;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ApplyLoanLockTasklet implements Tasklet {
|
||||
|
||||
private static final long NUMBER_OF_RETRIES = 3;
|
||||
private final FineractProperties fineractProperties;
|
||||
private final LoanLockingService loanLockingService;
|
||||
private final RetrieveLoanIdService retrieveLoanIdService;
|
||||
private final CustomJobParameterResolver customJobParameterResolver;
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
||||
@Override
|
||||
public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull ChunkContext chunkContext) throws Exception {
|
||||
public RepeatStatus execute(@NotNull StepContribution contribution, @NotNull ChunkContext chunkContext)
|
||||
throws LoanLockCannotBeAppliedException {
|
||||
ExecutionContext executionContext = contribution.getStepExecution().getExecutionContext();
|
||||
long numberOfExecutions = contribution.getStepExecution().getCommitCount();
|
||||
LoanCOBParameter loanCOBParameter = (LoanCOBParameter) executionContext.get(LoanCOBConstant.LOAN_COB_PARAMETER);
|
||||
List<Long> loanIds;
|
||||
if (Objects.isNull(loanCOBParameter)
|
||||
@@ -66,16 +76,36 @@ public class ApplyLoanLockTasklet implements Tasklet {
|
||||
List<LoanAccountLock> accountLocks = new ArrayList<>();
|
||||
loanIdPartitions.forEach(loanIdPartition -> accountLocks.addAll(loanLockingService.findAllByLoanIdIn(loanIdPartition)));
|
||||
|
||||
List<Long> alreadyLockedByChunkProcessingAccountIds = accountLocks.stream()
|
||||
.filter(e -> LockOwner.LOAN_COB_CHUNK_PROCESSING.equals(e.getLockOwner())).map(LoanAccountLock::getLoanId).toList();
|
||||
|
||||
List<Long> toBeProcessedLoanIds = new ArrayList<>(loanIds);
|
||||
toBeProcessedLoanIds.removeAll(alreadyLockedByChunkProcessingAccountIds);
|
||||
List<Long> alreadyLockedAccountIds = accountLocks.stream().map(LoanAccountLock::getLoanId).toList();
|
||||
|
||||
toBeProcessedLoanIds.removeAll(alreadyLockedAccountIds);
|
||||
try {
|
||||
applyLocks(toBeProcessedLoanIds);
|
||||
} catch (Exception e) {
|
||||
if (numberOfExecutions > NUMBER_OF_RETRIES) {
|
||||
String message = "There was an error applying lock to loan accounts.";
|
||||
log.error("{}", message, e);
|
||||
throw new LoanLockCannotBeAppliedException(message, e);
|
||||
} else {
|
||||
return RepeatStatus.CONTINUABLE;
|
||||
}
|
||||
}
|
||||
|
||||
loanLockingService.applyLock(toBeProcessedLoanIds, LockOwner.LOAN_COB_CHUNK_PROCESSING);
|
||||
return RepeatStatus.FINISHED;
|
||||
}
|
||||
|
||||
private void applyLocks(List<Long> toBeProcessedLoanIds) {
|
||||
transactionTemplate.setPropagationBehavior(PROPAGATION_REQUIRES_NEW);
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(@NotNull TransactionStatus status) {
|
||||
loanLockingService.applyLock(toBeProcessedLoanIds, LockOwner.LOAN_COB_CHUNK_PROCESSING);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int getInClauseParameterSizeLimit() {
|
||||
return fineractProperties.getQuery().getInClauseParameterSizeLimit();
|
||||
}
|
||||
|
||||
+3
-2
@@ -131,7 +131,8 @@ public class LoanCOBWorkerConfiguration {
|
||||
|
||||
@Bean
|
||||
public ApplyLoanLockTasklet applyLock() {
|
||||
return new ApplyLoanLockTasklet(fineractProperties, loanLockingService, retrieveLoanIdService, customJobParameterResolver);
|
||||
return new ApplyLoanLockTasklet(fineractProperties, loanLockingService, retrieveLoanIdService, customJobParameterResolver,
|
||||
transactionTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -142,7 +143,7 @@ public class LoanCOBWorkerConfiguration {
|
||||
@Bean
|
||||
@StepScope
|
||||
public LoanItemReader cobWorkerItemReader() {
|
||||
return new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver);
|
||||
return new LoanItemReader(loanRepository, retrieveLoanIdService, customJobParameterResolver, loanLockingService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.apache.fineract.cob.common.CustomJobParameterResolver;
|
||||
import org.apache.fineract.cob.data.LoanCOBParameter;
|
||||
import org.apache.fineract.cob.domain.LoanAccountLock;
|
||||
import org.apache.fineract.cob.domain.LockOwner;
|
||||
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
@@ -34,12 +36,14 @@ public class LoanItemReader extends AbstractLoanItemReader {
|
||||
|
||||
private final RetrieveLoanIdService retrieveLoanIdService;
|
||||
private final CustomJobParameterResolver customJobParameterResolver;
|
||||
private final LoanLockingService loanLockingService;
|
||||
|
||||
public LoanItemReader(LoanRepository loanRepository, RetrieveLoanIdService retrieveLoanIdService,
|
||||
CustomJobParameterResolver customJobParameterResolver) {
|
||||
CustomJobParameterResolver customJobParameterResolver, LoanLockingService loanLockingService) {
|
||||
super(loanRepository);
|
||||
this.retrieveLoanIdService = retrieveLoanIdService;
|
||||
this.customJobParameterResolver = customJobParameterResolver;
|
||||
this.loanLockingService = loanLockingService;
|
||||
}
|
||||
|
||||
@BeforeStep
|
||||
@@ -56,7 +60,16 @@ public class LoanItemReader extends AbstractLoanItemReader {
|
||||
loanIds = retrieveLoanIdService.retrieveAllNonClosedLoansByLastClosedBusinessDateAndMinAndMaxLoanId(loanCOBParameter,
|
||||
customJobParameterResolver.getCustomJobParameterById(stepExecution, LoanCOBConstant.IS_CATCH_UP_PARAMETER_NAME)
|
||||
.map(Boolean::parseBoolean).orElse(false));
|
||||
|
||||
List<Long> lockedByCOBChunkProcessingAccountIds = getLoanIdsLockedWithChunkProcessingLock(loanIds);
|
||||
loanIds.retainAll(lockedByCOBChunkProcessingAccountIds);
|
||||
}
|
||||
setRemainingData(new ArrayList<>(loanIds));
|
||||
}
|
||||
|
||||
private List<Long> getLoanIdsLockedWithChunkProcessingLock(List<Long> loanIds) {
|
||||
List<LoanAccountLock> accountLocks = new ArrayList<>();
|
||||
accountLocks.addAll(loanLockingService.findAllByLoanIdInAndLockOwner(loanIds, LockOwner.LOAN_COB_CHUNK_PROCESSING));
|
||||
return accountLocks.stream().map(LoanAccountLock::getLoanId).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,5 +32,7 @@ public interface LoanLockingService {
|
||||
|
||||
LoanAccountLock findByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner);
|
||||
|
||||
List<LoanAccountLock> findAllByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner);
|
||||
|
||||
void applyLock(List<Long> loanIds, LockOwner lockOwner);
|
||||
}
|
||||
|
||||
+5
@@ -85,6 +85,11 @@ public class LoanLockingServiceImpl implements LoanLockingService {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LoanAccountLock> findAllByLoanIdInAndLockOwner(List<Long> loanIds, LockOwner lockOwner) {
|
||||
return loanAccountLockRepository.findAllByLoanIdInAndLockOwner(loanIds, lockOwner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyLock(List<Long> loanIds, LockOwner lockOwner) {
|
||||
LocalDate cobBusinessDate = ThreadLocalContextUtil.getBusinessDateByType(BusinessDateType.COB_DATE);
|
||||
|
||||
+8
-2
@@ -218,8 +218,14 @@ public class InlineLoanCOBExecutorServiceImpl implements InlineExecutorService<L
|
||||
protected void doInTransactionWithoutResult(@NotNull TransactionStatus status) {
|
||||
List<LoanAccountLock> loanAccountLocks = getLoanAccountLocks(loanIds, businessDate);
|
||||
loanAccountLocks.forEach(loanAccountLock -> {
|
||||
loanAccountLock.setNewLockOwner(LockOwner.LOAN_INLINE_COB_PROCESSING);
|
||||
loanAccountLockRepository.save(loanAccountLock);
|
||||
try {
|
||||
loanAccountLock.setNewLockOwner(LockOwner.LOAN_INLINE_COB_PROCESSING);
|
||||
loanAccountLockRepository.saveAndFlush(loanAccountLock);
|
||||
} catch (Exception e) {
|
||||
String message = "Error updating lock on loan account. Locked loan ID: %s".formatted(loanAccountLock.getLoanId());
|
||||
log.error("{}", message, e);
|
||||
throw new LoanAccountLockCannotBeOverruledException(message, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user