org.agrona.IoUtil Java Examples

The following examples show how to use org.agrona.IoUtil. 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: ArchiveTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static File makeTestDirectory()
{
    final File archiveDir = new File(SystemUtil.tmpDirName(), "archive-test");
    if (archiveDir.exists())
    {
        System.err.println("Warning archive directory exists, deleting: " + archiveDir.getAbsolutePath());
        IoUtil.delete(archiveDir, false);
    }

    if (!archiveDir.mkdirs())
    {
        throw new IllegalStateException("failed to make archive test directory: " + archiveDir.getAbsolutePath());
    }

    return archiveDir;
}
 
Example #2
Source File: MappedFile.java    From artio with Apache License 2.0 6 votes vote down vote up
public static MappedFile map(final File bufferFile, final int size)
{
    final FileChannel fileChannel;
    try
    {
        if (bufferFile.exists())
        {
            // NB: closing RAF or FileChannel closes them both
            fileChannel = new RandomAccessFile(bufferFile, "rw").getChannel();
        }
        else
        {
            fileChannel = IoUtil.createEmptyFile(bufferFile, size);
        }

        final MappedByteBuffer mappedBuffer = fileChannel.map(READ_WRITE, 0, fileChannel.size());
        return new MappedFile(bufferFile, fileChannel, new UnsafeBuffer(mappedBuffer));
    }
    catch (final IOException ex)
    {
        LangUtil.rethrowUnchecked(ex);
        return null;
    }
}
 
Example #3
Source File: ArchiveLoggingAgentTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void before(final String enabledEvents, final EnumSet<ArchiveEventCode> expectedEvents)
{
    System.setProperty(EventLogAgent.READER_CLASSNAME_PROP_NAME, StubEventLogReaderAgent.class.getName());
    System.setProperty(EventConfiguration.ENABLED_ARCHIVE_EVENT_CODES_PROP_NAME, enabledEvents);
    AgentTests.beforeAgent();

    latch = new CountDownLatch(expectedEvents.size());
    LOGGED_EVENTS.clear();
    WAIT_LIST.addAll(expectedEvents.stream().map(ArchiveEventLogger::toEventCodeId).collect(toSet()));

    testDir = Paths.get(IoUtil.tmpDirName(), "archive-test").toFile();
    if (testDir.exists())
    {
        IoUtil.delete(testDir, false);
    }
}
 
Example #4
Source File: FixBenchmarkServer.java    From artio with Apache License 2.0 6 votes vote down vote up
private static EngineConfiguration engineConfiguration()
{
    final String acceptorLogs = "acceptor_logs";
    final File dir = new File(acceptorLogs);
    if (dir.exists())
    {
        IoUtil.delete(dir, false);
    }

    final EngineConfiguration configuration = new EngineConfiguration();
    configuration.printAeronStreamIdentifiers(true);
    configuration.authenticationStrategy((logon) -> !REJECT_LOGON);

    return configuration
        .bindTo("localhost", BenchmarkConfiguration.PORT)
        .libraryAeronChannel(AERON_CHANNEL)
        .logFileDir(acceptorLogs)
        .logInboundMessages(LOG_INBOUND_MESSAGES)
        .logOutboundMessages(LOG_OUTBOUND_MESSAGES)
        .framerIdleStrategy(idleStrategy());
}
 
Example #5
Source File: FileReceiver.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void fileChunk(
    final long correlationId,
    final long chunkOffset,
    final long chunkLength,
    final DirectBuffer buffer,
    final int offset)
{
    final UnsafeBuffer fileBuffer = fileSessionByIdMap.get(correlationId);
    buffer.getBytes(offset + CHUNK_PAYLOAD_OFFSET, fileBuffer, (int)chunkOffset, (int)chunkLength);

    if ((chunkOffset + chunkLength) >= fileBuffer.capacity())
    {
        fileSessionByIdMap.remove(correlationId);
        IoUtil.unmap(fileBuffer.byteBuffer());
    }
}
 
Example #6
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 #7
Source File: CncFileReader.java    From aeron with Apache License 2.0 6 votes vote down vote up
private CncFileReader(final MappedByteBuffer cncByteBuffer)
{
    this.cncByteBuffer = cncByteBuffer;

    final DirectBuffer cncMetaDataBuffer = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaDataBuffer.getInt(cncVersionOffset(0));

    try
    {
        checkVersion(cncVersion);
    }
    catch (final AeronException e)
    {
        IoUtil.unmap(cncByteBuffer);
        throw e;
    }

    this.cncVersion = cncVersion;
    this.cncSemanticVersion = SemanticVersion.toString(cncVersion);

    this.toDriverBuffer = CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer);

    this.countersReader = new CountersReader(
        createCountersMetaDataBuffer(cncByteBuffer, cncMetaDataBuffer),
        createCountersValuesBuffer(cncByteBuffer, cncMetaDataBuffer));
}
 
Example #8
Source File: FixLibrary.java    From artio with Apache License 2.0 6 votes vote down vote up
private boolean removeParentDirectory(final String path)
{
    final File file = new File(path);
    if (file.exists() && !file.delete())
    {
        return false;
    }

    final File parentFile = file.getParentFile();
    if (parentFile != null && parentFile.exists() && parentFile.listFiles().length == 0)
    {
        IoUtil.delete(parentFile, true);
    }

    return true;
}
 
Example #9
Source File: SessionContextsTest.java    From artio with Apache License 2.0 6 votes vote down vote up
@Test
public void copiesOldSessionContextFile() throws IOException
{
    final File backupLocation = File.createTempFile("sessionContexts", "tmp");
    try
    {
        final SessionContext aContext = sessionContexts.onLogon(aSession, fixDictionary);
        sessionContexts.onDisconnect(aContext.sessionId());

        final byte[] oldData = new byte[BUFFER_SIZE];
        buffer.getBytes(0, oldData);

        sessionContexts.reset(backupLocation);

        verify(mappedFile).transferTo(backupLocation);
    }
    finally
    {
        IoUtil.deleteIfExists(backupLocation);
    }
}
 
Example #10
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 #11
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 #12
Source File: LogBuffers.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void close()
{
    Throwable error = null;
    try
    {
        CloseHelper.close(fileChannel);
    }
    catch (final Throwable t)
    {
        error = t;
    }

    for (int i = 0, length = mappedByteBuffers.length; i < length; i++)
    {
        final MappedByteBuffer mappedByteBuffer = mappedByteBuffers[i];
        mappedByteBuffers[i] = null;
        IoUtil.unmap(mappedByteBuffer);
    }

    if (error != null)
    {
        LangUtil.rethrowUnchecked(error);
    }
}
 
Example #13
Source File: DistinctErrorLogTestWatcher.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void captureErrors(final String aeronDirectoryName)
{
    final File cncFile = CommonContext.newCncFile(aeronDirectoryName);
    assertTrue(cncFile.exists());

    MappedByteBuffer cncByteBuffer = null;

    try (RandomAccessFile file = new RandomAccessFile(cncFile, "r");
        FileChannel channel = file.getChannel())
    {
        cncByteBuffer = channel.map(READ_ONLY, 0, channel.size());
        final AtomicBuffer errorLogBuffer = CommonContext.errorLogBuffer(cncByteBuffer);

        ErrorLogReader.read(errorLogBuffer, this::onObservation);
    }
    catch (final IOException ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        IoUtil.unmap(cncByteBuffer);
    }
}
 
Example #14
Source File: CTestMediaDriver.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void close()
{
    try
    {
        terminateDriver();
        if (!aeronMediaDriverProcess.waitFor(10, TimeUnit.SECONDS))
        {
            aeronMediaDriverProcess.destroyForcibly();
            throw new RuntimeException("Failed to shutdown cleaning, forcing close");
        }

        if (null != driverOutputConsumer)
        {
            driverOutputConsumer.exitCode(context.aeronDirectoryName(), aeronMediaDriverProcess.exitValue());
        }
    }
    catch (final InterruptedException ex)
    {
        throw new RuntimeException("Interrupted while waiting for shutdown", ex);
    }

    IoUtil.delete(new File(context.aeronDirectoryName()), false);
}
 
Example #15
Source File: DriverLoggingAgentTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@AfterEach
public void after()
{
    AgentTests.afterAgent();

    LOGGED_EVENTS.clear();
    WAIT_LIST.clear();

    if (testDir != null && testDir.exists())
    {
        IoUtil.delete(testDir, false);
    }
}
 
Example #16
Source File: CncFileReader.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void close()
{
    if (!isClosed)
    {
        isClosed = true;
        IoUtil.unmap(cncByteBuffer);
    }
}
 
Example #17
Source File: FileStoreLogFactoryTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@AfterEach
public void cleanupFiles()
{
    CloseHelper.close(rawLog);
    CloseHelper.close(fileStoreLogFactory);
    IoUtil.delete(DATA_DIR, false);
}
 
Example #18
Source File: FileReceiver.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void createFile(final long correlationId, final long length, final String filename)
{
    if (fileSessionByIdMap.containsKey(correlationId))
    {
        throw new IllegalStateException("correlationId is in use: " + correlationId);
    }

    final File file = new File(storageDir, filename);
    if (file.exists() && !file.delete())
    {
        throw new IllegalStateException("failed to delete existing file: " + file);
    }

    if (length == 0)
    {
        try
        {
            if (!file.createNewFile())
            {
                throw new IllegalStateException("failed to create " + filename);
            }
        }
        catch (final IOException ex)
        {
            LangUtil.rethrowUnchecked(ex);
        }
    }
    else
    {
        fileSessionByIdMap.put(correlationId, new UnsafeBuffer(IoUtil.mapNewFile(file, length, false)));
    }
}
 
Example #19
Source File: FileStoreLogFactoryTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void createDataDir()
{
    IoUtil.ensureDirectoryExists(DATA_DIR, "data");
    final String absolutePath = DATA_DIR.getAbsolutePath();
    fileStoreLogFactory = new FileStoreLogFactory(
        absolutePath, PAGE_SIZE, PERFORM_STORAGE_CHECKS, LOW_STORAGE_THRESHOLD, mock(ErrorHandler.class));
}
 
Example #20
Source File: ClusterLoggingAgentTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@AfterEach
public void after()
{
    CloseHelper.closeAll(clusteredMediaDriver.consensusModule(), container, clusteredMediaDriver);
    AgentTests.afterAgent();

    if (testDir != null && testDir.exists())
    {
        IoUtil.delete(testDir, false);
    }
}
 
Example #21
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 #22
Source File: DriverLoggingAgentTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void before(final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents)
{
    System.setProperty(EventLogAgent.READER_CLASSNAME_PROP_NAME, StubEventLogReaderAgent.class.getName());
    System.setProperty(EventConfiguration.ENABLED_EVENT_CODES_PROP_NAME, enabledEvents);
    AgentTests.beforeAgent();

    latch = new CountDownLatch(expectedEvents.size());
    WAIT_LIST.addAll(expectedEvents.stream().map(DriverEventCode::id).collect(toSet()));

    testDir = Paths.get(IoUtil.tmpDirName(), "driver-test").toFile();
    if (testDir.exists())
    {
        IoUtil.delete(testDir, false);
    }
}
 
Example #23
Source File: ArchiveLoggingAgentTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@AfterEach
public void after()
{
    AgentTests.afterAgent();

    LOGGED_EVENTS.clear();
    WAIT_LIST.clear();

    if (testDir != null && testDir.exists())
    {
        IoUtil.delete(testDir, false);
    }
}
 
Example #24
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 #25
Source File: ClusterBackup.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Delete the cluster directory.
 */
public void deleteDirectory()
{
    if (null != clusterDir)
    {
        IoUtil.delete(clusterDir, false);
    }
}
 
Example #26
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 #27
Source File: MediaDriverTestWatcher.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void deleteFiles()
{
    outputFilesByAeronDirectoryName.forEach(
        (aeronDirectoryName, files) ->
        {
            IoUtil.delete(files.stdout, false);
            IoUtil.delete(files.stderr, false);
        });
}
 
Example #28
Source File: LossReportTestUtil.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static void verifyLossOccurredForStream(final String aeronDirectoryName, final int streamId)
    throws IOException
{
    final File lossReportFile = LossReportUtil.file(aeronDirectoryName);
    assertTrue(lossReportFile.exists());

    MappedByteBuffer mappedByteBuffer = null;

    try (RandomAccessFile file = new RandomAccessFile(lossReportFile, "r");
        FileChannel channel = file.getChannel())
    {
        mappedByteBuffer = channel.map(READ_ONLY, 0, channel.size());
        final AtomicBuffer buffer = new UnsafeBuffer(mappedByteBuffer);

        final LossReportReader.EntryConsumer lossEntryConsumer = mock(LossReportReader.EntryConsumer.class);
        LossReportReader.read(buffer, lossEntryConsumer);

        verify(lossEntryConsumer).accept(
            longThat((l) -> l > 0),
            longThat((l) -> l > 0),
            anyLong(),
            anyLong(),
            anyInt(),
            eq(streamId),
            any(),
            any());
    }
    finally
    {
        IoUtil.unmap(mappedByteBuffer);
    }
}
 
Example #29
Source File: Backup.java    From artio with Apache License 2.0 5 votes vote down vote up
public void cleanup()
{
    if (backupLocation.exists())
    {
        IoUtil.delete(backupLocation, false);
    }
}
 
Example #30
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 Aeron directory?
 *
 * @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 boolean isDriverActive(final long driverTimeoutMs, final Consumer<String> logger)
{
    final MappedByteBuffer cncByteBuffer = mapExistingCncFile(logger);
    try
    {
        return isDriverActive(driverTimeoutMs, logger, cncByteBuffer);
    }
    finally
    {
        IoUtil.unmap(cncByteBuffer);
    }
}