Java Code Examples for org.agrona.concurrent.UnsafeBuffer#getIntVolatile()

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#getIntVolatile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the length of a frame from the header as a volatile read.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @return the value for the frame length.
 */
public static int frameLengthVolatile(final UnsafeBuffer buffer, final int termOffset)
{
    int frameLength = buffer.getIntVolatile(termOffset);

    if (ByteOrder.nativeOrder() != LITTLE_ENDIAN)
    {
        frameLength = Integer.reverseBytes(frameLength);
    }

    return frameLength;
}
 
Example 2
Source File: CommonContext.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Is a media driver active in the current mapped CnC buffer? If the driver is mid start then it will wait for
 * up to the driverTimeoutMs by checking for the cncVersion being set.
 *
 * @param driverTimeoutMs for the driver liveness check.
 * @param logger          for feedback as liveness checked.
 * @param cncByteBuffer   for the existing CnC file.
 * @return true if a driver is active or false if not.
 */
public static boolean isDriverActive(
    final long driverTimeoutMs, final Consumer<String> logger, final ByteBuffer cncByteBuffer)
{
    if (null == cncByteBuffer)
    {
        return false;
    }

    final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);

    final long startTimeMs = System.currentTimeMillis();
    int cncVersion;
    while (0 == (cncVersion = cncMetaDataBuffer.getIntVolatile(CncFileDescriptor.cncVersionOffset(0))))
    {
        if (System.currentTimeMillis() > (startTimeMs + driverTimeoutMs))
        {
            throw new DriverTimeoutException("CnC file is created but not initialised.");
        }

        sleep(1);
    }

    CncFileDescriptor.checkVersion(cncVersion);

    final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(
        CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));

    final long timestampMs = toDriverBuffer.consumerHeartbeatTime();
    final long nowMs = System.currentTimeMillis();
    final long timestampAgeMs = nowMs - timestampMs;

    logger.accept("INFO: Aeron toDriver consumer heartbeat is (ms): " + timestampAgeMs);

    return timestampAgeMs <= driverTimeoutMs;
}
 
Example 3
Source File: CommonContext.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Request a driver to run its termination hook.
 *
 * @param directory   for the driver.
 * @param tokenBuffer containing the optional token for the request.
 * @param tokenOffset within the tokenBuffer at which the token begins.
 * @param tokenLength of the token in the tokenBuffer.
 * @return true if request was sent or false if request could not be sent.
 */
public static boolean requestDriverTermination(
    final File directory,
    final DirectBuffer tokenBuffer,
    final int tokenOffset,
    final int tokenLength)
{
    final File cncFile = new File(directory, CncFileDescriptor.CNC_FILE);

    if (cncFile.exists() && cncFile.length() > 0)
    {
        final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "CnC file");
        try
        {
            final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
            final int cncVersion = cncMetaDataBuffer.getIntVolatile(cncVersionOffset(0));

            CncFileDescriptor.checkVersion(cncVersion);

            final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(
                CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
            final long clientId = toDriverBuffer.nextCorrelationId();

            final DriverProxy driverProxy = new DriverProxy(toDriverBuffer, clientId);

            return driverProxy.terminateDriver(tokenBuffer, tokenOffset, tokenLength);
        }
        finally
        {
            IoUtil.unmap(cncByteBuffer);
        }
    }

    return false;
}
 
Example 4
Source File: TermGapScanner.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Scan for gaps from the scanOffset up to a limit offset. Each gap will be reported to the {@link GapHandler}.
 *
 * @param termBuffer  to be scanned for a gap.
 * @param termId      of the current term buffer.
 * @param termOffset  at which to start scanning.
 * @param limitOffset at which to stop scanning.
 * @param handler     to call if a gap is found.
 * @return offset of last contiguous frame
 */
public static int scanForGap(
    final UnsafeBuffer termBuffer,
    final int termId,
    final int termOffset,
    final int limitOffset,
    final GapHandler handler)
{
    int offset = termOffset;
    do
    {
        final int frameLength = frameLengthVolatile(termBuffer, offset);
        if (frameLength <= 0)
        {
            break;
        }

        offset += align(frameLength, FRAME_ALIGNMENT);
    }
    while (offset < limitOffset);

    final int gapBeginOffset = offset;
    if (offset < limitOffset)
    {
        final int limit = limitOffset - ALIGNED_HEADER_LENGTH;
        while (offset < limit)
        {
            offset += FRAME_ALIGNMENT;

            if (0 != termBuffer.getIntVolatile(offset))
            {
                offset -= ALIGNED_HEADER_LENGTH;
                break;
            }
        }

        final int gapLength = (offset - gapBeginOffset) + ALIGNED_HEADER_LENGTH;
        handler.onGap(termId, gapBeginOffset, gapLength);
    }

    return gapBeginOffset;
}
 
Example 5
Source File: MarkFile.java    From agrona with Apache License 2.0 4 votes vote down vote up
public static MappedByteBuffer mapExistingMarkFile(
    final File markFile,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    final long startTimeMs = epochClock.time();
    final long deadlineMs = startTimeMs + timeoutMs;

    while (!markFile.exists() || markFile.length() <= 0)
    {
        if (epochClock.time() > deadlineMs)
        {
            throw new IllegalStateException("Mark file not created: " + markFile.getName());
        }

        sleep(16);
    }

    final MappedByteBuffer byteBuffer = waitForFileMapping(logger, markFile, deadlineMs, epochClock);
    final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);

    int version;
    while (0 == (version = buffer.getIntVolatile(versionFieldOffset)))
    {
        if (epochClock.time() > deadlineMs)
        {
            throw new IllegalStateException("Mark file is created but not initialised");
        }

        sleep(1);
    }

    versionCheck.accept(version);

    while (0 == buffer.getLongVolatile(timestampFieldOffset))
    {
        if (epochClock.time() > deadlineMs)
        {
            throw new IllegalStateException("No non zero timestamp detected");
        }

        sleep(1);
    }

    return byteBuffer;
}
 
Example 6
Source File: MarkFile.java    From agrona with Apache License 2.0 4 votes vote down vote up
public static MappedByteBuffer mapNewOrExistingMarkFile(
    final File markFile,
    final boolean shouldPreExist,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final long totalFileLength,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    MappedByteBuffer byteBuffer = null;

    try (FileChannel channel = FileChannel.open(markFile.toPath(), CREATE, READ, WRITE, SPARSE))
    {
        byteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, totalFileLength);
        final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);

        if (shouldPreExist)
        {
            final int version = buffer.getIntVolatile(versionFieldOffset);

            if (null != logger)
            {
                logger.accept("INFO: Mark file exists: " + markFile);
            }

            versionCheck.accept(version);

            final long timestampMs = buffer.getLongVolatile(timestampFieldOffset);
            final long nowMs = epochClock.time();
            final long timestampAgeMs = nowMs - timestampMs;

            if (null != logger)
            {
                logger.accept("INFO: heartbeat is (ms): " + timestampAgeMs);
            }

            if (timestampAgeMs < timeoutMs)
            {
                throw new IllegalStateException("Active Mark file detected");
            }
        }
    }
    catch (final Exception ex)
    {
        if (null != byteBuffer)
        {
            IoUtil.unmap(byteBuffer);
        }

        throw new RuntimeException(ex);
    }

    return byteBuffer;
}
 
Example 7
Source File: MarkFile.java    From agrona with Apache License 2.0 4 votes vote down vote up
public static boolean isActive(
    final MappedByteBuffer byteBuffer,
    final EpochClock epochClock,
    final long timeoutMs,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    if (null == byteBuffer)
    {
        return false;
    }

    final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);

    final long startTimeMs = epochClock.time();
    final long deadlineMs = startTimeMs + timeoutMs;
    int version;
    while (0 == (version = buffer.getIntVolatile(versionFieldOffset)))
    {
        if (epochClock.time() > deadlineMs)
        {
            throw new IllegalStateException("Mark file is created but not initialised");
        }

        sleep(1);
    }

    versionCheck.accept(version);

    final long timestampMs = buffer.getLongVolatile(timestampFieldOffset);
    final long nowMs = epochClock.time();
    final long timestampAgeMs = nowMs - timestampMs;

    if (null != logger)
    {
        logger.accept("INFO: heartbeat is (ms): " + timestampAgeMs);
    }

    return timestampAgeMs <= timeoutMs;
}
 
Example 8
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get whether the log is considered connected or not by the driver.
 *
 * @param metadataBuffer containing the meta data.
 * @return whether the log is considered connected or not by the driver.
 */
public static boolean isConnected(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getIntVolatile(LOG_IS_CONNECTED_OFFSET) == 1;
}
 
Example 9
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the count of active transports for the Image.
 *
 * @param metadataBuffer containing the meta data.
 * @return count of active transports.
 */
public static int activeTransportCount(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getIntVolatile(LOG_ACTIVE_TRANSPORT_COUNT);
}
 
Example 10
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the active term count used by the producer of this log. Consumers may have a different
 * active term count if they are running behind. The read is done with volatile semantics.
 *
 * @param metadataBuffer containing the meta data.
 * @return the value of the active term count used by the producer of this log.
 */
public static int activeTermCount(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getIntVolatile(LOG_ACTIVE_TERM_COUNT_OFFSET);
}