Author: jleroux
Date: Tue Nov 17 08:36:35 2009
New Revision: 881194
URL: http://svn.apache.org/viewvc?rev=881194&view=rev
Log:
A patch from Philippe Mouawad "Ofbiz freeze"
(https://issues.apache.org/jira/browse/OFBIZ-2124) - OFBIZ-2124
In a certain scenario the application freezes, no more AJP connector threads
are available.
After analysis the lock seems to come from very slow response time in
GenericDelegator.clearAllCaches and particularily in CacheLineTable.keySet.
Furthermore, looking at org.ofbiz.base.util.cache.UtilCache, the class seems to
have some synchronisation problems:
1) utilCacheTable.put calls are not synchronized
2) findCache synchronizes access to utilCacheTable while only getting an
element which has big performance impact since utilCacheTable is static
3) clearExpiredFromAllCaches and clearAllCaches does not synchronize while
clearCachesThatStartWith does, why ?
The patch synchronizes correctly the access to utilCacheTable and optimises
findCache
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
URL:
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java?rev=881194&r1=881193&r2=881194&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
(original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java Tue
Nov 17 08:36:35 2009
@@ -19,6 +19,7 @@
package org.ofbiz.base.util.cache;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -116,7 +117,9 @@
setPropertiesParams(cacheName);
createCache();
- utilCacheTable.put(name, this);
+ synchronized (utilCacheTable) {
+ utilCacheTable.put(name, this);
+ }
}
public UtilCache(String cacheName, int maxSize, long expireTime, boolean
useSoftReference) {
@@ -145,8 +148,9 @@
setPropertiesParams(name);
createCache();
-
- utilCacheTable.put(name, this);
+ synchronized (utilCacheTable) {
+ utilCacheTable.put(name, this);
+ }
}
/** This constructor takes a name for the cache, puts itself in the
utilCacheTable.
@@ -160,8 +164,9 @@
setPropertiesParams("default");
setPropertiesParams(cacheName);
createCache();
-
- utilCacheTable.put(name, this);
+ synchronized (utilCacheTable) {
+ utilCacheTable.put(name, this);
+ }
}
/** This constructor takes a name for the cache, puts itself in the
utilCacheTable.
@@ -174,8 +179,9 @@
setPropertiesParams("default");
setPropertiesParams(cacheName);
createCache();
-
- utilCacheTable.put(name, this);
+ synchronized (utilCacheTable) {
+ utilCacheTable.put(name, this);
+ }
}
/** Default constructor, all members stay at default values as defined in
cache.properties, or the defaults in this file if cache.properties is not
found, or there are no 'default' entries in it. */
@@ -184,7 +190,9 @@
name = "default" + this.getNextDefaultIndex("default");
createCache();
- utilCacheTable.put(name, this);
+ synchronized (utilCacheTable) {
+ utilCacheTable.put(name, this);
+ }
}
protected String getNextDefaultIndex(String cacheName) {
@@ -405,10 +413,24 @@
/** Removes all elements from this cache */
public static void clearAllCaches() {
- for (Map.Entry<String, UtilCache<?, ?>> entry:
utilCacheTable.entrySet()) {
- UtilCache<?, ?> utilCache = entry.getValue();
- utilCache.clear();
+ // We make a copy since clear may take time
+ List<UtilCache<?,?>> list = getUtilCacheTableValuesImage();
+ for (UtilCache<?,?> cache : list) {
+ cache.clear();
+ }
+ list.clear();
+ }
+
+ /**
+ * Return an image of the values at a time
+ * @return {...@link List}
+ */
+ private static List getUtilCacheTableValuesImage() {
+ List list = new ArrayList(utilCacheTable.size());
+ synchronized (utilCacheTable) {
+ list.addAll(utilCacheTable.values());
}
+ return list;
}
/** Getter for the name of the UtilCache instance.
@@ -639,10 +661,12 @@
/** Clears all expired cache entries from all caches */
public static void clearExpiredFromAllCaches() {
- for (Map.Entry<String, UtilCache<?, ?>> entry:
utilCacheTable.entrySet()) {
- UtilCache<?, ?> utilCache = entry.getValue();
+ // We make a copy since clear may take time
+ List<UtilCache<?,?>> list = getUtilCacheTableValuesImage();
+ for (UtilCache<?,?> utilCache : list) {
utilCache.clearExpired();
}
+ list.clear();
}
/** Checks for a non-expired key in a specific cache */
@@ -657,13 +681,20 @@
public static void clearCachesThatStartWith(String startsWith) {
synchronized (utilCacheTable) {
- for (Map.Entry<String, UtilCache<?, ?>> entry:
utilCacheTable.entrySet()) {
- String name = entry.getKey();
- if (name.startsWith(startsWith)) {
- UtilCache<?, ?> cache = entry.getValue();
- cache.clear();
+ List<UtilCache<?, ?>> cachesToClear = FastList.newInstance();
+ synchronized (utilCacheTable) {
+ for (Map.Entry<String, UtilCache<?, ?>> entry:
utilCacheTable.entrySet()) {
+ String name = entry.getKey();
+ if (name.startsWith(startsWith)) {
+ UtilCache<?, ?> cache = entry.getValue();
+ cachesToClear.add(cache);
+ }
}
}
+ for (UtilCache<?,?> cache : cachesToClear) {
+ cache.clear();
+ }
+ cachesToClear.clear();
}
}
@@ -675,8 +706,6 @@
@SuppressWarnings("unchecked")
public static <K, V> UtilCache<K, V> findCache(String cacheName) {
- synchronized (UtilCache.utilCacheTable) {
- return (UtilCache<K, V>) UtilCache.utilCacheTable.get(cacheName);
- }
+ return (UtilCache<K, V>) UtilCache.utilCacheTable.get(cacheName);
}
}