use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class DriverLoggingAgentTest method testLogMediaDriverEvents.
private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
before(enabledEvents, expectedEvents);
final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
while (publication.offer(offerBuffer) < 0) {
Tests.yield();
}
final MutableInteger counter = new MutableInteger();
final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
while (0 == subscription.poll(handler, 1)) {
Tests.yield();
}
assertEquals(counter.get(), 1);
}
final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
while (!WAIT_LIST.isEmpty()) {
Tests.yieldingIdle(errorMessage);
}
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class ArchivingMediaDriver method main.
/**
* Launch an {@link Archive} with an embedded {@link MediaDriver} and await a shutdown signal.
*
* @param args command line argument which is a list for properties files as URLs or filenames.
*/
@SuppressWarnings("try")
public static void main(final String[] args) {
loadPropertiesFiles(args);
final ShutdownSignalBarrier barrier = new ShutdownSignalBarrier();
final MediaDriver.Context ctx = new MediaDriver.Context().terminationHook(barrier::signalAll);
try (ArchivingMediaDriver ignore = launch(ctx, new Archive.Context())) {
barrier.await();
System.out.println("Shutdown Archive...");
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class EmbeddedDualExclusiveThroughput method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread sleep delay is interrupted.
*/
@SuppressWarnings("MethodLength")
public static void main(final String[] args) throws InterruptedException {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedDualExclusiveThroughput::printRate);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
final AvailableImageHandler handler = (image) -> System.out.println("source connection=" + image.sourceIdentity());
final ChannelUriStringBuilder builder = new ChannelUriStringBuilder().media(CommonContext.UDP_MEDIA).controlMode(CommonContext.MDC_CONTROL_MODE_MANUAL);
final String sourceUriOne = builder.controlEndpoint("localhost:20550").tags("1").build();
final String sourceUriTwo = builder.controlEndpoint("localhost:20551").tags("2").build();
try (MediaDriver mediaDriver = MediaDriver.launch();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID, handler, null);
ExclusivePublication publicationOne = aeron.addExclusivePublication(sourceUriOne, STREAM_ID);
ExclusivePublication publicationTwo = aeron.addExclusivePublication(sourceUriTwo, STREAM_ID)) {
publicationOne.addDestination(CHANNEL);
publicationTwo.addDestination(CHANNEL);
while (subscription.imageCount() < 2) {
Thread.yield();
}
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
do {
System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES * 2, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
printingActive = true;
long backPressureCountOne = 0;
long backPressureCountTwo = 0;
for (long a = 0, b = 0; a < NUMBER_OF_MESSAGES || b < NUMBER_OF_MESSAGES; ) {
idleStrategy.reset();
boolean failedOne = false;
boolean failedTwo = false;
if (a < NUMBER_OF_MESSAGES) {
if (publicationOne.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) > 0) {
a++;
} else {
backPressureCountOne++;
failedOne = true;
}
}
if (b < NUMBER_OF_MESSAGES) {
if (publicationTwo.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) > 0) {
b++;
} else {
backPressureCountTwo++;
failedTwo = true;
}
}
if (failedOne || failedTwo) {
idleStrategy.idle();
}
}
System.out.println("Done streaming." + " backPressureRatioOne=" + ((double) backPressureCountOne / NUMBER_OF_MESSAGES) + " backPressureRatioTwo=" + ((double) backPressureCountTwo / NUMBER_OF_MESSAGES));
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
running.set(false);
reporter.halt();
executor.shutdown();
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class StreamingPublisher method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if interrupted during linger.
*/
public static void main(final String[] args) throws InterruptedException {
if (MESSAGE_LENGTH < SIZE_OF_LONG) {
throw new IllegalArgumentException("Message length must be at least " + SIZE_OF_LONG + " bytes");
}
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context context = new Aeron.Context();
if (EMBEDDED_MEDIA_DRIVER) {
context.aeronDirectoryName(driver.aeronDirectoryName());
}
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), StreamingPublisher::printRate);
final ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(reporter);
// clean up resources when this try block is finished.
try (Aeron aeron = Aeron.connect(context);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
do {
printingActive = true;
System.out.format("%nStreaming %,d messages of%s size %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, RANDOM_MESSAGE_LENGTH ? " random" : "", MESSAGE_LENGTH, CHANNEL, STREAM_ID);
long backPressureCount = 0;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
final int length = LENGTH_GENERATOR.getAsInt();
OFFER_BUFFER.putLong(0, i);
idleStrategy.reset();
while (publication.offer(OFFER_BUFFER, 0, length, null) < 0L) {
// The offer failed, which is usually due to the publication
// being temporarily blocked. Retry the offer after a short
// spin/yield/sleep, depending on the chosen IdleStrategy.
backPressureCount++;
idleStrategy.idle();
}
reporter.onMessage(length);
}
System.out.println("Done streaming. Back pressure ratio " + ((double) backPressureCount / NUMBER_OF_MESSAGES));
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
}
reporter.halt();
executor.shutdown();
CloseHelper.close(driver);
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class EmbeddedThroughput method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread is interrupted during linger.
*/
public static void main(final String[] args) throws InterruptedException {
loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final AtomicBoolean running = new AtomicBoolean(true);
try (MediaDriver mediaDriver = MediaDriver.launch();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
do {
System.out.format("%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
printingActive = true;
long backPressureCount = 0;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
OFFER_BUFFER.putLong(0, i);
idleStrategy.reset();
while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) {
backPressureCount++;
idleStrategy.idle();
}
}
System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
if (LINGER_TIMEOUT_MS > 0) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
running.set(false);
reporter.halt();
executor.shutdown();
}
}
Aggregations