Search in sources :

Example 1 with DriverTimeoutException

use of io.aeron.exceptions.DriverTimeoutException in project nd4j by deeplearning4j.

the class AeronNDArrayPublisher method publish.

/**
 * Publish an ndarray
 * to an aeron channel
 * @param message
 * @throws Exception
 */
public void publish(NDArrayMessage message) throws Exception {
    if (!init)
        init();
    // Create a context, needed for client connection to media driver
    // A separate media driver process needs to be running prior to starting this application
    // Create an Aeron instance with client-provided context configuration and connect to the
    // media driver, and create a Publication.  The Aeron and Publication classes implement
    // AutoCloseable, and will automatically clean up resources when this try block is finished.
    boolean connected = false;
    if (aeron == null) {
        try {
            while (!connected) {
                aeron = Aeron.connect(ctx);
                connected = true;
            }
        } catch (Exception e) {
            log.warn("Reconnecting on publisher...failed to connect");
        }
    }
    int connectionTries = 0;
    while (publication == null && connectionTries < NUM_RETRIES) {
        try {
            publication = aeron.addPublication(channel, streamId);
            log.info("Created publication on channel " + channel + " and stream " + streamId);
        } catch (DriverTimeoutException e) {
            Thread.sleep(1000 * (connectionTries + 1));
            log.warn("Failed to connect due to driver time out on channel " + channel + " and stream " + streamId + "...retrying in " + connectionTries + " seconds");
            connectionTries++;
        }
    }
    if (!connected && connectionTries >= 3 || publication == null) {
        throw new IllegalStateException("Publisher unable to connect to channel " + channel + " and stream " + streamId);
    }
    // Allocate enough buffer size to hold maximum message length
    // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
    log.info("Publishing to " + channel + " on stream Id " + streamId);
    // ensure default values are set
    INDArray arr = message.getArr();
    if (isCompress())
        while (!message.getArr().isCompressed()) Nd4j.getCompressor().compressi(arr, "GZIP");
    // array is large, need to segment
    if (NDArrayMessage.byteBufferSizeForMessage(message) >= publication.maxMessageLength()) {
        NDArrayMessageChunk[] chunks = NDArrayMessage.chunks(message, publication.maxMessageLength() / 128);
        for (int i = 0; i < chunks.length; i++) {
            ByteBuffer sendBuff = NDArrayMessageChunk.toBuffer(chunks[i]);
            sendBuff.rewind();
            DirectBuffer buffer = new UnsafeBuffer(sendBuff);
            sendBuffer(buffer);
        }
    } else {
        // send whole array
        DirectBuffer buffer = NDArrayMessage.toBuffer(message);
        sendBuffer(buffer);
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DriverTimeoutException(io.aeron.exceptions.DriverTimeoutException) NDArrayMessageChunk(org.nd4j.aeron.ipc.chunk.NDArrayMessageChunk) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) DriverTimeoutException(io.aeron.exceptions.DriverTimeoutException)

Example 2 with DriverTimeoutException

use of io.aeron.exceptions.DriverTimeoutException 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 starting 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;
}
Also used : ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) DriverTimeoutException(io.aeron.exceptions.DriverTimeoutException) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 3 with DriverTimeoutException

use of io.aeron.exceptions.DriverTimeoutException 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 4 with DriverTimeoutException

use of io.aeron.exceptions.DriverTimeoutException in project Aeron by real-logic.

the class Aeron method waitForFileMapping.

@SuppressWarnings("try")
private static MappedByteBuffer waitForFileMapping(final File file, final EpochClock clock, final long deadlineMs) {
    while (true) {
        while (!file.exists() || file.length() < CncFileDescriptor.META_DATA_LENGTH) {
            if (clock.time() > deadlineMs) {
                throw new DriverTimeoutException("CnC file not created: " + file.getAbsolutePath());
            }
            sleep(Configuration.IDLE_SLEEP_MS);
        }
        try (FileChannel fileChannel = FileChannel.open(file.toPath(), READ, WRITE)) {
            final long fileSize = fileChannel.size();
            if (fileSize < CncFileDescriptor.META_DATA_LENGTH) {
                if (clock.time() > deadlineMs) {
                    throw new DriverTimeoutException("CnC file is created but not populated");
                }
                fileChannel.close();
                sleep(Configuration.IDLE_SLEEP_MS);
                continue;
            }
            return fileChannel.map(READ_WRITE, 0, fileSize);
        } catch (final NoSuchFileException ignore) {
        } catch (final IOException ex) {
            throw new AeronException("cannot open CnC file", ex);
        }
    }
}
Also used : AeronException(io.aeron.exceptions.AeronException) FileChannel(java.nio.channels.FileChannel) DriverTimeoutException(io.aeron.exceptions.DriverTimeoutException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Aggregations

DriverTimeoutException (io.aeron.exceptions.DriverTimeoutException)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)3 ManyToOneRingBuffer (org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer)2 AeronException (io.aeron.exceptions.AeronException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 DirectBuffer (org.agrona.DirectBuffer)1 NDArrayMessageChunk (org.nd4j.aeron.ipc.chunk.NDArrayMessageChunk)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1