All Implemented Interfaces:
RrdFileBackend

public class RrdNioBackendextends ByteBufferBackend implements RrdFileBackend
Backend which is used to store RRD data to ordinary disk files using java.nio.* package. This is the default backend engine.

This version buffers all stat files in a single temporary buffer file sequentially, keeping track of each file's offset and length, then flushes all to the mapped file in batch.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Helper class representing a buffered stat file segment's location in the temp buffer file.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Data structure to track each buffered stat file's offset and length within the single temp buffer file.
    The mapped file buffer.
    private static final Method
    Method cleaning a mapped buffer without unsafe, the preferred path on Java 8.
    private static final Method
    Method invoked on the cleaner, old JDKs.
    private final FileChannel
    The channel of the rrd file.
    private static final Object
    Serializes flushes across every backend instance.
    private static final Method
    Method cleaning a mapped buffer via unsafe, for newer JDKs.
    private final boolean
    True if the file is opened read only.
    private ScheduledFuture<?>
    The running periodic sync task, if any.
    private FileChannel
    FileChannel to write and read stats data from the single temp buffer file.
    private Path
    Path to the single temporary file used to buffer all stat files data sequentially.
    private static final Object
    The unsafe object the cleaner runs on, if any.

    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
    Modifier
    Constructor
    Description
    protected
    RrdNioBackend(String path, boolean readOnly, RrdSyncThreadPool threadPool, int syncPeriod)
    Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bufferStatFile(ByteBuffer data, long targetFileOffset)
    Buffers one stat file's data into the single temp buffer file sequentially.
    void
    Closes the underlying RRD file.
    private void
    Closes and deletes the temp buffer file.
    void
    Flushes all buffered stat files sequentially from the temp buffer file into their designated positions in the mapped RRD file in one batch operation.
    Returns canonical path to the file on the disk.
    long
    Returns the number of RRD bytes in the underlying storage.
    private void
    mapFile(long length)
    Maps the file into memory.
    protected void
    setLength(long newLength)
    Sets the number of bytes in the underlying RRD storage.
    protected void
    Override sync to disable unbuffered flush.
    private void
    Cleans and releases the mapped buffer.

    Methods inherited from class ByteBufferBackend

    getCharBuffer, isDirty, read, readDouble, readDouble, readInt, readLong, readShort, rrdClose, setByteBuffer, write, writeDouble, writeDouble, writeDouble, writeInt, writeLong, writeShort, writeString
    Modifier and Type
    Method
    Description
    protected CharBuffer
    getCharBuffer(long offset, int size)
    Extract a CharBuffer from the backend, used by readString
    protected boolean
    Check whether the buffer has been modified since the last sync.
    protected void
    read(long offset, byte[] b)
    Reads a number of bytes from the RRD file on the disk
    double
    readDouble(long offset)
    Reads a double value from the underlying storage at the given offset.
    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 void
    Closes the underlying backend.
    protected void
    Set the byte buffer to use for backing storage.
    protected void
    write(long offset, byte[] b)
    Writes bytes to the underlying RRD file on the disk
    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 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 RrdBackend

    done, getFactory, getPath, getUri, isCachingAllowed, isInstanceCreated, readAll, readString, writeString
    Modifier and Type
    Method
    Description
    (package private) void
    Set the factory and reference for this backend.
    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 final String
    readString(long offset)
    Reads a string from 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.

    Methods inherited from class Object

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

    • cleanerMethod

      private static final Method cleanerMethod
      Method cleaning a mapped buffer without unsafe, the preferred path on Java 8.
    • cleanMethod

      private static final Method cleanMethod
      Method invoked on the cleaner, old JDKs.
    • invokeCleaner

      private static final Method invokeCleaner
      Method cleaning a mapped buffer via unsafe, for newer JDKs. The call is terminally deprecated and a live mapping never blocks the file on POSIX, so it is resolved only where a mapping does hold the file open, which is Windows.
    • unsafe

      private static final Object unsafe
      The unsafe object the cleaner runs on, if any.
    • byteBuffer

      private MappedByteBuffer byteBuffer
      The mapped file buffer.
    • file

      private final FileChannel file
      The channel of the rrd file.
    • readOnly

      private final boolean readOnly
      True if the file is opened read only.
    • syncRunnableHandle

      private ScheduledFuture<?> syncRunnableHandle
      The running periodic sync task, if any.
    • tempBufferFilePath

      private Path tempBufferFilePath
      Path to the single temporary file used to buffer all stat files data sequentially.
    • FLUSH_LOCK

      private static final Object FLUSH_LOCK
      Serializes flushes across every backend instance.

      All backends stage their pending segments in one shared scratch file, so two concurrent flushes corrupt each other: one truncates the file while the other is still mapping its segments out of it. The usual protection is a single-threaded sync pool, but rrd4j snapshots the pool size into a static singleton when the pool class initializes, so the setting only takes effect if it happens before anything else touches rrd4j - otherwise the pool keeps its default size and the invariant breaks with no error. Taking the lock here makes the invariant hold by construction rather than by ordering.

    • tempBufferFileChannel

      private FileChannel tempBufferFileChannel
      FileChannel to write and read stats data from the single temp buffer file.
    • bufferedStatSegments

      private final List<RrdNioBackend.StatFileSegment> bufferedStatSegments
      Data structure to track each buffered stat file's offset and length within the single temp buffer file.
  • Constructor Details

    • RrdNioBackend

      protected RrdNioBackend(String path, boolean readOnly, RrdSyncThreadPool threadPool, int syncPeriod) throws IOException
      Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
      Parameters:
      path - Path to a file
      readOnly - True, if file should be open in read-only mode. False otherwise
      threadPool - Sync thread pool; can be null
      syncPeriod - Sync period in seconds
      Throws:
      IOException - On I/O error
  • Method Details

    • mapFile

      private void mapFile(long length) throws IOException
      Maps the file into memory.
      Parameters:
      length - the length to map
      Throws:
      IOException - if the file cannot be mapped
    • unmapFile

      private void unmapFile()
      Cleans and releases the mapped buffer.
    • setLength

      protected void setLength(long newLength) throws IOException
      Sets the number of bytes in the underlying RRD storage. This method is called only once, immediately after a new RRD storage gets created. Sets length of the underlying RRD file. This method is called only once, immediately after new RRD creation.
      Specified by:
      setLength in class RrdBackend
      Parameters:
      newLength - Length of the underlying RRD storage in bytes.
      Throws:
      IllegalArgumentException - if the length is bigger than the possible mapping position (2GiB).
      IOException - Thrown in case of I/O error.
    • close

      public void close() throws IOException
      Closes the underlying RRD file. Also closes and deletes the temporary buffer file used for buffering.
      Overrides:
      close in class ByteBufferBackend
      Throws:
      IOException - On I/O error
    • closeTempResources

      private void closeTempResources()
      Closes and deletes the temp buffer file.
    • bufferStatFile

      public void bufferStatFile(ByteBuffer data, long targetFileOffset) throws IOException
      Buffers one stat file's data into the single temp buffer file sequentially. This method appends the data to the temp file and records the offset and length, including the target offset in the mapped file where this stat file belongs.
      Parameters:
      data - ByteBuffer containing the stat file's bytes to buffer
      targetFileOffset - Position in the mapped RRD file to write this stat file to on flush
      Throws:
      IOException - If writing to temp file fails
    • flushBufferedStatFiles

      public void flushBufferedStatFiles()
      Flushes all buffered stat files sequentially from the temp buffer file into their designated positions in the mapped RRD file in one batch operation. After flushing, the temp buffer file and index are cleared for the next batch.
    • sync

      protected void sync()
      Override sync to disable unbuffered flush.
    • getLength

      public long getLength() throws IOException
      Description copied from class: RrdBackend
      Returns the number of RRD bytes in the underlying storage.
      Specified by:
      getLength in class RrdBackend
      Returns:
      Number of RRD bytes in the storage.
      Throws:
      IOException - Thrown in case of I/O error.
    • getCanonicalPath

      public String getCanonicalPath()
      Description copied from interface: RrdFileBackend
      Returns canonical path to the file on the disk.
      Specified by:
      getCanonicalPath in interface RrdFileBackend
      Returns:
      Canonical file path