/*
* 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 2008 Sun Microsystems, Inc.
*/
/**
* This class defines a utility that will be used to pre-load the Directory
* Server entry cache. Pre-loader is multi-threaded and consist of the
* following threads:
*
* - The Arbiter thread which monitors overall pre-load progress and manages
* pre-load worker threads by adding or removing them as deemed necessary.
*
* - The Collector thread which collects all entries stored within the
* backend and places them to a blocking queue workers consume from.
*
* - Worker threads which are responsible for monitoring the collector feed
* and processing the actual entries for cache storage.
*
* This implementation is self-adjusting to any system workload and does not
* require any configuration parameters to optimize for initial system
*/
class EntryCachePreloader
{
/**
* The tracer object for the debug logger.
*/
/**
* BackendImpl object.
*/
/**
* Interrupt flag for the arbiter to terminate worker threads.
*/
/**
* Processed entries counter.
*/
/**
* Progress report resolution.
*/
/**
* Default resolution time.
*/
public static final long
/**
* Effective synchronization time.
*/
private static long syncSleepTime;
/**
* Default queue capacity.
*/
public static final int
/**
* Effective queue capacity.
*/
private static int queueCapacity;
/**
* Worker threads.
*/
new LinkedList<Thread>());
/**
* Collector thread.
*/
new EntryCacheCollector();
/**
* This queue is for workers to take from.
*/
/**
* The number of bytes in a megabyte.
*/
/**
* Constructs the Entry Cache Pre-loader for
* a given JEB implementation instance.
*
* @param jeb The JEB instance to pre-load.
*/
// These should not be exposed as configuration
// parameters and are only useful for testing.
"org.opends.server.entrycache.preload.sleep",
"org.opends.server.entrycache.preload.queue",
}
/**
* The Arbiter thread.
*/
protected void preload()
{
// Start collector thread first.
// Kick off a single worker.
new EntryCachePreloadWorker();
// Progress report timer task.
// Persistent state restore progress report.
public void run() {
long freeMemory =
}
}
};
// Cycle to monitor progress and adjust workers.
long processedEntriesCycle = 0;
long processedEntriesDelta = 0;
long processedEntriesDeltaLow = 0;
long processedEntriesDeltaHigh = 0;
long lastKnownProcessedEntries = 0;
try {
// Spawn another worker if scaling up.
new EntryCachePreloadWorker();
}
// Interrupt random worker if scaling down.
// Leave at least one worker to progress.
interruptFlag.set(true);
}
}
}
// Join the collector.
}
// Join all spawned workers.
if (workerThread.isAlive()) {
workerThread.join();
}
}
// Cancel progress report task and report done.
} catch (InterruptedException ex) {
if (debugEnabled()) {
}
// Interrupt the collector.
// Interrupt all preload threads.
}
jeb.getBackendID()));
} finally {
// Kill the timer task.
}
}
/**
* The worker thread.
*/
public EntryCachePreloadWorker() {
super("Entry Cache Preload Worker");
}
public void run() {
// Check if interrupted.
if (Thread.interrupted()) {
return;
}
// Check for scaling down interruption.
if (interruptFlag.compareAndSet(true, false)) {
break;
}
// Dequeue the next entry.
try {
if (preloadEntry == null) {
continue;
}
long entryID =
try {
// Even if the entry does not end up in the cache its still
// treated as a processed entry anyways.
if (debugEnabled()) {
}
}
break;
}
}
}
}
/**
* The Collector thread.
*/
public EntryCacheCollector() {
super("Entry Cache Preload Collector");
}
public void run() {
try {
// Check if interrupted.
if (Thread.interrupted()) {
return;
}
try {
if (ecIterator.hasNext()) {
} else {
break;
}
} else {
continue;
}
}
// BUG cursor might be null ? If not why testing below ?
// Reset cursor and continue.
try {
} catch (DatabaseException de) {
if (debugEnabled()) {
}
}
continue;
}
} else {
continue;
}
} catch (InterruptedException e) {
return;
} catch (Exception e) {
if (debugEnabled()) {
}
}
}
} finally {
// Always close cursor.
try {
} catch (DatabaseException de) {
if (debugEnabled()) {
}
}
}
}
}
}
/**
* This inner class represents pre-load entry object.
*/
private class PreloadEntry {
// Encoded Entry.
public byte[] entryBytes;
// Encoded EntryID.
public byte[] entryIDBytes;
/**
* Default constructor.
*/
this.entryBytes = entryBytes;
this.entryIDBytes = entryIDBytes;
}
}
}