From fc4f2af3dbcbfa3d7cb777befd66d18b05997bb5 Mon Sep 17 00:00:00 2001 From: Isuru Wijesiri Date: Thu, 31 Aug 2023 00:39:29 +0530 Subject: [PATCH] Add min transaction count --- .../handlers/transaction/TransactionCountHandler.java | 2 +- .../transaction/TransactionCounterConstants.java | 3 +++ .../handlers/transaction/config/APIMConfigFetcher.java | 10 +++++++--- .../transaction/config/TransactionCounterConfig.java | 5 +++++ .../producer/TransactionRecordProducer.java | 4 +++- .../apimgt/impl/internal/APIManagerComponent.java | 1 - 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java index 21899fc3c2bf..c67be402f426 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCountHandler.java @@ -13,7 +13,7 @@ import org.wso2.carbon.apimgt.gateway.handlers.transaction.config.TransactionCounterConfig; import java.lang.reflect.Constructor; -import java.util.concurrent.*; +import java.util.concurrent.RejectedExecutionException; public class TransactionCountHandler extends AbstractExtendedSynapseHandler { diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java index 7f290f98d4f4..025702d61083 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/TransactionCounterConstants.java @@ -12,6 +12,7 @@ public class TransactionCounterConstants { public static final String PRODUCER_THREAD_POOL_SIZE = "producerThreadPoolSize"; public static final String TRANSACTION_COUNT_RECORD_INTERVAL = "transactionCountRecordInterval"; public static final String MAX_TRANSACTION_COUNT = "maxTransactionCount"; + public static final String MIN_TRANSACTION_COUNT = "minTransactionCount"; public static final String CONSUMER_COMMIT_INTERVAL = "consumerCommitInterval"; public static final String MAX_TRANSACTION_RECORDS_PER_COMMIT = "maxTransactionRecordsPerCommit"; public static final String MAX_RETRY_COUNT = "maxRetryCount"; @@ -28,6 +29,8 @@ public class TransactionCounterConstants { public static final String GATEWAY_STORE_CLASS = GATEWAY_CONFIG_ROOT + ".StoreClass"; public static final String GATEWAY_MAX_TRANSACTION_COUNT = GATEWAY_CONFIG_ROOT + ".MaxTransactionCount"; + public static final String GATEWAY_MIN_TRANSACTION_COUNT = GATEWAY_CONFIG_ROOT + + ".MinTransactionCount"; public static final String GATEWAY_RECORD_INTERVAL = GATEWAY_CONFIG_ROOT + ".ProducerScheduledInterval"; public static final String GATEWAY_MAX_RETRY_COUNT = GATEWAY_CONFIG_ROOT + ".MaxRetryCount"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java index 98234afdd03a..46af5144f5df 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/APIMConfigFetcher.java @@ -57,6 +57,11 @@ private APIMConfigFetcher() throws TransactionCounterInitializationException { temp = Objects.requireNonNull(temp, "Max transaction count cannot be null"); Double MAX_TRANSACTION_COUNT = Double.parseDouble(temp); + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, + TransactionCounterConstants.GATEWAY_MIN_TRANSACTION_COUNT); + temp = Objects.requireNonNull(temp, "Min transaction count cannot be null"); + Double MIN_TRANSACTION_COUNT = Double.parseDouble(temp); + temp = (String) getFirstProperty.invoke(apiManagerConfiguration, TransactionCounterConstants.GATEWAY_CONSUMER_COMMIT_INTERVAL); temp = Objects.requireNonNull(temp, "Consumer commit interval cannot be null"); @@ -93,6 +98,7 @@ private APIMConfigFetcher() throws TransactionCounterInitializationException { configMap.put(TransactionCounterConstants.PRODUCER_THREAD_POOL_SIZE, PRODUCER_THREAD_POOL_SIZE); configMap.put(TransactionCounterConstants.TRANSACTION_COUNT_RECORD_INTERVAL , TRANSACTION_COUNT_RECORD_INTERVAL); configMap.put(TransactionCounterConstants.MAX_TRANSACTION_COUNT, MAX_TRANSACTION_COUNT); + configMap.put(TransactionCounterConstants.MIN_TRANSACTION_COUNT, MIN_TRANSACTION_COUNT); configMap.put(TransactionCounterConstants.CONSUMER_COMMIT_INTERVAL, CONSUMER_COMMIT_INTERVAL); configMap.put(TransactionCounterConstants.MAX_TRANSACTION_RECORDS_PER_COMMIT, MAX_TRANSACTION_RECORDS_PER_COMMIT); configMap.put(TransactionCounterConstants.MAX_RETRY_COUNT, MAX_RETRY_COUNT); @@ -104,10 +110,8 @@ private APIMConfigFetcher() throws TransactionCounterInitializationException { // This error won't be thrown here because it is already checked in TransactionCountConfig } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new TransactionCounterInitializationException(); - } catch (NumberFormatException e) { + } catch (NumberFormatException | NullPointerException e) { throw new TransactionCounterInitializationException("Error while reading the config values", e); - } catch (NullPointerException e) { - throw new TransactionCounterInitializationException("All configuration values needs to be provided", e); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java index 7a40d614fdf0..6272d1b59852 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/config/TransactionCounterConfig.java @@ -41,6 +41,11 @@ public static double getMaxTransactionCount() { configFetcher.getConfigValue(TransactionCounterConstants.MAX_TRANSACTION_COUNT)); } + public static double getMinTransactionCount() { + return Double.parseDouble( + configFetcher.getConfigValue(TransactionCounterConstants.MIN_TRANSACTION_COUNT)); + } + public static int getTransactionCountRecordInterval() { return Integer.parseInt( configFetcher.getConfigValue(TransactionCounterConstants.TRANSACTION_COUNT_RECORD_INTERVAL)); diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java index cf339395d851..ec67d60f0783 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/transaction/producer/TransactionRecordProducer.java @@ -16,6 +16,7 @@ public class TransactionRecordProducer { private static double MAX_TRANSACTION_COUNT; + private static double MIN_TRANSACTION_COUNT; private static int TRANSACTION_COUNT_RECORD_INTERVAL; private static final Log LOG = LogFactory.getLog(TransactionRecordProducer.class); private static TransactionRecordProducer instance = null; @@ -28,6 +29,7 @@ private TransactionRecordProducer(TransactionRecordQueue transactionRecordQueue, // Obtain config values MAX_TRANSACTION_COUNT = TransactionCounterConfig.getMaxTransactionCount(); + MIN_TRANSACTION_COUNT = TransactionCounterConfig.getMinTransactionCount(); TRANSACTION_COUNT_RECORD_INTERVAL = TransactionCounterConfig.getTransactionCountRecordInterval(); this.transactionRecordQueue = transactionRecordQueue; @@ -76,7 +78,7 @@ private void produceRecordScheduled() { lock.lock(); try { int transactionCountValue = transactionCount.get(); - if (transactionCountValue != 0) { + if (transactionCountValue >= MIN_TRANSACTION_COUNT) { TransactionRecord transactionRecord = new TransactionRecord(transactionCountValue); LOG.info("Transaction count is added to the queue from scheduled producer"); transactionRecordQueue.add(transactionRecord); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java index 27576f53c44f..d43fc8a39596 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java @@ -214,7 +214,6 @@ protected void activate(ComponentContext componentContext) throws Exception { bundleContext.registerService(Notifier.class.getName(),new ExternallyDeployedApiNotifier(),null); bundleContext.registerService(Notifier.class.getName(),new KeyTemplateNotifier(), null); bundleContext.registerService(Notifier.class.getName(), new CorrelationConfigNotifier(), null); - // temp APIManagerConfigurationServiceImpl configurationService = new APIManagerConfigurationServiceImpl(configuration); ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(configurationService); APIMgtDBUtil.initialize();