From a8e7872cc3b71b7daa5475d15efb7e58a74610a7 Mon Sep 17 00:00:00 2001 From: Manoj SP Date: Wed, 12 Jan 2022 18:34:32 +0530 Subject: [PATCH 1/2] [MOSIP-19429] added thread pool with config prop and logic to monitor --- .../IdAuthFraudAnalysisEventManager.java | 4 +- .../common/service/config/IdAuthConfig.java | 67 +++++++++++++++++++ .../common/service/helper/WebSubHelper.java | 2 +- .../common/service/util/EnvUtil.java | 9 +++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/manager/IdAuthFraudAnalysisEventManager.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/manager/IdAuthFraudAnalysisEventManager.java index 2f91159e692..37795a437cb 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/manager/IdAuthFraudAnalysisEventManager.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/manager/IdAuthFraudAnalysisEventManager.java @@ -53,7 +53,7 @@ public class IdAuthFraudAnalysisEventManager { @Autowired private ObjectMapper mapper; - @Async + @Async("fraudAnalysisExecutor") public void analyseDigitalSignatureFailure(String uri, Map request, String errorMessage) { if (EnvUtil.getIsFraudAnalysisEnabled()) { List pathSegments = Arrays.asList(uri.split("/")); @@ -73,7 +73,7 @@ public void analyseDigitalSignatureFailure(String uri, Map reque } } - @Async + @Async("fraudAnalysisExecutor") public void analyseEvent(AutnTxn txn) { if (EnvUtil.getIsFraudAnalysisEnabled()) { IdAuthFraudAnalysisEventDTO eventData = createEventData(txn.getRefId(), txn.getRequestTrnId(), diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java index 3b80de46c44..b29e183c5d7 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java @@ -2,15 +2,20 @@ import java.util.Arrays; import java.util.Locale; +import java.util.concurrent.Executor; import java.util.stream.Collectors; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @@ -20,7 +25,10 @@ import io.mosip.authentication.common.service.util.EnvUtil; import io.mosip.authentication.core.constant.RestServicesConstants; import io.mosip.authentication.core.indauth.dto.IdType; +import io.mosip.authentication.core.logger.IdaLogger; import io.mosip.idrepository.core.builder.RestRequestBuilder; +import io.mosip.kernel.core.logger.spi.Logger; +import io.mosip.kernel.core.util.StringUtils; import io.mosip.kernel.dataaccess.hibernate.config.HibernateDaoConfig; /** @@ -30,6 +38,8 @@ * */ public abstract class IdAuthConfig extends HibernateDaoConfig { + + private static final Logger logger = IdaLogger.getLogger(IdAuthConfig.class); /** The environment. */ @Autowired @@ -113,5 +123,62 @@ public RestRequestBuilder getRestRequestBuilder() { return new RestRequestBuilder(Arrays.stream(RestServicesConstants.values()) .map(RestServicesConstants::getServiceName).collect(Collectors.toList())); } + + @Bean + @Primary + public Executor executor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); + executor.setThreadNamePrefix("idauth-"); + executor.setWaitForTasksToCompleteOnShutdown(true); + executor.initialize(); + return executor; + } + + @Bean + @Qualifier("webSubHelperExecutor") + public Executor webSubHelperExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); + executor.setThreadNamePrefix("idauth-websub-"); + executor.setWaitForTasksToCompleteOnShutdown(true); + executor.initialize(); + return executor; + } + + @Bean + @Qualifier("fraudAnalysisExecutor") + public Executor fraudAnalysisExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); + executor.setThreadNamePrefix("idauth-fraud-analysis-"); + executor.setWaitForTasksToCompleteOnShutdown(true); + executor.initialize(); + return executor; + } + + @Scheduled(fixedRateString = "${" + "mosip.ida.monitor-thread-queue-in-ms" + ":10000}") + public void monitorThreadQueueLimit() { + if (StringUtils.isNotBlank(EnvUtil.getMonitorAsyncThreadQueue())) { + ThreadPoolTaskExecutor threadPoolTaskExecutor = (ThreadPoolTaskExecutor) executor(); + ThreadPoolTaskExecutor webSubHelperExecutor = (ThreadPoolTaskExecutor) webSubHelperExecutor(); + ThreadPoolTaskExecutor fraudAnalysisExecutor = (ThreadPoolTaskExecutor) fraudAnalysisExecutor(); + String monitoringLog = "Thread Name : {} Thread Active Count: {} Thread Task count: {} Thread queue count: {}"; + logThreadQueueDetails(threadPoolTaskExecutor, threadPoolTaskExecutor.getThreadPoolExecutor().getQueue().size(), monitoringLog); + logThreadQueueDetails(webSubHelperExecutor, webSubHelperExecutor.getThreadPoolExecutor().getQueue().size(), monitoringLog); + logThreadQueueDetails(fraudAnalysisExecutor, fraudAnalysisExecutor.getThreadPoolExecutor().getQueue().size(), monitoringLog); + } + } + + private void logThreadQueueDetails(ThreadPoolTaskExecutor threadPoolTaskExecutor, int threadPoolQueueSize, + String monitoringLog) { + if (threadPoolQueueSize > EnvUtil.getAsyncThreadQueueThreshold()) + logger.info(monitoringLog, threadPoolTaskExecutor.getThreadNamePrefix(), + threadPoolTaskExecutor.getActiveCount(), + threadPoolTaskExecutor.getThreadPoolExecutor().getTaskCount(), threadPoolQueueSize); + } } diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/helper/WebSubHelper.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/helper/WebSubHelper.java index ea981fefbf2..ddf78ce1cb2 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/helper/WebSubHelper.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/helper/WebSubHelper.java @@ -136,7 +136,7 @@ public void initRegistrar(WebSubEventTopicRegistrar registrar, Supplier * @param eventModel the event model */ @WithRetry - @Async + @Async("webSubHelperExecutor") public void publishEvent(String eventTopic, U eventModel) { publisher.publishUpdate(eventTopic, eventModel, MediaType.APPLICATION_JSON_VALUE, null, publisherUrl); } diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/util/EnvUtil.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/util/EnvUtil.java index b4b2590b432..200121581d8 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/util/EnvUtil.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/util/EnvUtil.java @@ -191,6 +191,12 @@ public class EnvUtil { @Getter @Setter private static String internalAuthInternalBioRefId; + @Getter @Setter private static Integer activeAsyncThreadCount; + + @Getter @Setter private static String monitorAsyncThreadQueue; + + @Getter @Setter private static Integer asyncThreadQueueThreshold; + @Autowired private Environment env; @@ -258,6 +264,9 @@ public void init() { setInternalAuthTrustValidationRequired(this.getProperty("mosip.ida.internal.trust-validation-required", Boolean.class, false)); setInternalAuthInternalRefId(this.getProperty(INTERNAL_REFERENCE_ID)); setInternalAuthInternalBioRefId(this.getProperty(INTERNAL_BIO_REFERENCE_ID)); + setActiveAsyncThreadCount(this.getProperty("mosip.ida.active-async-thread-count", Integer.class)); + setMonitorAsyncThreadQueue(this.getProperty("mosip.ida.monitor-thread-queue-in-ms")); + setAsyncThreadQueueThreshold(this.getProperty("mosip.ida.max-thread-queue-threshold", Integer.class, 0)); } public String getProperty(String key) { From cef61df6cb67333fc639db38bc03e1153291f145 Mon Sep 17 00:00:00 2001 From: Manoj SP Date: Wed, 12 Jan 2022 18:36:30 +0530 Subject: [PATCH 2/2] [MOSIP-19429] updated thread group count --- .../authentication/common/service/config/IdAuthConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java index b29e183c5d7..557d5bfdc4c 100644 --- a/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java +++ b/authentication/authentication-common/src/main/java/io/mosip/authentication/common/service/config/IdAuthConfig.java @@ -128,7 +128,7 @@ public RestRequestBuilder getRestRequestBuilder() { @Primary public Executor executor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 3)); executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); executor.setThreadNamePrefix("idauth-"); executor.setWaitForTasksToCompleteOnShutdown(true); @@ -140,7 +140,7 @@ public Executor executor() { @Qualifier("webSubHelperExecutor") public Executor webSubHelperExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 3)); executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); executor.setThreadNamePrefix("idauth-websub-"); executor.setWaitForTasksToCompleteOnShutdown(true); @@ -152,7 +152,7 @@ public Executor webSubHelperExecutor() { @Qualifier("fraudAnalysisExecutor") public Executor fraudAnalysisExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); - executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 4)); + executor.setCorePoolSize(Math.floorDiv(EnvUtil.getActiveAsyncThreadCount(), 3)); executor.setMaxPoolSize(EnvUtil.getActiveAsyncThreadCount()); executor.setThreadNamePrefix("idauth-fraud-analysis-"); executor.setWaitForTasksToCompleteOnShutdown(true);