io.aeron.Image Java Examples

The following examples show how to use io.aeron.Image. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FixEngine.java    From artio with Apache License 2.0 6 votes vote down vote up
private Image replayImage(final String name, final int replaySessionId)
{
    final Subscription subscription = aeron.addSubscription(
        IPC_CHANNEL, configuration.outboundReplayStream());
    StreamInformation.print(name, subscription, configuration);

    // Await replay publication
    while (true)
    {
        final Image image = subscription.imageBySessionId(replaySessionId);
        if (image != null)
        {
            return image;
        }

        invokeAeronConductor();

        Thread.yield();
    }
}
 
Example #2
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void onBlockThrowsArchiveExceptionIfCurrentThreadWasInterrupted() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();

    assertFalse(Thread.interrupted());
    try
    {
        Thread.currentThread().interrupt();
        final ArchiveException exception = assertThrows(
            ArchiveException.class,
            () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
        assertEquals(GENERIC, exception.errorCode());
        assertEquals("file closed by interrupt, recording aborted", exception.getMessage());
        assertTrue(recordingWriter.isClosed());
    }
    finally
    {
        assertTrue(Thread.interrupted());
    }
}
 
Example #3
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void closeShouldCloseTheUnderlyingFile() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();
    assertFalse(recordingWriter.isClosed());

    recordingWriter.close();
    assertTrue(recordingWriter.isClosed());

    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    delete(segmentFile.toPath()); // can delete since the underlying FileChannel was already closed
}
 
Example #4
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void initShouldOpenAFileChannel() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    final File segmentFile = segmentFile(1, 0);
    assertFalse(segmentFile.exists());

    try
    {
        recordingWriter.init();
        assertTrue(segmentFile.exists());
    }
    finally
    {
        recordingWriter.close();
    }
}
 
Example #5
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void onBlockThrowNullPointerExceptionIfChecksumBufferIsNullButCrcIsEnabled() throws IOException
{
    final Image image = mockImage(0L);
    final Context ctx = new Context().archiveDir(archiveDir);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, ctx, null, null, crc32());
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64));
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 1024);

    recordingWriter.init();

    try
    {
        assertThrows(
            NullPointerException.class,
            () -> recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1));
    }
    finally
    {
        recordingWriter.close();
    }
}
 
Example #6
Source File: SingleNodeCluster.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void onStart(final Cluster cluster, final Image snapshotImage)
{
    this.cluster = cluster;
    this.idleStrategy = cluster.idleStrategy();

    if (null != snapshotImage)
    {
        System.out.println("onStart load snapshot");
        final FragmentHandler fragmentHandler =
            (buffer, offset, length, header) -> messageCount = buffer.getInt(offset);

        while (snapshotImage.poll(fragmentHandler, 1) <= 0)
        {
            idleStrategy.idle();
        }

        System.out.println("snapshot messageCount=" + messageCount);
    }
}
 
Example #7
Source File: Framer.java    From artio with Apache License 2.0 6 votes vote down vote up
private void tryAcquireLibrarySessions(final LiveLibraryInfo library)
{
    final int librarySessionId = library.aeronSessionId();
    final Image image = librarySubscription.imageBySessionId(librarySessionId);
    long libraryPosition = finalImagePositions.lookupPosition(librarySessionId);
    if (image != null)
    {
        libraryPosition = image.position();
    }

    final boolean indexed = sentIndexedPosition(librarySessionId, libraryPosition);
    if (!configuration.logOutboundMessages() || indexed)
    {
        acquireLibrarySessions(library);
    }
    else
    {
        library.acquireAtPosition(libraryPosition);
        librariesBeingAcquired.add(library);
    }
}
 
Example #8
Source File: RecordingSessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static Image mockImage(final Subscription subscription)
{
    final Image image = mock(Image.class);

    when(image.sessionId()).thenReturn(SESSION_ID);
    when(image.initialTermId()).thenReturn(INITIAL_TERM_ID);
    when(image.sourceIdentity()).thenReturn(SOURCE_IDENTITY);
    when(image.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(image.mtuLength()).thenReturn(MTU_LENGTH);
    when(image.joinPosition()).thenReturn(START_POSITION);
    when(image.subscription()).thenReturn(subscription);

    return image;
}
 
Example #9
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
private Image mockImage(final long joinPosition)
{
    final Image image = mock(Image.class);
    when(image.termBufferLength()).thenReturn(TERM_LENGTH);
    when(image.joinPosition()).thenReturn(joinPosition);
    return image;
}
 
Example #10
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockShouldNotComputeCrcForThePaddingFrame() throws IOException
{
    final Image image = mockImage(0L);
    final Context ctx = new Context().archiveDir(archiveDir);
    final UnsafeBuffer checksumBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64));
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, ctx, null, checksumBuffer, crc32());

    recordingWriter.init();

    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(512));
    final int length = 128;
    final byte[] data = new byte[length - HEADER_LENGTH];

    final int sessionId = 5;
    final int termId = 18;
    fill(data, (byte)99);
    frameType(termBuffer, 0, HDR_TYPE_PAD);
    frameTermId(termBuffer, 0, termId);
    frameLengthOrdered(termBuffer, 0, length);
    frameSessionId(termBuffer, 0, sessionId);
    termBuffer.putBytes(HEADER_LENGTH, data);

    recordingWriter.onBlock(termBuffer, 0, HEADER_LENGTH, -1, -1);
    recordingWriter.close();

    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, 0));
    assertEquals(termId, frameTermId(fileBuffer, 0));
    assertEquals(length, frameLength(fileBuffer, 0));
    assertEquals(sessionId, frameSessionId(fileBuffer, 0));
}
 
Example #11
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void initThrowsIOExceptionIfItCannotOpenAFileChannel() throws IOException
{
    final File notADirectory = new File(archiveDir, "dummy.txt");
    assertTrue(notADirectory.createNewFile());

    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(notADirectory), null, null, null);

    assertThrows(IOException.class, recordingWriter::init);
    assertTrue(recordingWriter.isClosed());
}
 
Example #12
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void roundTripMessages(
    final FragmentHandler fragmentHandler,
    final Publication pingPublication,
    final Subscription pongSubscription,
    final long numMessages)
{
    while (!pongSubscription.isConnected())
    {
        Thread.yield();
    }

    final Image image = pongSubscription.imageAtIndex(0);

    for (long i = 0; i < numMessages; i++)
    {
        long offeredPosition;

        do
        {
            OFFER_BUFFER.putLong(0, System.nanoTime());
        }
        while ((offeredPosition = pingPublication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null)) < 0L);

        PONG_HANDLER_IDLE_STRATEGY.reset();
        do
        {
            while (image.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT) <= 0)
            {
                PONG_HANDLER_IDLE_STRATEGY.idle();
            }
        }
        while (image.position() < offeredPosition);
    }
}
 
Example #13
Source File: EmbeddedPingPong.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void availablePongImageHandler(final Image image)
{
    final Subscription subscription = image.subscription();
    if (PONG_STREAM_ID == subscription.streamId() && PONG_CHANNEL.equals(subscription.channel()))
    {
        PONG_IMAGE_LATCH.countDown();
    }
}
 
Example #14
Source File: BasicAuctionClusteredService.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void onStart(final Cluster cluster, final Image snapshotImage)
{
    this.cluster = cluster;                      // <1>
    this.idleStrategy = cluster.idleStrategy();  // <2>

    if (null != snapshotImage)                   // <3>
    {
        loadSnapshot(cluster, snapshotImage);
    }
}
 
Example #15
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockShouldWriteHeaderOfThePaddingFrameAndAdvanceFilePositionByThePaddingLength() throws IOException
{
    final int segmentOffset = 96;
    final long startPosition = 7 * TERM_LENGTH + segmentOffset;
    final Image image = mockImage(startPosition);
    final RecordingWriter recordingWriter = new RecordingWriter(
        5, startPosition, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(1024));
    frameType(termBuffer, 0, HDR_TYPE_PAD);
    frameLengthOrdered(termBuffer, 0, 1024);
    frameSessionId(termBuffer, 0, 111);

    final byte[] data = new byte[992];
    fill(data, (byte)-1);
    termBuffer.putBytes(HEADER_LENGTH, data);

    recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1);

    recordingWriter.close();
    final File segmentFile = segmentFile(5, startPosition);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));

    final byte[] preamble = new byte[segmentOffset];
    fileBuffer.getBytes(0, preamble, 0, segmentOffset);
    assertArrayEquals(new byte[segmentOffset], preamble);
    assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, segmentOffset));
    assertEquals(1024, frameLength(fileBuffer, segmentOffset));
    assertEquals(111, frameSessionId(fileBuffer, segmentOffset));

    final byte[] fileBytes = new byte[992];
    fileBuffer.getBytes(segmentOffset + HEADER_LENGTH, fileBytes, 0, 992);
    assertArrayEquals(new byte[992], fileBytes);
}
 
Example #16
Source File: MultipleSubscribersWithFragmentAssembly.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * This handler is called when image is unavailable.
 *
 * @param image that has gone inactive.
 */
public static void eventUnavailableImage(final Image image)
{
    final Subscription subscription = image.subscription();
    System.out.format(
        "inactive image on %s streamId %d sessionId %x%n",
        subscription.channel(), subscription.streamId(), image.sessionId());
}
 
Example #17
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockShouldWriteHeaderAndContentsOfTheNonPaddingFrame() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(128));
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 128);
    final byte[] data = new byte[96];
    fill(data, (byte)7);
    termBuffer.putBytes(HEADER_LENGTH, data);

    recordingWriter.onBlock(termBuffer, 0, 128, -1, -1);

    recordingWriter.close();
    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(128, frameLength(fileBuffer, 0));
    assertEquals(0, frameSessionId(fileBuffer, 0));

    final byte[] fileBytes = new byte[96];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 96);
    assertArrayEquals(data, fileBytes);
}
 
Example #18
Source File: MultipleSubscribersWithFragmentAssembly.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Print the information for an available image to stdout.
 *
 * @param image that has been created.
 */
public static void eventAvailableImage(final Image image)
{
    final Subscription subscription = image.subscription();
    System.out.format(
        "new image on %s streamId %x sessionId %x from %s%n",
        subscription.channel(), subscription.streamId(), image.sessionId(), image.sourceIdentity());
}
 
Example #19
Source File: TestNode.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void onStart(final Cluster cluster, final Image snapshotImage)
{
    super.onStart(cluster, snapshotImage);

    if (null != snapshotImage)
    {
        activeSessionCount = cluster.clientSessions().size();

        final FragmentHandler handler =
            (buffer, offset, length, header) -> messageCount = buffer.getInt(offset);

        int fragmentCount = 0;
        while (true)
        {
            final int fragments = snapshotImage.poll(handler, 10);
            fragmentCount += fragments;

            if (snapshotImage.isClosed() || snapshotImage.isEndOfStream())
            {
                break;
            }

            idleStrategy.idle(fragments);
        }

        if (fragmentCount != SNAPSHOT_FRAGMENT_COUNT)
        {
            throw new AgentTerminationException(
                "unexpected snapshot length: expected=" + SNAPSHOT_FRAGMENT_COUNT + " actual=" + fragmentCount);
        }

        wasSnapshotLoaded = true;
    }
}
 
Example #20
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockThrowsNullPointerExceptionIfInitWasNotCalled()
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);

    assertThrows(
        NullPointerException.class,
        () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
    assertTrue(recordingWriter.isClosed());
}
 
Example #21
Source File: RecordingWriter.java    From aeron with Apache License 2.0 5 votes vote down vote up
RecordingWriter(
    final long recordingId,
    final long startPosition,
    final int segmentLength,
    final Image image,
    final Archive.Context ctx,
    final FileChannel archiveDirChannel,
    final UnsafeBuffer checksumBuffer,
    final Checksum checksum)
{
    this.recordingId = recordingId;
    this.archiveDirChannel = archiveDirChannel;
    this.segmentLength = segmentLength;

    archiveDir = ctx.archiveDir();
    forceWrites = ctx.fileSyncLevel() > 0;
    forceMetadata = ctx.fileSyncLevel() > 1;

    countedErrorHandler = ctx.countedErrorHandler();

    this.checksumBuffer = checksumBuffer;
    this.checksum = checksum;

    final int termLength = image.termBufferLength();
    final long joinPosition = image.joinPosition();
    segmentBasePosition = segmentFileBasePosition(startPosition, joinPosition, termLength, segmentLength);
    segmentOffset = (int)(joinPosition - segmentBasePosition);
}
 
Example #22
Source File: BasicAuctionClusteredService.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void loadSnapshot(final Cluster cluster, final Image snapshotImage)
{
    final MutableBoolean allDataLoaded = new MutableBoolean(false);

    while (!snapshotImage.isEndOfStream())                                                       // <1>
    {
        final int fragmentsPolled = snapshotImage.poll(
            (buffer, offset, length, header) -> // <2>
            {
                assert length >= SNAPSHOT_MESSAGE_LENGTH;                                        // <3>

                final long customerId = buffer.getLong(offset + SNAPSHOT_CUSTOMER_ID_OFFSET);
                final long price = buffer.getLong(offset + SNAPSHOT_PRICE_OFFSET);

                auction.loadInitialState(price, customerId);                                     // <4>

                allDataLoaded.set(true);
            },
            1);

        if (allDataLoaded.value)                                                                 // <5>
        {
            break;
        }

        idleStrategy.idle(fragmentsPolled);                                                      // <6>
    }

    assert snapshotImage.isEndOfStream();                                                        // <7>
    assert allDataLoaded.value;
}
 
Example #23
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static void pipeMessages(
    final Subscription subscription, final ExclusivePublication publication, final AtomicBoolean running)
{
    final IdleStrategy idleStrategy = idleStrategy();
    final BufferClaim bufferClaim = new BufferClaim();
    final FragmentHandler dataHandler =
        (buffer, offset, length, header) ->
        {
            long result;
            while ((result = publication.tryClaim(length, bufferClaim)) <= 0)
            {
                checkPublicationResult(result);
            }

            bufferClaim
                .flags(header.flags())
                .putBytes(buffer, offset, length)
                .commit();
        };

    final Image image = subscription.imageAtIndex(0);
    final int fragmentLimit = fragmentLimit();

    while (true)
    {
        final int fragmentsRead = image.poll(dataHandler, fragmentLimit);
        if (0 == fragmentsRead)
        {
            if (image.isClosed() || !running.get())
            {
                break;
            }
        }

        idleStrategy.idle(fragmentsRead);
    }
}
 
Example #24
Source File: SequenceNumberIndexTest.java    From artio with Apache License 2.0 5 votes vote down vote up
private long indexRecord()
{
    long position = 0;
    while (position < 1)
    {
        position = publication.offer(buffer, START, fragmentLength());

        Thread.yield();
    }

    /*System.out.println("position = " + position);
    System.out.println("p = " + p);*/


    Image image = null;
    while (image == null || image.position() < position)
    {
        if (image == null)
        {
            image = subscription.imageBySessionId(publication.sessionId());
        }

        if (image != null)
        {
            image.poll(writer, 1);
        }
    }

    return position;
}
 
Example #25
Source File: FixArchiveScanner.java    From artio with Apache License 2.0 5 votes vote down vote up
private Image lookupImage(final Subscription replaySubscription, final int sessionId)
{
    Image image = null;

    while (image == null)
    {
        idleStrategy.idle();
        image = replaySubscription.imageBySessionId(sessionId);
    }
    idleStrategy.reset();

    return image;
}
 
Example #26
Source File: SubscriptionSlowPeeker.java    From artio with Apache License 2.0 5 votes vote down vote up
private LibrarySlowPeeker newLibraryPeeker(final int aeronSessionId)
{
    final Image peekImage = peekSubscription.imageBySessionId(aeronSessionId);
    final Image normalImage = normalSubscription.imageBySessionId(aeronSessionId);

    if (peekImage == null || normalImage == null)
    {
        return null;
    }

    return new LibrarySlowPeeker(peekImage, normalImage);
}
 
Example #27
Source File: FinalImagePositions.java    From artio with Apache License 2.0 5 votes vote down vote up
public void onUnavailableImage(final Image image)
{
    final Integer sessionId = image.sessionId();
    final long position = image.position();

    final Long old = sessionIdToPosition.remove(sessionId);
    if (old != LEAK_WITNESS) // lgtm [java/reference-equality-of-boxed-types]
    {
        sessionIdToPosition.put(sessionId, position);
    }
}
 
Example #28
Source File: AeronUtil.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
 * Print the information for an unavailable image to stdout.
 *
 * @param image that has gone inactive
 */
public static void printUnavailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.println(String.format("Unavailable image on %s streamId=%d sessionId=%d", subscription.channel(),
                    subscription.streamId(), image.sessionId()));
}
 
Example #29
Source File: AeronUtil.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Print the information for an available image to stdout.
 *
 * @param image that has been created
 */
public static void printAvailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.println(String.format("Available image on %s streamId=%d sessionId=%d from %s",
                    subscription.channel(), subscription.streamId(), image.sessionId(), image.sourceIdentity()));
}
 
Example #30
Source File: AeronUtil.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Print the information for an unavailable image to stdout.
 *
 * @param image that has gone inactive
 */
public static void printUnavailableImage(final Image image) {
    final Subscription subscription = image.subscription();
    System.out.println(String.format("Unavailable image on %s streamId=%d sessionId=%d", subscription.channel(),
                    subscription.streamId(), image.sessionId()));
}