java.lang.Object
org.rrd4j.core.RrdDbPool
This class should be used to synchronize access to RRD files in a multithreaded environment. This class should be also used to prevent opening of too many RRD files at the same time (thus avoiding operating system limits).
It can also be used a factory for RrdDb, using a default backend factory.
In case of interruptions, it throws IllegalStateException.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumWhat to do with a freed pool slot.private static classThrown when the pool is at capacity.private static classHolder for the one pool instance.private static classOne pooled database with its usage state.Field Summary
Modifier and TypeFieldDescriptionprivate RrdBackendFactoryFactory used for URIs without a scheme.private final ConditionCondition awaited when the pool is full.static final intInitial capacity of the pool i.e.private intEntries the pool may hold at once.private final ConcurrentMap<URI, RrdDbPool.RrdEntry> Pooled entries, by canonical URI.private SemaphorePermits limiting concurrent pool use.private final ReentrantReadWriteLock.ReadLockRead lock for normal pool use.private final ReentrantReadWriteLock.WriteLockWrite lock for capacity changes.private final AtomicBooleanTrue while a thread waits for pool capacity.Constructor Summary
ConstructorsConstructorDescriptionConstructor for RrdDbPool.RrdDbPool(RrdBackendFactory defaultFactory) Constructor for RrdDbPool.Method Summary
Modifier and TypeMethodDescriptionprivate RrdBackendFactorycheckFactory(URI uri) Resolves the backend factory for a URI.private intgetCanonicalUriUsage(URI uri) Counts entries sharing a canonical path.intReturns the maximum number of simultaneously open RRD.private RrdDbPool.RrdEntryFinds or creates the pooled entry for a URI.static RrdDbPoolCreates a single instance of the class on the first call, or returns already existing one.intgetOpenCount(String path) Returns the number of usage for a RRD.intgetOpenCount(URI uri) Returns the number of usage for a RRD.intgetOpenCount(RrdDb rrdDb) Returns the number of usage for a RRD.intReturns the number of open RRD.String[]Returns an array of open RRD.URI[]Returns an array of open RRD URI.java.util.stream.Stream<URI> Returns an stream open RRD.Wait until the pool is empty and return a lock that prevent any additions of new RrdDb references until it's released.private voidHands the freed slot to the next waiting request.private RrdDbPool.RrdEntryrequestEmpty(URI uri) Got an empty reference, use it only if slots are available But don't hold any lock waiting for itrequestRrdDb(String path) Requests a RrdDb reference for the given RRD path.requestRrdDb(String path, String sourcePath) Requests a RrdDb reference for the given path.requestRrdDb(URI uri) Requests a RrdDb reference for the given RRD URI.requestRrdDb(URI uri, String sourcePath) Requests a RrdDb reference for the given URI.(package private) RrdDbrequestRrdDb(URI uri, RrdBackendFactory factory) Request RRD(package private) RrdDbrequestRrdDb(URI uri, RrdBackendFactory factory, DataImporter importer) Request RRD with importprivate RrdDbrequestRrdDb(RrdDb.Builder builder, URI uri, RrdBackendFactory factory) Opens or creates the database for a request.requestRrdDb(RrdDef rrdDef) Requests a RrdDb reference for the given RRD definition object.(package private) RrdDbrequestRrdDb(RrdDef rrdDef, RrdBackendFactory factory) Request RRD from definitionvoidsetCapacity(int newCapacity) Sets the maximum number of simultaneously open RRD.private RrdDbPool.RrdEntryWait for a empty reference with no usage
Field Details
INITIAL_CAPACITY
public static final int INITIAL_CAPACITYInitial capacity of the pool i.e. maximum number of simultaneously open RRD. The pool will never open too many RRD at the same time.- See Also:
maxCapacity
private int maxCapacityEntries the pool may hold at once.usage
Permits limiting concurrent pool use.usageWLock
Write lock for capacity changes.usageRLock
Read lock for normal pool use.fullCondition
Condition awaited when the pool is full.waitFull
True while a thread waits for pool capacity.pool
Pooled entries, by canonical URI.defaultFactory
Factory used for URIs without a scheme.
Constructor Details
RrdDbPool
public RrdDbPool()Constructor for RrdDbPool. It will use the default backend factory.RrdDbPool
Constructor for RrdDbPool.- Parameters:
defaultFactory- the default factory used when given a simple path of a RRD.
Method Details
getInstance
Creates a single instance of the class on the first call, or returns already existing one. Uses Initialization On Demand Holder idiom.- Returns:
- Single instance of this class
getOpenFileCount
public int getOpenFileCount()Returns the number of open RRD.- Returns:
- Number of currently open RRD held in the pool.
getOpenUri
getOpenUriStream
Returns an stream open RRD.- Returns:
- Stream with canonical URI to open RRD path held in the pool.
getOpenFiles
Returns an array of open RRD.- Returns:
- Array with canonical path to open RRD path held in the pool.
getEntry
Finds or creates the pooled entry for a URI.- Parameters:
uri- the canonical URIcancreate- true to create a missing entry- Returns:
- the entry, or null if not found
- Throws:
InterruptedException- if interrupted while waiting
passNext
Hands the freed slot to the next waiting request.- Parameters:
a- the action chosen for the waitere- the entry being passed, may be null
requestRrdDb
Requests a RrdDb reference for the given RRD path.- If the RRD is already open, previously returned RrdDb reference will be returned. Its usage count will be incremented by one.
- If the RRD is not already open and the number of already open RRD is less than
getCapacity(), it will be opened and a new RrdDb reference will be returned. If the RRD is not already open and the number of already open RRD is equal togetCapacity(), the method blocks until some RRD are closed.
The path is transformed to an URI using the default factory defined at the creation of the pool.
- Parameters:
path- Path to existing RRD.- Returns:
- reference for the given RRD.
- Throws:
IOException- Thrown in case of I/O error
requestRrdDb
Requests a RrdDb reference for the given RRD URI.- If the RRD is already open, previously returned RrdDb reference will be returned. Its usage count will be incremented by one.
- If the RRD is not already open and the number of already open RRD is less than
getCapacity(), it will be opened and a new RrdDb reference will be returned. If the RRD is not already open and the number of already open RRD is equal togetCapacity(), the method blocks until some RRD are closed.
If the default backend factory for the pool can handle this URI, it will be used, or else
RrdBackendFactory.findFactory(URI)will be used to find the backend factory used.- Parameters:
uri-URIto existing RRD file- Returns:
- reference for the give RRD file
- Throws:
IOException- Thrown in case of I/O error
waitEmpty
Wait for a empty reference with no usage- Parameters:
uri- the canonical storage URI of the RRD to wait for- Returns:
- an reference with no usage
- Throws:
InterruptedException- if the thread is interrupted while waiting
requestEmpty
Got an empty reference, use it only if slots are available But don't hold any lock waiting for it- Parameters:
uri- the canonical storage URI of the RRD to wait for- Returns:
- an reference with no usage
- Throws:
InterruptedException- if the thread is interrupted while waiting
requestRrdDb
Request RRD- Parameters:
uri- the canonical storage URI of the RRD to openfactory- the backend factory that opens the storage- Returns:
- the pooled RrdDb, whose use count this call has incremented
- Throws:
IOException- if the storage cannot be opened
requestRrdDb
Request RRD from definition- Parameters:
rrdDef- the definition of the RRD to create, which also supplies the canonical URIfactory- the backend factory that opens the storage- Returns:
- the newly created pooled RrdDb, with a use count of one
- Throws:
IOException- if the storage cannot be created
requestRrdDb
private RrdDb requestRrdDb(RrdDb.Builder builder, URI uri, RrdBackendFactory factory) throws IOException Opens or creates the database for a request.- Parameters:
builder- the database builderuri- the canonical URIfactory- the backend factory- Returns:
- the open database
- Throws:
IOException- if the database cannot be opened
requestRrdDb
Request RRD with import- Parameters:
uri- the canonical storage URI of the RRD to createfactory- the backend factory that opens the storageimporter- the data importer run over the new RRD while it is still unpooled- Returns:
- the newly created pooled RrdDb, with a use count of one
- Throws:
IOException- if the storage cannot be created
requestRrdDb
Requests a RrdDb reference for the given RRD definition object.- If the RRD with the path specified in the RrdDef object is already open, the method blocks until the file is closed.
- If the RRD is not already open and the number of already open RRD is less than
getCapacity(), a new RRD will be created and it's RrdDb reference will be returned. If the RRD is not already open and the number of already open RRD is equal togetCapacity(), the method blocks until some RrdDb references are closed.
If the factory defined when creating the pool can handle the URI, it will be used, or else
RrdBackendFactory.findFactory(URI)will be used.- Parameters:
rrdDef- Definition of the RRD file to be created.- Returns:
- Reference to the newly created RRD file.
- Throws:
IOException- Thrown in case of I/O errorIllegalStateException- if the thread was interrupted
requestRrdDb
Requests a RrdDb reference for the given path. The RRD will be created from external data (from XML dump or RRDTool's binary RRD file).- If the RRD with the path specified is already open, the method blocks until the file is closed.
- If the RRD is not already open and the number of already open RRD is less than
getCapacity(), a new RRD will be created and it's RrdDb reference will be returned. If the RRD is not already open and the number of already open RRD is equal togetCapacity(), the method blocks until some RrdDb references are closed.
The path is transformed to an URI using the default factory of the pool.
- Parameters:
path- Path to the RRD that should be created.sourcePath- Path to external data which is to be converted to Rrd4j's native RRD file format.- Returns:
- Reference to the newly created RRD.
- Throws:
IOException- Thrown in case of I/O errorIllegalStateException- if the thread was interrupted
requestRrdDb
Requests a RrdDb reference for the given URI. The RRD will be created from external data (from XML dump or RRDTool's binary RRD file).- If the RRD with the URI specified is already open, the method blocks until the file is closed.
- If the RRD is not already open and the number of already open RRD is less than
getCapacity(), a new RRD will be created and it's RrdDb reference will be returned. If the RRD is not already open and the number of already open RRD is equal togetCapacity(), the method blocks until some RrdDb references are closed.
RrdBackendFactory.findFactory(URI)will be used to choose the factory.- Parameters:
uri- URI to the RRD that should be createdsourcePath- Path to external data which is to be converted to Rrd4j's native RRD file format- Returns:
- Reference to the newly created RRD
- Throws:
IOException- Thrown in case of I/O errorIllegalStateException- if the thread was interrupted
setCapacity
public void setCapacity(int newCapacity) Sets the maximum number of simultaneously open RRD.- Parameters:
newCapacity- Maximum number of simultaneously open RRD.- Throws:
IllegalStateException- if called while the pool is not empty or the thread was interrupted.
getCapacity
public int getCapacity()Returns the maximum number of simultaneously open RRD.- Returns:
- maximum number of simultaneously open RRD
- Throws:
IllegalStateException- if the thread was interrupted
getOpenCount
Returns the number of usage for a RRD.- Parameters:
rrdDb- RrdDb reference for which informations is needed.- Returns:
- the number of request for this RRD.
- Throws:
IllegalStateException- if the thread was interrupted
getOpenCount
Returns the number of usage for a RRD.The path is transformed to an URI using the default factory.
- Parameters:
path- RRD's path for which informations is needed.- Returns:
- the number of request for this RRD.
- Throws:
IllegalStateException- if the thread was interrupted
getOpenCount
Returns the number of usage for a RRD.- Parameters:
uri- RRD's URI for which informations is needed.- Returns:
- the number of request for this RRD.
- Throws:
IllegalStateException- if the thread was interrupted
getCanonicalUriUsage
Counts entries sharing a canonical path.- Parameters:
uri- the URI to count- Returns:
- the number of users
lockEmpty
Wait until the pool is empty and return a lock that prevent any additions of new RrdDb references until it's released.- Parameters:
timeout- the time to wait for the write lockunit- the time unit of the timeout argument- Returns:
- a lock to release when operations on this pool are finished.
- Throws:
InterruptedException- if interrupted whole waiting for the lock
checkFactory
Resolves the backend factory for a URI.- Parameters:
uri- the URI to resolve- Returns:
- the factory to use