From d2c21d27c9905df8b5a9968e2fe25eca07608d50 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Mon, 5 Sep 2022 14:38:13 +0530 Subject: [PATCH 01/17] initial commit --- .../enforcer/jmx/MBeanManagementFactory.java | 41 +++++++ .../enforcer/jmx/MBeanRegistrator.java | 103 ++++++++++++++++++ .../enforcer/server/EnforcerWorkerPool.java | 10 +- .../enforcer/server/NativeThreadFactory.java | 7 +- .../server/NativeThreadFactoryMBean.java | 28 +++++ 5 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java new file mode 100644 index 0000000000..43486650a0 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.jmx; + +import javax.management.MBeanServer; +import javax.management.MBeanServerFactory; + +/** + * The ManagementFactory class is a factory class for getting managed beans for + * the Enforcer. + */ +public class MBeanManagementFactory { + + /* + * @return A MBeanServer instance. + * If one already exists, it will return that else it will create a new one and + * return. + */ + public static MBeanServer getMBeanServer() { + MBeanServer mBeanServer; + if (MBeanServerFactory.findMBeanServer(null).size() > 0) { + mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0); + } else { + mBeanServer = MBeanServerFactory.createMBeanServer(); + } + return mBeanServer; + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java new file mode 100644 index 0000000000..2ca14d2d86 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.jmx; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; +import javax.management.MBeanRegistrationException; +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.NotCompliantMBeanException; +import javax.management.ObjectName; + +/** + * The class which is responsible for registering MBeans. + */ +public class MBeanRegistrator { + private static final Logger logger = LoggerFactory.getLogger(MBeanRegistrator.class); + private static List mBeans = new ArrayList<>(); + + private static final String SERVER_PACKAGE = "org.wso2.choreo.connect.enforcer"; + + private MBeanRegistrator() { + } + + /** + * Registers an object as an MBean with the MBean server. + * + * @param mBeanInstance - The MBean to be registered as an MBean. + */ + public static void registerMBean(Object mBeanInstance) throws RuntimeException { + + String className = mBeanInstance.getClass().getName(); + if (className.indexOf('.') != -1) { + className = className.substring(className.lastIndexOf('.') + 1); + } + + String objectName = SERVER_PACKAGE + ":type=" + className; + try { + // MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + Set set = mBeanServer.queryNames(new ObjectName(objectName), null); + if (set.isEmpty()) { + try { + ObjectName name = new ObjectName(objectName); + mBeanServer.registerMBean(mBeanInstance, name); + mBeans.add(name); + } catch (InstanceAlreadyExistsException e) { + String msg = "MBean " + objectName + " already exists"; + logger.error(msg, e); + throw new RuntimeException(msg, e); + } catch (MBeanRegistrationException | NotCompliantMBeanException e) { + String msg = "Execption when registering MBean"; + logger.error(msg, e); + throw new RuntimeException(msg, e); + } + } else { + String msg = "MBean " + objectName + " already exists"; + logger.error(msg); + throw new RuntimeException(msg); + } + } catch (MalformedObjectNameException e) { + String msg = "Could not register " + mBeanInstance.getClass() + " MBean"; + logger.error(msg); + throw new RuntimeException(msg, e); + } + } + + /** + * Unregisters all MBeans from the MBean server. + * + */ + public static void unregisterAllMBeans() { + MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); + mBeans.forEach(mBean -> { + try { + mBeanServer.unregisterMBean(mBean); + } catch (InstanceNotFoundException | MBeanRegistrationException e) { + logger.error("Cannot unregister MBean " + mBean.getCanonicalName(), e); + } + }); + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java index dff40de04b..0970831fe8 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -27,7 +28,8 @@ import java.util.concurrent.TimeUnit; /** - * Class that holds the thread pool that serves the external auth requests coming from the router component. + * Class that holds the thread pool that serves the external auth requests + * coming from the router component. */ public class EnforcerWorkerPool { private final BlockingQueue blockingQueue; @@ -37,8 +39,12 @@ public class EnforcerWorkerPool { public EnforcerWorkerPool(int core, int max, int keepAlive, int queueLength, String threadGroupName, String threadGroupId) { this.blockingQueue = queueLength == -1 ? new LinkedBlockingQueue() : new LinkedBlockingQueue(queueLength); + NativeThreadFactory workerThreadFactory = new NativeThreadFactory(new ThreadGroup(threadGroupName), + threadGroupId); this.executor = new EnforcerThreadPoolExecutor(core, max, (long) keepAlive, TimeUnit.SECONDS, - this.blockingQueue, new NativeThreadFactory(new ThreadGroup(threadGroupName), threadGroupId)); + this.blockingQueue, workerThreadFactory); + MBeanRegistrator.registerMBean(workerThreadFactory); + } public ThreadPoolExecutor getExecutor() { diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java index 39ef6f13d6..5046023a10 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java @@ -24,7 +24,7 @@ /** * Creates thread with specific thread group and name prefix. */ -public class NativeThreadFactory implements ThreadFactory { +public class NativeThreadFactory implements ThreadFactory, NativeThreadFactoryMBean { final ThreadGroup group; final AtomicInteger count; @@ -47,4 +47,9 @@ public Thread newThread(final Runnable runnable) { t.setPriority(Thread.NORM_PRIORITY); return t; } + + @Override + public int getActiveCount() { + return this.group.activeCount(); + } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java new file mode 100644 index 0000000000..43553b54c4 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.choreo.connect.enforcer.server; + +/** + * Creates thread with specific thread group and name prefix. + */ +public interface NativeThreadFactoryMBean { + + // Returns an estimate of the number of active worker threads. + public int getActiveCount(); +} From 4ea7d25089db5f73bad172530bcfa48f2df23566 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Wed, 7 Sep 2022 15:26:55 +0530 Subject: [PATCH 02/17] ext auth jmx metrics --- .../connect/enforcer/grpc/ExtAuthService.java | 2 + .../choreo/connect/enforcer/jmx/JMXAgent.java | 58 ++++++++++++++++++ .../enforcer/jmx/MBeanRegistrator.java | 4 +- .../metrics/jmx/api/ExtAuthMetricsMXBean.java | 18 ++++++ .../metrics/jmx/impl/ExtAuthMetrics.java | 60 +++++++++++++++++++ .../connect/enforcer/server/AuthServer.java | 3 + 6 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java index bc06feae5c..88c81d852a 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java @@ -40,6 +40,7 @@ import org.wso2.choreo.connect.enforcer.constants.HttpConstants; import org.wso2.choreo.connect.enforcer.metrics.MetricsExporter; import org.wso2.choreo.connect.enforcer.metrics.MetricsManager; +import org.wso2.choreo.connect.enforcer.metrics.jmx.impl.ExtAuthMetrics; import org.wso2.choreo.connect.enforcer.server.HttpRequestHandler; import org.wso2.choreo.connect.enforcer.tracing.TracingConstants; import org.wso2.choreo.connect.enforcer.tracing.TracingContextHolder; @@ -92,6 +93,7 @@ public void check(CheckRequest request, StreamObserver responseOb MetricsExporter metricsExporter = MetricsManager.getInstance(); metricsExporter.trackMetric("enforcerLatency", System.currentTimeMillis() - starTimestamp); } + ExtAuthMetrics.getInstance().recordMetric(System.currentTimeMillis() - starTimestamp); } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java new file mode 100644 index 0000000000..f6cbe6074d --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.jmx; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.remote.JMXConnectorServer; +import javax.management.remote.JMXConnectorServerFactory; +import javax.management.remote.JMXServiceURL; + +/** + * JMX Client + */ +public class JMXAgent { + private static final Logger logger = LoggerFactory.getLogger(JMXAgent.class); + private static JMXConnectorServer jmxConnectorServer; + private static String hostnameDefault = ""; + private static String rmiServerPort = "11111"; + private static String rmiRegistryPort = "9999"; + private static final String JAVA_RMI_SERVER_HOSTNAME = "java.rmi.server.hostname"; + + public static void initJMXAgent() { + try { + + String hostname = System.getProperty(JAVA_RMI_SERVER_HOSTNAME); + if (hostname == null || hostname.isEmpty()) { + hostname = hostnameDefault; + System.setProperty(JAVA_RMI_SERVER_HOSTNAME, hostname); + } + + String jmxURL = "service:jmx:rmi://" + hostname + ":" + rmiServerPort + + "/jndi/rmi://" + hostname + ":" + rmiRegistryPort + "/jmxrmi"; + JMXServiceURL jmxServiceURL = new JMXServiceURL(jmxURL); + + jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, + MBeanManagementFactory.getMBeanServer()); + jmxConnectorServer.start(); + logger.info("JMXServerManager JMX Service URL : " + jmxServiceURL.toString()); + } catch (Throwable throwable) { + logger.error("Failed to start JMX Agent", throwable); + } + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java index 2ca14d2d86..ab6315a183 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -18,7 +18,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.management.ManagementFactory; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -57,8 +56,7 @@ public static void registerMBean(Object mBeanInstance) throws RuntimeException { String objectName = SERVER_PACKAGE + ":type=" + className; try { - // MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); - MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); Set set = mBeanServer.queryNames(new ObjectName(objectName), null); if (set.isEmpty()) { try { diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java new file mode 100644 index 0000000000..b2c6cbf03b --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java @@ -0,0 +1,18 @@ +package org.wso2.choreo.connect.enforcer.metrics.jmx.api; + +/** + * MBean API for ExtAuth Service metrics. + */ +public interface ExtAuthMetricsMXBean { + + public long getTotalRequestCount(); + + public long getTotalResponseCount(); + + public long getAverageResponseTimeMillis(); + + public long getMaxResponseTimeMillis(); + + public long getMinResponseTimeMillis(); + +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java new file mode 100644 index 0000000000..03cf4da412 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -0,0 +1,60 @@ +package org.wso2.choreo.connect.enforcer.metrics.jmx.impl; + +import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; +import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMXBean; + +/** + * Singleton MBean for ExtAuth Service metrics. + */ +public class ExtAuthMetrics implements ExtAuthMetricsMXBean { + + private static ExtAuthMetrics extAuthMetricsMBean = null; + + private long totalRequestCount = 0; + + private long totalResponseCount = 0; + + private long averageResponseTimeMillis = 0; + + private long maxResponseTimeMillis = Long.MIN_VALUE; + + private long minResponseTimeMillis = Long.MAX_VALUE; + + private ExtAuthMetrics() { + MBeanRegistrator.registerMBean(this); + } + + public static ExtAuthMetrics getInstance() { + if (extAuthMetricsMBean == null) { + extAuthMetricsMBean = new ExtAuthMetrics(); + } + return extAuthMetricsMBean; + } + + public long getTotalRequestCount() { + return totalRequestCount; + }; + + public long getTotalResponseCount() { + return totalResponseCount; + }; + + public long getAverageResponseTimeMillis() { + return averageResponseTimeMillis; + }; + + public long getMaxResponseTimeMillis() { + return maxResponseTimeMillis; + }; + + public long getMinResponseTimeMillis() { + return minResponseTimeMillis; + }; + + public synchronized void recordMetric(long timeMillis) { + this.totalRequestCount += 1; + this.averageResponseTimeMillis = (this.averageResponseTimeMillis + timeMillis) / totalRequestCount; + this.minResponseTimeMillis = Math.min(this.minResponseTimeMillis, timeMillis); + this.maxResponseTimeMillis = Math.max(this.maxResponseTimeMillis, timeMillis); + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java index 37b8c388f2..778b6942da 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java @@ -108,6 +108,9 @@ public static void main(String[] args) { // Create a new server to listen on port 8081 Server server = initServer(); + // Initialize JMX Agent + // JMXAgent.initJMXAgent(); + // Enable global filters if (enforcerConfig.getAnalyticsConfig().isEnabled() || enforcerConfig.getMetricsConfig().isMetricsEnabled()) { From 69ae49346b5ee44cae0ecc6e61bd32707d6d6538 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Thu, 8 Sep 2022 18:24:22 +0530 Subject: [PATCH 03/17] jmx enabled flag added "-Dchoreo.connect.metrics.enable" --- .../connect/enforcer/grpc/ExtAuthService.java | 5 ++- .../choreo/connect/enforcer/jmx/JMXUtils.java | 18 ++++++++++ .../metrics/jmx/api/ExtAuthMetricsMXBean.java | 34 +++++++++++++++++-- .../metrics/jmx/impl/ExtAuthMetrics.java | 29 +++++++++------- 4 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java index 88c81d852a..8f94676ab7 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java @@ -38,6 +38,7 @@ import org.wso2.choreo.connect.enforcer.api.ResponseObject; import org.wso2.choreo.connect.enforcer.constants.APIConstants; import org.wso2.choreo.connect.enforcer.constants.HttpConstants; +import org.wso2.choreo.connect.enforcer.jmx.JMXUtils; import org.wso2.choreo.connect.enforcer.metrics.MetricsExporter; import org.wso2.choreo.connect.enforcer.metrics.MetricsManager; import org.wso2.choreo.connect.enforcer.metrics.jmx.impl.ExtAuthMetrics; @@ -93,7 +94,9 @@ public void check(CheckRequest request, StreamObserver responseOb MetricsExporter metricsExporter = MetricsManager.getInstance(); metricsExporter.trackMetric("enforcerLatency", System.currentTimeMillis() - starTimestamp); } - ExtAuthMetrics.getInstance().recordMetric(System.currentTimeMillis() - starTimestamp); + if (JMXUtils.isJMXMetricsEnabled()) { + ExtAuthMetrics.getInstance().recordMetric(System.currentTimeMillis() - starTimestamp); + } } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java new file mode 100644 index 0000000000..232e8141cf --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java @@ -0,0 +1,18 @@ +package org.wso2.choreo.connect.enforcer.jmx; + +/** + * JMX Utilities + */ +public class JMXUtils { + + private static final String CHOREO_CONNECT_JMX_METRICS_ENABLE = "choreo.connect.jmx.metrics.enable"; + + /** + * Return true if jmx metrics enabled, otherwise false. + * + * @return boolean + */ + public static boolean isJMXMetricsEnabled() { + return Boolean.getBoolean(CHOREO_CONNECT_JMX_METRICS_ENABLE); + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java index b2c6cbf03b..5606ddc35e 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java @@ -5,14 +5,44 @@ */ public interface ExtAuthMetricsMXBean { + /** + * Getter for total request count. + * + * @return long + */ public long getTotalRequestCount(); - public long getTotalResponseCount(); - + /** + * Getter for average response time in milli seconds. + * + * @return long + */ public long getAverageResponseTimeMillis(); + /** + * Getter for maximum response time in milliseconds. + * + * @return long + */ public long getMaxResponseTimeMillis(); + /** + * Getter for mimnimum response time in milliseconds. + * + * @return long + */ public long getMinResponseTimeMillis(); + /** + * Calculates the metrics for response time and records them. + * + * @param responseTimeMillis + */ + public void recordMetric(long responseTimeMillis); + + /** + * Resets all the metrics to thier initial values. + */ + public void resetExtAuthMetrics(); + } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index 03cf4da412..1899e21330 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -11,19 +11,19 @@ public class ExtAuthMetrics implements ExtAuthMetricsMXBean { private static ExtAuthMetrics extAuthMetricsMBean = null; private long totalRequestCount = 0; - - private long totalResponseCount = 0; - private long averageResponseTimeMillis = 0; - private long maxResponseTimeMillis = Long.MIN_VALUE; - private long minResponseTimeMillis = Long.MAX_VALUE; private ExtAuthMetrics() { MBeanRegistrator.registerMBean(this); } + /** + * Getter for the Singleton ExtAuthMetrics instance. + * + * @return ExtAuthMetrics + */ public static ExtAuthMetrics getInstance() { if (extAuthMetricsMBean == null) { extAuthMetricsMBean = new ExtAuthMetrics(); @@ -35,10 +35,6 @@ public long getTotalRequestCount() { return totalRequestCount; }; - public long getTotalResponseCount() { - return totalResponseCount; - }; - public long getAverageResponseTimeMillis() { return averageResponseTimeMillis; }; @@ -51,10 +47,17 @@ public long getMinResponseTimeMillis() { return minResponseTimeMillis; }; - public synchronized void recordMetric(long timeMillis) { + public synchronized void recordMetric(long responseTimeMillis) { this.totalRequestCount += 1; - this.averageResponseTimeMillis = (this.averageResponseTimeMillis + timeMillis) / totalRequestCount; - this.minResponseTimeMillis = Math.min(this.minResponseTimeMillis, timeMillis); - this.maxResponseTimeMillis = Math.max(this.maxResponseTimeMillis, timeMillis); + this.averageResponseTimeMillis = (this.averageResponseTimeMillis + responseTimeMillis) / totalRequestCount; + this.minResponseTimeMillis = Math.min(this.minResponseTimeMillis, responseTimeMillis); + this.maxResponseTimeMillis = Math.max(this.maxResponseTimeMillis, responseTimeMillis); + } + + public synchronized void resetExtAuthMetrics() { + this.totalRequestCount = 0; + this.averageResponseTimeMillis = 0; + this.maxResponseTimeMillis = Long.MIN_VALUE; + this.minResponseTimeMillis = Long.MAX_VALUE; } } From acbb67f7463afa0e3e62b683efee2cb26ccac3e1 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 09:48:10 +0530 Subject: [PATCH 04/17] ext auth thread pool config mbean added --- .../connect/enforcer/config/ConfigHolder.java | 3 + .../enforcer/config/dto/ThreadPoolConfig.java | 2 +- .../config/dto/ThreadPoolConfigMBean.java | 34 +++++++++++ .../choreo/connect/enforcer/jmx/JMXAgent.java | 58 ------------------- .../choreo/connect/enforcer/jmx/JMXUtils.java | 2 +- .../enforcer/jmx/MBeanRegistrator.java | 16 ----- ...csMXBean.java => ExtAuthMetricsMBean.java} | 2 +- .../metrics/jmx/impl/ExtAuthMetrics.java | 4 +- .../enforcer/server/EnforcerWorkerPool.java | 7 +-- .../enforcer/server/NativeThreadFactory.java | 7 +-- .../server/NativeThreadFactoryMBean.java | 28 --------- 11 files changed, 44 insertions(+), 119 deletions(-) create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java delete mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java rename enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/{ExtAuthMetricsMXBean.java => ExtAuthMetricsMBean.java} (96%) delete mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/ConfigHolder.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/ConfigHolder.java index 08d205ff73..1543da8435 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/ConfigHolder.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/ConfigHolder.java @@ -65,6 +65,7 @@ import org.wso2.choreo.connect.enforcer.config.dto.TracingDTO; import org.wso2.choreo.connect.enforcer.constants.APIConstants; import org.wso2.choreo.connect.enforcer.constants.Constants; +import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; import org.wso2.choreo.connect.enforcer.throttle.databridge.agent.conf.AgentConfiguration; import org.wso2.choreo.connect.enforcer.util.BackendJwtUtils; import org.wso2.choreo.connect.enforcer.util.FilterUtils; @@ -217,6 +218,8 @@ private void populateAuthService(Service cdsAuth) { authDto.setMaxMessageSize(cdsAuth.getMaxMessageSize()); ThreadPoolConfig threadPool = new ThreadPoolConfig(); + MBeanRegistrator.registerMBean(threadPool); + threadPool.setCoreSize(cdsAuth.getThreadPool().getCoreSize()); threadPool.setKeepAliveTime(cdsAuth.getThreadPool().getKeepAliveTime()); threadPool.setMaxSize(cdsAuth.getThreadPool().getMaxSize()); diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfig.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfig.java index 31a1443f3f..cecbb3fc8b 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfig.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfig.java @@ -21,7 +21,7 @@ /** * Holds the configurations related to threading of gRPC netty server. */ -public class ThreadPoolConfig { +public class ThreadPoolConfig implements ThreadPoolConfigMBean { private int coreSize; private int maxSize; private int keepAliveTime; diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java new file mode 100644 index 0000000000..7b016e34d3 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java @@ -0,0 +1,34 @@ +package org.wso2.choreo.connect.enforcer.config.dto; + +/** + * MBean API for Thread Pool Configuration. + */ +public interface ThreadPoolConfigMBean { + /** + * Getter for core size. + * + * @return int + */ + public int getCoreSize(); + + /** + * Getter for max size. + * + * @return int + */ + public int getMaxSize(); + + /** + * Getter for keep alive size. + * + * @return int + */ + public int getKeepAliveTime(); + + /** + * Getter for queue size. + * + * @return int + */ + public int getQueueSize(); +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java deleted file mode 100644 index f6cbe6074d..0000000000 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.wso2.choreo.connect.enforcer.jmx; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.management.remote.JMXConnectorServer; -import javax.management.remote.JMXConnectorServerFactory; -import javax.management.remote.JMXServiceURL; - -/** - * JMX Client - */ -public class JMXAgent { - private static final Logger logger = LoggerFactory.getLogger(JMXAgent.class); - private static JMXConnectorServer jmxConnectorServer; - private static String hostnameDefault = ""; - private static String rmiServerPort = "11111"; - private static String rmiRegistryPort = "9999"; - private static final String JAVA_RMI_SERVER_HOSTNAME = "java.rmi.server.hostname"; - - public static void initJMXAgent() { - try { - - String hostname = System.getProperty(JAVA_RMI_SERVER_HOSTNAME); - if (hostname == null || hostname.isEmpty()) { - hostname = hostnameDefault; - System.setProperty(JAVA_RMI_SERVER_HOSTNAME, hostname); - } - - String jmxURL = "service:jmx:rmi://" + hostname + ":" + rmiServerPort - + "/jndi/rmi://" + hostname + ":" + rmiRegistryPort + "/jmxrmi"; - JMXServiceURL jmxServiceURL = new JMXServiceURL(jmxURL); - - jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, - MBeanManagementFactory.getMBeanServer()); - jmxConnectorServer.start(); - logger.info("JMXServerManager JMX Service URL : " + jmxServiceURL.toString()); - } catch (Throwable throwable) { - logger.error("Failed to start JMX Agent", throwable); - } - } -} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java index 232e8141cf..540d34ecb9 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java @@ -8,7 +8,7 @@ public class JMXUtils { private static final String CHOREO_CONNECT_JMX_METRICS_ENABLE = "choreo.connect.jmx.metrics.enable"; /** - * Return true if jmx metrics enabled, otherwise false. + * Returns true if jmx metrics enabled as a system property, otherwise false. * * @return boolean */ diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java index ab6315a183..a64836832d 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -23,7 +23,6 @@ import java.util.Set; import javax.management.InstanceAlreadyExistsException; -import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; @@ -83,19 +82,4 @@ public static void registerMBean(Object mBeanInstance) throws RuntimeException { throw new RuntimeException(msg, e); } } - - /** - * Unregisters all MBeans from the MBean server. - * - */ - public static void unregisterAllMBeans() { - MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); - mBeans.forEach(mBean -> { - try { - mBeanServer.unregisterMBean(mBean); - } catch (InstanceNotFoundException | MBeanRegistrationException e) { - logger.error("Cannot unregister MBean " + mBean.getCanonicalName(), e); - } - }); - } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java similarity index 96% rename from enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java rename to enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java index 5606ddc35e..7ef193d251 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java @@ -3,7 +3,7 @@ /** * MBean API for ExtAuth Service metrics. */ -public interface ExtAuthMetricsMXBean { +public interface ExtAuthMetricsMBean { /** * Getter for total request count. diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index 1899e21330..1c4aeb40d6 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -1,12 +1,12 @@ package org.wso2.choreo.connect.enforcer.metrics.jmx.impl; import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; -import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMXBean; +import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMBean; /** * Singleton MBean for ExtAuth Service metrics. */ -public class ExtAuthMetrics implements ExtAuthMetricsMXBean { +public class ExtAuthMetrics implements ExtAuthMetricsMBean { private static ExtAuthMetrics extAuthMetricsMBean = null; diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java index 0970831fe8..aa056d3c71 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java @@ -20,7 +20,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -39,12 +38,8 @@ public class EnforcerWorkerPool { public EnforcerWorkerPool(int core, int max, int keepAlive, int queueLength, String threadGroupName, String threadGroupId) { this.blockingQueue = queueLength == -1 ? new LinkedBlockingQueue() : new LinkedBlockingQueue(queueLength); - NativeThreadFactory workerThreadFactory = new NativeThreadFactory(new ThreadGroup(threadGroupName), - threadGroupId); this.executor = new EnforcerThreadPoolExecutor(core, max, (long) keepAlive, TimeUnit.SECONDS, - this.blockingQueue, workerThreadFactory); - MBeanRegistrator.registerMBean(workerThreadFactory); - + this.blockingQueue, new NativeThreadFactory(new ThreadGroup(threadGroupName), threadGroupId)); } public ThreadPoolExecutor getExecutor() { diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java index 5046023a10..39ef6f13d6 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactory.java @@ -24,7 +24,7 @@ /** * Creates thread with specific thread group and name prefix. */ -public class NativeThreadFactory implements ThreadFactory, NativeThreadFactoryMBean { +public class NativeThreadFactory implements ThreadFactory { final ThreadGroup group; final AtomicInteger count; @@ -47,9 +47,4 @@ public Thread newThread(final Runnable runnable) { t.setPriority(Thread.NORM_PRIORITY); return t; } - - @Override - public int getActiveCount() { - return this.group.activeCount(); - } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java deleted file mode 100644 index 43553b54c4..0000000000 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/NativeThreadFactoryMBean.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.wso2.choreo.connect.enforcer.server; - -/** - * Creates thread with specific thread group and name prefix. - */ -public interface NativeThreadFactoryMBean { - - // Returns an estimate of the number of active worker threads. - public int getActiveCount(); -} From 20d831cc621febf8d2a94c0051bc34fb92a9c2b3 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 09:51:26 +0530 Subject: [PATCH 05/17] cleanup --- .../org/wso2/choreo/connect/enforcer/server/AuthServer.java | 3 --- .../choreo/connect/enforcer/server/EnforcerWorkerPool.java | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java index 778b6942da..37b8c388f2 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java @@ -108,9 +108,6 @@ public static void main(String[] args) { // Create a new server to listen on port 8081 Server server = initServer(); - // Initialize JMX Agent - // JMXAgent.initJMXAgent(); - // Enable global filters if (enforcerConfig.getAnalyticsConfig().isEnabled() || enforcerConfig.getMetricsConfig().isMetricsEnabled()) { diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java index aa056d3c71..dff40de04b 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/EnforcerWorkerPool.java @@ -27,8 +27,7 @@ import java.util.concurrent.TimeUnit; /** - * Class that holds the thread pool that serves the external auth requests - * coming from the router component. + * Class that holds the thread pool that serves the external auth requests coming from the router component. */ public class EnforcerWorkerPool { private final BlockingQueue blockingQueue; From 8f1894b0b0177b5e0d118c1b93e120c57847ba82 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 11:30:44 +0530 Subject: [PATCH 06/17] mbean renamed --- ...xtAuthMetricsMBean.java => ExtAuthMetricsMXBean.java} | 9 +-------- .../enforcer/metrics/jmx/impl/ExtAuthMetrics.java | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) rename enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/{ExtAuthMetricsMBean.java => ExtAuthMetricsMXBean.java} (78%) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java similarity index 78% rename from enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java rename to enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java index 7ef193d251..e6415bb8c5 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java @@ -3,7 +3,7 @@ /** * MBean API for ExtAuth Service metrics. */ -public interface ExtAuthMetricsMBean { +public interface ExtAuthMetricsMXBean { /** * Getter for total request count. @@ -33,13 +33,6 @@ public interface ExtAuthMetricsMBean { */ public long getMinResponseTimeMillis(); - /** - * Calculates the metrics for response time and records them. - * - * @param responseTimeMillis - */ - public void recordMetric(long responseTimeMillis); - /** * Resets all the metrics to thier initial values. */ diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index 1c4aeb40d6..1899e21330 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -1,12 +1,12 @@ package org.wso2.choreo.connect.enforcer.metrics.jmx.impl; import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; -import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMBean; +import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMXBean; /** * Singleton MBean for ExtAuth Service metrics. */ -public class ExtAuthMetrics implements ExtAuthMetricsMBean { +public class ExtAuthMetrics implements ExtAuthMetricsMXBean { private static ExtAuthMetrics extAuthMetricsMBean = null; From da4df631d0cc864642efb9163ae6cc2fa5cbc46a Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 11:33:10 +0530 Subject: [PATCH 07/17] import order fixed --- .../org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java index 16c75252be..973e2ece72 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/grpc/ExtAuthService.java @@ -37,8 +37,8 @@ import org.wso2.choreo.connect.enforcer.api.ResponseObject; import org.wso2.choreo.connect.enforcer.constants.APIConstants; import org.wso2.choreo.connect.enforcer.constants.HttpConstants; -import org.wso2.choreo.connect.enforcer.jmx.JMXUtils; import org.wso2.choreo.connect.enforcer.deniedresponse.DeniedResponsePreparer; +import org.wso2.choreo.connect.enforcer.jmx.JMXUtils; import org.wso2.choreo.connect.enforcer.metrics.MetricsExporter; import org.wso2.choreo.connect.enforcer.metrics.MetricsManager; import org.wso2.choreo.connect.enforcer.metrics.jmx.impl.ExtAuthMetrics; From eeb4357c49c9ed715bde3ffe8060e772d4999246 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 13:25:22 +0530 Subject: [PATCH 08/17] jmx connector agent added --- .../choreo/connect/enforcer/jmx/JMXAgent.java | 62 +++++++++++++++++++ .../connect/enforcer/server/AuthServer.java | 4 ++ 2 files changed, 66 insertions(+) create mode 100644 enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java new file mode 100644 index 0000000000..9ab333a200 --- /dev/null +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.jmx; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.rmi.registry.LocateRegistry; + +import javax.management.remote.JMXConnectorServer; +import javax.management.remote.JMXConnectorServerFactory; +import javax.management.remote.JMXServiceURL; + +/** + * JMX Connector Agent + */ +public class JMXAgent { + private static final Logger logger = LoggerFactory.getLogger(JMXAgent.class); + private static JMXConnectorServer jmxConnectorServer; + private static final String DEFAULT_RMI_SERVER_PORT = "1111"; + private static final String DEFAULT_RMI_REGISTRY_PORT = "9999"; + private static final String JAVA_JMX_RMI_SERVICE_PORT = "com.sun.management.jmxremote.port"; + private static final String JAVA_JMX_RMI_REGISTRY_PORT = "com.sun.management.jmxremote.rmi.port"; + + public static void initJMXAgent() { + if (JMXUtils.isJMXMetricsEnabled()) { + try { + String hostname = InetAddress.getLocalHost().getHostAddress(); + String rmiServerPort = System.getProperty(JAVA_JMX_RMI_SERVICE_PORT, DEFAULT_RMI_SERVER_PORT); + String rmiRegistryPort = System.getProperty(JAVA_JMX_RMI_REGISTRY_PORT, DEFAULT_RMI_REGISTRY_PORT); + + LocateRegistry.createRegistry(Integer.parseInt(rmiRegistryPort)); + + String jmxURL = "service:jmx:rmi://" + hostname + ":" + rmiServerPort + + "/jndi/rmi://" + hostname + ":" + rmiRegistryPort + "/jmxrmi"; + JMXServiceURL jmxServiceURL = new JMXServiceURL(jmxURL); + + jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, + MBeanManagementFactory.getMBeanServer()); + jmxConnectorServer.start(); + logger.info("JMXAgent JMX Service URL : " + jmxServiceURL.toString()); + } catch (Throwable throwable) { + logger.error("Failed to start JMX Agent", throwable); + } + } + } +} diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java index 37b8c388f2..5a6d6b8cbd 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/server/AuthServer.java @@ -43,6 +43,7 @@ import org.wso2.choreo.connect.enforcer.grpc.WebSocketFrameService; import org.wso2.choreo.connect.enforcer.grpc.interceptors.AccessLogInterceptor; import org.wso2.choreo.connect.enforcer.grpc.interceptors.OpenTelemetryInterceptor; +import org.wso2.choreo.connect.enforcer.jmx.JMXAgent; import org.wso2.choreo.connect.enforcer.keymgt.KeyManagerHolder; import org.wso2.choreo.connect.enforcer.metrics.MetricsManager; import org.wso2.choreo.connect.enforcer.security.jwt.validator.RevokedJWTDataHolder; @@ -137,6 +138,9 @@ public static void main(String[] args) { server.start(); logger.info("Sever started Listening in port : " + 8081); + // Initialize JMX Agent + JMXAgent.initJMXAgent(); + //TODO: Get the tenant domain from config SubscriptionDataHolder.getInstance().getTenantSubscriptionStore().initializeStore(); KeyManagerHolder.getInstance().init(); From fd5bac0419c2d3a9daba3d3228c80dd52e76d95e Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 9 Sep 2022 16:10:40 +0530 Subject: [PATCH 09/17] licence header added --- .../config/dto/ThreadPoolConfigMBean.java | 16 ++++++++++++++++ .../choreo/connect/enforcer/jmx/JMXUtils.java | 16 ++++++++++++++++ .../metrics/jmx/api/ExtAuthMetricsMXBean.java | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java index 7b016e34d3..167cbcce61 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/config/dto/ThreadPoolConfigMBean.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.config.dto; /** diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java index 540d34ecb9..a96ac1599a 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.jmx; /** diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java index e6415bb8c5..9da04f60b2 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.metrics.jmx.api; /** From 9ff92f256d7ebdafb6cd11937c6f22f40fdf480a Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Mon, 12 Sep 2022 11:08:19 +0530 Subject: [PATCH 10/17] license added, system property name updated --- .../choreo/connect/enforcer/jmx/JMXUtils.java | 2 +- .../metrics/jmx/impl/ExtAuthMetrics.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java index a96ac1599a..438e6f521f 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXUtils.java @@ -21,7 +21,7 @@ */ public class JMXUtils { - private static final String CHOREO_CONNECT_JMX_METRICS_ENABLE = "choreo.connect.jmx.metrics.enable"; + private static final String CHOREO_CONNECT_JMX_METRICS_ENABLE = "choreo.connect.jmx.metrics.enabled"; /** * Returns true if jmx metrics enabled as a system property, otherwise false. diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index 1899e21330..aa7a962aa6 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.wso2.choreo.connect.enforcer.metrics.jmx.impl; import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; From 35138458f6ab65d84eec3bdbca1dc789802126da Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Mon, 12 Sep 2022 11:37:48 +0530 Subject: [PATCH 11/17] formalised logging --- .../choreo/connect/enforcer/jmx/JMXAgent.java | 11 +++++++---- .../connect/enforcer/jmx/MBeanRegistrator.java | 16 +++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java index 9ab333a200..a621a8f754 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java @@ -16,8 +16,10 @@ package org.wso2.choreo.connect.enforcer.jmx; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.wso2.choreo.connect.enforcer.commons.logging.ErrorDetails; +import org.wso2.choreo.connect.enforcer.commons.logging.LoggingConstants; import java.net.InetAddress; import java.rmi.registry.LocateRegistry; @@ -30,7 +32,7 @@ * JMX Connector Agent */ public class JMXAgent { - private static final Logger logger = LoggerFactory.getLogger(JMXAgent.class); + private static final Logger logger = LogManager.getLogger(JMXAgent.class); private static JMXConnectorServer jmxConnectorServer; private static final String DEFAULT_RMI_SERVER_PORT = "1111"; private static final String DEFAULT_RMI_REGISTRY_PORT = "9999"; @@ -55,7 +57,8 @@ public static void initJMXAgent() { jmxConnectorServer.start(); logger.info("JMXAgent JMX Service URL : " + jmxServiceURL.toString()); } catch (Throwable throwable) { - logger.error("Failed to start JMX Agent", throwable); + logger.error("Failed to start JMX Agent", ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6805), + throwable); } } } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java index a64836832d..6f7f4bf6b5 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -15,8 +15,10 @@ */ package org.wso2.choreo.connect.enforcer.jmx; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.wso2.choreo.connect.enforcer.commons.logging.ErrorDetails; +import org.wso2.choreo.connect.enforcer.commons.logging.LoggingConstants; import java.util.ArrayList; import java.util.List; @@ -33,7 +35,7 @@ * The class which is responsible for registering MBeans. */ public class MBeanRegistrator { - private static final Logger logger = LoggerFactory.getLogger(MBeanRegistrator.class); + private static final Logger logger = LogManager.getLogger(MBeanRegistrator.class); private static List mBeans = new ArrayList<>(); private static final String SERVER_PACKAGE = "org.wso2.choreo.connect.enforcer"; @@ -64,21 +66,21 @@ public static void registerMBean(Object mBeanInstance) throws RuntimeException { mBeans.add(name); } catch (InstanceAlreadyExistsException e) { String msg = "MBean " + objectName + " already exists"; - logger.error(msg, e); + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6801), e); throw new RuntimeException(msg, e); } catch (MBeanRegistrationException | NotCompliantMBeanException e) { String msg = "Execption when registering MBean"; - logger.error(msg, e); + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6802), e); throw new RuntimeException(msg, e); } } else { String msg = "MBean " + objectName + " already exists"; - logger.error(msg); + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6803)); throw new RuntimeException(msg); } } catch (MalformedObjectNameException e) { String msg = "Could not register " + mBeanInstance.getClass() + " MBean"; - logger.error(msg); + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6804), e); throw new RuntimeException(msg, e); } } From c9349fa579cdc9a19ace31dfa5fee331156cdb5a Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Tue, 13 Sep 2022 09:46:31 +0530 Subject: [PATCH 12/17] add jmx enabled check for registerMBean --- .../enforcer/jmx/MBeanRegistrator.java | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java index 6f7f4bf6b5..a829ac63c0 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -50,38 +50,43 @@ private MBeanRegistrator() { */ public static void registerMBean(Object mBeanInstance) throws RuntimeException { - String className = mBeanInstance.getClass().getName(); - if (className.indexOf('.') != -1) { - className = className.substring(className.lastIndexOf('.') + 1); - } + if (JMXUtils.isJMXMetricsEnabled()) { + String className = mBeanInstance.getClass().getName(); + if (className.indexOf('.') != -1) { + className = className.substring(className.lastIndexOf('.') + 1); + } - String objectName = SERVER_PACKAGE + ":type=" + className; - try { - MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); - Set set = mBeanServer.queryNames(new ObjectName(objectName), null); - if (set.isEmpty()) { - try { - ObjectName name = new ObjectName(objectName); - mBeanServer.registerMBean(mBeanInstance, name); - mBeans.add(name); - } catch (InstanceAlreadyExistsException e) { + String objectName = SERVER_PACKAGE + ":type=" + className; + try { + MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); + Set set = mBeanServer.queryNames(new ObjectName(objectName), null); + if (set.isEmpty()) { + try { + ObjectName name = new ObjectName(objectName); + mBeanServer.registerMBean(mBeanInstance, name); + mBeans.add(name); + } catch (InstanceAlreadyExistsException e) { + String msg = "MBean " + objectName + " already exists"; + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6801), e); + throw new RuntimeException(msg, e); + } catch (MBeanRegistrationException | NotCompliantMBeanException e) { + String msg = "Execption when registering MBean"; + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6802), e); + throw new RuntimeException(msg, e); + } + } else { String msg = "MBean " + objectName + " already exists"; - logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6801), e); - throw new RuntimeException(msg, e); - } catch (MBeanRegistrationException | NotCompliantMBeanException e) { - String msg = "Execption when registering MBean"; - logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6802), e); - throw new RuntimeException(msg, e); + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6803)); + throw new RuntimeException(msg); } - } else { - String msg = "MBean " + objectName + " already exists"; - logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6803)); - throw new RuntimeException(msg); + } catch (MalformedObjectNameException e) { + String msg = "Could not register " + mBeanInstance.getClass() + " MBean"; + logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6804), e); + throw new RuntimeException(msg, e); } - } catch (MalformedObjectNameException e) { - String msg = "Could not register " + mBeanInstance.getClass() + " MBean"; - logger.error(msg, ErrorDetails.errorLog(LoggingConstants.Severity.MINOR, 6804), e); - throw new RuntimeException(msg, e); + } else { + logger.debug("JMX Metrics should be enabled to register MBean instance: {}", + mBeanInstance.getClass().getName()); } } } From 5b14038196d29a9c3135feb88c539a2efc2f275a Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Tue, 13 Sep 2022 14:32:56 +0530 Subject: [PATCH 13/17] add last five mins request counter and reset timer --- .../choreo/connect/enforcer/jmx/JMXAgent.java | 2 +- .../metrics/jmx/api/ExtAuthMetricsMXBean.java | 5 ++++ .../metrics/jmx/impl/ExtAuthMetrics.java | 25 ++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java index a621a8f754..aef2cbcf5b 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java @@ -34,7 +34,7 @@ public class JMXAgent { private static final Logger logger = LogManager.getLogger(JMXAgent.class); private static JMXConnectorServer jmxConnectorServer; - private static final String DEFAULT_RMI_SERVER_PORT = "1111"; + private static final String DEFAULT_RMI_SERVER_PORT = "11111"; private static final String DEFAULT_RMI_REGISTRY_PORT = "9999"; private static final String JAVA_JMX_RMI_SERVICE_PORT = "com.sun.management.jmxremote.port"; private static final String JAVA_JMX_RMI_REGISTRY_PORT = "com.sun.management.jmxremote.rmi.port"; diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java index 9da04f60b2..cfa5c15778 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/api/ExtAuthMetricsMXBean.java @@ -54,4 +54,9 @@ public interface ExtAuthMetricsMXBean { */ public void resetExtAuthMetrics(); + /** + * Resets all the metrics to thier initial values. + */ + public long getRequestCountInLastFiveMinutes(); + } diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index aa7a962aa6..b87cb16d5a 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -19,13 +19,18 @@ import org.wso2.choreo.connect.enforcer.jmx.MBeanRegistrator; import org.wso2.choreo.connect.enforcer.metrics.jmx.api.ExtAuthMetricsMXBean; +import java.util.Timer; +import java.util.TimerTask; + /** * Singleton MBean for ExtAuth Service metrics. */ -public class ExtAuthMetrics implements ExtAuthMetricsMXBean { +public class ExtAuthMetrics extends TimerTask implements ExtAuthMetricsMXBean { + private static final long REQUEST_COUNT_INTERVAL_MILLIS = 5 * 60 * 1000; private static ExtAuthMetrics extAuthMetricsMBean = null; + private long requestCountInLastFiveMinutes = 0; private long totalRequestCount = 0; private long averageResponseTimeMillis = 0; private long maxResponseTimeMillis = Long.MIN_VALUE; @@ -42,38 +47,56 @@ private ExtAuthMetrics() { */ public static ExtAuthMetrics getInstance() { if (extAuthMetricsMBean == null) { + Timer timer = new Timer(); extAuthMetricsMBean = new ExtAuthMetrics(); + timer.schedule(extAuthMetricsMBean, 0, REQUEST_COUNT_INTERVAL_MILLIS); } return extAuthMetricsMBean; } + @Override public long getTotalRequestCount() { return totalRequestCount; }; + @Override public long getAverageResponseTimeMillis() { return averageResponseTimeMillis; }; + @Override public long getMaxResponseTimeMillis() { return maxResponseTimeMillis; }; + @Override public long getMinResponseTimeMillis() { return minResponseTimeMillis; }; public synchronized void recordMetric(long responseTimeMillis) { + this.requestCountInLastFiveMinutes += 1; this.totalRequestCount += 1; this.averageResponseTimeMillis = (this.averageResponseTimeMillis + responseTimeMillis) / totalRequestCount; this.minResponseTimeMillis = Math.min(this.minResponseTimeMillis, responseTimeMillis); this.maxResponseTimeMillis = Math.max(this.maxResponseTimeMillis, responseTimeMillis); } + @Override public synchronized void resetExtAuthMetrics() { this.totalRequestCount = 0; this.averageResponseTimeMillis = 0; this.maxResponseTimeMillis = Long.MIN_VALUE; this.minResponseTimeMillis = Long.MAX_VALUE; } + + @Override + public synchronized void run() { + requestCountInLastFiveMinutes = 0; + } + + @Override + public long getRequestCountInLastFiveMinutes() { + return requestCountInLastFiveMinutes; + } } From b5d084826cd0244302e4d66104f45c14c537c470 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Sun, 25 Sep 2022 15:42:51 +0530 Subject: [PATCH 14/17] synchronized getInstance method --- .../connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index b87cb16d5a..0a7488d63a 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -45,7 +45,7 @@ private ExtAuthMetrics() { * * @return ExtAuthMetrics */ - public static ExtAuthMetrics getInstance() { + public synchronized static ExtAuthMetrics getInstance() { if (extAuthMetricsMBean == null) { Timer timer = new Timer(); extAuthMetricsMBean = new ExtAuthMetrics(); From 4fe299c0f8fe75fc3bf3e844f94dd7a9d8eb0c7d Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Sun, 25 Sep 2022 16:40:09 +0530 Subject: [PATCH 15/17] styling issue fixed --- .../connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index 0a7488d63a..fa73076772 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -45,7 +45,7 @@ private ExtAuthMetrics() { * * @return ExtAuthMetrics */ - public synchronized static ExtAuthMetrics getInstance() { + public static synchronized ExtAuthMetrics getInstance() { if (extAuthMetricsMBean == null) { Timer timer = new Timer(); extAuthMetricsMBean = new ExtAuthMetrics(); From 6e33a8b8fc134384961dfca0489e812341c9b008 Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Mon, 26 Sep 2022 14:14:57 +0530 Subject: [PATCH 16/17] double checked locking for getInstance method --- .../enforcer/metrics/jmx/impl/ExtAuthMetrics.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java index fa73076772..7e975e6d4b 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/metrics/jmx/impl/ExtAuthMetrics.java @@ -45,11 +45,15 @@ private ExtAuthMetrics() { * * @return ExtAuthMetrics */ - public static synchronized ExtAuthMetrics getInstance() { + public static ExtAuthMetrics getInstance() { if (extAuthMetricsMBean == null) { - Timer timer = new Timer(); - extAuthMetricsMBean = new ExtAuthMetrics(); - timer.schedule(extAuthMetricsMBean, 0, REQUEST_COUNT_INTERVAL_MILLIS); + synchronized (ExtAuthMetrics.class) { + if (extAuthMetricsMBean == null) { + Timer timer = new Timer(); + extAuthMetricsMBean = new ExtAuthMetrics(); + timer.schedule(extAuthMetricsMBean, 0, REQUEST_COUNT_INTERVAL_MILLIS); + } + } } return extAuthMetricsMBean; } From 0ffeff4defd8e773cb63c00289857dc59abff45b Mon Sep 17 00:00:00 2001 From: Amila Senadheera Date: Fri, 21 Oct 2022 09:38:58 +0530 Subject: [PATCH 17/17] use string.format and comment corrected --- .../java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java | 5 +++-- .../choreo/connect/enforcer/jmx/MBeanManagementFactory.java | 3 ++- .../wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java index aef2cbcf5b..6a2658437a 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/JMXAgent.java @@ -48,8 +48,9 @@ public static void initJMXAgent() { LocateRegistry.createRegistry(Integer.parseInt(rmiRegistryPort)); - String jmxURL = "service:jmx:rmi://" + hostname + ":" + rmiServerPort - + "/jndi/rmi://" + hostname + ":" + rmiRegistryPort + "/jmxrmi"; + String jmxURL = String.format("service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi", hostname, + rmiServerPort, + hostname, rmiRegistryPort); JMXServiceURL jmxServiceURL = new JMXServiceURL(jmxURL); jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java index 43486650a0..c46b003351 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanManagementFactory.java @@ -25,9 +25,10 @@ public class MBeanManagementFactory { /* - * @return A MBeanServer instance. * If one already exists, it will return that else it will create a new one and * return. + * + * @return A MBeanServer instance. */ public static MBeanServer getMBeanServer() { MBeanServer mBeanServer; diff --git a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java index a829ac63c0..ad208ba274 100644 --- a/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java +++ b/enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/jmx/MBeanRegistrator.java @@ -56,7 +56,7 @@ public static void registerMBean(Object mBeanInstance) throws RuntimeException { className = className.substring(className.lastIndexOf('.') + 1); } - String objectName = SERVER_PACKAGE + ":type=" + className; + String objectName = String.format("%s:type=%s", SERVER_PACKAGE, className); try { MBeanServer mBeanServer = MBeanManagementFactory.getMBeanServer(); Set set = mBeanServer.queryNames(new ObjectName(objectName), null);