Class DynamicMetadataCache<IdentifierType,MetadataType>
java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
net.shibboleth.shared.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 ClassesModifier and TypeClassDescriptionprivate classCleanup task that removes expired and idle metadata from the backing store. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DurationThe interval at which the cleanup task should run.private Function<CriteriaSet,MetadataType> The function to use to fetch/load metadata if either none exists, or the existing is stale.private com.codahale.metrics.Gauge<Integer>Metrics Gauge for the number of live indexed metadata.private DurationThe initial cleanup task delay.private final org.slf4j.LoggerClass logger.private DurationMaximum cache duration.private DurationThe maximum idle time for which the resolver will keep data for a given entityID, before it is removed.private Function<ExpirationTimeContext<MetadataType>,Instant> Strategy used to compute an expiration time from a metadata instance.static final StringMetric name for the gauge of the number of live entityIDs.static final StringMetric name for the ratio gauge of fetches to resolve requests.static final StringMetric name for the timer forfetch(MetadataManagementData, Object, CriteriaSet).static final StringMetric name for the timer forget(CriteriaSet).private StringBase name for Metrics instrumentation names.private final Function<IdentifierType,MetadataManagementData<IdentifierType>> Mapping function to use when creating new metadata management data.private DurationMinimum cache duration.private com.codahale.metrics.RatioGaugeMetrics RatioGauge for count of origin fetches to gets.private booleanFlag indicating whether idle entity data should be removed.private com.codahale.metrics.TimerMetrics Timer forfetch(MetadataManagementData, Object, CriteriaSet).private com.codahale.metrics.TimerMetrics Timer forget(CriteriaSet). -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor.protectedDynamicMetadataCache(DynamicBackingStore<IdentifierType, MetadataType> store, ScheduledExecutorService executor) Protected constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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 voidprotected voidprivate voidfetch(MetadataManagementData<IdentifierType> mgmtData, IdentifierType identifier, CriteriaSet criteria) Fetch metadata using the supplied fetch function, then save it to the backing store.get(CriteriaSet criteria) Get the list of metadata matching the given identifier.protected DynamicBackingStore<IdentifierType,MetadataType> Get the backing store.protected Function<ExpirationTimeContext<MetadataType>,Instant> Get the metadata expiration time strategy.private voidInitialize 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.voidSet the initial cleanup task delay.voidsetMaxCacheDuration(Duration duration) Set the maximum cache duration for metadata.voidSet the maximum idle time for which the resolver will keep data for a given entityID, before it is removed.voidSet 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, writeToBackingStoreMethods inherited from class net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent
setIdMethods inherited from class net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
ensureId, getId, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentExceptionMethods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent
checkComponentActive, checkSetterPreconditions, destroy, initialize, isDestroyed, isInitializedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.shibboleth.shared.component.IdentifiedComponent
getId
-
Field Details
-
METRIC_TIMER_GET
Metric name for the timer forget(CriteriaSet).- See Also:
-
METRIC_GAUGE_NUM_LIVE_INDEX_METADATA
Metric name for the gauge of the number of live entityIDs.- See Also:
-
METRIC_TIMER_FETCH_FROM_ORIGIN_SOURCE
Metric name for the timer forfetch(MetadataManagementData, Object, CriteriaSet).- See Also:
-
METRIC_RATIOGAUGE_FETCH_TO_GET
Metric name for the ratio gauge of fetches to resolve requests.- See Also:
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
cleanupTaskInterval
The interval at which the cleanup task should run. -
initialCleanupTaskDelay
The initial cleanup task delay. -
maxIdleEntityData
The maximum idle time for which the resolver will keep data for a given entityID, before it is removed. -
removeIdleEntityData
private boolean removeIdleEntityDataFlag indicating whether idle entity data should be removed. -
minCacheDuration
Minimum cache duration. -
maxCacheDuration
Maximum cache duration. -
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> metadataExpirationTimeStrategyStrategy used to compute an expiration time from a metadata instance. -
mgmtMappingFunction
@Nonnull private final Function<IdentifierType,MetadataManagementData<IdentifierType>> mgmtMappingFunctionMapping function to use when creating new metadata management data. -
metricsBaseName
Base name for Metrics instrumentation names. -
timerGet
@Nullable private com.codahale.metrics.Timer timerGetMetrics Timer forget(CriteriaSet). -
timerFetchFromSource
@Nullable private com.codahale.metrics.Timer timerFetchFromSourceMetrics Timer forfetch(MetadataManagementData, Object, CriteriaSet). -
ratioGaugeFetchToGet
@Nullable private com.codahale.metrics.RatioGauge ratioGaugeFetchToGetMetrics RatioGauge for count of origin fetches to gets. -
gaugeNumLiveIndexedMetadata
Metrics Gauge for the number of live indexed metadata.
-
-
Constructor Details
-
DynamicMetadataCache
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 Details
-
setMinCacheDuration
Set the minimum cache duration for metadata.- Parameters:
duration- the minimum cache duration
-
setMaxCacheDuration
Set the maximum cache duration for metadata.- Parameters:
duration- the maximum cache duration
-
setFetchStrategy
Set the metadata fetching strategy.- Parameters:
strategy- the strategy used to fetch metadata using a 'read-through' semantic.
-
setInitialCleanupTaskDelay
Set the initial cleanup task delay.- Parameters:
delay- The initialCleanupTaskDelay to set.
-
doDestroy
protected void doDestroy()- Overrides:
doDestroyin classAbstractMetadataCache<IdentifierType,MetadataType>
-
setCleanupTaskInterval
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
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
- Overrides:
doInitializein classAbstractMetadataCache<IdentifierType,MetadataType> - Throws:
ComponentInitializationException
-
initializeMetricsInstrumentation
private void initializeMetricsInstrumentation()Initialize the Metrics-based instrumentation. -
setMetricsBaseName
Set the base name for Metrics instrumentation.- Parameters:
baseName- the Metrics base name
-
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.
-
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
@Nonnull 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 @NonnullElements 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.
-