You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2012/02/09 22:46:48 UTC

svn commit: r1242546 - in /commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl: DefaultEvictionPolicy.java EvictionConfig.java EvictionPolicy.java GenericKeyedObjectPool.java GenericObjectPool.java

Author: markt
Date: Thu Feb  9 21:46:48 2012
New Revision: 1242546

URL: http://svn.apache.org/viewvc?rev=1242546&view=rev
Log:
Start to implement POOL-100.
Extract the current eviction policy into a separate class and provide an interface for other policies to be defined.
TODO: Allow other policies to be configured.

Added:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java   (with props)
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java   (with props)
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java   (with props)
Modified:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java

Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java?rev=1242546&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java (added)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java Thu Feb  9 21:46:48 2012
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.pool2.impl;
+
+public class DefaultEvictionPolicy<T> implements EvictionPolicy<T> {
+
+    public boolean evict(EvictionConfig config, PooledObject<T> underTest,
+            int idleCount) {
+        
+        if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
+                config.getMinIdle() < idleCount) ||
+                config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
+            return true;
+        }
+        return false;
+    }
+}

Propchange: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/DefaultEvictionPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java?rev=1242546&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java (added)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java Thu Feb  9 21:46:48 2012
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.pool2.impl;
+
+/**
+ * This class is used by pool implementations to pass configuration information
+ * to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also have
+ * its own specific configuration attributes.  
+ */
+public class EvictionConfig {
+
+    private final long idleEvictTime;
+    private final long idleSoftEvictTime;
+    private final int minIdle;
+    
+
+    public EvictionConfig(long poolIdleEvictTime, long poolIdleSoftEvictTime,
+            int minIdle) {
+        if (poolIdleEvictTime > 0) {
+            idleEvictTime = poolIdleEvictTime;
+        } else {
+            idleEvictTime = Long.MAX_VALUE;
+        }
+        if (poolIdleSoftEvictTime > 0) {
+            idleSoftEvictTime = poolIdleSoftEvictTime;
+        } else {
+            idleSoftEvictTime  = Long.MAX_VALUE;
+        }
+        this.minIdle = minIdle;
+    }
+
+    public long getIdleEvictTime() {
+        return idleEvictTime;
+    }
+
+    public long getIdleSoftEvictTime() {
+        return idleSoftEvictTime;
+    }
+    
+    public int getMinIdle() {
+        return minIdle;
+    }
+}

Propchange: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java?rev=1242546&view=auto
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java (added)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java Thu Feb  9 21:46:48 2012
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.commons.pool2.impl;
+
+public interface EvictionPolicy<T> {
+    boolean evict(EvictionConfig config, PooledObject<T> underTest,
+            int idleCount);
+}

Propchange: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/EvictionPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1242546&r1=1242545&r2=1242546&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java (original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java Thu Feb  9 21:46:48 2012
@@ -238,6 +238,9 @@ public class GenericKeyedObjectPool<K,T>
         this.factory = factory;
         setConfig(config);
 
+        // TODO Get from config
+        this.evictionPolicy = new DefaultEvictionPolicy<T>();
+        
         startEvictor(getMinEvictableIdleTimeMillis());
 
         initStats();
@@ -1320,19 +1323,16 @@ public class GenericKeyedObjectPool<K,T>
             return;
         }
 
+        PooledObject<T> underTest = null;
+
         synchronized (evictionLock) {
+            EvictionConfig evictionConfig = new EvictionConfig(
+                    getMinEvictableIdleTimeMillis(),
+                    getSoftMinEvictableIdleTimeMillis(),
+                    getMinIdlePerKey());
+            
             boolean testWhileIdle = getTestWhileIdle();
-            long idleEvictTime = Long.MAX_VALUE;
-            long idleSoftEvictTime = Long.MAX_VALUE;
             
-            if (getMinEvictableIdleTimeMillis() > 0) {
-                idleEvictTime = getMinEvictableIdleTimeMillis();
-            }
-            if (getSoftMinEvictableIdleTimeMillis() > 0) {
-                idleSoftEvictTime = getSoftMinEvictableIdleTimeMillis();
-            }
-    
-            PooledObject<T> underTest = null;
             LinkedBlockingDeque<PooledObject<T>> idleObjects = null;
              
             for (int i = 0, m = getNumTests(); i < m; i++) {
@@ -1389,10 +1389,8 @@ public class GenericKeyedObjectPool<K,T>
                     continue;
                 }
     
-                if (idleEvictTime < underTest.getIdleTimeMillis() ||
-                        (idleSoftEvictTime < underTest.getIdleTimeMillis() &&
-                                getMaxIdlePerKey() <
-                                poolMap.get(evictionKey).getIdleObjects().size())) {
+                if (evictionPolicy.evict(evictionConfig, underTest,
+                        poolMap.get(evictionKey).getIdleObjects().size())) {
                     destroy(evictionKey, underTest, true);
                     destroyedByEvictorCount.incrementAndGet();
                 } else {
@@ -2254,6 +2252,11 @@ public class GenericKeyedObjectPool<K,T>
      */
     private K evictionKey = null; // @GuardedBy("evictionLock")
 
+    /**
+     * Policy that determines if an object is eligible for eviction or not.
+     */
+    private EvictionPolicy<T> evictionPolicy;
+    
     /** Object used to ensure thread safety of eviction process */ 
     private final Object evictionLock = new Object();
 

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java?rev=1242546&r1=1242545&r2=1242546&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java (original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericObjectPool.java Thu Feb  9 21:46:48 2012
@@ -191,6 +191,9 @@ public class GenericObjectPool<T> extend
         this.factory = factory;
         setConfig(config);
 
+        // TODO Get from config
+        this.evictionPolicy = new DefaultEvictionPolicy<T>();
+        
         startEvictor(getTimeBetweenEvictionRunsMillis());
 
         initStats();
@@ -1048,16 +1051,12 @@ public class GenericObjectPool<T> extend
         PooledObject<T> underTest = null;
 
         synchronized (evictionLock) {
-            boolean testWhileIdle = getTestWhileIdle();
-            long idleEvictTime = Long.MAX_VALUE;
-            long idleSoftEvictTime = Long.MAX_VALUE;
+            EvictionConfig evictionConfig = new EvictionConfig(
+                    getMinEvictableIdleTimeMillis(),
+                    getSoftMinEvictableIdleTimeMillis(),
+                    getMinIdle());
             
-            if (getMinEvictableIdleTimeMillis() > 0) {
-                idleEvictTime = getMinEvictableIdleTimeMillis();
-            }
-            if (getSoftMinEvictableIdleTimeMillis() > 0) {
-                idleSoftEvictTime = getSoftMinEvictableIdleTimeMillis();
-            }
+            boolean testWhileIdle = getTestWhileIdle();
                     
             for (int i = 0, m = getNumTests(); i < m; i++) {
                 if (evictionIterator == null || !evictionIterator.hasNext()) {
@@ -1089,9 +1088,8 @@ public class GenericObjectPool<T> extend
                     continue;
                 }
     
-                if (idleEvictTime < underTest.getIdleTimeMillis() ||
-                        (idleSoftEvictTime < underTest.getIdleTimeMillis() &&
-                                getMinIdle() < idleObjects.size())) {
+                if (evictionPolicy.evict(evictionConfig, underTest,
+                        idleObjects.size())) {
                     destroy(underTest);
                     destroyedByEvictorCount.incrementAndGet();
                 } else {
@@ -1573,6 +1571,11 @@ public class GenericObjectPool<T> extend
     /** An iterator for {@link #idleObjects} that is used by the evictor. */
     private Iterator<PooledObject<T>> evictionIterator = null; // @GuardedBy("evictionLock")
 
+    /**
+     * Policy that determines if an object is eligible for eviction or not.
+     */
+    private EvictionPolicy<T> evictionPolicy;
+
     /** Object used to ensure thread safety of eviction process */ 
     private final Object evictionLock = new Object();