commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sandy...@apache.org
Subject svn commit: r388885 - in /jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite: CompositeKeyedObjectPool.java CompositeObjectPool.java GrowManager.java
Date Sun, 26 Mar 2006 08:16:38 GMT
Author: sandymac
Date: Sun Mar 26 00:16:35 2006
New Revision: 388885

URL: http://svn.apache.org/viewcvs?rev=388885&view=rev
Log:
Changes neesed for "CompositeKeyedObjectPool"s to work:
Detect when making new objects is relatively slow or expensive.
When it is, try to anticipate demand and create idle objects
while allowing concurrent pool access. The idea came from a
discussion with Peter Steijn on ways to reduce pool latency.

Modified:
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeKeyedObjectPool.java
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeObjectPool.java
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/GrowManager.java

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeKeyedObjectPool.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeKeyedObjectPool.java?rev=388885&r1=388884&r2=388885&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeKeyedObjectPool.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeKeyedObjectPool.java
Sun Mar 26 00:16:35 2006
@@ -100,6 +100,10 @@
             if (pool == null) {
                 pool = poolFactory.createPool();
                 objectPools.put(key, pool);
+                // Tell CompositeObjectPools that we own them.
+                if (pool instanceof CompositeObjectPool) {
+                    ((CompositeObjectPool)pool).setOwningCompositeKeyedObjectPool(this);
+                }
             }
         }
         return pool;

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeObjectPool.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeObjectPool.java?rev=388885&r1=388884&r2=388885&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeObjectPool.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/CompositeObjectPool.java
Sun Mar 26 00:16:35 2006
@@ -101,6 +101,11 @@
      */
     private final CompositeObjectPoolFactory.FactoryConfig factoryConfig;
 
+    /**
+     * When not-<code>null</code> this pool is part of a {@link CompositeKeyedObjectPool}.
+     */
+    private CompositeKeyedObjectPool owningCompositeKeyedObjectPool = null;
+
     CompositeObjectPool(final PoolableObjectFactory factory, final Manager manager, final
Lender lender, final Tracker tracker, final boolean validateOnReturn) {
         this(factory, manager, lender, tracker, validateOnReturn, null);
     }
@@ -169,6 +174,25 @@
      */
     List getPool() {
         return pool;
+    }
+
+    /**
+     * Set the owner of this pool.
+     * @param ckop the owner of this pool
+     */
+    void setOwningCompositeKeyedObjectPool(final CompositeKeyedObjectPool ckop) {
+        if (owningCompositeKeyedObjectPool != null) {
+            throw new IllegalStateException("CompositeObjectPools cannot change ownership.");
+        }
+        owningCompositeKeyedObjectPool = ckop;
+    }
+
+    /**
+     * Get the owner of this pool or <code>null</code>.
+     * @return the owner of this pool or <code>null</code>
+     */
+    CompositeKeyedObjectPool getOwningCompositeKeyedObjectPool() {
+        return owningCompositeKeyedObjectPool;
     }
 
     /**

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/GrowManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/GrowManager.java?rev=388885&r1=388884&r2=388885&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/GrowManager.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/GrowManager.java
Sun Mar 26 00:16:35 2006
@@ -155,7 +155,14 @@
      */
     private void schedulePrefill() {
         if (objectPool.getLender().size() == 0 && isMakeObjectExpensive()) {
-            PREFILL_TIMER.schedule(new PrefillTask(), 0L);
+            // When we are part of a CompositeKeyedObjectPool we need to pass the key to
the Timer's thread.
+            final CompositeKeyedObjectPool ckop = objectPool.getOwningCompositeKeyedObjectPool();
+            if (ckop != null) {
+                final Object key = ckop.getKeys().get();
+                PREFILL_TIMER.schedule(new KeyedPrefillTask(key), 0L);
+            } else {
+                PREFILL_TIMER.schedule(new PrefillTask(), 0L);
+            }
         }
     }
 
@@ -174,6 +181,35 @@
                 }
             } catch (Exception e) {
                 // swallowed
+            }
+        }
+    }
+
+    /**
+     * A <code>TimerTask</code> that will add another object if the pool is empty.
+     */
+    private class KeyedPrefillTask extends TimerTask {
+        private final Object key;
+
+        KeyedPrefillTask(final Object key) {
+            this.key = key;
+        }
+
+        public void run() {
+            ThreadLocal keys = null;
+            try {
+                if (objectPool.getNumIdle() == 0) {
+                    keys = objectPool.getOwningCompositeKeyedObjectPool().getKeys();
+                    keys.set(key);
+                    objectPool.addObject();
+                }
+            } catch (Exception e) {
+                // swallowed
+            } finally {
+                // clear the key
+                if (keys != null) {
+                    keys.set(null);
+                }
             }
         }
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message