alfresco-community-repo/source/java/org/alfresco/repo/cache/InMemoryCacheStatistics.java
Matt Ward d9c11a7ca7 Merged BRANCHES/DEV/mward/head_cachestats to HEAD:
89575: ACE-3327: Work In Progress. TX cache statistics.
   89642: ACE-3327: Exposes statistics through JMX. Adds hit ratio.
   89649: ACE-3327: Added "@since 5.0" to new classes.
   89691: ACE-3327: improved TransactionalCache stats tests.
   89743: ACE-3327: fixed lack of thread safety for InMemoryCacheStatistics.
   89752: ACE-3327: now possible to disable/enable tx cache statistics per-cache with properties.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@89798 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2014-11-03 18:08:50 +00:00

241 lines
7.9 KiB
Java

/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.cache;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.alfresco.repo.cache.TransactionStats.OpType;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Simple non-persistent implementation of {@link CacheStatistics}. Statistics
* are empty at repository startup.
*
* @since 5.0
* @author Matt Ward
*/
public class InMemoryCacheStatistics implements CacheStatistics, ApplicationContextAware
{
/** Read/Write locks by cache name */
private final ConcurrentMap<String, ReentrantReadWriteLock> locks = new ConcurrentHashMap<>();
private Map<String, Map<OpType, OperationStats>> cacheToStatsMap = new HashMap<>();
private ApplicationContext applicationContext;
@Override
public long count(String cacheName, OpType opType)
{
ReadLock readLock = getReadLock(cacheName);
readLock.lock();
try
{
Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
if (cacheStats == null)
{
throw new NoStatsForCache(cacheName);
}
OperationStats opStats = cacheStats.get(opType);
return opStats.count;
}
finally
{
readLock.unlock();
}
}
@Override
public double meanTime(String cacheName, OpType opType)
{
ReadLock readLock = getReadLock(cacheName);
readLock.lock();
try
{
Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
if (cacheStats == null)
{
throw new NoStatsForCache(cacheName);
}
OperationStats opStats = cacheStats.get(opType);
return opStats.meanTime();
}
finally
{
readLock.unlock();
}
}
@Override
public void add(String cacheName, TransactionStats txStats)
{
boolean registerCacheStats = false;
WriteLock writeLock = getWriteLock(cacheName);
writeLock.lock();
try
{
// Are we adding new stats for a previously unseen cache?
registerCacheStats = !cacheToStatsMap.containsKey(cacheName);
if (registerCacheStats)
{
// There are no statistics yet for this cache.
cacheToStatsMap.put(cacheName, new HashMap<OpType, OperationStats>());
}
Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
for (OpType opType : OpType.values())
{
SummaryStatistics txOpSummary = txStats.getTimings(opType);
long count = txOpSummary.getN();
double totalTime = txOpSummary.getSum();
OperationStats oldStats = cacheStats.get(opType);
OperationStats newStats;
if (oldStats == null)
{
newStats = new OperationStats(totalTime, count);
}
else
{
newStats = new OperationStats(oldStats, totalTime, count);
}
cacheStats.put(opType, newStats);
}
}
finally
{
writeLock.unlock();
}
if (registerCacheStats)
{
// We've added stats for a previously unseen cache, raise an event
// so that an MBean for the cache may be registered, for example.
applicationContext.publishEvent(new CacheStatisticsCreated(this, cacheName));
}
}
@Override
public double hitMissRatio(String cacheName)
{
ReadLock readLock = getReadLock(cacheName);
readLock.lock();
try
{
Map<OpType, OperationStats> cacheStats = cacheToStatsMap.get(cacheName);
if (cacheStats == null)
{
throw new NoStatsForCache(cacheName);
}
long hits = cacheStats.get(OpType.GET_HIT).count;
long misses = cacheStats.get(OpType.GET_MISS).count;
return (double)hits / (hits+misses);
}
finally
{
readLock.unlock();
}
}
/**
* Represents a single cache operation type's statistics.
*/
private static final class OperationStats
{
/** Total time spent in operations of this type */
private final double totalTime;
/** Count of how many instances of this operation occurred. */
private final long count;
public OperationStats(double totalTime, long count)
{
this.totalTime = totalTime;
this.count = count;
}
public OperationStats(OperationStats source, double totalTime, long count)
{
if (Double.compare(source.totalTime, Double.NaN) == 0)
{
// No previous time to add new time to.
this.totalTime = totalTime;
}
else
{
this.totalTime = source.totalTime + totalTime;
}
this.count = source.count + count;
}
public double meanTime()
{
return totalTime / count;
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
/**
* Gets a {@link ReentrantReadWriteLock} for a specific cache, lazily
* creating the lock if necessary. Locks may be created per cache
* (rather than hashing to a smaller pool) since the number of
* caches is not too large.
*
* @param cacheName Cache name to obtain lock for.
* @return ReentrantReadWriteLock
*/
private ReentrantReadWriteLock getLock(String cacheName)
{
if (!locks.containsKey(cacheName))
{
ReentrantReadWriteLock newLock = new ReentrantReadWriteLock();
if (locks.putIfAbsent(cacheName, newLock) == null)
{
// Lock was successfully added to map.
return newLock;
};
}
return locks.get(cacheName);
}
private ReadLock getReadLock(String cacheName)
{
ReadLock readLock = getLock(cacheName).readLock();
return readLock;
}
private WriteLock getWriteLock(String cacheName)
{
WriteLock writeLock = getLock(cacheName).writeLock();
return writeLock;
}
}