📄 rrddbpool.java
字号:
// *
// * It's that simple. When the reference is requested for the first time, RrdDbPool will open the RRD file
// * for you and make some internal note that the RRD file is used only once. When the reference
// * to the same file (same RRD file path) is requested for the second time, the same RrdDb
// * reference will be returned, and its usage count will be increased by one. When the
// * reference is released its usage count will be decremented by one.<p>
// *
// * When the reference count drops to zero, RrdDbPool will not close the underlying
// * RRD file immediatelly. Instead of it, it will be marked as 'eligible for closing'.
// * If someone request the same RRD file again (before it gets closed), the same
// * reference will be returned again.<p>
// *
// * RrdDbPool has a 'garbage collector' which runs in a separate, low-priority
// * thread and gets activated only when the number of RRD files kept in the
// * pool is too big (greater than number returned from {@link #getCapacity getCapacity()}).
// * Only RRD files with a reference count equal to zero
// * will be eligible for closing. Unreleased RrdDb references are never invalidated.
// * RrdDbPool object keeps track of the time when each RRD file
// * becomes eligible for closing so that the oldest RRD file gets closed first.<p>
// *
// * Initial RrdDbPool capacity is set to {@link #INITIAL_CAPACITY}. Use {@link #setCapacity(int)}
// * method to change it at any time.<p>
// *
// * <b>WARNING:</b>Never use close() method on the reference returned from the pool.
// * When the reference is no longer needed, return it to the pool with the
// * {@link #release(RrdDb) release()} method.<p>
// *
// * However, you are not forced to use RrdDbPool methods to obtain RrdDb references
// * to RRD files, 'ordinary' RrdDb constructors are still available. But RrdDbPool class
// * offers serious performance improvement especially in complex applications with many
// * threads and many simultaneously open RRD files.<p>
// *
// * The pool is thread-safe. Not that the {@link RrdDb} objects returned from the pool are
// * also thread-safe<p>
// *
// * You should know that each operating system has its own internal limit on the number
// * of simultaneously open files. The capacity of your RrdDbPool should be
// * reasonably smaller than the limit imposed by your operating system.<p>
// *
// * <b>WARNING:</b> The pool cannot be used to manipulate RrdDb objects
// * with {@link RrdBackend backends} different from default.<p>
// */
//public class RrdDbPool implements Runnable {
// static final String GC_THREAD_NAME = "RrdDbPool GC thread";
// static final String CLOSING_THREAD_NAME = "RrdDbPool closing thread";
// private static final boolean DEBUG = false;
//
// // singleton pattern
// private static RrdDbPool ourInstance;
// private boolean closingOnExit = true;
//
// private Thread shutdownHook = new Thread(CLOSING_THREAD_NAME) {
// public void run() {
// try {
// close();
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// }
// };
//
// /**
// * Constant to represent the maximum number of internally open RRD files
// * which still does not force garbage collector (the process which closes RRD files) to run.
// */
// public static final int INITIAL_CAPACITY = 500;
// private int capacity = INITIAL_CAPACITY, maxUsedCapacity;
// private boolean active = true;
//
// /**
// * Constant to represent the internal behaviour of the pool.
// * Defaults to <code>true</code> but can be changed at runtime. See
// * {@link #setLimitedCapacity(boolean)} for more information.
// */
// public static final boolean LIMITED_CAPACITY = false;
// private boolean limitedCapacity = LIMITED_CAPACITY;
//
// /**
// * Constant to represent the priority of the background thread which closes excessive RRD files
// * which are no longer in use.
// */
// public static final int GC_THREAD_PRIORITY = /** Thread.NORM_PRIORITY - */ 1;
//
// private HashMap<String, RrdEntry> rrdMap = new HashMap<String, RrdEntry>(INITIAL_CAPACITY);
// private LinkedHashMap<String, RrdEntry> rrdIdleMap = new LinkedHashMap<String, RrdEntry>(INITIAL_CAPACITY);
// private RrdBackendFactory factory;
// private int poolHitsCount = 0, poolRequestsCount = 0;
//
// /**
// * Returns an instance to RrdDbPool object. Only one such object may exist in each JVM.
// *
// * @return Instance to RrdDbPool object.
// */
// public synchronized static RrdDbPool getInstance() {
// if (ourInstance == null) {
// ourInstance = new RrdDbPool();
// ourInstance.startGarbageCollector();
// }
// return ourInstance;
// }
//
// private RrdDbPool() {
// setClosingOnExit(closingOnExit);
// }
//
// /**
// * Checks the exiting behaviour of RrdDbPool.
// * @return <code>True</code>, if all RRD files are to be closed
// * when application invokes <code>System.exit()</code>.
// * <code>False</code> otherwise. The default behaviour is <code>true</code>
// * (all RRD files will be closed on exit).
// */
// public synchronized boolean isClosingOnExit() {
// return closingOnExit;
// }
//
// /**
// * Sets the exiting behaviour of RrdDbPool.
// * @param closingOnExit <code>True</code>, if all RRD files are to be closed
// * when application invokes <code>System.exit()</code>.
// * <code>False</code> otherwise. The default behaviour is <code>true</code>
// * (all RRD files will be closed on exit).
// */
// public synchronized void setClosingOnExit(boolean closingOnExit) {
// Runtime runtime = Runtime.getRuntime();
// runtime.removeShutdownHook(shutdownHook);
// if(closingOnExit) {
// runtime.addShutdownHook(shutdownHook);
// }
// this.closingOnExit = closingOnExit;
// }
//
// private void startGarbageCollector() {
// Thread gcThread = new Thread(this, GC_THREAD_NAME);
// gcThread.setPriority(GC_THREAD_PRIORITY);
// gcThread.setDaemon(true);
// gcThread.start();
// }
//
// /**
// * Returns a reference to an existing RRD file with the specified path.
// * If the file is already open in the pool, existing reference to it will be returned.
// * Otherwise, the file is open and a newly created reference to it is returned.
// *
// * @param path Relative or absolute path to a RRD file.
// * @return Reference to a RrdDb object (RRD file).
// * @throws IOException Thrown in case of I/O error.
// * @throws RrdException Thrown in case of JRobin specific error.
// */
// public synchronized RrdDb requestRrdDb(String path) throws IOException, RrdException {
// proveActive();
// poolRequestsCount++;
// String canonicalPath = getCanonicalPath(path);
// for(;;) {
// RrdEntry rrdEntry = rrdMap.get(canonicalPath);
// if (rrdEntry != null) {
// // already open, use it!
// reportUsage(canonicalPath, rrdEntry);
// poolHitsCount++;
//// debug("CACHED: " + rrdEntry.dump());
// return rrdEntry.getRrdDb();
// }
// else if(!limitedCapacity || rrdMap.size() < capacity) {
// // not found, open it
// RrdDb rrdDb = createRrdDb(path, null);
// rrdEntry = new RrdEntry(rrdDb);
// addRrdEntry(canonicalPath, rrdEntry);
//// debug("ADDED: " + rrdEntry.dump());
// return rrdDb;
// }
// else {
// // we have to wait
// try {
// wait();
// }
// catch (InterruptedException e) {
// throw new RrdException("Request for file '" + path + "' was interrupted");
// }
// }
// }
// }
//
// /**
// * Returns a reference to a new RRD file. The new file will have the specified
// * relative or absolute path, and its contents will be provided from the specified
// * XML file (RRDTool comaptible).
// *
// * @param path Relative or absolute path to a new RRD file.
// * @param xmlPath Relative or absolute path to an existing XML dump file (RRDTool comaptible)
// * @return Reference to a RrdDb object (RRD file).
// * @throws IOException Thrown in case of I/O error.
// * @throws RrdException Thrown in case of JRobin specific error.
// */
// public synchronized RrdDb requestRrdDb(String path, String xmlPath)
// throws IOException, RrdException {
// return requestNewRrdDb(path, xmlPath);
// }
//
// /**
// * Returns a reference to a new RRD file. The new file will be created based on the
// * definition contained in a RrdDef object.
// *
// * @param rrdDef RRD definition object
// * @return Reference to a RrdDb object (RRD file).
// * @throws IOException Thrown in case of I/O error.
// * @throws RrdException Thrown in case of JRobin specific error.
// */
// public synchronized RrdDb requestRrdDb(RrdDef rrdDef) throws IOException, RrdException {
// return requestNewRrdDb(rrdDef.getPath(), rrdDef);
// }
//
// private RrdDb requestNewRrdDb(String path, Object creationDef) throws IOException, RrdException {
// proveActive();
// poolRequestsCount++;
// String canonicalPath = getCanonicalPath(path);
// for(;;) {
// RrdEntry rrdEntry = rrdMap.get(canonicalPath);
// if(rrdEntry != null) {
// // already open
// removeIfIdle(canonicalPath, rrdEntry);
// }
// else if(!limitedCapacity || rrdMap.size() < capacity) {
// RrdDb rrdDb = createRrdDb(path, creationDef);
// RrdEntry newRrdEntry = new RrdEntry(rrdDb);
// addRrdEntry(canonicalPath, newRrdEntry);
//// debug("ADDED: " + newRrdEntry.dump());
// return rrdDb;
// }
// else {
// // we have to wait
// try {
// wait();
// }
// catch (InterruptedException e) {
// throw new RrdException("Request for file '" + path + "' was interrupted");
// }
// }
// }
// }
//
// private RrdDb createRrdDb(String path, Object creationDef) throws RrdException, IOException {
// if(creationDef == null) {
// // existing RRD
// return new RrdDb(path, getFactory());
// }
// else if(creationDef instanceof String) {
// // XML input
// return new RrdDb(path, (String) creationDef, getFactory());
// }
// else if(creationDef instanceof RrdDef) {
// // RrdDef
// return new RrdDb((RrdDef) creationDef, getFactory());
// }
// else {
// throw new RrdException("Unexpected input object type: " +
// creationDef.getClass().getName());
// }
// }
//
// private void reportUsage(String canonicalPath, RrdEntry rrdEntry) {
// if (rrdEntry.reportUsage() == 1) {
// // must not be garbage collected
// rrdIdleMap.remove(canonicalPath);
// }
// }
//
// private void reportRelease(String canonicalPath, RrdEntry rrdEntry) {
// if (rrdEntry.reportRelease() == 0) {
// // ready to be garbage collected
// rrdIdleMap.put(canonicalPath, rrdEntry);
// }
// }
//
// private void addRrdEntry(String canonicalPath, RrdEntry newRrdEntry) {
// rrdMap.put(canonicalPath, newRrdEntry);
// maxUsedCapacity = Math.max(rrdMap.size(), maxUsedCapacity);
// // notify waiting threads
// notifyAll();
// }
//
// private void removeIfIdle(String canonicalPath, RrdEntry rrdEntry)
// throws RrdException, IOException {
// // already open, check if active (not released)
// if (rrdEntry.isInUse()) {
// // not released, not allowed here
// throw new RrdException("Cannot create new RrdDb file: " +
// "File '" + canonicalPath + "' already in use");
// } else {
// // open but released... safe to close it
//// debug("WILL BE RECREATED: " + rrdEntry.dump());
// removeRrdEntry(canonicalPath, rrdEntry);
// }
// }
//
// private void removeRrdEntry(String canonicalPath, RrdEntry rrdEntry) throws IOException {
// rrdEntry.closeRrdDb();
// rrdMap.remove(canonicalPath);
// rrdIdleMap.remove(canonicalPath);
//// debug("REMOVED: " + rrdEntry.dump());
// }
//
// /**
// * Method used to report that the reference to a RRD file is no longer needed. File that
// * is no longer needed (all references to it are released) is marked 'eligible for
// * closing'. It will be eventually closed by the pool when the number of open RRD files
// * becomes too big. Most recently released files will be closed last.
// *
// * @param rrdDb Reference to RRD file that is no longer needed.
// * @throws IOException Thrown in case of I/O error.
// * @throws RrdException Thrown in case of JRobin specific error.
// */
// public synchronized void release(RrdDb rrdDb) throws IOException, RrdException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -