use of io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldMigrateSegmentsForStreamNotSegmentAligned.
@Test
@InterruptAfter(10)
void shouldMigrateSegmentsForStreamNotSegmentAligned() throws IOException {
final int initialTermId = 7;
final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
final long startPosition = (TERM_LENGTH * 2L) + (FRAME_ALIGNMENT * 2L);
uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH);
try (Publication publication = aeronArchive.addRecordedExclusivePublication(uriBuilder.build(), STREAM_ID)) {
final CountersReader counters = aeron.countersReader();
final int dstCounterId = awaitRecordingCounterId(counters, publication.sessionId());
final long dstRecordingId = RecordingPos.getRecordingId(counters, dstCounterId);
offerToPosition(publication, "dst-message-", targetPosition);
awaitPosition(counters, dstCounterId, publication.position());
aeronArchive.stopRecording(publication);
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + (SEGMENT_LENGTH * 2L), TERM_LENGTH, SEGMENT_LENGTH);
final long purgedSegments = aeronArchive.purgeSegments(dstRecordingId, segmentFileBasePosition);
assertEquals(2L, purgedSegments);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(dstRecordingId));
final long srcRecordingId;
final String migrateChannel = uriBuilder.initialPosition(startPosition, initialTermId, TERM_LENGTH).endpoint("localhost:4444").build();
try (Publication migratePub = aeronArchive.addRecordedExclusivePublication(migrateChannel, STREAM_ID)) {
final int srcCounterId = awaitRecordingCounterId(counters, migratePub.sessionId());
srcRecordingId = RecordingPos.getRecordingId(counters, srcCounterId);
offerToPosition(migratePub, "src-message-", segmentFileBasePosition);
awaitPosition(counters, srcCounterId, migratePub.position());
aeronArchive.stopRecording(migratePub);
}
final File archiveDir = archive.context().archiveDir();
final String srcPrefix = srcRecordingId + "-";
final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
assertThat(srcFiles, arrayContainingInAnyOrder(Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH * 3L)));
final String dstPrefix = dstRecordingId + "-";
String[] dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
assertThat(dstFiles, arrayContaining(Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)));
final byte[] srcBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(srcRecordingId, segmentFileBasePosition)).toPath());
assertEquals(SEGMENT_LENGTH, srcBytes.length);
final byte[] dstBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)).toPath());
assertEquals(SEGMENT_LENGTH, dstBytes.length);
assertThat(srcBytes, not(equalTo(dstBytes)));
final long migratedSegments = aeronArchive.migrateSegments(srcRecordingId, dstRecordingId);
assertEquals(2L, migratedSegments);
assertEquals(startPosition, aeronArchive.getStartPosition(dstRecordingId));
assertEquals(startPosition, aeronArchive.getStopPosition(srcRecordingId));
Tests.await(() -> {
final String[] files = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
return null != files && 0 == files.length;
});
dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
final byte[] migratedBytes = Files.readAllBytes(new File(archiveDir, Archive.segmentFileName(dstRecordingId, segmentFileBasePosition)).toPath());
assertArrayEquals(dstBytes, migratedBytes);
}
}
use of io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldMigrateSegmentsForStreamJoinedAtTheBeginning.
@Test
@InterruptAfter(10)
void shouldMigrateSegmentsForStreamJoinedAtTheBeginning() throws IOException {
final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
try (Publication publication = aeronArchive.addRecordedPublication(uriBuilder.build(), STREAM_ID)) {
final CountersReader counters = aeron.countersReader();
final int dstCounterId = awaitRecordingCounterId(counters, publication.sessionId());
final long dstRecordingId = RecordingPos.getRecordingId(counters, dstCounterId);
offerToPosition(publication, "dst-message-", targetPosition);
awaitPosition(counters, dstCounterId, publication.position());
aeronArchive.stopRecording(publication);
final long startPosition = 0L;
final long migratePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
final long count = aeronArchive.purgeSegments(dstRecordingId, migratePosition);
assertEquals(2L, count);
assertEquals(migratePosition, aeronArchive.getStartPosition(dstRecordingId));
final long srcRecordingId;
final String migrateChannel = uriBuilder.initialPosition(startPosition, publication.initialTermId(), TERM_LENGTH).endpoint("localhost:4444").build();
try (Publication migratePub = aeronArchive.addRecordedExclusivePublication(migrateChannel, STREAM_ID)) {
final int srcCounterId = awaitRecordingCounterId(counters, migratePub.sessionId());
srcRecordingId = RecordingPos.getRecordingId(counters, srcCounterId);
offerToPosition(migratePub, "src-message-", SEGMENT_LENGTH * 4 + TERM_LENGTH + FRAME_ALIGNMENT);
awaitPosition(counters, srcCounterId, migratePub.position());
aeronArchive.stopRecording(migratePub);
}
aeronArchive.truncateRecording(srcRecordingId, migratePosition);
final File archiveDir = archive.context().archiveDir();
final String srcPrefix = srcRecordingId + "-";
Tests.await(() -> {
final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
if (null != srcFiles && 2 == srcFiles.length) {
assertThat(srcFiles, arrayContainingInAnyOrder(Archive.segmentFileName(srcRecordingId, 0), Archive.segmentFileName(srcRecordingId, SEGMENT_LENGTH)));
return true;
}
return false;
});
final Path srcFile = new File(archiveDir, Archive.segmentFileName(srcRecordingId, migratePosition) + ".del").toPath();
Files.write(srcFile, new byte[] { 0x1, 0x2, 0x3 }, StandardOpenOption.CREATE_NEW);
final String dstPrefix = dstRecordingId + "-";
String[] dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
final Path dstFile = new File(archiveDir, Archive.segmentFileName(dstRecordingId, migratePosition)).toPath();
final byte[] dstBytes = Files.readAllBytes(dstFile);
assertEquals(SEGMENT_LENGTH, dstBytes.length);
final long migratedSegments = aeronArchive.migrateSegments(srcRecordingId, dstRecordingId);
assertEquals(2L, migratedSegments);
assertEquals(startPosition, aeronArchive.getStartPosition(dstRecordingId));
Tests.await(() -> {
final String[] srcFiles = archiveDir.list((dir, name) -> name.startsWith(srcPrefix));
return null != srcFiles && 0 == srcFiles.length;
});
dstFiles = archiveDir.list((dir, name) -> name.startsWith(dstPrefix));
assertThat(dstFiles, arrayContainingInAnyOrder(Archive.segmentFileName(dstRecordingId, 0), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 2L), Archive.segmentFileName(dstRecordingId, SEGMENT_LENGTH * 3L)));
final byte[] migratedBytes = Files.readAllBytes(dstFile);
assertArrayEquals(dstBytes, migratedBytes);
}
}
use of io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT in project Aeron by real-logic.
the class ArchiveToolTests method verifyAllOptionsTruncateOnPageStraddle.
@Test
void verifyAllOptionsTruncateOnPageStraddle() {
final Checksum checksum = crc32();
try (Catalog catalog = openCatalogReadWrite(archiveDir, epochClock, MIN_CAPACITY, checksum, null)) {
catalog.forEach((recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> catalog.updateChecksum(recordingDescriptorOffset));
}
assertFalse(verify(out, archiveDir, allOf(VerifyOption.class), checksum, epochClock, (file) -> true));
try (Catalog catalog = openCatalogReadOnly(archiveDir, epochClock)) {
assertRecording(catalog, invalidRecording0, INVALID, -119969720, NULL_POSITION, NULL_POSITION, 1, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording1, INVALID, 768794941, FRAME_ALIGNMENT - 7, NULL_POSITION, 2, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording2, INVALID, -1340428433, 1024, FRAME_ALIGNMENT * 2, 3, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording3, INVALID, 1464972620, 0, FRAME_ALIGNMENT * 5 + 11, 4, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording4, INVALID, 21473288, SEGMENT_LENGTH, NULL_POSITION, 5, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording5, INVALID, -2119992405, 0, SEGMENT_LENGTH, 6, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording6, INVALID, 2054096463, 0, NULL_POSITION, 7, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording7, INVALID, -1050175867, 0, NULL_POSITION, 8, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording8, INVALID, -504693275, 0, NULL_POSITION, 9, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording9, INVALID, -2036430506, 0, NULL_POSITION, 10, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording10, INVALID, -414736820, 128, NULL_POSITION, 11, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording11, INVALID, 1983095657, 0, NULL_POSITION, 12, NULL_TIMESTAMP, 5, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording12, INVALID, -1308504240, 0, NULL_POSITION, 13, NULL_TIMESTAMP, 9, 6, "ch1", "src1");
assertRecording(catalog, invalidRecording13, INVALID, -273182324, 0, NULL_POSITION, 14, NULL_TIMESTAMP, 0, 13, "ch1", "src1");
assertRecording(catalog, invalidRecording14, INVALID, 213018412, 128, NULL_POSITION, -14, 41, -14, 0, "ch1", "src1");
assertRecording(catalog, validRecording0, VALID, 356725588, 0, TERM_LENGTH + 64, 15, 100, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording1, VALID, -1571032591, 1024, 1024, 16, 200, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording2, VALID, 114203747, TERM_LENGTH * 3 + 96, TERM_LENGTH * 3 + 96, 17, 300, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording3, INVALID, 963969455, 7 * TERM_LENGTH + 96, 7 * TERM_LENGTH + 128, 18, NULL_TIMESTAMP, 7, 13, "ch2", "src2");
assertRecording(catalog, validRecording4, INVALID, 162247708, 21 * TERM_LENGTH + (TERM_LENGTH - 64), 22 * TERM_LENGTH + 992, 19, 1, -25, 7, "ch2", "src2");
assertRecording(catalog, validRecording51, VALID, -940881948, 0, 64 + PAGE_SIZE, 20, 777, 0, 20, "ch2", "src2");
assertRecording(catalog, validRecording52, INVALID, 1046083782, 0, NULL_POSITION, 21, NULL_TIMESTAMP, 0, 52, "ch2", "src2");
assertRecording(catalog, validRecording53, INVALID, 428178649, 0, NULL_POSITION, 22, NULL_TIMESTAMP, 0, 53, "ch2", "src2");
assertRecording(catalog, validRecording6, VALID, -175549265, 352, 960, 23, 400, 0, 6, "ch2", "src2");
}
Mockito.verify(out, times(24)).println(any(String.class));
}
use of io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT in project Aeron by real-logic.
the class ArchiveToolTests method verifyNoOptionsDoNotTruncateOnPageStraddle.
@Test
void verifyNoOptionsDoNotTruncateOnPageStraddle() {
assertFalse(verify(out, archiveDir, emptySet(), null, epochClock, (file) -> false));
try (Catalog catalog = openCatalogReadOnly(archiveDir, epochClock)) {
assertRecording(catalog, invalidRecording0, INVALID, 0, NULL_POSITION, NULL_POSITION, 1, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording1, INVALID, 0, FRAME_ALIGNMENT - 7, NULL_POSITION, 2, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording2, INVALID, 0, 1024, FRAME_ALIGNMENT * 2, 3, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording3, INVALID, 0, 0, FRAME_ALIGNMENT * 5 + 11, 4, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording4, INVALID, 0, SEGMENT_LENGTH, NULL_POSITION, 5, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording5, INVALID, 0, 0, SEGMENT_LENGTH, 6, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording6, INVALID, 0, 0, NULL_POSITION, 7, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording7, INVALID, 0, 0, NULL_POSITION, 8, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording8, INVALID, 0, 0, NULL_POSITION, 9, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording9, INVALID, 0, 0, NULL_POSITION, 10, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording10, INVALID, 0, 128, NULL_POSITION, 11, NULL_TIMESTAMP, 0, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording11, INVALID, 0, 0, NULL_POSITION, 12, NULL_TIMESTAMP, 5, 1, "ch1", "src1");
assertRecording(catalog, invalidRecording12, INVALID, 0, 0, NULL_POSITION, 13, NULL_TIMESTAMP, 9, 6, "ch1", "src1");
assertRecording(catalog, invalidRecording13, INVALID, 0, 0, NULL_POSITION, 14, NULL_TIMESTAMP, 0, 13, "ch1", "src1");
assertRecording(catalog, invalidRecording14, INVALID, 0, 128, NULL_POSITION, -14, 41, -14, 0, "ch1", "src1");
assertRecording(catalog, validRecording0, VALID, 0, 0, TERM_LENGTH + 64, 15, 100, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording1, VALID, 0, 1024, 1024, 16, 200, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording2, VALID, 0, TERM_LENGTH * 3 + 96, TERM_LENGTH * 3 + 96, 17, 300, 0, 2, "ch2", "src2");
assertRecording(catalog, validRecording3, VALID, 0, 7 * TERM_LENGTH + 96, 11 * TERM_LENGTH + 320, 18, 400, 7, 13, "ch2", "src2");
assertRecording(catalog, validRecording4, VALID, 0, 21 * TERM_LENGTH + (TERM_LENGTH - 64), 22 * TERM_LENGTH + 992, 19, 1, -25, 7, "ch2", "src2");
assertRecording(catalog, validRecording51, VALID, 0, 0, 64 + PAGE_SIZE, 20, 777, 0, 20, "ch2", "src2");
assertRecording(catalog, validRecording52, VALID, 0, 0, 128 + MTU_LENGTH, 21, 500, 0, 52, "ch2", "src2");
assertRecording(catalog, validRecording53, VALID, 0, 0, 64 + 3 * PAGE_SIZE, 22, 600, 0, 53, "ch2", "src2");
assertRecording(catalog, validRecording6, VALID, 0, 352, 960, 23, 700, 0, 6, "ch2", "src2");
}
Mockito.verify(out, times(24)).println(any(String.class));
}
Aggregations