Search in sources :

Example 16 with DataHeaderFlyweight

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

the class CatalogTest method shouldFixTimestampAndPositionAfterFailurePageStraddle.

@Test
public void shouldFixTimestampAndPositionAfterFailurePageStraddle() 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 = ByteBuffer.allocateDirect(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 32);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(128);
        log.write(bb, PAGE_SIZE - 32);
        bb.clear();
        flyweight.frameLength(0);
        log.write(bb, PAGE_SIZE - 32 + 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)) {
        assertTrue(catalog.forEntry((he, hd, e, decoder) -> {
            assertThat(decoder.stopTimestamp(), is(42L));
            assertThat(decoder.stopPosition(), is((long) PAGE_SIZE - 32));
        }, newRecordingId));
    }
}
Also used : Catalog.wrapDescriptorDecoder(io.aeron.archive.Catalog.wrapDescriptorDecoder) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) StandardOpenOption(java.nio.file.StandardOpenOption) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test) PAGE_SIZE(io.aeron.archive.Catalog.PAGE_SIZE) IoUtil(org.agrona.IoUtil) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) NULL_TIMESTAMP(io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP) RecordingDescriptorDecoder(io.aeron.archive.codecs.RecordingDescriptorDecoder) After(org.junit.After) EpochClock(org.agrona.concurrent.EpochClock) Matchers.is(org.hamcrest.Matchers.is) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Archive.segmentFileName(io.aeron.archive.Archive.segmentFileName) Assert(org.junit.Assert) FileChannel(java.nio.channels.FileChannel) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) Before(org.junit.Before) FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 17 with DataHeaderFlyweight

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

the class RecordingSessionTest method before.

@Before
public void before() throws Exception {
    when(mockPosition.getWeak()).then((invocation) -> positionLong);
    when(mockPosition.get()).then((invocation) -> positionLong);
    doAnswer((invocation) -> {
        positionLong = invocation.getArgument(0);
        return null;
    }).when(mockPosition).setOrdered(anyLong());
    termFile = File.createTempFile("test.rec", "sourceIdentity");
    mockLogBufferChannel = FileChannel.open(termFile.toPath(), CREATE, READ, WRITE);
    mockLogBufferMapped = new UnsafeBuffer(mockLogBufferChannel.map(FileChannel.MapMode.READ_WRITE, 0, TERM_BUFFER_LENGTH));
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
    headerFlyweight.wrap(mockLogBufferMapped, TERM_OFFSET, DataHeaderFlyweight.HEADER_LENGTH);
    headerFlyweight.termOffset(TERM_OFFSET).sessionId(SESSION_ID).streamId(STREAM_ID).headerType(DataHeaderFlyweight.HDR_TYPE_DATA).frameLength(RECORDED_BLOCK_LENGTH);
    context = new Archive.Context().segmentFileLength(SEGMENT_FILE_SIZE).archiveDir(archiveDir).catalog(mockCatalog).epochClock(epochClock);
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Before(org.junit.Before)

Example 18 with DataHeaderFlyweight

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

the class ReplaySessionTest method shouldReplayFromActiveRecording.

@Test
public void shouldReplayFromActiveRecording() {
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
    final int recordingId = RECORDING_ID + 1;
    recordingSummary.recordingId = recordingId;
    recordingSummary.stopPosition = NULL_POSITION;
    when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
    position.setOrdered(START_POSITION);
    final RecordingWriter writer = new RecordingWriter(recordingId, START_POSITION, JOIN_POSITION, TERM_BUFFER_LENGTH, context, ARCHIVE_DIR_CHANNEL, position);
    when(epochClock.time()).thenReturn(TIME);
    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);
    final long length = 5 * FRAME_LENGTH;
    final long correlationId = 1L;
    final ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession);
    when(mockReplayPub.isClosed()).thenReturn(false);
    when(mockReplayPub.isConnected()).thenReturn(false);
    replaySession.doWork();
    assertEquals(replaySession.state(), ReplaySession.State.INIT);
    when(mockReplayPub.isConnected()).thenReturn(true);
    replaySession.doWork();
    assertEquals(replaySession.state(), ReplaySession.State.REPLAY);
    verify(mockControlSession).sendOkResponse(eq(correlationId), anyLong(), eq(proxy));
    mockPublication(mockReplayPub, termBuffer);
    assertNotEquals(0, replaySession.doWork());
    assertThat(messageCounter, is(2));
    validateFrame(termBuffer, 0, FrameDescriptor.UNFRAGMENTED);
    validateFrame(termBuffer, 1, FrameDescriptor.BEGIN_FRAG_FLAG);
    assertEquals(0, replaySession.doWork());
    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();
    when(position.isClosed()).thenReturn(true);
    when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
    assertNotEquals(0, replaySession.doWork());
    validateFrame(termBuffer, 2, FrameDescriptor.END_FRAG_FLAG);
    verify(mockReplayPub).appendPadding(FRAME_LENGTH - HEADER_LENGTH);
    assertTrue(replaySession.isDone());
    replaySession.close();
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 19 with DataHeaderFlyweight

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

the class ArchiveToolTests method writeToSegmentFile.

private void writeToSegmentFile(final File file, final int fileLength, final SegmentWriter segmentWriter) throws IOException {
    final ByteBuffer byteBuffer = allocate(MTU_LENGTH);
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight(byteBuffer);
    try (FileChannel channel = FileChannel.open(file.toPath(), READ, WRITE)) {
        segmentWriter.write(byteBuffer, dataHeaderFlyweight, channel);
        final long size = channel.size();
        if (fileLength != size) {
            channel.truncate(fileLength);
            if (size < fileLength) {
                byteBuffer.put(0, (byte) 0).limit(1).position(0);
                channel.write(byteBuffer, fileLength - 1);
            }
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ByteBuffer(java.nio.ByteBuffer)

Example 20 with DataHeaderFlyweight

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

the class RecordingSessionTest method before.

@BeforeEach
public void before() throws Exception {
    when(mockPosition.getWeak()).then((invocation) -> positionLong);
    when(mockPosition.get()).then((invocation) -> positionLong);
    doAnswer((invocation) -> {
        positionLong = invocation.getArgument(0);
        return null;
    }).when(mockPosition).setOrdered(anyLong());
    termFile = File.createTempFile("test.rec", "sourceIdentity");
    mockLogBufferChannel = FileChannel.open(termFile.toPath(), CREATE, READ, WRITE);
    mockLogBufferMapped = new UnsafeBuffer(mockLogBufferChannel.map(FileChannel.MapMode.READ_WRITE, 0, TERM_BUFFER_LENGTH));
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
    headerFlyweight.wrap(mockLogBufferMapped, TERM_OFFSET, DataHeaderFlyweight.HEADER_LENGTH);
    headerFlyweight.termOffset(TERM_OFFSET).sessionId(SESSION_ID).streamId(STREAM_ID).headerType(DataHeaderFlyweight.HDR_TYPE_DATA).frameLength(RECORDED_BLOCK_LENGTH);
    context = new Archive.Context().segmentFileLength(SEGMENT_LENGTH).archiveDir(archiveDir).epochClock(epochClock);
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach)

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