All Implemented Interfaces:
RrdFileBackend

public class RrdSafeFileBackendextends RrdRandomAccessFileBackend
Backend which is used to store RRD data to ordinary files on the disk, using locking. This backend is SAFE: it locks the underlying RRD file during update/fetch operations, and caches only static parts of a RRD file in memory. Therefore, this backend is safe to be used when RRD files should be shared between several JVMs at the same time. However, this backend is a little bit slow since it does not use fast java.nio.* package (it's still based on the RandomAccessFile class).
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static class 
    Internal counters for tracking file locking operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final RrdSafeFileBackend.Counters
    Lock counts shared by every backend.
    private FileLock
    The lock held on the file, null when unlocked.

    Fields inherited from class RrdRandomAccessFileBackend

    rafile
    Modifier and Type
    Field
    Description
    protected final RandomAccessFile
    Random access file handle.

    Fields inherited from class RrdBackend

    BYTEORDER
    Modifier and Type
    Field
    Description
    protected static final ByteOrder
    All ByteBuffer usage should use this standard order.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RrdSafeFileBackend(String path, long lockWaitTime, long lockRetryPeriod)
    Creates RrdFileBackend object for the given file path, backed by RandomAccessFile object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Releases the exclusive file lock, if held, then closes the underlying file.
    static String
    Returns a human readable summary of the file locking counters.
    private void
    lockFile(long lockWaitTime, long lockRetryPeriod)
    Takes an exclusive lock, retrying until the wait ends.

    Methods inherited from class RrdRandomAccessFileBackend

    getCanonicalPath, getLength, read, setLength, write
    Modifier and Type
    Method
    Description
    Returns canonical path to the file on the disk.
    long
    Returns the number of RRD bytes in the underlying storage.
    void
    read(long offset, byte[] b)
    Reads a number of bytes from the RRD file on the disk
    protected void
    setLength(long length)
    Sets the number of bytes in the underlying RRD storage.
    protected void
    write(long offset, byte[] b)
    Writes bytes to the underlying RRD file on the disk

    Methods inherited from class RrdBackend

    done, getCharBuffer, getFactory, getPath, getUri, isCachingAllowed, isInstanceCreated, readAll, readDouble, readDouble, readInt, readLong, readShort, readString, rrdClose, writeDouble, writeDouble, writeDouble, writeInt, writeLong, writeShort, writeString, writeString
    Modifier and Type
    Method
    Description
    (package private) void
    Set the factory and reference for this backend.
    protected CharBuffer
    getCharBuffer(long offset, int size)
    Extract a CharBuffer from the backend, used by readString
    Get the factory for this backend.
    Returns path to the storage.
    Return the URI associated to this backend, using the factory to generate it from the path.
    protected boolean
    This method suggests the caching policy to the Rrd4j frontend (high-level) classes.
    (package private) static boolean
    Checks if a backend instance has been created.
    final byte[]
    Reads all RRD bytes from the underlying storage.
    protected double
    readDouble(long offset)
    Reads a double value from the underlying storage at the given offset.
    protected double[]
    readDouble(long offset, int count)
    Reads an array of double values from the underlying storage at the given offset.
    protected int
    readInt(long offset)
    Reads an integer value from the underlying storage at the given offset.
    protected long
    readLong(long offset)
    Reads a long value from the underlying storage at the given offset.
    protected short
    readShort(long offset)
    Reads a short value from the underlying storage at the given offset.
    protected final String
    readString(long offset)
    Reads a string from the underlying storage at the given offset.
    protected void
    Closes the underlying backend.
    protected void
    writeDouble(long offset, double value)
    Writes a double value to the underlying storage at the given offset.
    protected void
    writeDouble(long offset, double[] values)
    Writes an array of double values to the underlying storage at the given offset.
    protected void
    writeDouble(long offset, double value, int count)
    Writes multiple copies of a double value to the underlying storage at the given offset.
    protected void
    writeInt(long offset, int value)
    Writes an integer value to the underlying storage at the given offset.
    protected void
    writeLong(long offset, long value)
    Writes a long value to the underlying storage at the given offset.
    protected void
    writeShort(long offset, short value)
    Writes a short value to the underlying storage at the given offset.
    protected final void
    writeString(long offset, String value)
    Writes a string to the underlying storage at the given offset.
    protected void
    writeString(long offset, String value, int length)
    Writes a string to the underlying storage at the given offset with a fixed length.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • counters

      private static final RrdSafeFileBackend.Counters counters
      Lock counts shared by every backend.
    • lock

      private FileLock lock
      The lock held on the file, null when unlocked.
  • Constructor Details

    • RrdSafeFileBackend

      public RrdSafeFileBackend(String path, long lockWaitTime, long lockRetryPeriod) throws IOException
      Creates RrdFileBackend object for the given file path, backed by RandomAccessFile object.
      Parameters:
      path - Path to a file
      lockWaitTime - lock waiting time in milliseconds.
      lockRetryPeriod - lock retry period in milliseconds.
      Throws:
      IOException - Thrown in case of I/O error.
  • Method Details

    • lockFile

      private void lockFile(long lockWaitTime, long lockRetryPeriod) throws IOException
      Takes an exclusive lock, retrying until the wait ends.
      Parameters:
      lockWaitTime - ms to keep retrying
      lockRetryPeriod - ms between attempts
      Throws:
      IOException - if the lock cannot be taken
    • close

      public void close() throws IOException
      Releases the exclusive file lock, if held, then closes the underlying file.
      Overrides:
      close in class RrdRandomAccessFileBackend
      Throws:
      IOException - if the lock cannot be released or the file cannot be closed.
    • getLockInfo

      public static String getLockInfo()
      Returns a human readable summary of the file locking counters.
      Returns:
      a String object.