Search in sources :

Example 1 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class StreamInstanceArchiveFragmentReader method readFragment.

private int readFragment(final FragmentHandler fragmentHandler, final UnsafeBuffer termMappedUnsafeBuffer, final int fragmentOffset, final int frameLength, final Header fragmentHeader) {
    if (frameLength <= 0) {
        final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
        headerFlyweight.wrap(termMappedUnsafeBuffer, fragmentOffset, DataHeaderFlyweight.HEADER_LENGTH);
        throw new IllegalStateException("Broken frame with length <= 0: " + headerFlyweight);
    }
    if (fragmentHeader.type() != PADDING_FRAME_TYPE) {
        final int fragmentDataOffset = fragmentOffset + DataHeaderFlyweight.DATA_OFFSET;
        final int fragmentDataLength = frameLength - DataHeaderFlyweight.HEADER_LENGTH;
        fragmentHandler.onFragment(termMappedUnsafeBuffer, fragmentDataOffset, fragmentDataLength, fragmentHeader);
        return 1;
    }
    return 0;
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight)

Example 2 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class ReplaySessionTest method setup.

@Before
public void setup() throws Exception {
    archiveFolder = makeTempFolder();
    proxy = Mockito.mock(ArchiverProtocolProxy.class);
    final EpochClock epochClock = mock(EpochClock.class);
    final StreamInstance streamInstance = new StreamInstance("source", 1, "channel", 1);
    try (StreamInstanceArchiveWriter writer = new StreamInstanceArchiveWriter(archiveFolder, epochClock, STREAM_INSTANCE_ID, TERM_BUFFER_LENGTH, INITIAL_TERM_ID, streamInstance)) {
        when(epochClock.time()).thenReturn(42L);
        final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
        buffer.setMemory(0, TERM_BUFFER_LENGTH, (byte) 0);
        final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
        headerFlyweight.wrap(buffer, INITIAL_TERM_OFFSET, DataHeaderFlyweight.HEADER_LENGTH);
        headerFlyweight.termOffset(INITIAL_TERM_OFFSET).termId(INITIAL_TERM_ID).headerType(DataHeaderFlyweight.HDR_TYPE_DATA).frameLength(1024);
        buffer.setMemory(INITIAL_TERM_OFFSET + DataHeaderFlyweight.HEADER_LENGTH, 1024 - DataHeaderFlyweight.HEADER_LENGTH, (byte) 1);
        final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
        header.buffer(buffer);
        header.offset(INITIAL_TERM_OFFSET);
        Assert.assertEquals(1024, header.frameLength());
        Assert.assertEquals(INITIAL_TERM_ID, header.termId());
        Assert.assertEquals(INITIAL_TERM_OFFSET, header.offset());
        writer.onFragment(buffer, header.offset() + DataHeaderFlyweight.HEADER_LENGTH, header.frameLength() - DataHeaderFlyweight.HEADER_LENGTH, header);
        when(epochClock.time()).thenReturn(84L);
    }
    try (StreamInstanceArchiveChunkReader chunkReader = new StreamInstanceArchiveChunkReader(STREAM_INSTANCE_ID, archiveFolder, INITIAL_TERM_ID, TERM_BUFFER_LENGTH, INITIAL_TERM_ID, INITIAL_TERM_OFFSET, 1024)) {
        chunkReader.readChunk((termBuff, termOffset, length) -> {
            final DataHeaderFlyweight hf = new DataHeaderFlyweight();
            hf.wrap(termBuff, termOffset, DataHeaderFlyweight.HEADER_LENGTH);
            Assert.assertEquals(INITIAL_TERM_ID, hf.termId());
            Assert.assertEquals(1024, hf.frameLength());
            return true;
        }, 1024);
    }
    final StreamInstanceArchiveFragmentReader reader = new StreamInstanceArchiveFragmentReader(STREAM_INSTANCE_ID, archiveFolder);
    final int polled = reader.poll((b, offset, length, h) -> {
        Assert.assertEquals(offset, INITIAL_TERM_OFFSET + DataHeaderFlyweight.HEADER_LENGTH);
        Assert.assertEquals(length, 1024 - DataHeaderFlyweight.HEADER_LENGTH);
    });
    Assert.assertEquals(1, polled);
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight)

Example 3 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project aeron by real-logic.

the class ReplaySessionTest method before.

@Before
public void before() {
    when(position.getWeak()).then((invocation) -> positionLong);
    when(position.get()).then((invocation) -> positionLong);
    when(mockArchiveConductor.catalog()).thenReturn(mockCatalog);
    doAnswer((invocation) -> {
        positionLong = invocation.getArgument(0);
        return null;
    }).when(position).setOrdered(anyLong());
    doAnswer((invocation) -> {
        final long delta = invocation.getArgument(0);
        positionLong += delta;
        return null;
    }).when(position).getAndAddOrdered(anyLong());
    context = new Archive.Context().archiveDir(archiveDir).epochClock(epochClock);
    recordingSummary.recordingId = RECORDING_ID;
    recordingSummary.startPosition = START_POSITION;
    recordingSummary.segmentFileLength = context.segmentFileLength();
    recordingSummary.initialTermId = INITIAL_TERM_ID;
    recordingSummary.termBufferLength = TERM_BUFFER_LENGTH;
    recordingSummary.mtuLength = MTU_LENGTH;
    recordingSummary.streamId = STREAM_ID;
    recordingSummary.sessionId = SESSION_ID;
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, JOIN_POSITION, TERM_BUFFER_LENGTH, context, ARCHIVE_DIR_CHANNEL, position);
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, 0, FrameDescriptor.UNFRAGMENTED, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 1, FrameDescriptor.BEGIN_FRAG_FLAG, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 2, FrameDescriptor.END_FRAG_FLAG, HDR_TYPE_DATA);
    recordFragment(writer, buffer, headerFwt, header, 3, FrameDescriptor.UNFRAGMENTED, HDR_TYPE_PAD);
    writer.close();
    recordingSummary.stopPosition = START_POSITION + 4 * FRAME_LENGTH;
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Before(org.junit.Before)

Example 4 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project aeron by real-logic.

the class CatalogTest method shouldFixTimestampAndPositionAfterFailureSamePage.

@Test
public void shouldFixTimestampAndPositionAfterFailureSamePage() throws Exception {
    final long newRecordingId = newRecording();
    new File(archiveDir, segmentFileName(newRecordingId, 0)).createNewFile();
    new File(archiveDir, segmentFileName(newRecordingId, 1)).createNewFile();
    new File(archiveDir, segmentFileName(newRecordingId, 2)).createNewFile();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 3));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = ByteBuffer.allocateDirect(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(1024);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(128);
        log.write(bb, 1024);
        bb.clear();
        flyweight.frameLength(0);
        log.write(bb, 1024 + 128);
    }
    try (Catalog catalog = new Catalog(archiveDir, clock)) {
        catalog.forEntry((he, hd, e, decoder) -> {
            assertThat(decoder.stopTimestamp(), is(NULL_TIMESTAMP));
            assertThat(decoder.stopPosition(), is(NULL_POSITION));
        }, newRecordingId);
    }
    currentTimeMs = 42L;
    try (Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock)) {
        catalog.forEntry((he, hd, e, decoder) -> {
            assertThat(decoder.stopTimestamp(), is(42L));
            assertThat(decoder.stopPosition(), is(SEGMENT_FILE_SIZE * 3 + 1024L + 128L));
        }, newRecordingId);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project aeron by real-logic.

the class CatalogTest method shouldFixTimestampAndPositionAfterFailureFullSegment.

@Test
public void shouldFixTimestampAndPositionAfterFailureFullSegment() throws Exception {
    final long newRecordingId = newRecording();
    final long expectedLastFrame = SEGMENT_FILE_SIZE - 128;
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = ByteBuffer.allocateDirect(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength((int) expectedLastFrame);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(128);
        log.write(bb, expectedLastFrame);
        bb.clear();
        flyweight.frameLength(0);
        log.write(bb, expectedLastFrame + 128);
        log.truncate(SEGMENT_FILE_SIZE);
    }
    try (Catalog catalog = new Catalog(archiveDir, clock)) {
        catalog.forEntry((he, hd, e, decoder) -> {
            assertThat(decoder.stopTimestamp(), is(NULL_TIMESTAMP));
            e.stopPosition(NULL_POSITION);
        }, newRecordingId);
    }
    currentTimeMs = 42L;
    try (Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock)) {
        catalog.forEntry((he, hd, e, decoder) -> {
            assertThat(decoder.stopTimestamp(), is(42L));
            assertThat(decoder.stopPosition(), is(expectedLastFrame));
        }, newRecordingId);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)25 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)16 ByteBuffer (java.nio.ByteBuffer)11 FileChannel (java.nio.channels.FileChannel)10 File (java.io.File)9 Test (org.junit.jupiter.api.Test)8 Catalog (io.aeron.archive.Catalog)6 Header (io.aeron.logbuffer.Header)6 MappedByteBuffer (java.nio.MappedByteBuffer)6 NULL_POSITION (io.aeron.archive.client.AeronArchive.NULL_POSITION)5 HEADER_LENGTH (io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH)5 StandardOpenOption (java.nio.file.StandardOpenOption)5 EpochClock (org.agrona.concurrent.EpochClock)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 CATALOG_FILE_NAME (io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME)4 FILE_IO_MAX_LENGTH_DEFAULT (io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT)4 Archive.segmentFileName (io.aeron.archive.Archive.segmentFileName)4 Checksum (io.aeron.archive.checksum.Checksum)4 NULL_TIMESTAMP (io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP)4 ArchiveException (io.aeron.archive.client.ArchiveException)4