Java Code Examples for org.agrona.IoUtil#mapExistingFile()

The following examples show how to use org.agrona.IoUtil#mapExistingFile() . 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: CommonContext.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Map the CnC file if it exists.
 *
 * @param logger for feedback
 * @return a new mapping for the file if it exists otherwise null;
 */
public MappedByteBuffer mapExistingCncFile(final Consumer<String> logger)
{
    final File cncFile = new File(aeronDirectory, CncFileDescriptor.CNC_FILE);

    if (cncFile.exists() && cncFile.length() > 0)
    {
        if (null != logger)
        {
            logger.accept("INFO: Aeron CnC file exists: " + cncFile);
        }

        return IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
    }

    return null;
}
 
Example 2
Source File: CommonContext.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Is a media driver active in the given directory?
 *
 * @param directory       to check
 * @param driverTimeoutMs for the driver liveness check.
 * @param logger          for feedback as liveness checked.
 * @return true if a driver is active or false if not.
 */
public static boolean isDriverActive(
    final File directory, final long driverTimeoutMs, final Consumer<String> logger)
{
    final File cncFile = new File(directory, CncFileDescriptor.CNC_FILE);

    if (cncFile.exists() && cncFile.length() > 0)
    {
        logger.accept("INFO: Aeron CnC file exists: " + cncFile);

        final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "CnC file");
        try
        {
            return isDriverActive(driverTimeoutMs, logger, cncByteBuffer);
        }
        finally
        {
            IoUtil.unmap(cncByteBuffer);
        }
    }

    return false;
}
 
Example 3
Source File: FileSender.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    if (args.length != 1)
    {
        System.out.println("Filename to be sent must be supplied as a command line argument");
        System.exit(1);
    }

    try (Aeron aeron = Aeron.connect();
        Publication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        while (!publication.isConnected())
        {
            Thread.sleep(1);
        }

        final File file = new File(args[0]);
        final UnsafeBuffer buffer = new UnsafeBuffer(IoUtil.mapExistingFile(file, "sending"));
        final long correlationId = aeron.nextCorrelationId();

        sendFileCreate(publication, correlationId, buffer.capacity(), file.getName());
        streamChunks(publication, correlationId, buffer);
    }
}
 
Example 4
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 5 votes vote down vote up
private void loadRecordingIdsFile()
{
    if (recordingIdsFile.exists())
    {
        final MappedByteBuffer mappedBuffer = IoUtil.mapExistingFile(recordingIdsFile, FILE_NAME);
        final UnsafeBuffer buffer = new UnsafeBuffer(mappedBuffer);
        try
        {
            final MessageHeaderDecoder header = new MessageHeaderDecoder();
            final PreviousRecordingDecoder previousRecording = new PreviousRecordingDecoder();

            header.wrap(buffer, 0);
            previousRecording.wrap(buffer, ENCODED_LENGTH, header.blockLength(), header.version());

            for (final InboundRecordingsDecoder inboundRecording : previousRecording.inboundRecordings())
            {
                inboundRecordingIds.free.add(inboundRecording.recordingId());
            }

            for (final OutboundRecordingsDecoder outboundRecording : previousRecording.outboundRecordings())
            {
                outboundRecordingIds.free.add(outboundRecording.recordingId());
            }
        }
        finally
        {
            IoUtil.unmap(mappedBuffer);
        }
    }
}
 
Example 5
Source File: LoggerUtil.java    From artio with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer map(final File file, final int size)
{
    if (file.exists())
    {
        return IoUtil.mapExistingFile(file, file.getName());
    }
    else
    {
        return mapNewFile(file, size);
    }
}
 
Example 6
Source File: MonitoringFile.java    From artio with Apache License 2.0 5 votes vote down vote up
MonitoringFile(final boolean newFile, final CommonConfiguration configuration)
{
    final File file = new File(configuration.monitoringFile()).getAbsoluteFile();
    absolutePath = file.getAbsolutePath();
    CloseChecker.validate(absolutePath);
    CloseChecker.onOpen(absolutePath, this);
    final int length;
    if (newFile)
    {
        IoUtil.deleteIfExists(file);

        length = configuration.monitoringBuffersLength();
        mappedByteBuffer = LoggerUtil.mapNewFile(file, length);
    }
    else
    {
        if (!file.exists() || !file.canRead() || !file.isFile())
        {
            throw new IllegalStateException("Unable to read from file: " + file);
        }

        mappedByteBuffer = IoUtil.mapExistingFile(file, "counters file");
        length = mappedByteBuffer.capacity();
    }

    final AtomicBuffer mappedFile = new UnsafeBuffer(mappedByteBuffer);
    errorBuffer = new UnsafeBuffer(mappedFile, 0, length);
}
 
Example 7
Source File: ClusterTool.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void printDriverErrors(final PrintStream out, final String aeronDirectory)
{
    out.println("Aeron driver error log (directory: " + aeronDirectory + "):");
    final File cncFile = new File(aeronDirectory, CncFileDescriptor.CNC_FILE);

    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, FileChannel.MapMode.READ_ONLY, "cnc");
    final DirectBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));

    CncFileDescriptor.checkVersion(cncVersion);
    CommonContext.printErrorLog(CncFileDescriptor.createErrorLogBuffer(cncByteBuffer, cncMetaDataBuffer), out);
}
 
Example 8
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 9
Source File: AeronStat.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public static CountersReader mapCounters() {
    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);

    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));

    if (CncFileDescriptor.CNC_VERSION != cncVersion) {
        throw new IllegalStateException("CnC version not supported: file version=" + cncVersion);
    }

    return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData),
                    createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
 
Example 10
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 4 votes vote down vote up
private void saveRecordingIdsFile()
{
    libraryIdToExtendPosition.values().forEach(pos -> outboundRecordingIds.free.add(pos.recordingId));

    try
    {
        final int inboundSize = inboundRecordingIds.size();
        final int outboundSize = outboundRecordingIds.size();

        final File saveFile = File.createTempFile(FILE_NAME, "tmp", new File(configuration.logFileDir()));
        final int requiredLength = MessageHeaderEncoder.ENCODED_LENGTH + PreviousRecordingEncoder.BLOCK_LENGTH +
            InboundRecordingsEncoder.HEADER_SIZE + OutboundRecordingsEncoder.HEADER_SIZE +
            InboundRecordingsEncoder.recordingIdEncodingLength() * inboundSize +
            OutboundRecordingsEncoder.recordingIdEncodingLength() * outboundSize;
        final MappedByteBuffer mappedBuffer = IoUtil.mapExistingFile(saveFile, FILE_NAME, 0, requiredLength);
        final UnsafeBuffer buffer = new UnsafeBuffer(mappedBuffer);
        try
        {
            final MessageHeaderEncoder header = new MessageHeaderEncoder();
            final PreviousRecordingEncoder previousRecording = new PreviousRecordingEncoder();

            previousRecording.wrapAndApplyHeader(buffer, 0, header);

            final InboundRecordingsEncoder inbound = previousRecording.inboundRecordingsCount(inboundSize);
            inboundRecordingIds.forEach(id -> inbound.next().recordingId(id));

            final OutboundRecordingsEncoder outbound = previousRecording.outboundRecordingsCount(outboundSize);
            outboundRecordingIds.forEach(id -> outbound.next().recordingId(id));

            mappedBuffer.force();
        }
        finally
        {
            IoUtil.unmap(mappedBuffer);
        }

        Files.move(saveFile.toPath(), recordingIdsFile.toPath(), ATOMIC_MOVE, REPLACE_EXISTING);
    }
    catch (final Throwable e)
    {
        e.printStackTrace();
        errorHandler.onError(e);
    }
}
 
Example 11
Source File: LoggerUtil.java    From artio with Apache License 2.0 4 votes vote down vote up
public static MappedByteBuffer mapExistingFile(final File file)
{
    return IoUtil.mapExistingFile(file, file.getName());
}
 
Example 12
Source File: DriverTool.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args)
{
    boolean printPidOnly = false;
    boolean terminateDriver = false;

    if (0 != args.length)
    {
        checkForHelp(args);

        if (args[0].equals("pid"))
        {
            printPidOnly = true;
        }
        else if (args[0].equals("terminate"))
        {
            terminateDriver = true;
        }
    }

    final File cncFile = CommonContext.newDefaultCncFile();
    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));

    checkVersion(cncVersion);

    final ManyToOneRingBuffer toDriver = new ManyToOneRingBuffer(createToDriverBuffer(cncByteBuffer, cncMetaData));

    if (printPidOnly)
    {
        System.out.println(pid(cncMetaData));
    }
    else if (terminateDriver)
    {
        final DriverProxy driverProxy = new DriverProxy(toDriver, toDriver.nextCorrelationId());

        if (!driverProxy.terminateDriver(null, 0, 0))
        {
            throw new AeronException("could not send termination request.");
        }
    }
    else
    {
        System.out.println("Command `n Control file: " + cncFile);
        System.out.format("Version: %d, PID: %d%n", cncVersion, pid(cncMetaData));
        printDateActivityAndStartTimestamps(startTimestampMs(cncMetaData), toDriver.consumerHeartbeatTime());
    }
}