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());
}
}
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;
}
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());
}));
}
}
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()));
}
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());
}));
}
}
Aggregations