You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2020/04/12 21:57:43 UTC
[commons-jcs] branch master updated (90b6a38 -> 43ed9a5)
This is an automated email from the ASF dual-hosted git repository.
tv pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git.
from 90b6a38 Move PartitionedCacheAccess & friends to sandbox
new 670642e Remove remains of singleton core
new 12ed8b9 Fix potential NPE
new f5c6489 Get rid of synchronized maps and sets
new ec4c3e0 Better concurrency
new 43ed9a5 Cleanup, use ElapsedTimer
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../jcs/auxiliary/disk/block/BlockDiskCache.java | 3 +-
.../auxiliary/disk/indexed/IndexedDiskCache.java | 9 +-
.../jcs/auxiliary/disk/jdbc/JDBCDiskCache.java | 3 +-
.../jcs/auxiliary/disk/jdbc/ShrinkerThread.java | 56 ++++-------
.../disk/jdbc/hsql/HSQLDiskCacheFactory.java | 51 +++++-----
.../disk/jdbc/mysql/MySQLTableOptimizer.java | 6 +-
.../socket/tcp/LateralTCPDiscoveryListener.java | 38 +++----
.../lateral/socket/tcp/LateralTCPListener.java | 17 ----
.../remote/AbstractRemoteCacheListener.java | 21 ----
.../auxiliary/remote/server/RemoteCacheServer.java | 19 +---
.../apache/commons/jcs/engine/CacheConstants.java | 34 -------
.../apache/commons/jcs/engine/behavior/ICache.java | 39 +++----
.../commons/jcs/engine/control/CompositeCache.java | 5 +-
.../jcs/engine/control/CompositeCacheManager.java | 34 ++++---
.../jcs/engine/memory/AbstractMemoryCache.java | 4 +-
.../jcs/utils/threadpool/ThreadPoolManager.java | 112 ++++++++-------------
.../jcs/JCSConcurrentCacheAccessUnitTest.java | 9 +-
.../threadpool/ThreadPoolManagerUnitTest.java | 4 +-
18 files changed, 174 insertions(+), 290 deletions(-)
delete mode 100644 commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/CacheConstants.java
[commons-jcs] 01/05: Remove remains of singleton core
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 670642e371c9eebbe7d1cbec001b993cc59e7fbf
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 19:25:26 2020 +0200
Remove remains of singleton core
---
.../lateral/socket/tcp/LateralTCPListener.java | 17 -----------------
.../remote/AbstractRemoteCacheListener.java | 21 ---------------------
2 files changed, 38 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
index 84e68cb..a2c2c36 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
@@ -36,7 +36,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
-import org.apache.commons.jcs.access.exception.CacheException;
import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor;
import org.apache.commons.jcs.auxiliary.lateral.behavior.ILateralCacheListener;
import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
@@ -45,7 +44,6 @@ import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.commons.jcs.engine.behavior.IShutdownObserver;
import org.apache.commons.jcs.engine.control.CompositeCache;
-import org.apache.commons.jcs.engine.control.CompositeCacheManager;
import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
import org.apache.commons.jcs.log.Log;
import org.apache.commons.jcs.log.LogManager;
@@ -365,21 +363,6 @@ public class LateralTCPListener<K, V>
*/
protected CompositeCache<K, V> getCache( String name )
{
- if ( getCacheManager() == null )
- {
- // revert to singleton on failure
- try
- {
- setCacheManager( CompositeCacheManager.getInstance() );
- }
- catch (CacheException e)
- {
- throw new RuntimeException("Could not retrieve cache manager instance", e);
- }
-
- log.debug( "cacheMgr = {0}", () -> getCacheManager() );
- }
-
return getCacheManager().getCache( name );
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
index e2681a7..eafbc54 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/AbstractRemoteCacheListener.java
@@ -22,7 +22,6 @@ package org.apache.commons.jcs.auxiliary.remote;
import java.io.IOException;
import java.net.UnknownHostException;
-import org.apache.commons.jcs.access.exception.CacheException;
import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
@@ -30,7 +29,6 @@ import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.ICacheElementSerialized;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.commons.jcs.engine.behavior.IElementSerializer;
-import org.apache.commons.jcs.engine.control.CompositeCacheManager;
import org.apache.commons.jcs.log.Log;
import org.apache.commons.jcs.log.LogManager;
import org.apache.commons.jcs.utils.net.HostNameUtil;
@@ -224,25 +222,6 @@ public abstract class AbstractRemoteCacheListener<K, V>
*/
protected ICompositeCacheManager getCacheManager()
{
- if ( cacheMgr == null )
- {
- try
- {
- cacheMgr = CompositeCacheManager.getInstance();
-
- log.debug( "had to get cacheMgr" );
- log.debug( "cacheMgr = {0}", cacheMgr );
- }
- catch (CacheException e)
- {
- log.error( "Could not get cacheMgr", e );
- }
- }
- else
- {
- log.debug( "already got cacheMgr = {0}", cacheMgr );
- }
-
return cacheMgr;
}
[commons-jcs] 02/05: Fix potential NPE
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 12ed8b916da076379247b4189521ec1dc8adbc47
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 20:31:28 2020 +0200
Fix potential NPE
---
.../org/apache/commons/jcs/engine/control/CompositeCacheManager.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
index 7f50bcb..1d29b56 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
@@ -591,7 +591,10 @@ public class CompositeCacheManager
synchronized (CompositeCacheManager.class)
{
// shutdown element event queue
- this.elementEventQueue.dispose();
+ if (this.elementEventQueue != null)
+ {
+ this.elementEventQueue.dispose();
+ }
// shutdown all scheduled jobs
this.scheduledExecutor.shutdownNow();
[commons-jcs] 03/05: Get rid of synchronized maps and sets
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit f5c64898fc210e3aa1c515b34fb5361c9027bb25
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 20:32:44 2020 +0200
Get rid of synchronized maps and sets
---
.../jcs/auxiliary/disk/jdbc/ShrinkerThread.java | 56 +++++++++-------------
.../disk/jdbc/hsql/HSQLDiskCacheFactory.java | 51 ++++++++++----------
.../socket/tcp/LateralTCPDiscoveryListener.java | 38 +++++++--------
.../jcs/JCSConcurrentCacheAccessUnitTest.java | 9 ++--
4 files changed, 71 insertions(+), 83 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
index 427cce6..24a9580 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
@@ -1,8 +1,7 @@
package org.apache.commons.jcs.auxiliary.disk.jdbc;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.Iterator;
+import java.util.concurrent.CopyOnWriteArraySet;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -39,8 +38,8 @@ public class ShrinkerThread
private static final Log log = LogManager.getLog( ShrinkerThread.class );
/** A set of JDBCDiskCache objects to call deleteExpired on. */
- private final Set<JDBCDiskCache<?, ?>> shrinkSet =
- Collections.synchronizedSet( new HashSet<>() );
+ private final CopyOnWriteArraySet<JDBCDiskCache<?, ?>> shrinkSet =
+ new CopyOnWriteArraySet<>();
/** Default time period to use. */
private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000;
@@ -98,40 +97,29 @@ public class ShrinkerThread
log.info( "Running JDBC disk cache shrinker. Number of regions [{0}]",
() -> shrinkSet.size() );
- Object[] caches = null;
-
- synchronized ( shrinkSet )
- {
- caches = this.shrinkSet.toArray();
- }
-
- if ( caches != null )
+ for (Iterator<JDBCDiskCache<?, ?>> i = shrinkSet.iterator(); i.hasNext();)
{
- for ( int i = 0; i < caches.length; i++ )
- {
- JDBCDiskCache<?, ?> cache = (JDBCDiskCache<?, ?>) caches[i];
+ JDBCDiskCache<?, ?> cache = i.next();
+ long start = System.currentTimeMillis();
+ int deleted = cache.deleteExpired();
+ long end = System.currentTimeMillis();
- long start = System.currentTimeMillis();
- int deleted = cache.deleteExpired();
- long end = System.currentTimeMillis();
+ log.info( "Deleted [{0}] expired for region [{1}] for table [{2}] in {3} ms.",
+ deleted, cache.getCacheName(), cache.getTableName(), end - start );
- log.info( "Deleted [{0}] expired for region [{1}] for table [{2}] in {3} ms.",
- deleted, cache.getCacheName(), cache.getTableName(), end - start );
+ // don't pause after the last call to delete expired.
+ if ( i.hasNext() )
+ {
+ log.info( "Pausing for [{0}] ms before shrinking the next region.",
+ this.getPauseBetweenRegionCallsMillis() );
- // don't pause after the last call to delete expired.
- if ( i < caches.length - 1 )
+ try
+ {
+ Thread.sleep( this.getPauseBetweenRegionCallsMillis() );
+ }
+ catch ( InterruptedException e )
{
- log.info( "Pausing for [{0}] ms before shrinking the next region.",
- this.getPauseBetweenRegionCallsMillis() );
-
- try
- {
- Thread.sleep( this.getPauseBetweenRegionCallsMillis() );
- }
- catch ( InterruptedException e )
- {
- log.warn( "Interrupted while waiting to delete expired for the next region." );
- }
+ log.warn( "Interrupted while waiting to delete expired for the next region." );
}
}
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java
index e2e964d..186e46b 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java
@@ -23,9 +23,7 @@ import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.disk.jdbc.JDBCDiskCache;
@@ -49,7 +47,7 @@ public class HSQLDiskCacheFactory
private static final Log log = LogManager.getLog( HSQLDiskCacheFactory.class );
/** The databases. */
- private Set<String> databases;
+ private CopyOnWriteArraySet<String> databases;
/**
* This factory method should create an instance of the hsqlcache.
@@ -79,7 +77,7 @@ public class HSQLDiskCacheFactory
public void initialize()
{
super.initialize();
- this.databases = Collections.synchronizedSet( new HashSet<>() );
+ this.databases = new CopyOnWriteArraySet<>();
}
/**
@@ -105,31 +103,34 @@ public class HSQLDiskCacheFactory
return;
}
- // TODO get this from the attributes.
- System.setProperty( "hsqldb.cache_scale", "8" );
+ synchronized (databases)
+ {
+ // TODO get this from the attributes.
+ System.setProperty( "hsqldb.cache_scale", "8" );
- // "org.hsqldb.jdbcDriver"
- String driver = attributes.getDriverClassName();
- // "sa"
- String user = attributes.getUserName();
- // ""
- String password = attributes.getPassword();
+ // "org.hsqldb.jdbcDriver"
+ String driver = attributes.getDriverClassName();
+ // "sa"
+ String user = attributes.getUserName();
+ // ""
+ String password = attributes.getPassword();
- try
- {
- Class.forName( driver ).newInstance();
- }
- catch (Exception e)
- {
- throw new SQLException( "Could not initialize driver " + driver, e );
- }
+ try
+ {
+ Class.forName( driver ).newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new SQLException( "Could not initialize driver " + driver, e );
+ }
- Connection cConn = DriverManager.getConnection( database, user, password );
- setupTable( cConn, attributes.getTableName() );
+ Connection cConn = DriverManager.getConnection( database, user, password );
+ setupTable( cConn, attributes.getTableName() );
- log.info( "Finished setting up database [{0}]", database);
+ log.info( "Finished setting up database [{0}]", database);
- databases.add( database );
+ databases.add( database );
+ }
}
/**
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java
index 94d37d8..6816ab2 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPDiscoveryListener.java
@@ -1,11 +1,9 @@
package org.apache.commons.jcs.auxiliary.lateral.socket.tcp;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArrayList;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -53,15 +51,15 @@ public class LateralTCPDiscoveryListener
* Map of no wait facades. these are used to determine which regions are locally configured to
* use laterals.
*/
- private final Map<String, LateralCacheNoWaitFacade<?, ?>> facades =
- Collections.synchronizedMap( new HashMap<>() );
+ private final ConcurrentMap<String, LateralCacheNoWaitFacade<?, ?>> facades =
+ new ConcurrentHashMap<>();
/**
* List of regions that are configured differently here than on another server. We keep track of
* this to limit the amount of info logging.
*/
- private final Set<String> knownDifferentlyConfiguredRegions =
- Collections.synchronizedSet( new HashSet<>() );
+ private final CopyOnWriteArrayList<String> knownDifferentlyConfiguredRegions =
+ new CopyOnWriteArrayList<>();
/** The name of the cache factory */
private String factoryName;
@@ -91,7 +89,7 @@ public class LateralTCPDiscoveryListener
* @param facade - facade (for region) => multiple lateral clients.
* @return true if the facade was not already registered.
*/
- public synchronized boolean addNoWaitFacade( String cacheName, LateralCacheNoWaitFacade<?, ?> facade )
+ public boolean addNoWaitFacade( String cacheName, LateralCacheNoWaitFacade<?, ?> facade )
{
boolean isNew = !containsNoWaitFacade( cacheName );
@@ -123,7 +121,9 @@ public class LateralTCPDiscoveryListener
public <K, V> boolean containsNoWait( String cacheName, LateralCacheNoWait<K, V> noWait )
{
@SuppressWarnings("unchecked") // Need to cast because of common map for all facades
- LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
+ LateralCacheNoWaitFacade<K, V> facade =
+ (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
+
if ( facade == null )
{
return false;
@@ -141,13 +141,14 @@ public class LateralTCPDiscoveryListener
* services.
* <p>
* @param noWait
- * @return true if we found the no wait and added it. False if the no wait was not present or it
+ * @return true if we found the no wait and added it. False if the no wait was not present or if
* we already had it.
*/
protected <K, V> boolean addNoWait( LateralCacheNoWait<K, V> noWait )
{
@SuppressWarnings("unchecked") // Need to cast because of common map for all facades
- LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
+ LateralCacheNoWaitFacade<K, V> facade =
+ (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
log.debug( "addNoWait > Got facade for {0} = {1}", noWait.getCacheName(), facade );
if ( facade != null )
@@ -158,12 +159,11 @@ public class LateralTCPDiscoveryListener
}
else
{
- if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) )
+ if ( knownDifferentlyConfiguredRegions.addIfAbsent( noWait.getCacheName() ) )
{
log.info( "addNoWait > Different nodes are configured differently "
+ "or region [{0}] is not yet used on this side.",
() -> noWait.getCacheName() );
- knownDifferentlyConfiguredRegions.add( noWait.getCacheName() );
}
return false;
}
@@ -179,7 +179,8 @@ public class LateralTCPDiscoveryListener
protected <K, V> boolean removeNoWait( LateralCacheNoWait<K, V> noWait )
{
@SuppressWarnings("unchecked") // Need to cast because of common map for all facades
- LateralCacheNoWaitFacade<K, V> facade = (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
+ LateralCacheNoWaitFacade<K, V> facade =
+ (LateralCacheNoWaitFacade<K, V>)facades.get( noWait.getCacheName() );
log.debug( "removeNoWait > Got facade for {0} = {1}", noWait.getCacheName(), facade);
if ( facade != null )
@@ -190,12 +191,11 @@ public class LateralTCPDiscoveryListener
}
else
{
- if ( !knownDifferentlyConfiguredRegions.contains( noWait.getCacheName() ) )
+ if ( knownDifferentlyConfiguredRegions.addIfAbsent( noWait.getCacheName() ) )
{
- log.info( "removeNoWait > Different nodes are configured differently "
+ log.info( "addNoWait > Different nodes are configured differently "
+ "or region [{0}] is not yet used on this side.",
() -> noWait.getCacheName() );
- knownDifferentlyConfiguredRegions.add( noWait.getCacheName() );
}
return false;
}
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
index 0775fc0..48932cb 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/JCSConcurrentCacheAccessUnitTest.java
@@ -19,16 +19,15 @@ package org.apache.commons.jcs;
* under the License.
*/
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
-import junit.framework.TestCase;
-
import org.apache.commons.jcs.access.GroupCacheAccess;
import org.apache.commons.jcs.access.exception.CacheException;
+import junit.framework.TestCase;
+
/**
* Test Case for JCS-73, modeled after the Groovy code by Alexander Kleymenov
*
@@ -67,7 +66,7 @@ public class JCSConcurrentCacheAccessUnitTest extends TestCase
JCS.setConfigFilename( "/TestJCS-73.ccf" );
cache = JCS.getGroupCacheInstance( "cache" );
errcount = new AtomicInteger(0);
- valueMismatchList = Collections.synchronizedList(new ArrayList<>());
+ valueMismatchList = new CopyOnWriteArrayList<>();
}
@Override
[commons-jcs] 04/05: Better concurrency
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit ec4c3e0739eb9bdb39c57febf93ce988f80134e2
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 23:56:52 2020 +0200
Better concurrency
---
.../jcs/utils/threadpool/ThreadPoolManager.java | 112 ++++++++-------------
.../threadpool/ThreadPoolManagerUnitTest.java | 4 +-
2 files changed, 46 insertions(+), 70 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
index ddcde27..e492b0c 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManager.java
@@ -1,26 +1,9 @@
package org.apache.commons.jcs.utils.threadpool;
-/*
- * 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.
- */
-
-import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
import java.util.Properties;
+import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
@@ -59,6 +42,9 @@ public class ThreadPoolManager
/** The default config, created using property defaults if present, else those above. */
private PoolConfiguration defaultConfig;
+ /** The default scheduler config, created using property defaults if present, else those above. */
+ private PoolConfiguration defaultSchedulerConfig;
+
/** the root property name */
private static final String PROP_NAME_ROOT = "thread_pool";
@@ -77,9 +63,6 @@ public class ThreadPoolManager
*/
private static volatile Properties props = null;
- /** singleton instance */
- private static ThreadPoolManager INSTANCE = null;
-
/** Map of names to pools. */
private ConcurrentHashMap<String, ExecutorService> pools;
@@ -87,6 +70,14 @@ public class ThreadPoolManager
private ConcurrentHashMap<String, ScheduledExecutorService> schedulerPools;
/**
+ * The ThreadPoolManager instance (holder pattern)
+ */
+ private static class ThreadPoolManagerHolder
+ {
+ static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
+ }
+
+ /**
* No instances please. This is a singleton.
*/
private ThreadPoolManager()
@@ -188,47 +179,44 @@ public class ThreadPoolManager
* <p>
* @return The single instance of the ThreadPoolManager
*/
- public static synchronized ThreadPoolManager getInstance()
+ public static ThreadPoolManager getInstance()
{
- if ( INSTANCE == null )
- {
- INSTANCE = new ThreadPoolManager();
- }
- return INSTANCE;
+ return ThreadPoolManagerHolder.INSTANCE;
}
/**
* Dispose of the instance of the ThreadPoolManger and shut down all thread pools
*/
- public static synchronized void dispose()
+ public static void dispose()
{
- if ( INSTANCE != null )
+ for ( Iterator<Map.Entry<String, ExecutorService>> i =
+ getInstance().pools.entrySet().iterator(); i.hasNext(); )
{
- for ( ExecutorService pool : INSTANCE.pools.values() )
+ Map.Entry<String, ExecutorService> entry = i.next();
+ try
{
- try
- {
- pool.shutdownNow();
- }
- catch (Throwable t)
- {
- log.warn("Failed to close pool {0}", pool, t);
- }
+ entry.getValue().shutdownNow();
}
-
- for ( ScheduledExecutorService pool : INSTANCE.schedulerPools.values() )
+ catch (Throwable t)
{
- try
- {
- pool.shutdownNow();
- }
- catch (Throwable t)
- {
- log.warn("Failed to close pool {0}", pool, t);
- }
+ log.warn("Failed to close pool {0}", entry.getKey(), t);
}
+ i.remove();
+ }
- INSTANCE = null;
+ for ( Iterator<Map.Entry<String, ScheduledExecutorService>> i =
+ getInstance().schedulerPools.entrySet().iterator(); i.hasNext(); )
+ {
+ Map.Entry<String, ScheduledExecutorService> entry = i.next();
+ try
+ {
+ entry.getValue().shutdownNow();
+ }
+ catch (Throwable t)
+ {
+ log.warn("Failed to close pool {0}", entry.getKey(), t);
+ }
+ i.remove();
}
}
@@ -245,7 +233,7 @@ public class ThreadPoolManager
{
ExecutorService pool = pools.computeIfAbsent(name, key -> {
log.debug( "Creating pool for name [{0}]", key );
- PoolConfiguration config = loadConfig( PROP_NAME_ROOT + "." + key );
+ PoolConfiguration config = loadConfig( PROP_NAME_ROOT + "." + key, defaultConfig );
return createPool( config, "JCS-ThreadPoolManager-" + key + "-" );
});
@@ -265,7 +253,6 @@ public class ThreadPoolManager
{
ScheduledExecutorService pool = schedulerPools.computeIfAbsent(name, key -> {
log.debug( "Creating scheduler pool for name [{0}]", key );
- PoolConfiguration defaultSchedulerConfig = loadConfig( DEFAULT_PROP_NAME_SCHEDULER_ROOT );
PoolConfiguration config = loadConfig( PROP_NAME_SCHEDULER_ROOT + "." + key,
defaultSchedulerConfig );
return createSchedulerPool( config, "JCS-ThreadPoolManager-" + key + "-", Thread.NORM_PRIORITY );
@@ -279,9 +266,9 @@ public class ThreadPoolManager
* <p>
* @return ArrayList of string names
*/
- public ArrayList<String> getPoolNames()
+ protected Set<String> getPoolNames()
{
- return new ArrayList<>(pools.keySet());
+ return pools.keySet();
}
/**
@@ -309,19 +296,8 @@ public class ThreadPoolManager
}
// set initial default and then override if new settings are available
- defaultConfig = new PoolConfiguration();
- defaultConfig = loadConfig( DEFAULT_PROP_NAME_ROOT );
- }
-
- /**
- * Configures the PoolConfiguration settings.
- * <p>
- * @param root the configuration key prefix
- * @return PoolConfiguration
- */
- private PoolConfiguration loadConfig( String root )
- {
- return loadConfig(root, defaultConfig);
+ defaultConfig = loadConfig( DEFAULT_PROP_NAME_ROOT, new PoolConfiguration() );
+ defaultSchedulerConfig = loadConfig( DEFAULT_PROP_NAME_SCHEDULER_ROOT, new PoolConfiguration() );
}
/**
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
index 753e775..3831954 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/threadpool/ThreadPoolManagerUnitTest.java
@@ -19,8 +19,8 @@ package org.apache.commons.jcs.utils.threadpool;
* under the License.
*/
-import java.util.ArrayList;
import java.util.Properties;
+import java.util.Set;
import java.util.concurrent.ExecutorService;
import org.apache.commons.jcs.utils.props.PropertyLoader;
@@ -80,7 +80,7 @@ public class ThreadPoolManagerUnitTest
String poolName2 = "testGetPoolNames2";
mgr.getExecutorService( poolName2 );
- ArrayList<String> names = mgr.getPoolNames();
+ Set<String> names = mgr.getPoolNames();
assertTrue( "Should have name in list.", names.contains( poolName1 ) );
assertTrue( "Should have name in list.", names.contains( poolName2 ) );
}
[commons-jcs] 05/05: Cleanup, use ElapsedTimer
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit 43ed9a55140bed5eb30074eeffd7ba6f0c820ebc
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 23:57:29 2020 +0200
Cleanup, use ElapsedTimer
---
.../jcs/auxiliary/disk/block/BlockDiskCache.java | 3 +-
.../auxiliary/disk/indexed/IndexedDiskCache.java | 9 +++--
.../jcs/auxiliary/disk/jdbc/JDBCDiskCache.java | 3 +-
.../jcs/auxiliary/disk/jdbc/ShrinkerThread.java | 6 ++--
.../disk/jdbc/mysql/MySQLTableOptimizer.java | 6 ++--
.../auxiliary/remote/server/RemoteCacheServer.java | 19 +++--------
.../apache/commons/jcs/engine/CacheConstants.java | 34 -------------------
.../apache/commons/jcs/engine/behavior/ICache.java | 39 ++++++++++++----------
.../commons/jcs/engine/control/CompositeCache.java | 5 ++-
.../jcs/engine/control/CompositeCacheManager.java | 29 ++++++++--------
.../jcs/engine/memory/AbstractMemoryCache.java | 4 +--
11 files changed, 56 insertions(+), 101 deletions(-)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskCache.java
index 90df796..1fd383c 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskCache.java
@@ -35,7 +35,6 @@ import java.util.stream.Collectors;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache;
-import org.apache.commons.jcs.engine.CacheConstants;
import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.IElementSerializer;
import org.apache.commons.jcs.engine.behavior.IRequireScheduler;
@@ -419,7 +418,7 @@ public class BlockDiskCache<K, V>
try
{
- if (key instanceof String && key.toString().endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
+ if (key instanceof String && key.toString().endsWith(NAME_COMPONENT_DELIMITER))
{
removed = performPartialKeyRemoval((String) key);
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
index ed4ecf9..68d9eef 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
@@ -40,7 +40,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache;
import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
-import org.apache.commons.jcs.engine.CacheConstants;
import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.IElementSerializer;
import org.apache.commons.jcs.engine.control.group.GroupAttrName;
@@ -405,7 +404,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V>
*/
protected boolean checkForDedOverlaps(IndexedDiskElementDescriptor[] sortedDescriptors)
{
- long start = System.currentTimeMillis();
+ ElapsedTimer timer = new ElapsedTimer();
boolean isOk = true;
long expectedNextPos = 0;
for (int i = 0; i < sortedDescriptors.length; i++)
@@ -422,8 +421,8 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V>
expectedNextPos = ded.pos + IndexedDisk.HEADER_SIZE_BYTES + ded.len;
}
}
- long end = System.currentTimeMillis();
- log.debug("{0}: Check for DED overlaps took {1} ms.", logCacheName, end - start);
+ log.debug("{0}: Check for DED overlaps took {1} ms.", () -> logCacheName,
+ () -> timer.getElapsedTime());
return isOk;
}
@@ -730,7 +729,7 @@ public class IndexedDiskCache<K, V> extends AbstractDiskCache<K, V>
{
storageLock.writeLock().lock();
- if (key instanceof String && key.toString().endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
+ if (key instanceof String && key.toString().endsWith(NAME_COMPONENT_DELIMITER))
{
removed = performPartialKeyRemoval((String) key);
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java
index 2bd7ff2..508eec2 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java
@@ -36,7 +36,6 @@ import javax.sql.DataSource;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.disk.AbstractDiskCache;
import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
-import org.apache.commons.jcs.engine.CacheConstants;
import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.commons.jcs.engine.behavior.IElementSerializer;
@@ -528,7 +527,7 @@ public class JDBCDiskCache<K, V>
try (Connection con = getDataSource().getConnection())
{
boolean partial = false;
- if ( key instanceof String && key.toString().endsWith( CacheConstants.NAME_COMPONENT_DELIMITER ) )
+ if ( key instanceof String && key.toString().endsWith( NAME_COMPONENT_DELIMITER ) )
{
// remove all keys of the same name group.
sql = "delete from " + getJdbcDiskCacheAttributes().getTableName()
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
index 24a9580..4e0a5d6 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
@@ -24,6 +24,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.commons.jcs.log.Log;
import org.apache.commons.jcs.log.LogManager;
+import org.apache.commons.jcs.utils.timing.ElapsedTimer;
/**
* Calls delete expired on the disk caches. The shrinker is run by a clock daemon. The shrinker
@@ -100,12 +101,11 @@ public class ShrinkerThread
for (Iterator<JDBCDiskCache<?, ?>> i = shrinkSet.iterator(); i.hasNext();)
{
JDBCDiskCache<?, ?> cache = i.next();
- long start = System.currentTimeMillis();
+ ElapsedTimer timer = new ElapsedTimer();
int deleted = cache.deleteExpired();
- long end = System.currentTimeMillis();
log.info( "Deleted [{0}] expired for region [{1}] for table [{2}] in {3} ms.",
- deleted, cache.getCacheName(), cache.getTableName(), end - start );
+ deleted, cache.getCacheName(), cache.getTableName(), timer.getElapsedTime() );
// don't pause after the last call to delete expired.
if ( i.hasNext() )
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java
index 58a8fd3..585947f 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java
@@ -29,6 +29,7 @@ import javax.sql.DataSource;
import org.apache.commons.jcs.auxiliary.disk.jdbc.TableState;
import org.apache.commons.jcs.log.Log;
import org.apache.commons.jcs.log.LogManager;
+import org.apache.commons.jcs.utils.timing.ElapsedTimer;
/**
* The MySQL Table Optimizer can optimize MySQL tables. It knows how to optimize for MySQL databases
@@ -115,7 +116,7 @@ public class MySQLTableOptimizer
*/
public boolean optimizeTable()
{
- long start = System.currentTimeMillis();
+ ElapsedTimer timer = new ElapsedTimer();
boolean success = false;
if ( tableState.getState() == TableState.OPTIMIZATION_RUNNING )
@@ -185,9 +186,8 @@ public class MySQLTableOptimizer
{
tableState.setState( TableState.FREE );
- long end = System.currentTimeMillis();
log.info( "Optimization of table [{0}] took {1} ms.",
- this.getTableName(), end - start );
+ () -> this.getTableName(), () -> timer.getElapsedTime() );
}
return success;
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServer.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServer.java
index e391786..4d5f3db 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServer.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/remote/server/RemoteCacheServer.java
@@ -50,6 +50,7 @@ import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
import org.apache.commons.jcs.log.Log;
import org.apache.commons.jcs.log.LogManager;
+import org.apache.commons.jcs.utils.timing.ElapsedTimer;
/**
* This class provides remote cache services. The remote cache server propagates events from local
@@ -78,9 +79,6 @@ public class RemoteCacheServer<K, V>
/** log instance */
private static final Log log = LogManager.getLog( RemoteCacheServer.class );
- /** timing -- if we should record operation times. */
- private static final boolean timing = true;
-
/** Number of puts into the cache. */
private int puts = 0;
@@ -264,12 +262,7 @@ public class RemoteCacheServer<K, V>
*/
private void processUpdate( ICacheElement<K, V> item, long requesterId )
{
- long start = 0;
- if ( timing )
- {
- start = System.currentTimeMillis();
- }
-
+ ElapsedTimer timer = new ElapsedTimer();
logUpdateInfo( item );
try
@@ -315,7 +308,7 @@ public class RemoteCacheServer<K, V>
c.update( item );
}
}
- catch ( Exception ce )
+ catch ( IOException ce )
{
// swallow
log.info( "Exception caught updating item. requesterId [{0}]: {1}",
@@ -347,11 +340,7 @@ public class RemoteCacheServer<K, V>
}
// TODO use JAMON for timing
- if ( timing )
- {
- long end = System.currentTimeMillis();
- log.debug( "put took {0} ms.", end - start);
- }
+ log.debug( "put took {0} ms.", () -> timer.getElapsedTime());
}
/**
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/CacheConstants.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/CacheConstants.java
deleted file mode 100644
index d474af6..0000000
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/CacheConstants.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.apache.commons.jcs.engine;
-
-/*
- * 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.
- */
-
-/**
- * Constants used throughout the JCS cache engine
- * <p>
- * @version $Id$
- */
-public interface CacheConstants
-{
- /** This is the name of the config file that we will look for by default. */
- String DEFAULT_CONFIG = "/cache.ccf";
-
- /** Delimiter of a cache name component. This is used for hierarchical deletion */
- String NAME_COMPONENT_DELIMITER = ":";
-}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/behavior/ICache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/behavior/ICache.java
index dc9d313..1dd0b7f 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/behavior/ICache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/behavior/ICache.java
@@ -1,5 +1,9 @@
package org.apache.commons.jcs.engine.behavior;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Set;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -22,23 +26,22 @@ package org.apache.commons.jcs.engine.behavior;
import org.apache.commons.jcs.engine.CacheStatus;
import org.apache.commons.jcs.engine.match.behavior.IKeyMatcher;
-import java.io.IOException;
-import java.util.Map;
-import java.util.Set;
-
/**
* This is the top level interface for all cache like structures. It defines the methods used
* internally by JCS to access, modify, and instrument such structures.
- *
+ *
* This allows for a suite of reusable components for accessing such structures, for example
* asynchronous access via an event queue.
*/
public interface ICache<K, V>
extends ICacheType
{
+ /** Delimiter of a cache name component. This is used for hierarchical deletion */
+ String NAME_COMPONENT_DELIMITER = ":";
+
/**
* Puts an item to the cache.
- *
+ *
* @param element
* @throws IOException
*/
@@ -47,7 +50,7 @@ public interface ICache<K, V>
/**
* Gets an item from the cache.
- *
+ *
* @param key
* @return a cache element, or null if there is no data in cache for this key
* @throws IOException
@@ -57,7 +60,7 @@ public interface ICache<K, V>
/**
* Gets multiple items from the cache based on the given set of keys.
- *
+ *
* @param keys
* @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no data in cache for any of these keys
* @throws IOException
@@ -67,11 +70,11 @@ public interface ICache<K, V>
/**
* Gets items from the cache matching the given pattern. Items from memory will replace those from remote sources.
- *
+ *
* This only works with string keys. It's too expensive to do a toString on every key.
- *
+ *
* Auxiliaries will do their best to handle simple expressions. For instance, the JDBC disk cache will convert * to % and . to _
- *
+ *
* @param pattern
* @return a map of K key to ICacheElement<K, V> element, or an empty map if there is no data matching the pattern.
* @throws IOException
@@ -81,7 +84,7 @@ public interface ICache<K, V>
/**
* Removes an item from the cache.
- *
+ *
* @param key
* @return false if there was an error in removal
* @throws IOException
@@ -91,7 +94,7 @@ public interface ICache<K, V>
/**
* Removes all cached items from the cache.
- *
+ *
* @throws IOException
*/
void removeAll()
@@ -106,35 +109,35 @@ public interface ICache<K, V>
/**
* Returns the current cache size in number of elements.
- *
+ *
* @return number of elements
*/
int getSize();
/**
* Returns the cache status.
- *
+ *
* @return Alive or Error
*/
CacheStatus getStatus();
/**
* Returns the cache stats.
- *
+ *
* @return String of important historical information.
*/
String getStats();
/**
* Returns the cache name.
- *
+ *
* @return usually the region name.
*/
String getCacheName();
/**
* Sets the key matcher used by get matching.
- *
+ *
* @param keyMatcher
*/
void setKeyMatcher( IKeyMatcher<K> keyMatcher );
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCache.java
index f3a5c40..7601996 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCache.java
@@ -36,7 +36,6 @@ import java.util.stream.Stream;
import org.apache.commons.jcs.access.exception.CacheException;
import org.apache.commons.jcs.access.exception.ObjectNotFoundException;
import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
-import org.apache.commons.jcs.engine.CacheConstants;
import org.apache.commons.jcs.engine.CacheStatus;
import org.apache.commons.jcs.engine.behavior.ICache;
import org.apache.commons.jcs.engine.behavior.ICacheElement;
@@ -242,9 +241,9 @@ public class CompositeCache<K, V>
{
if (cacheElement.getKey() instanceof String
- && cacheElement.getKey().toString().endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
+ && cacheElement.getKey().toString().endsWith(NAME_COMPONENT_DELIMITER))
{
- throw new IllegalArgumentException("key must not end with " + CacheConstants.NAME_COMPONENT_DELIMITER
+ throw new IllegalArgumentException("key must not end with " + NAME_COMPONENT_DELIMITER
+ " for a put operation");
}
else if (cacheElement.getKey() instanceof GroupId)
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
index 1d29b56..d735a47 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
@@ -43,7 +43,6 @@ import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
-import org.apache.commons.jcs.engine.CacheConstants;
import org.apache.commons.jcs.engine.CompositeCacheAttributes;
import org.apache.commons.jcs.engine.ElementAttributes;
import org.apache.commons.jcs.engine.behavior.ICache;
@@ -62,6 +61,7 @@ import org.apache.commons.jcs.log.LogManager;
import org.apache.commons.jcs.utils.config.OptionConverter;
import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
import org.apache.commons.jcs.utils.threadpool.ThreadPoolManager;
+import org.apache.commons.jcs.utils.timing.ElapsedTimer;
/**
* Manages a composite cache. This provides access to caches and is the primary way to shutdown the
@@ -80,12 +80,20 @@ public class CompositeCacheManager
/** JMX object name */
public static final String JMX_OBJECT_NAME = "org.apache.commons.jcs:type=JCSAdminBean";
+ /** This is the name of the config file that we will look for by default. */
+ private static final String DEFAULT_CONFIG = "/cache.ccf";
+
/** default region prefix */
private static final String DEFAULT_REGION = "jcs.default";
+ /** Should we use system property substitutions. */
+ private static final boolean DEFAULT_USE_SYSTEM_PROPERTIES = true;
+
+ /** Once configured, you can force a reconfiguration of sorts. */
+ private static final boolean DEFAULT_FORCE_RECONFIGURATION = false;
+
/** Caches managed by this cache manager */
- private final ConcurrentMap<String, ICache<?, ?>> caches =
- new ConcurrentHashMap<>();
+ private final ConcurrentMap<String, ICache<?, ?>> caches = new ConcurrentHashMap<>();
/** Number of clients accessing this cache manager */
private final AtomicInteger clients = new AtomicInteger(0);
@@ -117,12 +125,6 @@ public class CompositeCacheManager
/** The Singleton Instance */
private static CompositeCacheManager instance;
- /** Should we use system property substitutions. */
- private static final boolean DEFAULT_USE_SYSTEM_PROPERTIES = true;
-
- /** Once configured, you can force a reconfiguration of sorts. */
- private static final boolean DEFAULT_FORCE_RECONFIGURATION = false;
-
/** Stack for those waiting for notification of a shutdown. */
private final LinkedBlockingDeque<IShutdownObserver> shutdownObservers = new LinkedBlockingDeque<>();
@@ -156,7 +158,7 @@ public class CompositeCacheManager
*/
public static synchronized CompositeCacheManager getInstance() throws CacheException
{
- return getInstance( CacheConstants.DEFAULT_CONFIG );
+ return getInstance( DEFAULT_CONFIG );
}
/**
@@ -304,7 +306,7 @@ public class CompositeCacheManager
*/
public void configure() throws CacheException
{
- configure( CacheConstants.DEFAULT_CONFIG );
+ configure( DEFAULT_CONFIG );
}
/**
@@ -426,7 +428,7 @@ public class CompositeCacheManager
// configure the cache
CompositeCacheConfigurator configurator = newConfigurator();
- long start = System.currentTimeMillis();
+ ElapsedTimer timer = new ElapsedTimer();
// set default value list
this.defaultAuxValues = OptionConverter.findAndSubst( CompositeCacheManager.DEFAULT_REGION,
@@ -453,8 +455,7 @@ public class CompositeCacheManager
// setup preconfigured caches
configurator.parseRegions( properties, this );
- long end = System.currentTimeMillis();
- log.info( "Finished configuration in {0} ms.", end - start);
+ log.info( "Finished configuration in {0} ms.", () -> timer.getElapsedTime());
isConfigured = true;
}
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
index 0c63fd0..e0b1bdb 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
@@ -30,7 +30,7 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
-import org.apache.commons.jcs.engine.CacheConstants;
+import org.apache.commons.jcs.engine.behavior.ICache;
import org.apache.commons.jcs.engine.behavior.ICacheElement;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
import org.apache.commons.jcs.engine.control.CompositeCache;
@@ -415,7 +415,7 @@ public abstract class AbstractMemoryCache<K, V>
boolean removed = false;
// handle partial removal
- if (key instanceof String && ((String) key).endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
+ if (key instanceof String && ((String) key).endsWith(ICache.NAME_COMPONENT_DELIMITER))
{
removed = removeByHierarchy(key);
}