use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class ArchiveTest method shouldRecoverRecordingWithNonZeroStartPosition.
@Test
public void shouldRecoverRecordingWithNonZeroStartPosition() {
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
long resultingPosition;
final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
final long recordingId;
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
AeronArchive archive = AeronArchive.connect()) {
final int termLength = 128 * 1024;
final int initialTermId = 29;
final String channel = new ChannelUriStringBuilder().media(CommonContext.IPC_MEDIA).initialPosition(initialPosition, initialTermId, termLength).build();
final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));
while ((resultingPosition = publication.offer(buffer)) <= 0) {
Tests.yield();
}
final Aeron aeron = archive.context().aeron();
int counterId;
final int sessionId = publication.sessionId();
final CountersReader countersReader = aeron.countersReader();
while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId))) {
Tests.yield();
}
recordingId = RecordingPos.getRecordingId(countersReader, counterId);
while (countersReader.getCounterValue(counterId) < resultingPosition) {
Tests.yield();
}
}
try (Catalog catalog = openCatalog(archiveCtx)) {
final Catalog.CatalogEntryProcessor catalogEntryProcessor = (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> descriptorEncoder.stopPosition(Aeron.NULL_VALUE);
assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
}
final Archive.Context archiveCtxClone = archiveCtx.clone();
final MediaDriver.Context driverCtxClone = driverCtx.clone();
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
AeronArchive archive = AeronArchive.connect()) {
assertEquals(initialPosition, archive.getStartPosition(recordingId));
assertEquals(resultingPosition, archive.getStopPosition(recordingId));
} finally {
archiveCtxClone.deleteDirectory();
driverCtxClone.deleteDirectory();
}
}
use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldValidateSpecifiedPositionForConcurrentPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL, "aeron:udp?endpoint=localhost:24325" })
void shouldValidateSpecifiedPositionForConcurrentPublications(final String initialUri) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final int totalMessageLength = DataHeaderFlyweight.HEADER_LENGTH + msg.capacity();
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long positionMsg1 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg2 = positionCalculator.addMessage(totalMessageLength);
final long positionMsg3 = positionCalculator.addMessage(totalMessageLength);
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final String invalidPositionUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition + FrameDescriptor.FRAME_ALIGNMENT, initialTermId, termLength).build();
final String invalidInitialTermIdUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId + 1, termLength).build();
final String invalidTermLengthUri = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength << 1).build();
final int streamId = 1001;
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = aeron.addPublication(channel, streamId)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg1, publication.position());
Tests.await(() -> subscription.poll((buffer, offset, length, header) -> {
}, 1) == 1);
try (Publication publication2 = aeron.addPublication(channel, streamId)) {
assertEquals(positionMsg1, publication2.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg2, publication2.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg2, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
try (Publication publication3 = aeron.addPublication(initialUri, streamId)) {
assertEquals(positionMsg2, publication3.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(positionMsg3, publication3.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(positionMsg3, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
}
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidPositionUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidInitialTermIdUri, streamId));
assertThrows(RegistrationException.class, () -> aeron.addPublication(invalidTermLengthUri, streamId));
} finally {
context.deleteDirectory();
}
}
use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class ArchiveTest method shouldErrorWhenUnauthorised.
@Test
@InterruptAfter(10)
public void shouldErrorWhenUnauthorised() {
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final AuthorisationServiceSupplier authorisationServiceSupplier = () -> (protocolId, actionId, type, encodedPrincipal) -> {
return actionId != TruncateRecordingRequestDecoder.TEMPLATE_ID;
};
final Archive.Context archiveCtx = new Archive.Context().deleteArchiveOnStart(true).authorisationServiceSupplier(authorisationServiceSupplier).threadingMode(SHARED);
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx);
AeronArchive archive = AeronArchive.connect()) {
try {
archive.truncateRecording(0, 0);
} catch (final ArchiveException ex) {
assertEquals(ArchiveException.UNAUTHORISED_ACTION, ex.errorCode());
return;
}
fail("Expected exception");
} finally {
archiveCtx.deleteDirectory();
driverCtx.deleteDirectory();
}
}
use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldStartAtSpecifiedPositionForPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL + ",true", "aeron:udp?endpoint=localhost:24325,true", CommonContext.IPC_CHANNEL + ",false", "aeron:udp?endpoint=localhost:24325,false" })
void shouldStartAtSpecifiedPositionForPublications(final String initialUri, final boolean exclusive) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long nextPosition = positionCalculator.addMessage(DataHeaderFlyweight.HEADER_LENGTH + msg.capacity());
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final int streamId = 1001;
final Function<Aeron, Publication> publicationSupplier = exclusive ? (a) -> a.addExclusivePublication(channel, streamId) : (a) -> a.addPublication(channel, streamId);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = publicationSupplier.apply(aeron)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(nextPosition, publication.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(nextPosition, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
} finally {
context.deleteDirectory();
}
}
use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class ClusterNetworkTopologyTest method connectAndSendMessages.
private void connectAndSendMessages(final String ingressChannel, final String ingressEndpoints, final Selector selector, final double messageCount) {
final String message = "Hello World!";
final MutableDirectBuffer messageBuffer = new UnsafeBuffer(ByteBuffer.allocate(128));
final int length = messageBuffer.putStringAscii(0, message);
final MutableReference<String> egressResponse = new MutableReference<>();
final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length1, header) -> {
final String stringAscii = buffer.getStringAscii(offset);
egressResponse.set(stringAscii);
};
try (MediaDriver mediaDriver = MediaDriver.launchEmbedded(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true));
AeronCluster.AsyncConnect asyncConnect = AeronCluster.asyncConnect(new AeronCluster.Context().messageTimeoutNs(TimeUnit.SECONDS.toNanos(CLUSTER_START_ELECTION_TIMEOUT_S * 2)).egressListener(egressListener).egressChannel("aeron:udp?endpoint=10.42.0.1:0").aeronDirectoryName(mediaDriver.aeronDirectoryName()).ingressChannel(ingressChannel).ingressEndpoints(ingressEndpoints));
AeronCluster aeronCluster = pollUntilConnected(asyncConnect, selector)) {
for (int i = 0; i < messageCount; i++) {
Tests.await(() -> {
final long position = aeronCluster.offer(messageBuffer, 0, length);
pollSelector(selector);
return 0 < position;
}, SECONDS.toNanos(5));
Tests.await(() -> {
aeronCluster.pollEgress();
pollSelector(selector);
return message.equals(egressResponse.get());
}, SECONDS.toNanos(5));
}
}
}
Aggregations