|
@@ -29,17 +29,20 @@ import password.pwm.error.PwmException;
|
|
|
import password.pwm.error.PwmUnrecoverableException;
|
|
|
import password.pwm.health.HealthRecord;
|
|
|
import password.pwm.svc.PwmService;
|
|
|
+import password.pwm.util.java.ConditionalTaskExecutor;
|
|
|
import password.pwm.util.java.JsonUtil;
|
|
|
-import password.pwm.util.java.TimeDuration;
|
|
|
import password.pwm.util.logging.PwmLogger;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
import java.time.Instant;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.TreeMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
public class CacheService implements PwmService
|
|
|
{
|
|
@@ -49,7 +52,7 @@ public class CacheService implements PwmService
|
|
|
|
|
|
private STATUS status = STATUS.NEW;
|
|
|
|
|
|
- private Instant lastTraceOutput;
|
|
|
+ private ConditionalTaskExecutor traceDebugOutputter;
|
|
|
|
|
|
@Override
|
|
|
public STATUS status( )
|
|
@@ -86,6 +89,10 @@ public class CacheService implements PwmService
|
|
|
status = STATUS.OPENING;
|
|
|
final int maxMemItems = Integer.parseInt( pwmApplication.getConfig().readAppProperty( AppProperty.CACHE_MEMORY_MAX_ITEMS ) );
|
|
|
memoryCacheStore = new MemoryCacheStore( maxMemItems );
|
|
|
+ this.traceDebugOutputter = new ConditionalTaskExecutor(
|
|
|
+ ( ) -> outputTraceInfo(),
|
|
|
+ new ConditionalTaskExecutor.TimeDurationPredicate( 1, TimeUnit.MINUTES )
|
|
|
+ );
|
|
|
status = STATUS.OPEN;
|
|
|
}
|
|
|
|
|
@@ -104,7 +111,10 @@ public class CacheService implements PwmService
|
|
|
@Override
|
|
|
public ServiceInfoBean serviceInfo( )
|
|
|
{
|
|
|
- return new ServiceInfoBean( Collections.emptyList() );
|
|
|
+ final Map<String, String> debugInfo = new TreeMap<>( );
|
|
|
+ debugInfo.putAll( JsonUtil.deserializeStringMap( JsonUtil.serialize( memoryCacheStore.getCacheStoreInfo() ) ) );
|
|
|
+ debugInfo.putAll( JsonUtil.deserializeStringMap( JsonUtil.serializeMap( memoryCacheStore.storedClassHistogram( "histogram." ) ) ) );
|
|
|
+ return new ServiceInfoBean( Collections.emptyList(), debugInfo );
|
|
|
}
|
|
|
|
|
|
public Map<String, Serializable> debugInfo( )
|
|
@@ -112,6 +122,7 @@ public class CacheService implements PwmService
|
|
|
final Map<String, Serializable> debugInfo = new LinkedHashMap<>( );
|
|
|
debugInfo.put( "memory-statistics", memoryCacheStore.getCacheStoreInfo() );
|
|
|
debugInfo.put( "memory-items", new ArrayList<Serializable>( memoryCacheStore.getCacheDebugItems() ) );
|
|
|
+ debugInfo.put( "memory-histogram", new HashMap<>( memoryCacheStore.storedClassHistogram( "" ) ) );
|
|
|
return Collections.unmodifiableMap( debugInfo );
|
|
|
}
|
|
|
|
|
@@ -136,11 +147,11 @@ public class CacheService implements PwmService
|
|
|
}
|
|
|
final Instant expirationDate = cachePolicy.getExpiration();
|
|
|
memoryCacheStore.store( cacheKey, expirationDate, payload );
|
|
|
- outputTraceInfo();
|
|
|
+
|
|
|
+ traceDebugOutputter.conditionallyExecuteTask();
|
|
|
}
|
|
|
|
|
|
public <T> T get( final CacheKey cacheKey, final Class<T> classOfT )
|
|
|
- throws PwmUnrecoverableException
|
|
|
{
|
|
|
if ( cacheKey == null )
|
|
|
{
|
|
@@ -158,28 +169,21 @@ public class CacheService implements PwmService
|
|
|
payload = memoryCacheStore.read( cacheKey, classOfT );
|
|
|
}
|
|
|
|
|
|
- outputTraceInfo();
|
|
|
+ traceDebugOutputter.conditionallyExecuteTask();
|
|
|
|
|
|
return (T) payload;
|
|
|
}
|
|
|
|
|
|
private void outputTraceInfo( )
|
|
|
{
|
|
|
- if ( lastTraceOutput == null || TimeDuration.fromCurrent( lastTraceOutput ).isLongerThan( 30 * 1000 ) )
|
|
|
- {
|
|
|
- lastTraceOutput = Instant.now();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
final StringBuilder traceOutput = new StringBuilder();
|
|
|
if ( memoryCacheStore != null )
|
|
|
{
|
|
|
final CacheStoreInfo info = memoryCacheStore.getCacheStoreInfo();
|
|
|
traceOutput.append( ", memCache=" );
|
|
|
traceOutput.append( JsonUtil.serialize( info ) );
|
|
|
+ traceOutput.append( ", histogram=" );
|
|
|
+ traceOutput.append( JsonUtil.serializeMap( memoryCacheStore.storedClassHistogram( "" ) ) );
|
|
|
}
|
|
|
LOGGER.trace( traceOutput );
|
|
|
}
|