Search in sources :

Example 26 with DirectBuffer

use of org.agrona.DirectBuffer in project zeebe by zeebe-io.

the class GossipEventCreationHelper method writeRaftsIntoBuffer.

public static int writeRaftsIntoBuffer(List<RaftStateComposite> rafts, MutableDirectBuffer directBuffer) {
    final int raftCount = rafts.size();
    int offset = 0;
    directBuffer.putInt(offset, raftCount, ByteOrder.LITTLE_ENDIAN);
    offset += SIZE_OF_INT;
    for (int i = 0; i < raftCount; i++) {
        final RaftStateComposite raft = rafts.get(i);
        directBuffer.putInt(offset, raft.getPartition(), ByteOrder.LITTLE_ENDIAN);
        offset += SIZE_OF_INT;
        final DirectBuffer currentTopicName = raft.getTopicName();
        directBuffer.putInt(offset, currentTopicName.capacity(), ByteOrder.LITTLE_ENDIAN);
        offset += SIZE_OF_INT;
        directBuffer.putBytes(offset, currentTopicName, 0, currentTopicName.capacity());
        offset += currentTopicName.capacity();
        directBuffer.putByte(offset, raft.getRaftState() == RaftState.LEADER ? (byte) 1 : (byte) 0);
        offset += SIZE_OF_BYTE;
    }
    return offset;
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer)

Example 27 with DirectBuffer

use of org.agrona.DirectBuffer in project zeebe by zeebe-io.

the class SubscribeProcessor method processEvent.

@Override
public void processEvent() {
    final DirectBuffer subscriptionName = subscriberEvent.getName();
    if (subscriptionName.capacity() > maximumNameLength) {
        failedRequestState.wrapError("Cannot open topic subscription " + subscriberEvent.getNameAsString() + ". Subscription name must be " + maximumNameLength + " characters or shorter.");
        state = failedRequestState;
        return;
    } else {
        state = createProcessorState;
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer)

Example 28 with DirectBuffer

use of org.agrona.DirectBuffer in project zeebe by zeebe-io.

the class IncidentMap method wrapIncidentKey.

public IncidentMap wrapIncidentKey(long key) {
    final DirectBuffer result = map.get(key);
    if (result != null) {
        buffer.putBytes(0, result, 0, result.capacity());
    }
    this.isRead = result != null;
    this.key = key;
    return this;
}
Also used : DirectBuffer(org.agrona.DirectBuffer)

Example 29 with DirectBuffer

use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.

the class NDArrayMessageTest method testNDArrayMessageToAndFrom.

@Test
public void testNDArrayMessageToAndFrom() {
    NDArrayMessage message = NDArrayMessage.wholeArrayUpdate(Nd4j.scalar(1.0));
    DirectBuffer bufferConvert = NDArrayMessage.toBuffer(message);
    bufferConvert.byteBuffer().rewind();
    NDArrayMessage newMessage = NDArrayMessage.fromBuffer(bufferConvert, 0);
    assertEquals(message, newMessage);
    INDArray compressed = Nd4j.getCompressor().compress(Nd4j.scalar(1.0), "GZIP");
    NDArrayMessage messageCompressed = NDArrayMessage.wholeArrayUpdate(compressed);
    DirectBuffer bufferConvertCompressed = NDArrayMessage.toBuffer(messageCompressed);
    NDArrayMessage newMessageTest = NDArrayMessage.fromBuffer(bufferConvertCompressed, 0);
    assertEquals(messageCompressed, newMessageTest);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 30 with DirectBuffer

use of org.agrona.DirectBuffer 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)

Aggregations

DirectBuffer (org.agrona.DirectBuffer)102 Header (io.aeron.logbuffer.Header)18 MutableDirectBuffer (org.agrona.MutableDirectBuffer)16 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)15 File (java.io.File)14 MappedByteBuffer (java.nio.MappedByteBuffer)11 Test (org.junit.Test)11 MediaDriver (io.aeron.driver.MediaDriver)10 CountersReader (org.agrona.concurrent.status.CountersReader)10 ClusteredService (io.aeron.cluster.service.ClusteredService)8 Publication (io.aeron.Publication)7 ClientSession (io.aeron.cluster.service.ClientSession)7 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)7 ThreadingMode (io.aeron.driver.ThreadingMode)6 FragmentHandler (io.aeron.logbuffer.FragmentHandler)6 ByteBuffer (java.nio.ByteBuffer)6 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)5 InterruptAfter (io.aeron.test.InterruptAfter)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 LogBufferDescriptor (io.aeron.logbuffer.LogBufferDescriptor)4