/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions Copyright 2011-2013 ForgeRock AS
*/
/**
* This class defines a Directory Server entry cache that uses a FIFO to keep
* track of the entries. Entries that have been in the cache the longest are
* the most likely candidates for purging if space is needed. In contrast to
* other cache structures, the selection of entries to purge is not based on
* how frequently or recently the entries have been accessed. This requires
* significantly less locking (it will only be required when an entry is added
* or removed from the cache, rather than each time an entry is accessed).
* <BR><BR>
* Cache sizing is based on the percentage of free memory within the JVM, such
* that if enough memory is free, then adding an entry to the cache will not
* require purging, but if more than a specified percentage of the available
* memory within the JVM is already consumed, then one or more entries will need
* to be removed in order to make room for a new entry. It is also possible to
* configure a maximum number of entries for the cache. If this is specified,
* then the number of entries will not be allowed to exceed this value, but it
* may not be possible to hold this many entries if the available memory fills
* up first.
* <BR><BR>
* Other configurable parameters for this cache include the maximum length of
* time to block while waiting to acquire a lock, and a set of filters that may
* be used to define criteria for determining which entries are stored in the
* cache. If a filter list is provided, then only entries matching at least one
* of the given filters will be stored in the cache.
*/
public class FIFOEntryCache
extends EntryCache <FIFOEntryCacheCfg>
implements ConfigurationChangeListener<FIFOEntryCacheCfg>
{
/**
* The tracer object for the debug logger.
*/
/**
* The reference to the Java runtime used to determine the amount of memory
* currently in use.
*/
// The mapping between DNs and entries.
// The lock used to provide threadsafe access when changing the contents of
// the cache.
// The maximum amount of memory in bytes that the JVM will be allowed to use
// before we need to start purging entries.
private long maxAllowedMemory;
// The maximum number of entries that may be held in the cache.
private long maxEntries;
// Currently registered configuration object.
// The maximum length of time to try to obtain a lock before giving
// up.
/**
* Creates a new instance of this FIFO entry cache.
*/
public FIFOEntryCache()
{
super();
// All initialization should be performed in the initializeEntryCache.
}
/**
* {@inheritDoc}
*/
public void initializeEntryCache(
)
{
configuration.addFIFOChangeListener (this);
// Initialize the cache structures.
// Initialize locks.
cacheLock = new ReentrantReadWriteLock(true);
// Read configuration and apply changes.
boolean applyChanges = true;
);
if (!errorMessages.isEmpty()) {
}
}
throw new ConfigException(message);
}
}
/**
* {@inheritDoc}
*/
public void finalizeEntryCache()
{
try {
// Release all memory currently in use by this cache.
try {
} catch (Exception e) {
// This should never happen.
if (debugEnabled()) {
}
}
} finally {
}
}
/**
* {@inheritDoc}
*/
{
return false;
}
// Indicate whether the DN map contains the specified DN.
try {
} finally {
}
}
/**
* {@inheritDoc}
*/
{
// Simply return the entry from the DN map.
try {
if (e == null) {
// Indicate cache miss.
return null;
} else {
// Indicate cache hit.
return e.getEntry();
}
} finally {
}
}
/**
* {@inheritDoc}
*/
{
// Simply return the ID from the DN map.
try {
if (e == null) {
return -1;
} else {
return e.getEntryID();
}
} finally {
}
}
/**
* {@inheritDoc}
*/
{
// Locate specific backend map and return the entry DN by ID.
try {
if (backendMap != null) {
if (e != null) {
return e.getDN();
}
}
return null;
} finally {
}
}
/**
* {@inheritDoc}
*/
{
// Create the cache entry based on the provided information.
// Obtain a lock on the cache. If this fails, then don't do anything.
try
{
{
return;
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
return;
}
// At this point, we hold the lock. No matter what, we must release the
// lock before leaving this method, so do that in a finally block.
try
{
// See if the current memory usage is within acceptable constraints. If
// so, then add the entry to the cache (or replace it if it is already
// present). If not, then remove an existing entry and don't add the new
// entry.
if (usedMemory > maxAllowedMemory)
{
if (cachedEntry == null)
{
// The current entry wasn't there, let's remove an existing entry.
{
if (m != null)
{
}
}
}
else
{
// Try to remove the entry from the ID list as well.
{
// If this backend becomes empty now remove it from the idMap map.
{
}
}
}
}
else
{
// Add the entry to the cache. This will replace it if it is already
// present and add it if it isn't.
{
}
else
{
}
// See if a cap has been placed on the maximum number of entries in the
// cache. If so, then see if we have exceeded it and we need to purge
// entries until we're within the limit.
{
{
if (m != null)
{
}
entryCount--;
}
}
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
}
finally
{
}
}
/**
* {@inheritDoc}
*/
{
// Create the cache entry based on the provided information.
// Obtain a lock on the cache. If this fails, then don't do anything.
try
{
{
// We can't rule out the possibility of a conflict, so return false.
return false;
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
// We can't rule out the possibility of a conflict, so return false.
return false;
}
// At this point, we hold the lock. No matter what, we must release the
// lock before leaving this method, so do that in a finally block.
try
{
// See if the entry already exists in the cache. If it does, then we will
// fail and not actually store the entry.
{
return false;
}
// See if the current memory usage is within acceptable constraints. If
// so, then add the entry to the cache (or replace it if it is already
// present). If not, then remove an existing entry and don't add the new
// entry.
if (usedMemory > maxAllowedMemory)
{
{
if (m != null)
{
}
}
}
else
{
// Add the entry to the cache. This will replace it if it is already
// present and add it if it isn't.
{
}
else
{
}
// See if a cap has been placed on the maximum number of entries in the
// cache. If so, then see if we have exceeded it and we need to purge
// entries until we're within the limit.
{
{
if (m != null)
{
}
entryCount--;
}
}
}
// We'll always return true in this case, even if we didn't actually add
// the entry due to memory constraints.
return true;
}
catch (Exception e)
{
if (debugEnabled())
{
}
// We can't be sure there wasn't a conflict, so return false.
return false;
}
finally
{
}
}
/**
* {@inheritDoc}
*/
{
// Acquire the lock on the cache. We should not return until the entry is
// removed, so we will block until we can obtain the lock.
// FIXME -- An alternate approach could be to block for a maximum length of
// time and then if it fails then put it in a queue for processing by some
// other thread before it releases the lock.
// At this point, it is absolutely critical that we always release the lock
// before leaving this method, so do so in a finally block.
try
{
// Check the DN cache to see if the entry exists. If not, then don't do
// anything.
{
return;
}
// Try to remove the entry from the ID list as well.
{
// This should't happen, but the entry isn't cached in the ID map so
// we can return.
return;
}
// If this backend becomes empty now remove it from the idMap map.
{
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
// This shouldn't happen, but there's not much that we can do if it does.
}
finally
{
}
}
/**
* {@inheritDoc}
*/
public void clear()
{
// Acquire a lock on the cache. We should not return until the cache has
// been cleared, so we will block until we can obtain the lock.
// At this point, it is absolutely critical that we always release the lock
// before leaving this method, so do so in a finally block.
try
{
// Clear the DN cache.
// Clear the ID cache.
}
catch (Exception e)
{
if (debugEnabled())
{
}
// This shouldn't happen, but there's not much that we can do if it does.
}
finally
{
}
}
/**
* {@inheritDoc}
*/
{
// Acquire a lock on the cache. We should not return until the cache has
// been cleared, so we will block until we can obtain the lock.
// At this point, it is absolutely critical that we always release the lock
// before leaving this method, so do so in a finally block.
try
{
// Remove all references to entries for this backend from the ID cache.
{
// No entries were in the cache for this backend, so we can return
// without doing anything.
return;
}
// Unfortunately, there is no good way to dump the entries from the DN
// cache based on their backend, so we will need to iterate through the
// entries in the ID map and do it manually. Since this could take a
// while, we'll periodically release and re-acquire the lock in case
// anyone else is waiting on it so this doesn't become a stop-the-world
// event as far as the cache is concerned.
int entriesDeleted = 0;
{
{
}
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
// This shouldn't happen, but there's not much that we can do if it does.
}
finally
{
}
}
/**
* {@inheritDoc}
*/
{
// Determine which backend should be used for the provided base DN. If
// there is none, then we don't need to do anything.
{
return;
}
// Acquire a lock on the cache. We should not return until the cache has
// been cleared, so we will block until we can obtain the lock.
// At this point, it is absolutely critical that we always release the lock
// before leaving this method, so do so in a finally block.
try
{
}
catch (Exception e)
{
if (debugEnabled())
{
}
// This shouldn't happen, but there's not much that we can do if it does.
}
finally
{
}
}
/**
* Clears all entries at or below the specified base DN that are associated
* with the given backend. The caller must already hold the cache lock.
*
* @param baseDN The base DN below which all entries should be flushed.
* @param backend The backend for which to remove the appropriate entries.
*/
{
// See if there are any entries for the provided backend in the cache. If
// not, then return.
{
// No entries were in the cache for this backend, so we can return without
// doing anything.
return;
}
// Since the provided base DN could hold a subset of the information in the
// specified backend, we will have to do this by iterating through all the
// entries for that backend. Since this could take a while, we'll
// periodically release and re-acquire the lock in case anyone else is
// waiting on it so this doesn't become a stop-the-world event as far as the
// cache is concerned.
int entriesExamined = 0;
{
{
}
{
}
}
// See if the backend has any subordinate backends. If so, then process
// them recursively.
{
boolean isAppropriate = false;
{
{
isAppropriate = true;
break;
}
}
if (isAppropriate)
{
}
}
}
/**
* {@inheritDoc}
*/
public void handleLowMemory()
{
// Grab the lock on the cache and wait until we have it.
// At this point, it is absolutely critical that we always release the lock
// before leaving this method, so do so in a finally block.
try
{
// See how many entries are in the cache. If there are less than 1000,
// then we'll dump all of them. Otherwise, we'll dump 10% of the entries.
if (numEntries < 1000)
{
}
else
{
{
if (m != null)
{
}
numToDrop--;
}
}
}
catch (Exception e)
{
if (debugEnabled())
{
}
// This shouldn't happen, but there's not much that we can do if it does.
}
finally
{
}
}
/**
* {@inheritDoc}
*/
@Override()
{
}
/**
* {@inheritDoc}
*/
public boolean isConfigurationChangeAcceptable(
)
{
boolean applyChanges = false;
);
return errorHandler.getIsAcceptable();
}
/**
* {@inheritDoc}
*/
)
{
boolean applyChanges = true;
);
// Do not apply changes unless this cache is enabled.
if (configuration.isEnabled()) {
}
);
return changeResult;
}
/**
* Parses the provided configuration and configure the entry cache.
*
* @param configuration The new configuration containing the changes.
* @param applyChanges If true then take into account the new configuration.
* @param errorHandler An handler used to report errors.
*
* @return <CODE>true</CODE> if configuration is acceptable,
* or <CODE>false</CODE> otherwise.
*/
public boolean processEntryCacheConfig(
boolean applyChanges,
)
{
// Local variables to read configuration.
long newLockTimeout;
long newMaxEntries;
int newMaxMemoryPercent;
long newMaxAllowedMemory;
// Read configuration.
// Maximum memory the cache can use.
// Get include and exclude filters.
switch (errorHandler.getConfigPhase())
{
case PHASE_INIT:
case PHASE_ACCEPTABLE:
case PHASE_APPLY:
);
);
break;
}
{
}
return errorHandler.getIsAcceptable();
}
/**
* {@inheritDoc}
*/
{
try {
// If cache misses is maintained by default cache
// get it from there and if not point to itself.
null,
new Long(maxAllowedMemory),
);
} catch (Exception e) {
if (debugEnabled()) {
}
}
return attrs;
}
/**
* {@inheritDoc}
*/
{
}
/**
* {@inheritDoc}
*/
{
// Grab cache lock to prevent any modifications
// to the cache maps until a snapshot is taken.
try {
// Examining the real maps will hold the lock and can cause map
// modifications in case of any access order maps, make copies
// instead.
} finally {
}
// Check dnMap first.
}
// See if there is anything on idMap that isnt reflected on
// dnMap in case maps went out of sync.
}
}
}
}
}