use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.
the class NDArrayMessage method chunks.
/**
* Returns an array of
* message chunks meant to be sent
* in parallel.
* Each message chunk has the layout:
* messageType
* number of chunks
* chunkSize
* length of uuid
* uuid
* buffer index
* actual raw data
* @param message the message to turn into chunks
* @param chunkSize the chunk size
* @return an array of buffers
*/
public static NDArrayMessageChunk[] chunks(NDArrayMessage message, int chunkSize) {
int numChunks = numChunksForMessage(message, chunkSize);
NDArrayMessageChunk[] ret = new NDArrayMessageChunk[numChunks];
DirectBuffer wholeBuffer = NDArrayMessage.toBuffer(message);
String messageId = UUID.randomUUID().toString();
for (int i = 0; i < ret.length; i++) {
// data: only grab a chunk of the data
ByteBuffer view = (ByteBuffer) wholeBuffer.byteBuffer().asReadOnlyBuffer().position(i * chunkSize);
view.limit(Math.min(i * chunkSize + chunkSize, wholeBuffer.capacity()));
view.order(ByteOrder.nativeOrder());
view = view.slice();
NDArrayMessageChunk chunk = NDArrayMessageChunk.builder().id(messageId).chunkSize(chunkSize).numChunks(numChunks).messageType(MessageType.CHUNKED).chunkIndex(i).data(view).build();
// insert in to the array itself
ret[i] = chunk;
}
return ret;
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class ArchiveTest method shouldRecoverRecordingWithNonZeroStartPosition.
@Test
public void shouldRecoverRecordingWithNonZeroStartPosition() {
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
long resultingPosition;
final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
final long recordingId;
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
AeronArchive archive = AeronArchive.connect()) {
final int termLength = 128 * 1024;
final int initialTermId = 29;
final String channel = new ChannelUriStringBuilder().media(CommonContext.IPC_MEDIA).initialPosition(initialPosition, initialTermId, termLength).build();
final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));
while ((resultingPosition = publication.offer(buffer)) <= 0) {
Tests.yield();
}
final Aeron aeron = archive.context().aeron();
int counterId;
final int sessionId = publication.sessionId();
final CountersReader countersReader = aeron.countersReader();
while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId))) {
Tests.yield();
}
recordingId = RecordingPos.getRecordingId(countersReader, counterId);
while (countersReader.getCounterValue(counterId) < resultingPosition) {
Tests.yield();
}
}
try (Catalog catalog = openCatalog(archiveCtx)) {
final Catalog.CatalogEntryProcessor catalogEntryProcessor = (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> descriptorEncoder.stopPosition(Aeron.NULL_VALUE);
assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
}
final Archive.Context archiveCtxClone = archiveCtx.clone();
final MediaDriver.Context driverCtxClone = driverCtx.clone();
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
AeronArchive archive = AeronArchive.connect()) {
assertEquals(initialPosition, archive.getStartPosition(recordingId));
assertEquals(resultingPosition, archive.getStopPosition(recordingId));
} finally {
archiveCtxClone.deleteDirectory();
driverCtxClone.deleteDirectory();
}
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class ClusterTool method printDriverErrors.
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);
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class LocalSocketAddressStatus method findAddresses.
/**
* Find the list of currently bound local sockets.
*
* @param countersReader for the connected driver.
* @param channelStatus value for the channel which aggregates the transports.
* @param channelStatusId identity of the counter for the channel which aggregates the transports.
* @return the list of active bound local socket addresses.
*/
public static List<String> findAddresses(final CountersReader countersReader, final long channelStatus, final int channelStatusId) {
if (channelStatus != ChannelEndpointStatus.ACTIVE) {
return Collections.emptyList();
}
final ArrayList<String> bindings = new ArrayList<>(2);
final DirectBuffer buffer = countersReader.metaDataBuffer();
for (int i = 0, size = countersReader.maxCounterId(); i < size; i++) {
final int counterState = countersReader.getCounterState(i);
if (RECORD_ALLOCATED == counterState) {
if (countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID) {
final int recordOffset = CountersReader.metaDataOffset(i);
final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;
if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) && ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i)) {
final int length = buffer.getInt(keyIndex + LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET);
if (length > 0) {
bindings.add(buffer.getStringWithoutLengthAscii(keyIndex + LOCAL_SOCKET_ADDRESS_STRING_OFFSET, length));
}
}
}
} else if (RECORD_UNUSED == counterState) {
break;
}
}
return bindings;
}
use of org.agrona.DirectBuffer in project Aeron by real-logic.
the class HeartbeatTimestamp method isActive.
/**
* Is the counter active for usage? Checks to see if reclaimed or reused and matches registration id.
*
* @param countersReader to search within.
* @param counterId to test.
* @param counterTypeId to validate type.
* @param registrationId for the entity.
* @return true if still valid otherwise false.
*/
public static boolean isActive(final CountersReader countersReader, final int counterId, final int counterTypeId, final long registrationId) {
final DirectBuffer buffer = countersReader.metaDataBuffer();
final int recordOffset = CountersReader.metaDataOffset(counterId);
return countersReader.getCounterTypeId(counterId) == counterTypeId && buffer.getLong(recordOffset + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId && countersReader.getCounterState(counterId) == RECORD_ALLOCATED;
}
Aggregations