Class BackgroundCleaner
- java.lang.Object
-
- org.apache.derby.impl.services.cache.BackgroundCleaner
-
- All Implemented Interfaces:
Serviceable
final class BackgroundCleaner extends java.lang.Object implements Serviceable
A background cleaner thatConcurrentCache
can use to cleanCacheable
s asynchronously in a background instead of synchronously in the user threads. It is normally used by the replacement algorithm in order to make dirtyCacheable
s clean and evictable in the future. When the background cleaner is asked to clean an item, it puts the item in a queue and requests to be serviced by aDaemonService
running in a separate thread.
-
-
Field Summary
Fields Modifier and Type Field Description private ConcurrentCache
cacheManager
The cache manager owning this cleaner.private int
clientNumber
Subscription number for thisServiceable
.private DaemonService
daemonService
The service thread which performs the clean operations.private java.util.concurrent.ArrayBlockingQueue<CacheEntry>
queue
A queue of cache entries that need to be cleaned.private java.util.concurrent.atomic.AtomicBoolean
scheduled
Flag which tells whether the cleaner has a still unprocessed job scheduled with the daemon service.private boolean
shrink
Flag which tells whether the cleaner should try to shrink the cache the next time it wakes up.-
Fields inherited from interface org.apache.derby.iapi.services.daemon.Serviceable
DONE, REQUEUE
-
-
Constructor Summary
Constructors Constructor Description BackgroundCleaner(ConcurrentCache cache, DaemonService daemon, int queueSize)
Create a background cleaner instance and subscribe it to a daemon service.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
performWork(ContextManager context)
Clean the first entry in the queue.private void
requestService()
Notify the daemon service that the cleaner needs to be serviced.(package private) boolean
scheduleClean(CacheEntry entry)
Try to schedule a clean operation in the background cleaner.(package private) void
scheduleShrink()
Request that the cleaner tries to shrink the cache the next time it wakes up.boolean
serviceASAP()
Indicate that we want to be serviced ASAP.boolean
serviceImmediately()
Indicate that we don't want the work to happen immediately in the user thread.(package private) void
unsubscribe()
Stop subscribing to the daemon service.
-
-
-
Field Detail
-
daemonService
private final DaemonService daemonService
The service thread which performs the clean operations.
-
clientNumber
private final int clientNumber
Subscription number for thisServiceable
.
-
scheduled
private final java.util.concurrent.atomic.AtomicBoolean scheduled
Flag which tells whether the cleaner has a still unprocessed job scheduled with the daemon service. If this flag istrue
, calls toserviceNow()
won't result in the cleaner being serviced.
-
queue
private final java.util.concurrent.ArrayBlockingQueue<CacheEntry> queue
A queue of cache entries that need to be cleaned.
-
shrink
private volatile boolean shrink
Flag which tells whether the cleaner should try to shrink the cache the next time it wakes up.
-
cacheManager
private final ConcurrentCache cacheManager
The cache manager owning this cleaner.
-
-
Constructor Detail
-
BackgroundCleaner
BackgroundCleaner(ConcurrentCache cache, DaemonService daemon, int queueSize)
Create a background cleaner instance and subscribe it to a daemon service.- Parameters:
cache
- the cache manager that owns the cleanerdaemon
- the daemon service which perfoms the workqueueSize
- the maximum number of entries to keep in the queue (must be greater than 0)
-
-
Method Detail
-
scheduleClean
boolean scheduleClean(CacheEntry entry)
Try to schedule a clean operation in the background cleaner.- Parameters:
entry
- the entry that needs to be cleaned- Returns:
true
if the entry has been scheduled for clean,false
if the background cleaner can't clean the entry (its queue is full)
-
scheduleShrink
void scheduleShrink()
Request that the cleaner tries to shrink the cache the next time it wakes up.
-
requestService
private void requestService()
Notify the daemon service that the cleaner needs to be serviced.
-
unsubscribe
void unsubscribe()
Stop subscribing to the daemon service.
-
performWork
public int performWork(ContextManager context) throws StandardException
Clean the first entry in the queue. If there is more work, re-request service from the daemon service.- Specified by:
performWork
in interfaceServiceable
- Parameters:
context
- ignored- Returns:
- status for the performed work (normally
Serviceable.DONE
) - Throws:
StandardException
- ifCacheable.clean()
fails
-
serviceASAP
public boolean serviceASAP()
Indicate that we want to be serviced ASAP.- Specified by:
serviceASAP
in interfaceServiceable
- Returns:
true
-
serviceImmediately
public boolean serviceImmediately()
Indicate that we don't want the work to happen immediately in the user thread.- Specified by:
serviceImmediately
in interfaceServiceable
- Returns:
false
-
-