use of io.aeron.driver.ThreadingMode in project aeron by real-logic.
the class ArchiveTest method shouldListRegisteredRecordingSubscriptions.
@Test
@InterruptAfter(10)
public void shouldListRegisteredRecordingSubscriptions() {
final int expectedStreamId = 7;
final String channelOne = "aeron:ipc";
final String channelTwo = "aeron:udp?endpoint=localhost:5678";
final String channelThree = "aeron:udp?endpoint=localhost:4321";
final ArrayList<SubscriptionDescriptor> descriptors = new ArrayList<>();
@SuppressWarnings("Indentation") final RecordingSubscriptionDescriptorConsumer consumer = (controlSessionId, correlationId, subscriptionId, streamId, strippedChannel) -> descriptors.add(new SubscriptionDescriptor(controlSessionId, correlationId, subscriptionId, streamId, strippedChannel));
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx);
AeronArchive archive = AeronArchive.connect()) {
final long subIdOne = archive.startRecording(channelOne, expectedStreamId, LOCAL);
final long subIdTwo = archive.startRecording(channelTwo, expectedStreamId + 1, LOCAL);
final long subOdThree = archive.startRecording(channelThree, expectedStreamId + 2, LOCAL);
final int countOne = archive.listRecordingSubscriptions(0, 5, "ipc", expectedStreamId, true, consumer);
assertEquals(1, descriptors.size());
assertEquals(1, countOne);
descriptors.clear();
final int countTwo = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
assertEquals(3, descriptors.size());
assertEquals(3, countTwo);
archive.stopRecording(subIdTwo);
descriptors.clear();
final int countThree = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
assertEquals(2, descriptors.size());
assertEquals(2, countThree);
assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subIdOne).count());
assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subOdThree).count());
} finally {
archiveCtx.deleteDirectory();
driverCtx.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 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 MultiModuleSharedDriverTest method shouldSupportTwoSingleNodeClusters.
@Test
@InterruptAfter(20)
@SuppressWarnings({ "try", "methodlength" })
public void shouldSupportTwoSingleNodeClusters() {
final MediaDriver.Context driverCtx = new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)).dirDeleteOnShutdown(false).dirDeleteOnStart(true);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(ArchiveThreadingMode.SHARED).archiveDir(new File(SystemUtil.tmpDirName(), "archive")).recordingEventsEnabled(false).deleteArchiveOnStart(true);
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx)) {
final ConsensusModule.Context moduleCtx0 = new ConsensusModule.Context().clusterId(0).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-0")).logChannel("aeron:ipc?term-length=64k").logStreamId(100).serviceStreamId(104).consensusModuleStreamId(105).ingressChannel("aeron:udp?endpoint=localhost:9020").replicationChannel("aeron:udp?endpoint=localhost:0");
final ClusteredServiceContainer.Context containerCtx0 = new ClusteredServiceContainer.Context().clusterId(moduleCtx0.clusterId()).clusteredService(new EchoService()).clusterDir(moduleCtx0.clusterDir()).serviceStreamId(moduleCtx0.serviceStreamId()).consensusModuleStreamId(moduleCtx0.consensusModuleStreamId());
final ConsensusModule.Context moduleCtx1 = new ConsensusModule.Context().clusterId(1).deleteDirOnStart(true).clusterDir(new File(SystemUtil.tmpDirName(), "cluster-0-1")).logChannel("aeron:ipc?term-length=64k").logStreamId(200).serviceStreamId(204).consensusModuleStreamId(205).ingressChannel("aeron:udp?endpoint=localhost:9021").replicationChannel("aeron:udp?endpoint=localhost:0");
final ClusteredServiceContainer.Context containerCtx1 = new ClusteredServiceContainer.Context().clusteredService(new EchoService()).clusterDir(moduleCtx1.clusterDir()).serviceStreamId(moduleCtx1.serviceStreamId()).consensusModuleStreamId(moduleCtx1.consensusModuleStreamId()).clusterId(moduleCtx1.clusterId());
ConsensusModule consensusModule0 = null;
ClusteredServiceContainer container0 = null;
ConsensusModule consensusModule1 = null;
ClusteredServiceContainer container1 = null;
AeronCluster client0 = null;
AeronCluster client1 = null;
try {
consensusModule0 = ConsensusModule.launch(moduleCtx0);
consensusModule1 = ConsensusModule.launch(moduleCtx1);
container0 = ClusteredServiceContainer.launch(containerCtx0);
container1 = ClusteredServiceContainer.launch(containerCtx1);
final MutableReference<String> egress = new MutableReference<>();
final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length, header) -> egress.set(buffer.getStringWithoutLengthAscii(offset, length));
client0 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx0.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
client1 = AeronCluster.connect(new AeronCluster.Context().egressListener(egressListener).ingressChannel(moduleCtx1.ingressChannel()).egressChannel("aeron:udp?endpoint=localhost:0"));
echoMessage(client0, "Message 0", egress);
echoMessage(client1, "Message 1", egress);
} finally {
systemTestWatcher.dataCollector().add(moduleCtx0.clusterDir());
systemTestWatcher.dataCollector().add(moduleCtx1.clusterDir());
CloseHelper.closeAll(client0, client1, consensusModule0, consensusModule1, container0, container1);
}
} finally {
systemTestWatcher.dataCollector().add(driverCtx.aeronDirectory());
systemTestWatcher.dataCollector().add(archiveCtx.archiveDir());
}
}
use of io.aeron.driver.ThreadingMode in project Aeron by real-logic.
the class ArchiveTest method shouldListRegisteredRecordingSubscriptions.
@Test
@InterruptAfter(10)
public void shouldListRegisteredRecordingSubscriptions() {
final int expectedStreamId = 7;
final String channelOne = "aeron:ipc";
final String channelTwo = "aeron:udp?endpoint=localhost:5678";
final String channelThree = "aeron:udp?endpoint=localhost:4321";
final ArrayList<SubscriptionDescriptor> descriptors = new ArrayList<>();
@SuppressWarnings("Indentation") final RecordingSubscriptionDescriptorConsumer consumer = (controlSessionId, correlationId, subscriptionId, streamId, strippedChannel) -> descriptors.add(new SubscriptionDescriptor(controlSessionId, correlationId, subscriptionId, streamId, strippedChannel));
final MediaDriver.Context driverCtx = new MediaDriver.Context().dirDeleteOnStart(true).threadingMode(ThreadingMode.SHARED);
final Archive.Context archiveCtx = new Archive.Context().threadingMode(SHARED);
try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx, archiveCtx);
AeronArchive archive = AeronArchive.connect()) {
final long subIdOne = archive.startRecording(channelOne, expectedStreamId, LOCAL);
final long subIdTwo = archive.startRecording(channelTwo, expectedStreamId + 1, LOCAL);
final long subOdThree = archive.startRecording(channelThree, expectedStreamId + 2, LOCAL);
final int countOne = archive.listRecordingSubscriptions(0, 5, "ipc", expectedStreamId, true, consumer);
assertEquals(1, descriptors.size());
assertEquals(1, countOne);
descriptors.clear();
final int countTwo = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
assertEquals(3, descriptors.size());
assertEquals(3, countTwo);
archive.stopRecording(subIdTwo);
descriptors.clear();
final int countThree = archive.listRecordingSubscriptions(0, 5, "", expectedStreamId, false, consumer);
assertEquals(2, descriptors.size());
assertEquals(2, countThree);
assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subIdOne).count());
assertEquals(1L, descriptors.stream().filter((sd) -> sd.subscriptionId == subOdThree).count());
} finally {
archiveCtx.deleteDirectory();
driverCtx.deleteDirectory();
}
}
Aggregations