use of io.aeron.Publication 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.Publication 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.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDeleteDetachedSegmentsWhenStartNotSegmentAligned.
@Test
@InterruptAfter(10)
void shouldDeleteDetachedSegmentsWhenStartNotSegmentAligned() {
final String messagePrefix = "Message-Prefix-";
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)) {
assertEquals(startPosition, publication.position());
final CountersReader counters = aeron.countersReader();
final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
final long recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
aeronArchive.stopRecording(publication);
final String prefix = recordingId + "-";
final String[] files = archive.context().archiveDir().list((dir, name) -> name.startsWith(prefix));
assertThat(files, arrayWithSize(3));
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, startPosition + (SEGMENT_LENGTH * 2L), TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long deletedSegments = aeronArchive.deleteDetachedSegments(recordingId);
assertEquals(2L, deletedSegments);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
Tests.await(() -> {
final String[] updatedFiles = archive.context().archiveDir().list((dir, name) -> name.startsWith(prefix));
if (null != updatedFiles && 1 == updatedFiles.length) {
assertThat(updatedFiles, arrayContaining(Archive.segmentFileName(recordingId, segmentFileBasePosition)));
return true;
}
return false;
});
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ManageRecordingHistoryTest method shouldDetachThenAttachFullSegments.
@Test
@InterruptAfter(10)
void shouldDetachThenAttachFullSegments() {
final String messagePrefix = "Message-Prefix-";
final long targetPosition = (SEGMENT_LENGTH * 3L) + 1;
try (Publication publication = aeronArchive.addRecordedPublication(uriBuilder.build(), STREAM_ID)) {
final CountersReader counters = aeron.countersReader();
final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
final long recordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
aeronArchive.stopRecording(publication);
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
aeronArchive.detachSegments(recordingId, segmentFileBasePosition);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
final long attachSegments = aeronArchive.attachSegments(recordingId);
assertEquals(2L, attachSegments);
assertEquals(startPosition, aeronArchive.getStartPosition(recordingId));
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class ClusterTest method shouldRejectAnInvalidAdminRequest.
@Test
@InterruptAfter(10)
void shouldRejectAnInvalidAdminRequest() {
final AdminRequestType invalidRequestType = AdminRequestType.NULL_VAL;
final AtomicBoolean authorisationServiceCalled = new AtomicBoolean();
cluster = aCluster().withStaticNodes(3).withAuthorisationServiceSupplier(() -> (protocolId, actionId, type, encodedPrincipal) -> {
authorisationServiceCalled.set(true);
assertEquals(MessageHeaderDecoder.SCHEMA_ID, protocolId);
assertEquals(AdminRequestEncoder.TEMPLATE_ID, actionId);
assertEquals(invalidRequestType, type);
return true;
}).start();
systemTestWatcher.cluster(cluster);
cluster.awaitLeader();
final long requestCorrelationId = System.nanoTime();
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, invalidRequestType, AdminResponseCode.ERROR, "Unknown request type: " + invalidRequestType);
final AeronCluster client = cluster.connectClient();
final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(client.leadershipTermId()).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(invalidRequestType);
final Publication ingressPublication = client.ingressPublication();
while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
Tests.yield();
}
Tests.await(authorisationServiceCalled::get);
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
}
Aggregations