From 3893f9a42a3b2befbf3128a73dcdadc5736722be Mon Sep 17 00:00:00 2001 From: Thuvarakan Sritharan Date: Wed, 13 Dec 2023 12:24:08 +0530 Subject: [PATCH] Add new attributes to ConnectionsView bean --- .../passthru/jmx/ConnectionsView.java | 93 ++++++++++++++++++- .../passthru/jmx/ConnectionsViewMBean.java | 6 ++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsView.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsView.java index adfe95e96a..35eb816ac0 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsView.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsView.java @@ -53,10 +53,13 @@ public class ConnectionsView implements ConnectionsViewMBean { private static final int LONG_DATA_COLLECTION_PERIOD = 60 * 5; private static final int SAMPLES_PER_HOUR = (60 * 60)/LONG_DATA_COLLECTION_PERIOD; - + private Queue lastSecondRequestQueue = new LinkedList(); + private Queue openedConnectionsDataQueue = new LinkedList(); private Queue shortTermDataQueue = new LinkedList(); private Queue longTermDataQueue = new LinkedList(); + private AtomicInteger requestsReceived = new AtomicInteger(0); + private AtomicInteger openedConnections = new AtomicInteger(0); private AtomicInteger activeConnections = new AtomicInteger(0); private AtomicInteger shortTermOpenedConnections = new AtomicInteger(0); private AtomicInteger longTermOpenedConnections = new AtomicInteger(0); @@ -79,6 +82,34 @@ public ConnectionsView(String name) throws AxisFault { initCounters(requestSizeCounters); initCounters(responseSizeCounters); + Runnable requestTask = new Runnable() { + public void run() { + // We only need historical data for the last 1 minutes + // Therefore no need to keep data older than that... + if (lastSecondRequestQueue.size() == 60) { + lastSecondRequestQueue.remove(); + } + lastSecondRequestQueue.offer(requestsReceived.getAndSet(0)); + } + }; + // Delay the timer by 1 minute to prevent the task from starting immediately + scheduler.scheduleAtFixedRate(requestTask, 1, + 1, TimeUnit.SECONDS); + + Runnable secondsTask = new Runnable() { + public void run() { + // We only need historical data for the last 1 minutes + // Therefore no need to keep data older than that... + if (openedConnectionsDataQueue.size() == 60) { + openedConnectionsDataQueue.remove(); + } + openedConnectionsDataQueue.offer(openedConnections.getAndSet(0)); + } + }; + // Delay the timer by 1 minute to prevent the task from starting immediately + scheduler.scheduleAtFixedRate(secondsTask, 1, + 1, TimeUnit.SECONDS); + Runnable task = new Runnable() { public void run() { // We only need historical data for the last 15 minutes @@ -127,6 +158,7 @@ private void initCounters(AtomicInteger[] counters) { protected void connected() { activeConnections.incrementAndGet(); + openedConnections.incrementAndGet(); shortTermOpenedConnections.incrementAndGet(); longTermOpenedConnections.incrementAndGet(); } @@ -136,6 +168,8 @@ protected void disconnected() { } protected void requestReceived() { + + requestsReceived.incrementAndGet(); unservedRequests.incrementAndGet(); } @@ -183,6 +217,41 @@ public int getActiveConnections() { return activeConnections.get(); } + public int getLastSecondRequests() { + + return getTotalRequests(1); + } + + public int getLast15SecondRequests() { + + return getTotalRequests(15); + } + + public int getLastMinuteRequests() { + + return getTotalRequests(60); + } + + public int getLastSecondConnections() { + + return getSecondConnections(1); + } + + public int getLast5SecondConnections() { + + return getSecondConnections(5); + } + + public int getLast15SecondConnections() { + + return getSecondConnections(15); + } + + public int getLast30SecondConnections() { + + return getSecondConnections(30); + } + public int getLastMinuteConnections() { return getTotalConnections(1); } @@ -248,9 +317,29 @@ public void reset() { * @return The number of connections opened */ private int getTotalConnections(int n) { - int sum = 0; + Integer[] array = shortTermDataQueue.toArray(new Integer[shortTermDataQueue.size()]); + int sum = getTotal(array, n); + return sum; + } + + private int getTotalRequests(int n) { + + Integer[] array = lastSecondRequestQueue.toArray(new Integer[lastSecondRequestQueue.size()]); + int sum = getTotal(array, n); + return sum; + } + private int getSecondConnections(int n) { + + Integer[] array = openedConnectionsDataQueue.toArray(new Integer[openedConnectionsDataQueue.size()]); + int sum = getTotal(array, n); + return sum; + } + + private int getTotal(Integer[] array, int n) { + + int sum = 0; if (n > array.length) { for (int i = 0; i < array.length; i++) { sum += array[i]; diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsViewMBean.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsViewMBean.java index d1df57e1e6..48810a60f1 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsViewMBean.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/jmx/ConnectionsViewMBean.java @@ -25,6 +25,12 @@ public interface ConnectionsViewMBean { public int getActiveConnections(); + public int getLastSecondRequests(); + public int getLast15SecondRequests(); + public int getLastMinuteRequests(); + public int getLastSecondConnections(); + public int getLast5SecondConnections(); + public int getLast15SecondConnections(); public int getLastMinuteConnections(); public int getLast5MinuteConnections(); public int getLast15MinuteConnections();