use of io.aeron.driver.MediaDriver 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.MediaDriver in project Aeron by real-logic.
the class RecordingDescriptorCollectorTest method shouldAllowUserToRetainDescriptorsToPreventReuse.
@Test
void shouldAllowUserToRetainDescriptorsToPreventReuse(@TempDir final Path tempDir) {
try (MediaDriver mediaDriver = MediaDriver.launch(new MediaDriver.Context().dirDeleteOnStart(true));
Archive ignore = Archive.launch(new Archive.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()).archiveDir(tempDir.resolve("archive").toFile()).deleteArchiveOnStart(true));
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
AeronArchive aeronArchive = AeronArchive.connect(new AeronArchive.Context().aeron(aeron).ownsAeronClient(false))) {
createRecordings(aeronArchive, 3);
final RecordingDescriptorCollector collector = new RecordingDescriptorCollector(1);
long fromRecordingId = 0;
int count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
assertEquals(1, count);
final RecordingDescriptor desc0 = collector.descriptors().get(0);
fromRecordingId += count;
count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
assertEquals(1, count);
final RecordingDescriptor desc1 = collector.descriptors().get(0);
assertEquals(desc0.recordingId(), desc1.recordingId());
desc1.retain();
fromRecordingId += count;
count = aeronArchive.listRecordings(fromRecordingId, collector.poolSize(), collector.reset());
assertEquals(1, count);
final RecordingDescriptor desc2 = collector.descriptors().get(0);
assertNotEquals(desc1.recordingId(), desc2.recordingId());
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class GapFillLossTest method shouldGapFillWhenLossOccurs.
@Test
@InterruptAfter(10)
void shouldGapFillWhenLossOccurs() throws Exception {
final UnsafeBuffer srcBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(MSG_LENGTH));
srcBuffer.setMemory(0, MSG_LENGTH, (byte) 7);
final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).publicationTermBufferLength(LogBufferDescriptor.TERM_MIN_LENGTH);
final LossGenerator noLossGenerator = DebugChannelEndpointConfiguration.lossGeneratorSupplier(0, 0);
ctx.sendChannelEndpointSupplier((udpChannel, statusIndicator, context) -> new DebugSendChannelEndpoint(udpChannel, statusIndicator, context, noLossGenerator, noLossGenerator));
TestMediaDriver.enableLossGenerationOnReceive(ctx, 0.20, 0xcafebabeL, true, false);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(ctx, watcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(UNRELIABLE_CHANNEL, STREAM_ID);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
final Subscriber subscriber = new Subscriber(subscription);
final Thread subscriberThread = new Thread(subscriber);
subscriberThread.setDaemon(true);
subscriberThread.start();
long position = 0;
for (int i = 0; i < NUM_MESSAGES; i++) {
srcBuffer.putLong(0, i);
while ((position = publication.offer(srcBuffer)) < 0L) {
Tests.yield();
}
}
FINAL_POSITION.set(position);
subscriberThread.join();
verifyLossOccurredForStream(ctx.aeronDirectoryName(), STREAM_ID);
assertThat(subscriber.messageCount, lessThan(NUM_MESSAGES));
} finally {
ctx.deleteDirectory();
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class MultiClusteredServicesTest method shouldSupportMultipleServicesPerNode.
@Test
@InterruptAfter(20)
public void shouldSupportMultipleServicesPerNode() {
final List<TestCluster.NodeContext> nodeContexts = new ArrayList<>();
final List<TestCluster.ServiceContext> serviceContexts = new ArrayList<>();
final List<ClusteredMediaDriver> clusteredMediaDrivers = new ArrayList<>();
final List<ClusteredServiceContainer> clusteredServiceContainers = new ArrayList<>();
nodeContexts.add(TestCluster.nodeContext(0, true));
nodeContexts.add(TestCluster.nodeContext(1, true));
nodeContexts.add(TestCluster.nodeContext(2, true));
serviceContexts.add(TestCluster.serviceContext(0, 0, nodeContexts.get(0), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(0, 1, nodeContexts.get(0), ServiceB::new));
serviceContexts.add(TestCluster.serviceContext(1, 0, nodeContexts.get(1), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(1, 1, nodeContexts.get(1), ServiceB::new));
serviceContexts.add(TestCluster.serviceContext(2, 0, nodeContexts.get(2), ServiceA::new));
serviceContexts.add(TestCluster.serviceContext(2, 1, nodeContexts.get(2), ServiceB::new));
nodeContexts.forEach((context) -> {
try {
clusteredMediaDrivers.add(ClusteredMediaDriver.launch(context.mediaDriverCtx, context.archiveCtx, context.consensusModuleCtx));
} finally {
systemTestWatcher.dataCollector().add(context.mediaDriverCtx.aeronDirectory());
systemTestWatcher.dataCollector().add(context.archiveCtx.archiveDir());
systemTestWatcher.dataCollector().add(context.consensusModuleCtx.clusterDir());
}
});
serviceContexts.forEach((context) -> {
context.serviceContainerCtx.aeronDirectoryName(context.aeronCtx.aeronDirectoryName());
try {
clusteredServiceContainers.add(ClusteredServiceContainer.launch(context.serviceContainerCtx));
} finally {
systemTestWatcher.dataCollector().add(context.serviceContainerCtx.clusterDir());
}
});
final String aeronDirName = CommonContext.getAeronDirectoryName();
final MediaDriver clientMediaDriver = MediaDriver.launch(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).aeronDirectoryName(aeronDirName).nameResolver(new RedirectingNameResolver(TestCluster.DEFAULT_NODE_MAPPINGS)));
final AeronCluster client = AeronCluster.connect(new AeronCluster.Context().aeronDirectoryName(aeronDirName).ingressChannel(CommonContext.UDP_CHANNEL).ingressEndpoints(TestCluster.ingressEndpoints(0, 3)));
try {
final DirectBuffer buffer = new UnsafeBuffer(new byte[100]);
while (client.offer(buffer, 0, 100) < 0) {
Tests.yield();
}
Tests.awaitValue(serviceAMessageCount, 3);
Tests.awaitValue(serviceBMessageCount, 3);
} finally {
CloseHelper.closeAll(client, clientMediaDriver);
clusteredMediaDrivers.forEach((clusteredMediaDriver) -> clusteredMediaDriver.consensusModule().close());
CloseHelper.closeAll(clusteredServiceContainers);
CloseHelper.closeAll(clusteredMediaDrivers);
clientMediaDriver.context().deleteDirectory();
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class TestMediaDriverTest method connectToCMediaDriverWithoutSpecifyingAeronDir.
@Test
void connectToCMediaDriverWithoutSpecifyingAeronDir() {
assumeTrue(TestMediaDriver.shouldRunCMediaDriver());
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).dirDeleteOnShutdown(false);
assertEquals(CommonContext.getAeronDirectoryName(), context.aeronDirectoryName());
try (TestMediaDriver mediaDriver = CTestMediaDriver.launch(context, false, null);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(context.aeronDirectoryName()))) {
final File aeronDirectory = aeron.context().aeronDirectory();
assertNotNull(aeronDirectory);
assertTrue(aeronDirectory.exists());
assertNotNull(mediaDriver);
} finally {
context.deleteDirectory();
}
}
Aggregations