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;
}
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;
}
}
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;
}
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);
}
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);
}
}
Aggregations