private void releaseRrd(RrdDb rrd, Def def) throws IOException { String backend = def.getBackend(); if (poolUsed && backend == null) { RrdDbPool.getInstance().release(rrd); } else { rrd.close(); } }
/** * @param rrdDb * @return * @throws IOException * @see org.rrd4j.core.RrdDbPool#getOpenCount(org.rrd4j.core.RrdDb) */ public static int getOpenCount(RrdDb rrdDb) throws IOException { return instance.getOpenCount(rrdDb); }
/** * Wait for a empty reference with no usage * @param uri * @return an reference with no usage * @throws IOException * @throws InterruptedException */ private RrdEntry waitEmpty(URI uri) throws IOException, InterruptedException { RrdEntry ref = getEntry(uri, true); try { while (ref.count != 0) { //Not empty, give it back, but wait for signal passNext(ACTION.SWAP, ref); ref.waitempty.await(); ref = getEntry(uri, true); } return ref; } catch (InterruptedException e) { passNext(ACTION.SWAP, ref); Thread.currentThread().interrupt(); throw e; } }
/** * <p>Requests a RrdDb reference for the given RRD file path.</p> * <ul> * <li>If the file is already open, previously returned RrdDb reference will be returned. Its usage count * will be incremented by one. * <li>If the file is not already open and the number of already open RRD files is less than * {@link #INITIAL_CAPACITY}, the file will be open and a new RrdDb reference will be returned. * If the file is not already open and the number of already open RRD files is equal to * {@link #INITIAL_CAPACITY}, the method blocks until some RRD file is closed. * </ul> * * @param uri {@link URI} to existing RRD file * @return reference for the give RRD file * @throws java.io.IOException Thrown in case of I/O error * @deprecated Use the {@link org.rrd4j.core.RrdDb.Builder} instead. */ @Deprecated public RrdDb requestRrdDb(URI uri) throws IOException { RrdBackendFactory factory = RrdBackendFactory.findFactory(uri); return requestRrdDb(uri, factory); }
private RrdDb getRrd(Def def) throws IOException { String path = def.getPath(), backend = def.getBackend(); if (poolUsed && backend == null) { return RrdDbPool.getInstance().requestRrdDb(path); } else if (backend != null) { return new RrdDb(path, true, RrdBackendFactory.getFactory(backend)); } else { return new RrdDb(path, true); } }
/** * Import an external rrd data, import definition must have been done using {@link #setExternalPath(String)} * or {@link #setImporter(DataImporter)} * * @throws IOException in case of I/O error. * @throws IllegalArgumentException if the builder settings were incomplete */ @SuppressWarnings("deprecation") public void doimport() throws IOException { if (rrdDef != null || (importer == null && externalPath == null)) { throw new IllegalArgumentException("Not an importing configuration"); } if (path == null && uri == null) { throw new IllegalArgumentException("No rrd destination given"); } URI rrdUri = buildUri(path, uri, factory); factory = checkFactory(rrdUri, factory); if (!factory.canStore(rrdUri)) { throw new IllegalArgumentException("Given a factory incompatible with the URI"); } try (DataImporter rrdImporter = resoleImporter(externalPath, importer)) { if (usePool) { RrdDb db = resolvePool(pool).requestRrdDb(rrdUri, factory, importer); resolvePool(pool).release(db); } else { try (RrdDb db = new RrdDb(path, rrdUri, null, rrdImporter, factory, null)) { } } } }
/** * Closes RRD. No further operations are allowed on this RrdDb object. * * @throws java.io.IOException Thrown in case of I/O related error. */ @SuppressWarnings("deprecation") public synchronized void close() throws IOException { if (pool != null) { pool.release(this); } else { internalClose(); } }
@Override public void start() { super.start(); boolean filebasedbackend = backendFactory instanceof RrdFileBackendFactory; if(usepool || filebasedbackend) { try { String backendName = backendFactory.getName(); RrdBackendFactory.setDefaultFactory(backendName); logger.trace(Util.delayedFormatString("Store backend set to %s", backendName)); } catch (IllegalStateException e) { logger.warn("Trying to change default backend, a restart is needed"); } catch (Exception e) { throw new RuntimeException("Failed to configure RrdDbStoreFactory:" + e.getMessage(), e); } try { instance = RrdDbPool.getInstance(); instance.setCapacity(dbPoolSize); usepool = true; } catch (Exception e) { logger.warn("Trying to change rrd pool size, a restart is needed"); } } }
private static RrdDbPool resolvePool(RrdDbPool pool) { return pool != null ? pool : RrdDbPool.getInstance(); }
private RrdDb requestRrdDb(RrdDb.Builder builder, URI uri, RrdBackendFactory backend) throws IOException { RrdEntry ref = null; uri = backend.getCanonicalUri(uri); try { ref = requestEmpty(uri); ref.rrdDb = builder.setPath(uri).setBackendFactory(backend).setPool(this).build(); return ref.rrdDb; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("request interrupted for new rrd " + uri, e); } catch (RuntimeException e) { passNext(ACTION.DROP, ref); ref = null; throw e; } finally { if (ref != null) { passNext(ACTION.SWAP, ref); } } }
public String[] getOpenFiles() { return usepool ? instance.getOpenFiles() : new String[] {}; }
} catch (InterruptedException | RuntimeException e) { passNext(ACTION.SWAP, ref); Thread.currentThread().interrupt(); throw e;
/** * <p>Requests a RrdDb reference for the given RRD file path.</p> * <ul> * <li>If the file is already open, previously returned RrdDb reference will be returned. Its usage count * will be incremented by one. * <li>If the file is not already open and the number of already open RRD files is less than * {@link #INITIAL_CAPACITY}, the file will be open and a new RrdDb reference will be returned. * If the file is not already open and the number of already open RRD files is equal to * {@link #INITIAL_CAPACITY}, the method blocks until some RRD file is closed. * </ul> * <p>The path is transformed internally to URI using the default factory, that is the reference that will * be used elsewhere.</p> * * @param path Path to existing RRD file * @return reference for the give RRD file * @throws java.io.IOException Thrown in case of I/O error * @deprecated Use the {@link org.rrd4j.core.RrdDb.Builder} instead. */ @Deprecated public RrdDb requestRrdDb(String path) throws IOException { return requestRrdDb(defaultFactory.getUri(path), defaultFactory); }
/** * @param db */ public final static void releaseRrd(RrdDb db) { try { long start = System.currentTimeMillis(); if(usepool) instance.release(db); else db.close(); long finish = System.currentTimeMillis(); waitTime.addAndGet(finish - start); lockCount.incrementAndGet(); } catch (Exception e) { logger.debug("Strange error " + e); } }
public static final void prepare(String backend, int dbPoolSize) { usepool = false; if(backend != null) { try { RrdBackendFactory.setDefaultFactory(backend); logger.trace(Util.delayedFormatString("Store backend set to %s", backend)); } catch (IllegalArgumentException e) { logger.fatal("Backend not configured: " + e.getMessage()); } catch (IllegalStateException e) { logger.warn("Trying to change default backend, a restart is needed"); } } StoreOpener.backend = RrdBackendFactory.getDefaultFactory(); if(StoreOpener.backend instanceof RrdFileBackendFactory && dbPoolSize != 0) { try { instance = RrdDbPool.getInstance(); instance.setCapacity(dbPoolSize); usepool = true; } catch (Exception e) { } } logger.debug(Util.delayedFormatString("Store backend used is %s", StoreOpener.backend.getName())); logger.debug(Util.delayedFormatString("use pool: %b %d", usepool, dbPoolSize)); }
pool = RrdDbPool.getInstance();
RrdDb requestRrdDb(RrdDef rrdDef, RrdBackendFactory backend) throws IOException { RrdEntry ref = null; try { URI uri = backend.getCanonicalUri(rrdDef.getUri()); ref = requestEmpty(uri); ref.rrdDb = RrdDb.getBuilder().setRrdDef(rrdDef).setBackendFactory(backend).setPool(this).build(); return ref.rrdDb; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("request interrupted for new rrdDef " + rrdDef.getPath(), e); } catch (RuntimeException e) { passNext(ACTION.DROP, ref); ref = null; throw e; } finally { if (ref != null) { passNext(ACTION.SWAP, ref); } } }
/** * Retrieves the RrdDb instance matching a specific RRD datasource name * (usually a file name) and using a specified RrdBackendFactory. * * @param rrdFile Name of the RRD datasource. * @return RrdDb instance of the datasource. * @throws IOException Thrown in case of I/O error. */ public final static RrdDb getRrd(String rrdFile) throws IOException { File f = new File(rrdFile); String cp = f.getCanonicalPath(); long start = System.currentTimeMillis(); RrdDb db; if(usepool) db = instance.requestRrdDb(cp); else db = new RrdDb(cp, backend); long finish = System.currentTimeMillis(); waitTime.addAndGet(finish - start); lockCount.incrementAndGet(); return db; }
/** * @param db */ public void releaseRrd(RrdDb db) { try { long start = System.currentTimeMillis(); if(usepool) instance.release(db); else db.close(); long finish = System.currentTimeMillis(); waitTime.addAndGet(finish - start); lockCount.incrementAndGet(); } catch (Exception e) { logger.debug("Strange error " + e); } }
/** * @param path * @return * @throws IOException * @see org.rrd4j.core.RrdDbPool#getOpenCount(java.lang.String) */ public static int getOpenCount(String path) throws IOException { return instance.getOpenCount(path); } }