org.agrona.concurrent.EpochClock Java Examples

The following examples show how to use org.agrona.concurrent.EpochClock. 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: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void checksumRecording(
    final PrintStream out,
    final File archiveDir,
    final long recordingId,
    final boolean allFiles,
    final Checksum checksum,
    final EpochClock epochClock)
{
    try (Catalog catalog = openCatalogReadOnly(archiveDir, epochClock))
    {
        final CatalogEntryProcessor catalogEntryProcessor =
            (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) ->
            {
                final ByteBuffer buffer = ByteBuffer.allocateDirect(
                    align(descriptorDecoder.mtuLength(), CACHE_LINE_LENGTH));
                buffer.order(LITTLE_ENDIAN);
                checksum(buffer, out, archiveDir, allFiles, checksum, descriptorDecoder);
            };

        if (!catalog.forEntry(recordingId, catalogEntryProcessor))
        {
            throw new AeronException("no recording found with recordingId: " + recordingId);
        }
    }
}
 
Example #2
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static CatalogEntryProcessor createVerifyEntryProcessor(
    final PrintStream out,
    final File archiveDir,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final MutableInteger errorCount,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
    buffer.order(LITTLE_ENDIAN);
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);

    return (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(
        out,
        archiveDir,
        options,
        checksum,
        epochClock,
        errorCount,
        truncateFileOnPageStraddle,
        headerFlyweight,
        headerEncoder,
        descriptorEncoder,
        descriptorDecoder);
}
 
Example #3
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
static boolean verifyRecording(
    final PrintStream out,
    final File archiveDir,
    final long recordingId,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    try (Catalog catalog = openCatalog(archiveDir, epochClock))
    {
        final MutableInteger errorCount = new MutableInteger();
        if (!catalog.forEntry(recordingId, createVerifyEntryProcessor(
            out, archiveDir, options, checksum, epochClock, errorCount, truncateFileOnPageStraddle)))
        {
            throw new AeronException("no recording found with recordingId: " + recordingId);
        }

        return errorCount.get() == 0;
    }
}
 
Example #4
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
static boolean verify(
    final PrintStream out,
    final File archiveDir,
    final Set<VerifyOption> options,
    final Checksum checksum,
    final EpochClock epochClock,
    final ActionConfirmation<File> truncateFileOnPageStraddle)
{
    try (Catalog catalog = openCatalog(archiveDir, epochClock))
    {
        final MutableInteger errorCount = new MutableInteger();
        catalog.forEach(createVerifyEntryProcessor(
            out, archiveDir, options, checksum, epochClock, errorCount, truncateFileOnPageStraddle));

        return errorCount.get() == 0;
    }
}
 
Example #5
Source File: ArchiveTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Migrate previous archive {@link org.agrona.MarkFile}, {@link Catalog}, and recordings from previous version
 * to latest version.
 *
 * @param out        output stream to print results and errors to.
 * @param archiveDir that contains MarkFile, Catalog and recordings.
 */
public static void migrate(final PrintStream out, final File archiveDir)
{
    final EpochClock epochClock = INSTANCE;
    try (ArchiveMarkFile markFile = openMarkFileReadWrite(archiveDir, epochClock);
        Catalog catalog = openCatalogReadWrite(archiveDir, epochClock))
    {
        out.println("MarkFile version=" + fullVersionString(markFile.decoder().version()));
        out.println("Catalog version=" + fullVersionString(catalog.version()));
        out.println("Latest version=" + fullVersionString(ArchiveMarkFile.SEMANTIC_VERSION));

        final List<ArchiveMigrationStep> steps = ArchiveMigrationPlanner.createPlan(markFile.decoder().version());

        for (final ArchiveMigrationStep step : steps)
        {
            out.println("Migration step " + step.toString());
            step.migrate(out, markFile, catalog, archiveDir);
        }
    }
    catch (final Exception ex)
    {
        ex.printStackTrace(out);
    }
}
 
Example #6
Source File: LibraryPoller.java    From artio with Apache License 2.0 6 votes vote down vote up
LibraryPoller(
    final LibraryConfiguration configuration,
    final LibraryTimers timers,
    final FixCounters fixCounters,
    final LibraryTransport transport,
    final FixLibrary fixLibrary,
    final EpochClock epochClock)
{
    this.libraryId = configuration.libraryId();
    this.fixCounters = fixCounters;
    this.transport = transport;
    this.fixLibrary = fixLibrary;

    this.sessionTimer = timers.sessionTimer();
    this.receiveTimer = timers.receiveTimer();

    this.configuration = configuration;
    this.sessionIdStrategy = configuration.sessionIdStrategy();
    this.sessionExistsHandler = configuration.sessionExistsHandler();
    this.epochClock = epochClock;
    this.enginesAreClustered = configuration.libraryAeronChannels().size() > 1;
}
 
Example #7
Source File: HistogramLogAgent.java    From artio with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("FinalParameters")
public HistogramLogAgent(
    final List<Timer> timers,
    final String logFile,
    final long intervalInMs,
    final ErrorHandler errorHandler,
    final EpochClock milliClock,
    HistogramHandler histogramHandler,
    final String agentNamePrefix)
{
    this.timers = timers;
    this.intervalInMs = intervalInMs;
    this.milliClock = milliClock;
    this.agentNamePrefix = agentNamePrefix;

    if (histogramHandler == null)
    {
        histogramHandler = new HistogramLogWriter(timers.size(), logFile, errorHandler);
    }

    this.histogramHandler = histogramHandler;
    timers.forEach(timer -> this.histogramHandler.identifyTimer(timer.id(), timer.name()));
    histogramHandler.onEndTimerIdentification();
}
 
Example #8
Source File: CountersManager.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new counter buffer manager over two buffers.
 *
 * @param metaDataBuffer       containing the types, keys, and labels for the counters.
 * @param valuesBuffer         containing the values of the counters themselves.
 * @param labelCharset         for the label encoding.
 * @param epochClock           to use for determining time for keep counter from being reused after being freed.
 * @param freeToReuseTimeoutMs timeout (in milliseconds) to keep counter from being reused after being freed.
 */
public CountersManager(
    final AtomicBuffer metaDataBuffer,
    final AtomicBuffer valuesBuffer,
    final Charset labelCharset,
    final EpochClock epochClock,
    final long freeToReuseTimeoutMs)
{
    super(metaDataBuffer, valuesBuffer, labelCharset);

    valuesBuffer.verifyAlignment();
    this.epochClock = epochClock;
    this.freeToReuseTimeoutMs = freeToReuseTimeoutMs;

    if (metaDataBuffer.capacity() < (valuesBuffer.capacity() * 2))
    {
        throw new IllegalArgumentException("metadata buffer not sufficiently large");
    }
}
 
Example #9
Source File: MarkFile.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Map a pre-existing {@link MarkFile} if one present and is active.
 *
 * Total length of {@link MarkFile} will be mapped until {@link #close()} is called.
 *
 * @param directory            for the {@link MarkFile} file.
 * @param filename             of the {@link MarkFile} file.
 * @param versionFieldOffset   to use for version field access.
 * @param timestampFieldOffset to use for timestamp field access.
 * @param timeoutMs            for the activity check (in milliseconds) and for how long to wait for file to exist.
 * @param epochClock           to use for time checks.
 * @param versionCheck         to use for existing {@link MarkFile} file and version field.
 * @param logger               to use to signal progress or null.
 */
public MarkFile(
    final File directory,
    final String filename,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    validateOffsets(versionFieldOffset, timestampFieldOffset);

    this.parentDir = directory;
    this.markFile = new File(directory, filename);
    this.mappedBuffer = mapExistingMarkFile(
        markFile, versionFieldOffset, timestampFieldOffset, timeoutMs, epochClock, versionCheck, logger);
    this.buffer = new UnsafeBuffer(mappedBuffer);
    this.versionFieldOffset = versionFieldOffset;
    this.timestampFieldOffset = timestampFieldOffset;
}
 
Example #10
Source File: PossDupEnabler.java    From artio with Apache License 2.0 6 votes vote down vote up
public PossDupEnabler(
    final UtcTimestampEncoder utcTimestampEncoder,
    final BufferClaim bufferClaim,
    final Claimer claimer,
    final PreCommit onPreCommit,
    final Consumer<String> onIllegalStateFunc,
    final ErrorHandler errorHandler,
    final EpochClock clock,
    final int maxPayloadLength,
    final LogTag logTag)
{
    this.utcTimestampEncoder = utcTimestampEncoder;
    this.bufferClaim = bufferClaim;
    this.claimer = claimer;
    this.onPreCommit = onPreCommit;
    this.onIllegalStateFunc = onIllegalStateFunc;
    this.errorHandler = errorHandler;
    this.clock = clock;
    this.maxPayloadLength = maxPayloadLength;
    this.logTag = logTag;
}
 
Example #11
Source File: ArchiveMarkFile.java    From aeron with Apache License 2.0 6 votes vote down vote up
public ArchiveMarkFile(
    final File directory,
    final String filename,
    final EpochClock epochClock,
    final long timeoutMs,
    final Consumer<String> logger)
{
    this(
        directory,
        filename,
        epochClock,
        timeoutMs,
        (version) ->
        {
            if (SemanticVersion.major(version) != MAJOR_VERSION)
            {
                throw new IllegalArgumentException("mark file major version " + SemanticVersion.major(version) +
                    " does not match software:" + MAJOR_VERSION);
            }
        },
        logger);
}
 
Example #12
Source File: ArchiveMarkFile.java    From aeron with Apache License 2.0 6 votes vote down vote up
public ArchiveMarkFile(
    final File directory,
    final String filename,
    final EpochClock epochClock,
    final long timeoutMs,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    markFile = new MarkFile(
        directory,
        filename,
        MarkFileHeaderDecoder.versionEncodingOffset(),
        MarkFileHeaderDecoder.activityTimestampEncodingOffset(),
        timeoutMs,
        epochClock,
        versionCheck,
        logger);

    buffer = markFile.buffer();
    headerEncoder.wrap(buffer, 0);
    headerDecoder.wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);

    errorBuffer = headerDecoder.errorBufferLength() > 0 ?
        new UnsafeBuffer(buffer, headerDecoder.headerLength(), headerDecoder.errorBufferLength()) :
        new UnsafeBuffer(buffer, 0, 0);
}
 
Example #13
Source File: DirectSessionProxy.java    From artio with Apache License 2.0 6 votes vote down vote up
public DirectSessionProxy(
    final int sessionBufferSize,
    final GatewayPublication gatewayPublication,
    final SessionIdStrategy sessionIdStrategy,
    final SessionCustomisationStrategy customisationStrategy,
    final EpochClock clock,
    final long connectionId,
    final int libraryId,
    final ErrorHandler errorHandler,
    final EpochFractionFormat epochFractionPrecision)
{
    this.gatewayPublication = gatewayPublication;
    this.sessionIdStrategy = sessionIdStrategy;
    this.customisationStrategy = customisationStrategy;
    this.clock = clock;
    this.connectionId = connectionId;
    this.libraryId = libraryId;
    this.buffer = new MutableAsciiBuffer(new byte[sessionBufferSize]);
    this.errorHandler = errorHandler;
    lowSequenceNumber = new AsciiFormatter("MsgSeqNum too low, expecting %s but received %s");
    timestampEncoder = new UtcTimestampEncoder(epochFractionPrecision);
    timestampEncoder.initialise(clock.time());
}
 
Example #14
Source File: TestCluster.java    From aeron with Apache License 2.0 6 votes vote down vote up
void awaitResponseMessageCount(final int messageCount)
{
    final EpochClock epochClock = client.context().aeron().context().epochClock();
    long heartbeatDeadlineMs = epochClock.time() + TimeUnit.SECONDS.toMillis(1);
    long count;

    while ((count = responseCount.get()) < messageCount)
    {
        Thread.yield();
        if (Thread.interrupted())
        {
            final String message = "count=" + count + " awaiting=" + messageCount;
            Tests.unexpectedInterruptStackTrace(message);
            fail(message);
        }

        client.pollEgress();

        final long nowMs = epochClock.time();
        if (nowMs > heartbeatDeadlineMs)
        {
            client.sendKeepAlive();
            heartbeatDeadlineMs = nowMs + TimeUnit.SECONDS.toMillis(1);
        }
    }
}
 
Example #15
Source File: ClusterTool.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void nextBackupQuery(final PrintStream out, final File clusterDir, final long delayMs)
{
    if (markFileExists(clusterDir) || TIMEOUT_MS > 0)
    {
        try (ClusterMarkFile markFile = openMarkFile(clusterDir, System.out::println))
        {
            if (markFile.decoder().componentType() != ClusterComponentType.BACKUP)
            {
                out.println("not a cluster backup node");
            }
            else
            {
                final EpochClock epochClock = SystemEpochClock.INSTANCE;
                nextBackupQueryDeadlineMs(markFile, epochClock.time() + delayMs);
                out.format("%2$tF %1$tH:%1$tM:%1$tS setting next: %2$tF %2$tH:%2$tM:%2$tS%n",
                    new Date(),
                    new Date(nextBackupQueryDeadlineMs(markFile)));
            }
        }
    }
    else
    {
        out.println(ClusterMarkFile.FILENAME + " does not exist.");
    }
}
 
Example #16
Source File: ClusterMarkFile.java    From aeron with Apache License 2.0 5 votes vote down vote up
public ClusterMarkFile(
    final File directory,
    final String filename,
    final EpochClock epochClock,
    final long timeoutMs,
    final Consumer<String> logger)
{
    markFile = new MarkFile(
        directory,
        filename,
        MarkFileHeaderDecoder.versionEncodingOffset(),
        MarkFileHeaderDecoder.activityTimestampEncodingOffset(),
        timeoutMs,
        epochClock,
        (version) ->
        {
            if (SemanticVersion.major(version) != MAJOR_VERSION)
            {
                throw new ClusterException("mark file major version " + SemanticVersion.major(version) +
                    " does not match software:" + MAJOR_VERSION);
            }
        },
        logger);

    buffer = markFile.buffer();
    headerDecoder.wrap(buffer, 0, MarkFileHeaderDecoder.BLOCK_LENGTH, MarkFileHeaderDecoder.SCHEMA_VERSION);
    errorBuffer = new UnsafeBuffer(buffer, headerDecoder.headerLength(), headerDecoder.errorBufferLength());
}
 
Example #17
Source File: ConcurrentCountersManager.java    From agrona with Apache License 2.0 5 votes vote down vote up
public ConcurrentCountersManager(
    final AtomicBuffer metaDataBuffer,
    final AtomicBuffer valuesBuffer,
    final Charset labelCharset,
    final EpochClock epochClock,
    final long freeToReuseTimeoutMs)
{
    super(metaDataBuffer, valuesBuffer, labelCharset, epochClock, freeToReuseTimeoutMs);
}
 
Example #18
Source File: TestCluster.java    From aeron with Apache License 2.0 5 votes vote down vote up
void awaitServiceMessageCount(final TestNode node, final int messageCount)
{
    final TestNode.TestService service = node.service();
    final EpochClock epochClock = client.context().aeron().context().epochClock();
    long keepAliveDeadlineMs = epochClock.time() + TimeUnit.SECONDS.toMillis(1);
    long count;

    while ((count = service.messageCount()) < messageCount)
    {
        Thread.yield();
        if (Thread.interrupted())
        {
            final String message = "count=" + count + " awaiting=" + messageCount;
            Tests.unexpectedInterruptStackTrace(message);
            fail(message);
        }

        if (service.hasReceivedUnexpectedMessage())
        {
            fail("service received unexpected message");
        }

        final long nowMs = epochClock.time();
        if (nowMs > keepAliveDeadlineMs)
        {
            client.sendKeepAlive();
            keepAliveDeadlineMs = nowMs + TimeUnit.SECONDS.toMillis(1);
        }
    }
}
 
Example #19
Source File: ArchiveTool.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static ArchiveMarkFile openMarkFileReadWrite(final File archiveDir, final EpochClock epochClock)
{
    return new ArchiveMarkFile(
        archiveDir,
        ArchiveMarkFile.FILENAME,
        epochClock,
        TimeUnit.SECONDS.toMillis(5),
        (version) -> {},
        null);
}
 
Example #20
Source File: DistinctErrorLog.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new error log that will be written to a provided {@link AtomicBuffer}.
 *
 * @param buffer  into which the observation records are recorded.
 * @param clock   to be used for time stamping records.
 * @param charset for encoding the errors.
 */
public DistinctErrorLog(final AtomicBuffer buffer, final EpochClock clock, final Charset charset)
{
    buffer.verifyAlignment();
    this.clock = clock;
    this.buffer = buffer;
    this.charset = charset;
}
 
Example #21
Source File: MarkFile.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Create a directory and mark file if none present. Checking if an active Mark file exists and is active.
 * Old Mark file is deleted and recreated if not active.
 *
 * Total length of Mark file will be mapped until {@link #close()} is called.
 *
 * @param directory             for the Mark file.
 * @param filename              of the Mark file.
 * @param warnIfDirectoryExists for logging purposes.
 * @param dirDeleteOnStart      if desired.
 * @param versionFieldOffset    to use for version field access.
 * @param timestampFieldOffset  to use for timestamp field access.
 * @param totalFileLength       to allocate when creating new Mark file.
 * @param timeoutMs             for the activity check (in milliseconds).
 * @param epochClock            to use for time checks.
 * @param versionCheck          to use for existing Mark file and version field.
 * @param logger                to use to signal progress or null.
 */
public MarkFile(
    final File directory,
    final String filename,
    final boolean warnIfDirectoryExists,
    final boolean dirDeleteOnStart,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final int totalFileLength,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    validateOffsets(versionFieldOffset, timestampFieldOffset);

    ensureDirectoryExists(
        directory,
        filename,
        warnIfDirectoryExists,
        dirDeleteOnStart,
        versionFieldOffset,
        timestampFieldOffset,
        timeoutMs,
        epochClock,
        versionCheck,
        logger);

    this.parentDir = directory;
    this.markFile = new File(directory, filename);
    this.mappedBuffer = IoUtil.mapNewFile(markFile, totalFileLength);
    this.buffer = new UnsafeBuffer(mappedBuffer);
    this.versionFieldOffset = versionFieldOffset;
    this.timestampFieldOffset = timestampFieldOffset;
}
 
Example #22
Source File: MarkFile.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link MarkFile} if none present. Checking if an active {@link MarkFile} exists and is active.
 * Existing {@link MarkFile} is used if not active.
 *
 * Total length of Mark file will be mapped until {@link #close()} is called.
 *
 * @param markFile             to use.
 * @param shouldPreExist       or not.
 * @param versionFieldOffset   to use for version field access.
 * @param timestampFieldOffset to use for timestamp field access.
 * @param totalFileLength      to allocate when creating new {@link MarkFile}.
 * @param timeoutMs            for the activity check (in milliseconds).
 * @param epochClock           to use for time checks.
 * @param versionCheck         to use for existing {@link MarkFile} and version field.
 * @param logger               to use to signal progress or null.
 */

public MarkFile(
    final File markFile,
    final boolean shouldPreExist,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final int totalFileLength,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    validateOffsets(versionFieldOffset, timestampFieldOffset);

    this.parentDir = markFile.getParentFile();
    this.markFile = markFile;
    this.mappedBuffer = mapNewOrExistingMarkFile(
        markFile,
        shouldPreExist,
        versionFieldOffset,
        timestampFieldOffset,
        totalFileLength,
        timeoutMs,
        epochClock,
        versionCheck,
        logger);

    this.buffer = new UnsafeBuffer(mappedBuffer);
    this.versionFieldOffset = versionFieldOffset;
    this.timestampFieldOffset = timestampFieldOffset;
}
 
Example #23
Source File: GatewaySessions.java    From artio with Apache License 2.0 5 votes vote down vote up
GatewaySessions(
    final EpochClock epochClock,
    final GatewayPublication inboundPublication,
    final GatewayPublication outboundPublication,
    final SessionIdStrategy sessionIdStrategy,
    final SessionCustomisationStrategy customisationStrategy,
    final FixCounters fixCounters,
    final EngineConfiguration configuration,
    final ErrorHandler errorHandler,
    final SessionContexts sessionContexts,
    final SessionPersistenceStrategy sessionPersistenceStrategy,
    final SequenceNumberIndexReader sentSequenceNumberIndex,
    final SequenceNumberIndexReader receivedSequenceNumberIndex,
    final EpochFractionFormat epochFractionPrecision)
{
    this.epochClock = epochClock;
    this.inboundPublication = inboundPublication;
    this.outboundPublication = outboundPublication;
    this.sessionIdStrategy = sessionIdStrategy;
    this.customisationStrategy = customisationStrategy;
    this.fixCounters = fixCounters;
    this.authenticationStrategy = configuration.authenticationStrategy();
    this.validationStrategy = configuration.messageValidationStrategy();
    this.sessionBufferSize = configuration.sessionBufferSize();
    this.sendingTimeWindowInMs = configuration.sendingTimeWindowInMs();
    this.reasonableTransmissionTimeInMs = configuration.reasonableTransmissionTimeInMs();
    this.logAllMessages = configuration.logAllMessages();
    this.validateCompIdsOnEveryMessage = configuration.validateCompIdsOnEveryMessage();
    this.clock = configuration.clock();
    this.errorHandler = errorHandler;
    this.sessionContexts = sessionContexts;
    this.sessionPersistenceStrategy = sessionPersistenceStrategy;
    this.sentSequenceNumberIndex = sentSequenceNumberIndex;
    this.receivedSequenceNumberIndex = receivedSequenceNumberIndex;
    this.epochFractionPrecision = epochFractionPrecision;

    sendingTimeEncoder = new UtcTimestampEncoder(epochFractionPrecision);
}
 
Example #24
Source File: MarkFile.java    From agrona with Apache License 2.0 5 votes vote down vote up
public static MappedByteBuffer waitForFileMapping(
    final Consumer<String> logger,
    final File markFile,
    final long deadlineMs,
    final EpochClock epochClock)
{
    try (FileChannel fileChannel = FileChannel.open(markFile.toPath(), READ, WRITE))
    {
        while (fileChannel.size() < 4)
        {
            if (epochClock.time() > deadlineMs)
            {
                throw new IllegalStateException("Mark file is created but not populated");
            }

            sleep(16);
        }

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

        return fileChannel.map(READ_WRITE, 0, fileChannel.size());
    }
    catch (final IOException ex)
    {
        throw new IllegalStateException("cannot open mark file for reading", ex);
    }
}
 
Example #25
Source File: ExternallyControlledSystemTest.java    From artio with Apache License 2.0 5 votes vote down vote up
private SessionProxy sessionProxyFactory(
    final int sessionBufferSize,
    final GatewayPublication gatewayPublication,
    final SessionIdStrategy sessionIdStrategy,
    final SessionCustomisationStrategy customisationStrategy,
    final EpochClock clock,
    final long connectionId,
    final int libraryId,
    final ErrorHandler errorHandler,
    final EpochFractionFormat epochFractionPrecision)
{
    sessionProxyRequests++;
    return fakeSessionProxy;
}
 
Example #26
Source File: SessionProxyFactory.java    From artio with Apache License 2.0 5 votes vote down vote up
SessionProxy make(
int sessionBufferSize,
GatewayPublication gatewayPublication,
SessionIdStrategy sessionIdStrategy,
SessionCustomisationStrategy customisationStrategy,
EpochClock clock,
long connectionId,
int libraryId,
ErrorHandler errorHandler,
EpochFractionFormat epochFractionPrecision);
 
Example #27
Source File: MarkFile.java    From agrona with Apache License 2.0 4 votes vote down vote up
public static void ensureDirectoryExists(
    final File directory,
    final String filename,
    final boolean warnIfDirectoryExists,
    final boolean dirDeleteOnStart,
    final int versionFieldOffset,
    final int timestampFieldOffset,
    final long timeoutMs,
    final EpochClock epochClock,
    final IntConsumer versionCheck,
    final Consumer<String> logger)
{
    final File markFile = new File(directory, filename);

    if (directory.isDirectory())
    {
        if (warnIfDirectoryExists && null != logger)
        {
            logger.accept("WARNING: " + directory + " already exists.");
        }

        if (!dirDeleteOnStart)
        {
            final int offset = Math.min(versionFieldOffset, timestampFieldOffset);
            final int length = Math.max(versionFieldOffset, timestampFieldOffset) + SIZE_OF_LONG - offset;
            final MappedByteBuffer byteBuffer = mapExistingFile(markFile, logger, offset, length);

            try
            {
                if (isActive(
                    byteBuffer,
                    epochClock,
                    timeoutMs,
                    versionFieldOffset,
                    timestampFieldOffset,
                    versionCheck,
                    logger))
                {
                    throw new IllegalStateException("Active Mark file detected");
                }
            }
            finally
            {
                IoUtil.unmap(byteBuffer);
            }
        }

        IoUtil.delete(directory, false);
    }

    IoUtil.ensureDirectoryExists(directory, directory.toString());
}
 
Example #28
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 #29
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 #30
Source File: TestBackupNode.java    From aeron with Apache License 2.0 4 votes vote down vote up
EpochClock epochClock()
{
    return clusterBackupMediaDriver.clusterBackup().context().epochClock();
}