From 81b56fafa02fa67306d3050fd44662b4e31096d7 Mon Sep 17 00:00:00 2001 From: taskain7 Date: Fri, 26 Aug 2022 04:51:46 +0200 Subject: [PATCH] [FINERACT-1678] Loan COB Partitioner strategy fix --- .../cob/loan/LoanCOBManagerConfiguration.java | 8 ++-- .../fineract/cob/loan/LoanCOBPartitioner.java | 43 +++++++++++-------- ...RetrieveAllNonClosedLoanIdServiceImpl.java | 34 +++++++++++++++ .../cob/loan/RetrieveLoanIdConfiguration.java | 38 ++++++++++++++++ .../cob/loan/RetrieveLoanIdService.java | 26 +++++++++++ 5 files changed, 126 insertions(+), 23 deletions(-) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveAllNonClosedLoanIdServiceImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdConfiguration.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdService.java diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBManagerConfiguration.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBManagerConfiguration.java index 807f45d50..3e50260e1 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBManagerConfiguration.java +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBManagerConfiguration.java @@ -22,7 +22,6 @@ import org.apache.fineract.cob.COBBusinessStepService; import org.apache.fineract.cob.COBPropertyService; import org.apache.fineract.cob.listener.COBExecutionListenerRunner; import org.apache.fineract.infrastructure.jobs.service.JobName; -import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; @@ -47,9 +46,6 @@ public class LoanCOBManagerConfiguration { private JobBuilderFactory jobBuilderFactory; @Autowired private RemotePartitioningManagerStepBuilderFactory stepBuilderFactory; - - @Autowired - private LoanRepository loanRepository; @Autowired private COBPropertyService cobPropertyService; @Autowired @@ -62,10 +58,12 @@ public class LoanCOBManagerConfiguration { private JobExplorer jobExplorer; @Autowired private ApplicationContext applicationContext; + @Autowired(required = false) + private RetrieveLoanIdService retrieveLoanIdService; @Bean public LoanCOBPartitioner partitioner() { - return new LoanCOBPartitioner(loanRepository, cobPropertyService, cobBusinessStepService, jobOperator, jobExplorer); + return new LoanCOBPartitioner(cobPropertyService, cobBusinessStepService, jobOperator, jobExplorer, retrieveLoanIdService); } @Bean diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBPartitioner.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBPartitioner.java index 0857b6a01..39bb395d5 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBPartitioner.java +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/LoanCOBPartitioner.java @@ -29,8 +29,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.fineract.cob.COBBusinessStepService; import org.apache.fineract.cob.COBPropertyService; import org.apache.fineract.infrastructure.jobs.service.JobName; -import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.explore.JobExplorer; @@ -44,47 +42,56 @@ import org.springframework.batch.item.ExecutionContext; @RequiredArgsConstructor public class LoanCOBPartitioner implements Partitioner { - private final LoanRepository loanRepository; private final COBPropertyService cobPropertyService; private final COBBusinessStepService cobBusinessStepService; private final JobOperator jobOperator; private final JobExplorer jobExplorer; + private final RetrieveLoanIdService retrieveLoanIdService; - private static final String PARTITION_PREFIX = "partition"; + private static final String PARTITION_PREFIX = "partition_"; private static final String JOB_NAME = "LOAN_COB"; private static final String LOAN_COB_JOB_NAME = "LOAN_CLOSE_OF_BUSINESS"; @Override public Map partition(int gridSize) { - int partitionCount = cobPropertyService.getPartitionSize(JOB_NAME); + int partitionSize = cobPropertyService.getPartitionSize(JOB_NAME); TreeMap cobBusinessStepMap = cobBusinessStepService.getCOBBusinessStepMap(LoanCOBBusinessStep.class, LOAN_COB_JOB_NAME); if (cobBusinessStepMap.isEmpty()) { return stopJobExecution(); } - return getPartitions(partitionCount, cobBusinessStepMap); + return getPartitions(partitionSize, cobBusinessStepMap); } - @NotNull - private Map getPartitions(int partitionCount, TreeMap cobBusinessStepMap) { + private Map getPartitions(int partitionSize, TreeMap cobBusinessStepMap) { Map partitions = new HashMap<>(); - for (int i = 0; i < partitionCount; i++) { - ExecutionContext executionContext = new ExecutionContext(); - executionContext.put("loanIds", new ArrayList()); - executionContext.put("BusinessStepMap", cobBusinessStepMap); - partitions.put(PARTITION_PREFIX + i, executionContext); + List allNonClosedLoanIds = retrieveLoanIdService.retrieveLoanIds(); + if (allNonClosedLoanIds.isEmpty()) { + return stopJobExecution(); } - - List allNonClosedLoanIds = loanRepository.findAllNonClosedLoanIds(); - for (int i = 0; i < allNonClosedLoanIds.size(); i++) { - String key = PARTITION_PREFIX + (i % partitionCount); + int partitionIndex = 0; + createNewPartition(partitions, partitionIndex, cobBusinessStepMap); + for (Integer allNonClosedLoanId : allNonClosedLoanIds) { + if (partitions.get(PARTITION_PREFIX + partitionIndex).size() == partitionSize) { + partitionIndex++; + createNewPartition(partitions, partitionIndex, cobBusinessStepMap); + } + String key = PARTITION_PREFIX + partitionIndex; ExecutionContext executionContext = partitions.get(key); List data = (List) executionContext.get("loanIds"); - data.add(allNonClosedLoanIds.get(i)); + data.add(allNonClosedLoanId); } return partitions; } + private void createNewPartition(Map partitions, int partitionIndex, + TreeMap cobBusinessStepMap) { + ExecutionContext executionContext = new ExecutionContext(); + executionContext.put("loanIds", new ArrayList()); + executionContext.put("BusinessStepMap", cobBusinessStepMap); + partitions.put(PARTITION_PREFIX + partitionIndex, executionContext); + } + @Nullable private Map stopJobExecution() { Set runningJobExecutions = jobExplorer.findRunningJobExecutions(JobName.LOAN_COB.name()); diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveAllNonClosedLoanIdServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveAllNonClosedLoanIdServiceImpl.java new file mode 100644 index 000000000..1f84d1f84 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveAllNonClosedLoanIdServiceImpl.java @@ -0,0 +1,34 @@ +/** + * 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.loan; + +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository; + +@RequiredArgsConstructor +public class RetrieveAllNonClosedLoanIdServiceImpl implements RetrieveLoanIdService { + + private final LoanRepository loanRepository; + + @Override + public List retrieveLoanIds() { + return loanRepository.findAllNonClosedLoanIds(); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdConfiguration.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdConfiguration.java new file mode 100644 index 000000000..d878d0de0 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdConfiguration.java @@ -0,0 +1,38 @@ +/** + * 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.loan; + +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class RetrieveLoanIdConfiguration { + + @Autowired + private LoanRepository loanRepository; + + @Bean + @ConditionalOnMissingBean + public RetrieveLoanIdService retrieveLoanIdService() { + return new RetrieveAllNonClosedLoanIdServiceImpl(loanRepository); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdService.java b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdService.java new file mode 100644 index 000000000..5147b01e9 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/loan/RetrieveLoanIdService.java @@ -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.loan; + +import java.util.List; + +public interface RetrieveLoanIdService { + + List retrieveLoanIds(); +}