Search in sources :

Example 31 with DirectBuffer

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;
}
Also used : DirectBuffer(org.agrona.DirectBuffer) NDArrayMessageChunk(org.nd4j.aeron.ipc.chunk.NDArrayMessageChunk) ByteBuffer(java.nio.ByteBuffer)

Example 32 with DirectBuffer

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();
    }
}
Also used : Context(io.aeron.archive.Archive.Context) CommonContext(io.aeron.CommonContext) RecordingSubscriptionDescriptorConsumer(io.aeron.archive.client.RecordingSubscriptionDescriptorConsumer) IntConsumer(java.util.function.IntConsumer) LOCAL(io.aeron.archive.codecs.SourceLocation.LOCAL) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) AeronArchive(io.aeron.archive.client.AeronArchive) DEDICATED(io.aeron.archive.ArchiveThreadingMode.DEDICATED) Publication(io.aeron.Publication) MediaDriver(io.aeron.driver.MediaDriver) ManyToOneConcurrentLinkedQueue(org.agrona.concurrent.ManyToOneConcurrentLinkedQueue) AuthorisationServiceSupplier(io.aeron.security.AuthorisationServiceSupplier) InterruptingTestCallback(io.aeron.test.InterruptingTestCallback) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter) CountDownLatch(java.util.concurrent.CountDownLatch) TruncateRecordingRequestDecoder(io.aeron.archive.codecs.TruncateRecordingRequestDecoder) EXCLUDE(org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) DirectBuffer(org.agrona.DirectBuffer) Mockito.mock(org.mockito.Mockito.mock) Tests(io.aeron.test.Tests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) CountersReader(org.agrona.concurrent.status.CountersReader) EnumSource(org.junit.jupiter.params.provider.EnumSource) ArrayList(java.util.ArrayList) SystemEpochClock(org.agrona.concurrent.SystemEpochClock) Context(io.aeron.archive.Archive.Context) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) ValueSource(org.junit.jupiter.params.provider.ValueSource) RecordingPos(io.aeron.archive.status.RecordingPos) Aeron(io.aeron.Aeron) FileStore(java.nio.file.FileStore) SHARED(io.aeron.archive.ArchiveThreadingMode.SHARED) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) LogBufferDescriptor(io.aeron.logbuffer.LogBufferDescriptor) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MIN_CAPACITY(io.aeron.archive.Catalog.MIN_CAPACITY) ArchiveException(io.aeron.archive.client.ArchiveException) ThreadingMode(io.aeron.driver.ThreadingMode) Assertions(org.junit.jupiter.api.Assertions) AeronArchive(io.aeron.archive.client.AeronArchive) ChannelUriStringBuilder(io.aeron.ChannelUriStringBuilder) Publication(io.aeron.Publication) AeronArchive(io.aeron.archive.client.AeronArchive) Aeron(io.aeron.Aeron) CountersReader(org.agrona.concurrent.status.CountersReader) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Context(io.aeron.archive.Archive.Context) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with DirectBuffer

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);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) File(java.io.File) ClusterMarkFile(io.aeron.cluster.service.ClusterMarkFile)

Example 34 with DirectBuffer

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;
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer) ArrayList(java.util.ArrayList)

Example 35 with DirectBuffer

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;
}
Also used : MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer)

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