You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/01/01 15:54:06 UTC

[commons-pool] branch master updated: [POOL-393] Improve BaseGenericObjectPool's JMX Register performance (#199)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git


The following commit(s) were added to refs/heads/master by this push:
     new 60ab766c [POOL-393] Improve BaseGenericObjectPool's JMX Register performance (#199)
60ab766c is described below

commit 60ab766ca7c4608a740a8efb294a730802733ab9
Author: Niall Pemberton <ni...@gmail.com>
AuthorDate: Sun Jan 1 15:54:00 2023 +0000

    [POOL-393] Improve BaseGenericObjectPool's JMX Register performance (#199)
    
    * [POOL-393] Improve BaseGenericObjectPool's JMX Register performance when creating many pools
    
    * Fix typo
    
    Co-authored-by: Niall Pemberton <ni...@apache.org>
    Co-authored-by: Gary Gregory <ga...@users.noreply.github.com>
---
 src/changes/changes.xml                            |  3 +++
 .../commons/pool2/impl/BaseGenericObjectPool.java  | 11 ++++++---
 .../pool2/impl/TestBaseGenericObjectPool.java      | 28 ++++++++++++++++++++++
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f7b4a15b..48bcc0fc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -69,6 +69,9 @@ The <action> type attribute can be add,update,fix,remove.
     <action dev="ggregory" type="fix" due-to="Gary Gregory">
       Fail-fast on null input for DefaultPooledObjectInfo.DefaultPooledObjectInfo(PooledObject) with a NullPointerException.
     </action>       
+    <action dev="niallp" type="fix" due-to="Shichao Yuan, Phil Steitz, Niall Pemberton" issue="POOL-393">
+       Improve BaseGenericObjectPool's JMX Register performance when creating many pools.
+    </action>
     <!-- ADD -->
     <action dev="ggregory" type="add" due-to="Gary Gregory">
       Add PooledObject.getFullDuration().
diff --git a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
index 6996fb6e..62855aeb 100644
--- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
@@ -1219,9 +1219,14 @@ public abstract class BaseGenericObjectPool<T, E extends Exception> extends Base
                 } else {
                     objName = new ObjectName(base + jmxNamePrefix + i);
                 }
-                mbs.registerMBean(this, objName);
-                newObjectName = objName;
-                registered = true;
+                if (!mbs.isRegistered(objName)) {
+                    mbs.registerMBean(this, objName);
+                    newObjectName = objName;
+                    registered = true;
+                } else {
+                    // Increment the index and try again
+                    i++;
+                }
             } catch (final MalformedObjectNameException e) {
                 if (BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals(
                         jmxNamePrefix) && jmxNameBase.equals(base)) {
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java
index 115dd1b6..3a50840f 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestBaseGenericObjectPool.java
@@ -19,14 +19,23 @@ package org.apache.commons.pool2.impl;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import java.lang.management.ManagementFactory;
 import java.time.Duration;
+import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
 import org.apache.commons.pool2.TestException;
+import org.apache.commons.pool2.Waiter;
+import org.apache.commons.pool2.WaiterFactory;
 import org.apache.commons.pool2.impl.TestGenericObjectPool.SimpleFactory;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
 
 /**
  */
@@ -107,4 +116,23 @@ public class TestBaseGenericObjectPool {
             assertEquals(0, evictingPool.getNumIdle());
         }
     }
+    /**
+     * POOL-393
+     * Ensure JMX registration does not add too much latency to pool creation.
+     */
+    @Test
+    @Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
+    public void testJMXRegistrationLatency() throws Exception {
+        final int numPools = 1000;
+        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        final ArrayList<GenericObjectPool<Waiter, IllegalStateException>> pools = new ArrayList<>();
+        long startTime = System.currentTimeMillis();
+        for (int i = 0; i < numPools; i++) {
+            pools.add(new GenericObjectPool<Waiter, IllegalStateException>(
+                new WaiterFactory<String>(0,0,0,0,0,0), new GenericObjectPoolConfig<Waiter>()));
+        }
+        System.out.println("Duration: " + (System.currentTimeMillis() - startTime));
+        final ObjectName oname = pools.get(numPools - 1).getJmxName();
+        assertEquals(1, mbs.queryNames(oname, null).size());
+    }
 }