Search in sources :

Example 16 with ManyToOneRingBuffer

use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project aeron by real-logic.

the class IpcPublicationTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() {
    final RingBuffer toDriverCommands = new ManyToOneRingBuffer(new UnsafeBuffer(ByteBuffer.allocate(Configuration.CONDUCTOR_BUFFER_LENGTH_DEFAULT)));
    final UnsafeBuffer counterBuffer = new UnsafeBuffer(ByteBuffer.allocate(BUFFER_LENGTH));
    final UnsafeBuffer metaDataBuffer = new UnsafeBuffer(ByteBuffer.allocate(Configuration.countersMetadataBufferLength(BUFFER_LENGTH)));
    final CountersManager countersManager = new CountersManager(metaDataBuffer, counterBuffer, StandardCharsets.US_ASCII);
    final SystemCounters systemCounters = new SystemCounters(countersManager);
    final MediaDriver.Context ctx = new MediaDriver.Context().tempBuffer(new UnsafeBuffer(new byte[METADATA_LENGTH])).ipcTermBufferLength(TERM_BUFFER_LENGTH).toDriverCommands(toDriverCommands).logFactory(new TestLogFactory()).clientProxy(mock(ClientProxy.class)).driverCommandQueue(mock(ManyToOneConcurrentArrayQueue.class)).epochClock(SystemEpochClock.INSTANCE).cachedEpochClock(new CachedEpochClock()).cachedNanoClock(new CachedNanoClock()).countersManager(countersManager).systemCounters(systemCounters).nameResolver(DefaultNameResolver.INSTANCE).nanoClock(new CachedNanoClock());
    ctx.countersValuesBuffer(counterBuffer);
    driverProxy = new DriverProxy(toDriverCommands, CLIENT_ID);
    driverConductor = new DriverConductor(ctx);
    driverConductor.onStart();
    driverProxy.addPublication(CommonContext.IPC_CHANNEL, STREAM_ID);
    driverConductor.doWork();
    ipcPublication = driverConductor.getSharedIpcPublication(STREAM_ID);
    publisherLimit = new UnsafeBufferPosition(counterBuffer, ipcPublication.publisherLimitId());
}
Also used : ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) TestLogFactory(io.aeron.driver.buffer.TestLogFactory) SystemCounters(io.aeron.driver.status.SystemCounters) RingBuffer(org.agrona.concurrent.ringbuffer.RingBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) DriverProxy(io.aeron.DriverProxy) UnsafeBufferPosition(org.agrona.concurrent.status.UnsafeBufferPosition) CountersManager(org.agrona.concurrent.status.CountersManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with ManyToOneRingBuffer

use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project aeron by real-logic.

the class DriverTool method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
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());
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) AeronException(io.aeron.exceptions.AeronException) DriverProxy(io.aeron.DriverProxy) File(java.io.File)

Example 18 with ManyToOneRingBuffer

use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.

the class CommonContext method isDriverActive.

/**
     * Is a media driver active in the current Aeron directory?
     *
     * @param driverTimeoutMs for the driver liveness check
     * @param logHandler      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> logHandler) {
    final File dirFile = new File(aeronDirectoryName);
    if (dirFile.exists() && dirFile.isDirectory()) {
        final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
        logHandler.accept(String.format("INFO: Aeron directory %s exists", dirFile));
        if (cncFile.exists()) {
            MappedByteBuffer cncByteBuffer = null;
            logHandler.accept(String.format("INFO: Aeron CnC file %s exists", cncFile));
            try {
                cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
                final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
                final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
                if (CncFileDescriptor.CNC_VERSION != cncVersion) {
                    throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
                }
                final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
                final long timestamp = toDriverBuffer.consumerHeartbeatTime();
                final long now = System.currentTimeMillis();
                final long diff = now - timestamp;
                logHandler.accept(String.format("INFO: Aeron toDriver consumer heartbeat is %d ms old", diff));
                if (diff <= driverTimeoutMs) {
                    return true;
                }
            } catch (final Exception ex) {
                LangUtil.rethrowUnchecked(ex);
            } finally {
                IoUtil.unmap(cncByteBuffer);
            }
        }
    }
    return false;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) File(java.io.File)

Example 19 with ManyToOneRingBuffer

use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project aeron by real-logic.

the class CommonContext method isDriverActive.

/**
 * 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 MappedByteBuffer 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);
    }
    if (CNC_VERSION != cncVersion) {
        throw new IllegalStateException("Aeron CnC version does not match: required=" + CNC_VERSION + " version=" + cncVersion);
    }
    final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
    final long timestamp = toDriverBuffer.consumerHeartbeatTime();
    final long now = System.currentTimeMillis();
    final long timestampAge = now - timestamp;
    logger.accept("INFO: Aeron toDriver consumer heartbeat is (ms): " + timestampAge);
    return timestampAge <= driverTimeoutMs;
}
Also used : ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) DriverTimeoutException(io.aeron.exceptions.DriverTimeoutException) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 20 with ManyToOneRingBuffer

use of org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer in project Aeron by real-logic.

the class DriverEventLogger method logResolve.

/**
 * Log a resolution for a resolver and the associated result.
 *
 * @param code          representing the event type
 * @param resolverName  simple class name of the resolver
 * @param name          host name being resolved
 * @param address       address that was resolved to, can be null
 */
public void logResolve(final DriverEventCode code, final String resolverName, final String name, final InetAddress address) {
    final int length = trailingStringLength(resolverName, MAX_HOST_NAME_LENGTH) + trailingStringLength(name, MAX_HOST_NAME_LENGTH) + inetAddressLength(address);
    final int encodedLength = encodedLength(length);
    final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
    final int index = ringBuffer.tryClaim(toEventCodeId(code), encodedLength);
    if (index > 0) {
        try {
            encodeResolve((UnsafeBuffer) ringBuffer.buffer(), index, length, length, resolverName, name, address);
        } finally {
            ringBuffer.commit(index);
        }
    }
}
Also used : ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer)

Aggregations

ManyToOneRingBuffer (org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer)50 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)12 File (java.io.File)5 MappedByteBuffer (java.nio.MappedByteBuffer)5 DriverProxy (io.aeron.DriverProxy)4 DriverTimeoutException (io.aeron.exceptions.DriverTimeoutException)3 TestLogFactory (io.aeron.driver.buffer.TestLogFactory)2 SystemCounters (io.aeron.driver.status.SystemCounters)2 AeronException (io.aeron.exceptions.AeronException)2 DirectBuffer (org.agrona.DirectBuffer)2 RingBuffer (org.agrona.concurrent.ringbuffer.RingBuffer)2 CountersManager (org.agrona.concurrent.status.CountersManager)2 UnsafeBufferPosition (org.agrona.concurrent.status.UnsafeBufferPosition)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2