use of io.aeron.Publication in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldPurgeRecording.
@Test
@InterruptAfter(10)
void shouldPurgeRecording() throws IOException {
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 newRecordingId = RecordingPos.getRecordingId(counters, counterId);
offerToPosition(publication, messagePrefix, targetPosition);
awaitPosition(counters, counterId, publication.position());
final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
final RecordingSignalConsumer consumer = (controlSessionId, correlationId, recordingId, subscriptionId, position, transitionType) -> {
if (newRecordingId == recordingId) {
signalRef.set(transitionType);
}
};
final RecordingSignalAdapter adapter = new RecordingSignalAdapter(aeronArchive.controlSessionId(), ERROR_CONTROL_LISTENER, consumer, aeronArchive.controlResponsePoller().subscription(), FRAGMENT_LIMIT);
aeronArchive.stopRecording(publication);
assertEquals(RecordingSignal.STOP, awaitSignal(signalRef, adapter));
final String prefix = newRecordingId + "-";
final File archiveDir = archive.context().archiveDir();
assertTrue(new File(archiveDir, prefix + (SEGMENT_LENGTH * 4L) + ".rec").createNewFile());
assertTrue(new File(archiveDir, prefix + (SEGMENT_LENGTH * 5L) + ".rec.del").createNewFile());
aeronArchive.purgeRecording(newRecordingId);
assertEquals(RecordingSignal.DELETE, awaitSignal(signalRef, adapter));
assertThat(archiveDir.list(((dir, name) -> name.startsWith(prefix))), arrayWithSize(0));
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class ManageRecordingHistoryTest method shouldPurgeForStreamJoinedAtTheBeginning.
@Test
@InterruptAfter(10)
void shouldPurgeForStreamJoinedAtTheBeginning() {
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());
final long startPosition = 0L;
final long segmentFileBasePosition = AeronArchive.segmentFileBasePosition(startPosition, SEGMENT_LENGTH * 2L, TERM_LENGTH, SEGMENT_LENGTH);
final long count = aeronArchive.purgeSegments(recordingId, segmentFileBasePosition);
assertEquals(2L, count);
assertEquals(segmentFileBasePosition, aeronArchive.getStartPosition(recordingId));
aeronArchive.stopRecording(publication);
}
}
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 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();
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class ClusterTest method shouldRejectAnAdminRequestIfLeadershipTermIsInvalid.
@Test
@InterruptAfter(10)
void shouldRejectAnAdminRequestIfLeadershipTermIsInvalid() {
cluster = aCluster().withStaticNodes(3).start();
systemTestWatcher.cluster(cluster);
cluster.awaitLeader();
AeronCluster client = cluster.connectClient();
final long requestCorrelationId = System.nanoTime();
final long expectedLeadershipTermId = client.leadershipTermId();
final long invalidLeadershipTermId = expectedLeadershipTermId - 1000;
final AdminRequestType requestType = AdminRequestType.NULL_VAL;
final MutableBoolean responseReceived = injectAdminResponseEgressListener(requestCorrelationId, requestType, AdminResponseCode.ERROR, "Invalid leadership term: expected " + expectedLeadershipTermId + ", got " + invalidLeadershipTermId);
client = cluster.connectClient();
final AdminRequestEncoder adminRequestEncoder = new AdminRequestEncoder().wrapAndApplyHeader(cluster.msgBuffer(), 0, new MessageHeaderEncoder()).leadershipTermId(invalidLeadershipTermId).clusterSessionId(client.clusterSessionId()).correlationId(requestCorrelationId).requestType(requestType);
final Publication ingressPublication = client.ingressPublication();
while (ingressPublication.offer(adminRequestEncoder.buffer(), 0, MessageHeaderEncoder.ENCODED_LENGTH + adminRequestEncoder.encodedLength()) < 0) {
Tests.yield();
}
while (!responseReceived.get()) {
client.pollEgress();
Tests.yield();
}
}
Aggregations