Class DynamicMetadataCache<IdentifierType,MetadataType>
- java.lang.Object
-
- net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
-
- net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
-
- net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent
-
- net.shibboleth.oidc.metadata.cache.impl.AbstractMetadataCache<IdentifierType,MetadataType>
-
- net.shibboleth.oidc.metadata.cache.impl.DynamicMetadataCache<IdentifierType,MetadataType>
-
- Type Parameters:
IdentifierType- the metadata identifier type.MetadataType- the metadata type.
- All Implemented Interfaces:
MetadataCache<MetadataType>,Component,DestructableComponent,IdentifiableComponent,IdentifiedComponent,InitializableComponent
public class DynamicMetadataCache<IdentifierType,MetadataType> extends AbstractMetadataCache<IdentifierType,MetadataType>
A metadata cache implementation that supports 'read-through' semantics. Does not support 'refresh-ahead' semantics for loading about to expire values asynchronously ahead of time.Supports the following:
- Read-write locking on individual metadata entries. Including optimistic reads.
- Synchronous 'read-through' metadata fetching for new metadata.
- Synchronous 'read-through' metadata fetching for stale (past refresh point) metadata.
- A background task to remove expired and idle metadata.
This class should only be instantiated through the
MetadataCacheBuilder.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classDynamicMetadataCache.ExpiredAndIdleMetadataCleanupTaskCleanup task that removes expired and idle metadata from the backing store.
-
Field Summary
Fields Modifier and Type Field Description private DurationcleanupTaskIntervalThe interval at which the cleanup task should run.private Function<CriteriaSet,MetadataType>fetchStrategyThe function to use to fetch/load metadata if either none exists, or the existing is stale.private com.codahale.metrics.Gauge<Integer>gaugeNumLiveIndexedMetadataMetrics Gauge for the number of live indexed metadata.private DurationinitialCleanupTaskDelayThe initial cleanup task delay.private org.slf4j.LoggerlogClass logger.private DurationmaxCacheDurationMaximum cache duration.private DurationmaxIdleEntityDataThe maximum idle time for which the resolver will keep data for a given entityID, before it is removed.private Function<ExpirationTimeContext<MetadataType>,Instant>metadataExpirationTimeStrategyStrategy used to compute an expiration time from a metadata instance.static StringMETRIC_GAUGE_NUM_LIVE_INDEX_METADATAMetric name for the gauge of the number of live entityIDs.static StringMETRIC_RATIOGAUGE_FETCH_TO_GETMetric name for the ratio gauge of fetches to resolve requests.static StringMETRIC_TIMER_FETCH_FROM_ORIGIN_SOURCEMetric name for the timer forfetch(MetadataManagementData, Object, CriteriaSet).static StringMETRIC_TIMER_GETMetric name for the timer forget(CriteriaSet).private StringmetricsBaseNameBase name for Metrics instrumentation names.private Function<IdentifierType,MetadataManagementData<IdentifierType>>mgmtMappingFunctionMapping function to use when creating new metadata management data.private DurationminCacheDurationMinimum cache duration.private com.codahale.metrics.RatioGaugeratioGaugeFetchToGetMetrics RatioGauge for count of origin fetches to gets.private booleanremoveIdleEntityDataFlag indicating whether idle entity data should be removed.private com.codahale.metrics.TimertimerFetchFromSourceMetrics Timer forfetch(MetadataManagementData, Object, CriteriaSet).private com.codahale.metrics.TimertimerGetMetrics Timer forget(CriteriaSet).
-
Constructor Summary
Constructors Modifier Constructor Description protectedDynamicMetadataCache(DynamicBackingStore<IdentifierType,MetadataType> store)Constructor.protectedDynamicMetadataCache(DynamicBackingStore<IdentifierType,MetadataType> store, ScheduledExecutorService executor)Protected constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private InstantcomputeRefreshTriggerTime(Instant expirationTime, Instant nowDateTime)Compute the refresh trigger time.protected ExpirationTimeContext<MetadataType>createExpirationTimeContext(MetadataType metadata, Instant now)Create an expiration time context from the given metadata, the min and max cache durations, and the instant representing 'now'.protected voiddoDestroy()protected voiddoInitialize()private voidfetch(MetadataManagementData<IdentifierType> mgmtData, IdentifierType identifier, CriteriaSet criteria)Fetch metadata using the supplied fetch function, then save it to the backing store.List<MetadataType>get(CriteriaSet criteria)protected DynamicBackingStore<IdentifierType,MetadataType>getBackingStore()Get the backing store.protected Function<ExpirationTimeContext<MetadataType>,Instant>getMetadataExpirationTimeStrategy()Get the metadata expiration time strategy.private voidinitializeMetricsInstrumentation()Initialize the Metrics-based instrumentation.private List<MetadataType>read(MetadataManagementData<IdentifierType> mgmtData, IdentifierType identifier)Read metadata from the cache under the lock relating to the Identifier of the metadata to find.voidsetCleanupTaskInterval(Duration interval)Set the interval at which the cleanup task should run.voidsetFetchStrategy(Function<CriteriaSet,MetadataType> strategy)Set the metadata fetching strategy.voidsetInitialCleanupTaskDelay(Duration delay)Set the initial cleanup task delay.voidsetMaxCacheDuration(Duration duration)Set the maximum cache duration for metadata.voidsetMaxIdleEntityData(Duration max)Set the maximum idle time for which the resolver will keep data for a given entityID, before it is removed.voidsetMetadataExpirationTimeStrategy(Function<ExpirationTimeContext<MetadataType>,Instant> strategy)Set the metadata expiration time strategy.voidsetMetricsBaseName(String baseName)Set the base name for Metrics instrumentation.voidsetMinCacheDuration(Duration duration)Set the minimum cache duration for metadata.voidsetRemoveIdleEntityData(boolean flag)Set the flag indicating whether idle entity data should be removed.private voidstoreNewMetadata(MetadataManagementData<IdentifierType> mgmtData, MetadataType metadata, IdentifierType expectedIdentifier)Process the new metadata as follows: Apply the metadata filtering strategy configured. Check the fetched metadata's identifier matches that expected. Write the metadata to the backing store (cache). Update the metadata's management information with new refresh information.-
Methods inherited from class net.shibboleth.oidc.metadata.cache.impl.AbstractMetadataCache
errorHandlingWrapper, freshLoad, getCriteriaToIdentifierStrategy, getExecutorService, getIdentifierExtractionStrategy, getLogPrefix, getMetadataFilterStrategy, getMetadataValidPredicate, getRefreshDelayFactor, hasExpired, invalidate, invalidateAll, lookupIdentifier, lookupIndexedIdentifier, newFilterContext, setCriteriaToIdentifierStrategy, setIdentifierExtractionStrategy, setMetadataBeforeRemovalHook, setMetadataFilterStrategy, setMetadataValidPredicate, setRefreshDelayFactor, shouldAttemptRefresh, writeToBackingStore
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent
setId
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractIdentifiedInitializableComponent
getId
-
Methods inherited from class net.shibboleth.utilities.java.support.component.AbstractInitializableComponent
destroy, initialize, isDestroyed, isInitialized
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.shibboleth.utilities.java.support.component.IdentifiedComponent
getId
-
-
-
-
Field Detail
-
METRIC_TIMER_GET
public static final String METRIC_TIMER_GET
Metric name for the timer forget(CriteriaSet).- See Also:
- Constant Field Values
-
METRIC_GAUGE_NUM_LIVE_INDEX_METADATA
public static final String METRIC_GAUGE_NUM_LIVE_INDEX_METADATA
Metric name for the gauge of the number of live entityIDs.- See Also:
- Constant Field Values
-
METRIC_TIMER_FETCH_FROM_ORIGIN_SOURCE
public static final String METRIC_TIMER_FETCH_FROM_ORIGIN_SOURCE
Metric name for the timer forfetch(MetadataManagementData, Object, CriteriaSet).- See Also:
- Constant Field Values
-
METRIC_RATIOGAUGE_FETCH_TO_GET
public static final String METRIC_RATIOGAUGE_FETCH_TO_GET
Metric name for the ratio gauge of fetches to resolve requests.- See Also:
- Constant Field Values
-
log
private final org.slf4j.Logger log
Class logger.
-
cleanupTaskInterval
@NonnullAfterInit private Duration cleanupTaskInterval
The interval at which the cleanup task should run.
-
initialCleanupTaskDelay
@NonnullAfterInit private Duration initialCleanupTaskDelay
The initial cleanup task delay.
-
maxIdleEntityData
@NonnullAfterInit private Duration maxIdleEntityData
The maximum idle time for which the resolver will keep data for a given entityID, before it is removed.
-
removeIdleEntityData
private boolean removeIdleEntityData
Flag indicating whether idle entity data should be removed.
-
minCacheDuration
@NonnullAfterInit private Duration minCacheDuration
Minimum cache duration.
-
maxCacheDuration
@NonnullAfterInit private Duration maxCacheDuration
Maximum cache duration.
-
fetchStrategy
@NonnullAfterInit private Function<CriteriaSet,MetadataType> fetchStrategy
The function to use to fetch/load metadata if either none exists, or the existing is stale.
-
metadataExpirationTimeStrategy
@NonnullAfterInit private Function<ExpirationTimeContext<MetadataType>,Instant> metadataExpirationTimeStrategy
Strategy used to compute an expiration time from a metadata instance.
-
mgmtMappingFunction
@Nonnull private final Function<IdentifierType,MetadataManagementData<IdentifierType>> mgmtMappingFunction
Mapping function to use when creating new metadata management data.
-
metricsBaseName
@NonnullAfterInit private String metricsBaseName
Base name for Metrics instrumentation names.
-
timerGet
@Nullable private com.codahale.metrics.Timer timerGet
Metrics Timer forget(CriteriaSet).
-
timerFetchFromSource
@Nullable private com.codahale.metrics.Timer timerFetchFromSource
Metrics Timer forfetch(MetadataManagementData, Object, CriteriaSet).
-
ratioGaugeFetchToGet
@Nullable private com.codahale.metrics.RatioGauge ratioGaugeFetchToGet
Metrics RatioGauge for count of origin fetches to gets.
-
gaugeNumLiveIndexedMetadata
@Nullable private com.codahale.metrics.Gauge<Integer> gaugeNumLiveIndexedMetadata
Metrics Gauge for the number of live indexed metadata.
-
-
Constructor Detail
-
DynamicMetadataCache
protected DynamicMetadataCache(@Nonnull DynamicBackingStore<IdentifierType,MetadataType> store)Constructor.- Parameters:
store- the backing store to use as the cache store.
-
DynamicMetadataCache
protected DynamicMetadataCache(@Nonnull DynamicBackingStore<IdentifierType,MetadataType> store, @Nullable ScheduledExecutorService executor)Protected constructor. Used mainly for testing.- Parameters:
store- the backing store to use as the cache store.executor- override the executor service.
-
-
Method Detail
-
setMinCacheDuration
public void setMinCacheDuration(@Nonnull Duration duration)Set the minimum cache duration for metadata.- Parameters:
duration- the minimum cache duration
-
setMaxCacheDuration
public void setMaxCacheDuration(@Nonnull Duration duration)Set the maximum cache duration for metadata.- Parameters:
duration- the maximum cache duration
-
setFetchStrategy
public void setFetchStrategy(@Nonnull Function<CriteriaSet,MetadataType> strategy)Set the metadata fetching strategy.- Parameters:
strategy- the strategy used to fetch metadata using a 'read-through' semantic.
-
setInitialCleanupTaskDelay
public void setInitialCleanupTaskDelay(@Nonnull Duration delay)Set the initial cleanup task delay.- Parameters:
delay- The initialCleanupTaskDelay to set.
-
doDestroy
protected void doDestroy()
- Overrides:
doDestroyin classAbstractMetadataCache<IdentifierType,MetadataType>
-
setCleanupTaskInterval
public void setCleanupTaskInterval(@Nonnull Duration interval)Set the interval at which the cleanup task should run.Defaults to: 30 minutes.
- Parameters:
interval- the interval to set
-
setRemoveIdleEntityData
public void setRemoveIdleEntityData(boolean flag)
Set the flag indicating whether idle entity data should be removed.- Parameters:
flag- true if idle entity data should be removed, false otherwise
-
setMaxIdleEntityData
public void setMaxIdleEntityData(@Nonnull Duration max)Set the maximum idle time for which the resolver will keep data for a given entityID, before it is removed.Defaults to: 8 hours.
- Parameters:
max- the maximum entity data idle time
-
setMetadataExpirationTimeStrategy
public void setMetadataExpirationTimeStrategy(@Nonnull Function<ExpirationTimeContext<MetadataType>,Instant> strategy)Set the metadata expiration time strategy.- Parameters:
strategy- the strategy.
-
getMetadataExpirationTimeStrategy
@NonnullAfterInit protected Function<ExpirationTimeContext<MetadataType>,Instant> getMetadataExpirationTimeStrategy()
Get the metadata expiration time strategy.- Returns:
- the expiration time strategy.
-
doInitialize
protected void doInitialize() throws ComponentInitializationException- Overrides:
doInitializein classAbstractMetadataCache<IdentifierType,MetadataType>- Throws:
ComponentInitializationException
-
initializeMetricsInstrumentation
private void initializeMetricsInstrumentation()
Initialize the Metrics-based instrumentation.
-
setMetricsBaseName
public void setMetricsBaseName(@Nullable String baseName)Set the base name for Metrics instrumentation.- Parameters:
baseName- the Metrics base name
-
getBackingStore
@Nonnull protected DynamicBackingStore<IdentifierType,MetadataType> getBackingStore()
Get the backing store.Cast the backing store to the type used by this cache type.
- Overrides:
getBackingStorein classAbstractMetadataCache<IdentifierType,MetadataType>- Returns:
- the backing store. Can be null.
-
get
@Nonnull @NonnullElements public List<MetadataType> get(@Nonnull @NotEmpty CriteriaSet criteria) throws MetadataCacheException
- Throws:
MetadataCacheException
-
fetch
private void fetch(@Nonnull MetadataManagementData<IdentifierType> mgmtData, @Nonnull IdentifierType identifier, @Nonnull @NotEmpty CriteriaSet criteria) throws MetadataCacheExceptionFetch metadata using the supplied fetch function, then save it to the backing store.- Parameters:
mgmtData- the metadata management data.identifier- the identifier of the metadata to fetch.criteria- the criteria used in determining how to fetch the metadata.- Throws:
MetadataCacheException- on error.
-
storeNewMetadata
private void storeNewMetadata(@Nonnull MetadataManagementData<IdentifierType> mgmtData, @Nonnull MetadataType metadata, @Nonnull IdentifierType expectedIdentifier)Process the new metadata as follows:- Apply the metadata filtering strategy configured.
- Check the fetched metadata's identifier matches that expected.
- Write the metadata to the backing store (cache).
- Update the metadata's management information with new refresh information.
- Parameters:
mgmtData- the management data.metadata- the new metadata to process.expectedIdentifier- the identifier expected on the new metadata.
-
createExpirationTimeContext
protected ExpirationTimeContext<MetadataType> createExpirationTimeContext(@Nonnull MetadataType metadata, @Nonnull Instant now)
Create an expiration time context from the given metadata, the min and max cache durations, and the instant representing 'now'.- Parameters:
metadata- the metadata.now- the instant representing now.- Returns:
- an expiration time context.
-
computeRefreshTriggerTime
@Nonnull private Instant computeRefreshTriggerTime(@Nullable Instant expirationTime, @Nonnull Instant nowDateTime)
Compute the refresh trigger time.- Parameters:
expirationTime- the time at which the metadata effectively expiresnowDateTime- the current date time instant- Returns:
- the time after which refresh attempt(s) should be made
-
read
@Nonnull private List<MetadataType> read(@Nonnull MetadataManagementData<IdentifierType> mgmtData, @Nonnull IdentifierType identifier) throws MetadataCacheException
Read metadata from the cache under the lock relating to the Identifier of the metadata to find.The first read attempt is optimistic and occurs without acquiring a read lock. The optimistic read is validated to ensure another thread has not acquired a write lock in the meantime. If it has, a read lock is obtained and a further read is attempted - to ensure a consistent state. This should improve efficiency given that metadata reads will out number metadata fetch/writes.
- Parameters:
mgmtData- the metadata management data.identifier- the metadata identifier to use as a key to fetch.- Returns:
- a list of metadata that matches the given key, an empty list of none are found.
- Throws:
MetadataCacheException- on error.
-
-