Search in sources :

Example 21 with DataHeaderFlyweight

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

the class ReplaySessionTest method shouldCalculateBlockSizeBasedOnFullFragments.

@Test
public void shouldCalculateBlockSizeBasedOnFullFragments() throws IOException {
    context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, START_POSITION, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    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, 100, 11, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 128, 200, 22, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 352, 300, 33, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 672, 400, 44, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 1088, 500, 55, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, 1600, 6000, 66, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    writer.close();
    final int sessionId = 555;
    final int streamId = 777;
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION, 1_000_000, -1, mockReplayPub, mockControlSession, null, null)) {
        when(mockReplayPub.isClosed()).thenReturn(false);
        when(mockReplayPub.isConnected()).thenReturn(false);
        when(mockReplayPub.sessionId()).thenReturn(sessionId);
        when(mockReplayPub.streamId()).thenReturn(streamId);
        replaySession.doWork();
        assertEquals(replaySession.state(), ReplaySession.State.INIT);
        when(mockReplayPub.isConnected()).thenReturn(true);
        final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
        mockPublication(mockReplayPub, termBuffer);
        assertEquals(2, replaySession.doWork());
        assertThat(messageCounter, is(1));
        validateFrame(termBuffer, 0, 100, 11, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 128, 200, 22, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 352, 300, 33, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 672, 400, 44, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, 1088, 500, 55, UNFRAGMENTED, sessionId, streamId);
        verify(mockReplayPub, never()).appendPadding(anyInt());
    }
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test)

Example 22 with DataHeaderFlyweight

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

the class ReplaySessionTest method before.

@BeforeEach
public void before() throws IOException {
    context = new Archive.Context().segmentFileLength(SEGMENT_LENGTH).archiveDir(archiveDir).epochClock(epochClock).countedErrorHandler(countedErrorHandler);
    when(recordingPositionCounter.get()).then((invocation) -> recordingPosition);
    when(mockControlSession.archiveConductor()).thenReturn(mockArchiveConductor);
    when(mockArchiveConductor.catalog()).thenReturn(mockCatalog);
    when(mockArchiveConductor.context()).thenReturn(context);
    when(mockReplayPub.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(mockReplayPub.positionBitsToShift()).thenReturn(LogBufferDescriptor.positionBitsToShift(TERM_BUFFER_LENGTH));
    when(mockReplayPub.initialTermId()).thenReturn(INITIAL_TERM_ID);
    when(mockReplayPub.availableWindow()).thenReturn((long) TERM_BUFFER_LENGTH / 2);
    when(mockImage.termBufferLength()).thenReturn(TERM_BUFFER_LENGTH);
    when(mockImage.joinPosition()).thenReturn(JOIN_POSITION);
    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, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    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, INITIAL_TERM_OFFSET, FRAME_LENGTH, 0, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH * 2, FRAME_LENGTH, 2, END_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, INITIAL_TERM_OFFSET + FRAME_LENGTH * 3, FRAME_LENGTH, 3, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
    writer.close();
    recordingSummary.stopPosition = START_POSITION + 4 * FRAME_LENGTH;
}
Also used : AeronArchive(io.aeron.archive.client.AeronArchive) Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with DataHeaderFlyweight

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

the class CatalogTest method shouldFixTimestampAndPositionAfterFailureSamePage.

@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
void shouldFixTimestampAndPositionAfterFailureSamePage() throws Exception {
    final long newRecordingId = newRecording();
    new File(archiveDir, segmentFileName(newRecordingId, 0)).createNewFile();
    new File(archiveDir, segmentFileName(newRecordingId, SEGMENT_LENGTH)).createNewFile();
    new File(archiveDir, segmentFileName(newRecordingId, 2 * SEGMENT_LENGTH)).createNewFile();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 3 * SEGMENT_LENGTH));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = allocate(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)) {
        assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
            assertEquals(NULL_TIMESTAMP, descriptorDecoder.stopTimestamp());
            assertEquals(NULL_POSITION, descriptorDecoder.stopPosition());
        }));
    }
    currentTimeMs = 42L;
    try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer)) {
        assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
            assertEquals(42L, descriptorDecoder.stopTimestamp());
            assertEquals(SEGMENT_LENGTH * 3 + 1024L + 128L, descriptorDecoder.stopPosition());
        }));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Catalog(io.aeron.archive.Catalog) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) CatalogHeaderEncoder(io.aeron.archive.codecs.CatalogHeaderEncoder) ByteBuffer.allocate(java.nio.ByteBuffer.allocate) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) RecordingDescriptorHeaderDecoder(io.aeron.archive.codecs.RecordingDescriptorHeaderDecoder) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferUtil(org.agrona.BufferUtil) Checksum(io.aeron.archive.checksum.Checksum) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayUtil(org.agrona.collections.ArrayUtil) File(java.io.File) NULL_TIMESTAMP(io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP) RecordingDescriptorDecoder(io.aeron.archive.codecs.RecordingDescriptorDecoder) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ArchiveException(io.aeron.archive.client.ArchiveException) Assertions(org.junit.jupiter.api.Assertions) EpochClock(org.agrona.concurrent.EpochClock) Matchers.is(org.hamcrest.Matchers.is) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Checksums.crc32(io.aeron.archive.checksum.Checksums.crc32) Archive.segmentFileName(io.aeron.archive.Archive.segmentFileName) FileChannel(java.nio.channels.FileChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with DataHeaderFlyweight

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

the class CatalogTest method shouldThrowExceptionAfterFailureOnPageStraddle.

@Test
void shouldThrowExceptionAfterFailureOnPageStraddle() throws Exception {
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 128);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(256);
        log.write(bb, PAGE_SIZE - 128);
        bb.clear();
        bb.put(0, (byte) 0).limit(1).position(0);
        log.write(bb, PAGE_SIZE + 127);
    }
    final ArchiveException exception = assertThrows(ArchiveException.class, () -> {
        final Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer);
        catalog.close();
    });
    assertThat(exception.getMessage(), containsString(segmentFile.getAbsolutePath()));
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ArchiveException(io.aeron.archive.client.ArchiveException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with DataHeaderFlyweight

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

the class CatalogTest method shouldUseChecksumToVerifyLastFragmentAfterPageStraddle.

@Test
void shouldUseChecksumToVerifyLastFragmentAfterPageStraddle() throws Exception {
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 128);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(256);
        flyweight.sessionId(1025596259);
        log.write(bb, PAGE_SIZE - 128);
        bb.clear();
        bb.put(0, (byte) 0).limit(1).position(0);
        log.write(bb, PAGE_SIZE + 127);
    }
    currentTimeMs = 42L;
    try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, crc32(), null)) {
        assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
            assertEquals(42L, descriptorDecoder.stopTimestamp());
            assertEquals(PAGE_SIZE + 128, descriptorDecoder.stopPosition());
        }));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Catalog(io.aeron.archive.Catalog) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) CatalogHeaderEncoder(io.aeron.archive.codecs.CatalogHeaderEncoder) ByteBuffer.allocate(java.nio.ByteBuffer.allocate) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) RecordingDescriptorHeaderDecoder(io.aeron.archive.codecs.RecordingDescriptorHeaderDecoder) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferUtil(org.agrona.BufferUtil) Checksum(io.aeron.archive.checksum.Checksum) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayUtil(org.agrona.collections.ArrayUtil) File(java.io.File) NULL_TIMESTAMP(io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP) RecordingDescriptorDecoder(io.aeron.archive.codecs.RecordingDescriptorDecoder) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ArchiveException(io.aeron.archive.client.ArchiveException) Assertions(org.junit.jupiter.api.Assertions) EpochClock(org.agrona.concurrent.EpochClock) Matchers.is(org.hamcrest.Matchers.is) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Checksums.crc32(io.aeron.archive.checksum.Checksums.crc32) Archive.segmentFileName(io.aeron.archive.Archive.segmentFileName) FileChannel(java.nio.channels.FileChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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