Search in sources :

Example 1 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project Aeron by real-logic.

the class Configuration method agentIdleStrategy.

/**
     * Get the {@link IdleStrategy} that should be applied to {@link org.agrona.concurrent.Agent}s.
     *
     * @param strategyName       to be created.
     * @param controllableStatus status indicator for what the strategy should do.
     * @return the newly created IdleStrategy.
     */
public static IdleStrategy agentIdleStrategy(final String strategyName, final StatusIndicator controllableStatus) {
    IdleStrategy idleStrategy = null;
    switch(strategyName) {
        case DEFAULT_IDLE_STRATEGY:
            idleStrategy = new BackoffIdleStrategy(AGENT_IDLE_MAX_SPINS, AGENT_IDLE_MAX_YIELDS, AGENT_IDLE_MIN_PARK_NS, AGENT_IDLE_MAX_PARK_NS);
            break;
        case CONTROLLABLE_IDLE_STRATEGY:
            idleStrategy = new ControllableIdleStrategy(controllableStatus);
            controllableStatus.setOrdered(ControllableIdleStrategy.PARK);
            break;
        default:
            try {
                idleStrategy = (IdleStrategy) Class.forName(strategyName).newInstance();
            } catch (final Exception ex) {
                LangUtil.rethrowUnchecked(ex);
            }
            break;
    }
    return idleStrategy;
}
Also used : ControllableIdleStrategy(org.agrona.concurrent.ControllableIdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) ControllableIdleStrategy(org.agrona.concurrent.ControllableIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) ConfigurationException(io.aeron.driver.exceptions.ConfigurationException)

Example 2 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.

the class MultipleSubscribersWithFragmentAssembly method main.

public static void main(final String[] args) {
    System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
    final FragmentAssembler dataHandler1 = new FragmentAssembler(reassembledStringMessage1(STREAM_ID_1));
    final FragmentAssembler dataHandler2 = new FragmentAssembler(reassembledStringMessage2(STREAM_ID_2));
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
        Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
        int idleCount = 0;
        while (running.get()) {
            final int fragmentsRead1 = subscription1.poll(dataHandler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(dataHandler2, FRAGMENT_COUNT_LIMIT);
            if ((fragmentsRead1 + fragmentsRead2) == 0) {
                idleStrategy.idle(idleCount++);
            } else {
                idleCount = 0;
            }
        }
        System.out.println("Shutting down...");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Example 3 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.

the class EmbeddedRecordingThroughput method runRecordingEventPoller.

private void runRecordingEventPoller() {
    try (Subscription subscription = aeron.addSubscription(AeronArchive.Configuration.recordingEventsChannel(), AeronArchive.Configuration.recordingEventsStreamId())) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(10, 100, 1, 1);
        final RecordingEventsAdapter recordingEventsAdapter = new RecordingEventsAdapter(this, subscription, FRAGMENT_COUNT_LIMIT);
        while (isRunning) {
            idleStrategy.idle(recordingEventsAdapter.poll());
        }
    }
}
Also used : BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) RecordingEventsAdapter(io.aeron.archive.client.RecordingEventsAdapter)

Example 4 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project aeron by real-logic.

the class Configuration method agentIdleStrategy.

/**
 * Get the {@link IdleStrategy} that should be applied to {@link org.agrona.concurrent.Agent}s.
 *
 * @param strategyName       of the class to be created.
 * @param controllableStatus status indicator for what the strategy should do.
 * @return the newly created IdleStrategy.
 */
public static IdleStrategy agentIdleStrategy(final String strategyName, final StatusIndicator controllableStatus) {
    IdleStrategy idleStrategy = null;
    switch(strategyName) {
        case DEFAULT_IDLE_STRATEGY:
            idleStrategy = new BackoffIdleStrategy(IDLE_MAX_SPINS, IDLE_MAX_YIELDS, IDLE_MIN_PARK_NS, IDLE_MAX_PARK_NS);
            break;
        case CONTROLLABLE_IDLE_STRATEGY:
            idleStrategy = new ControllableIdleStrategy(controllableStatus);
            controllableStatus.setOrdered(ControllableIdleStrategy.PARK);
            break;
        default:
            try {
                idleStrategy = (IdleStrategy) Class.forName(strategyName).newInstance();
            } catch (final Exception ex) {
                LangUtil.rethrowUnchecked(ex);
            }
            break;
    }
    return idleStrategy;
}
Also used : ControllableIdleStrategy(org.agrona.concurrent.ControllableIdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) ControllableIdleStrategy(org.agrona.concurrent.ControllableIdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IOException(java.io.IOException) ConfigurationException(io.aeron.driver.exceptions.ConfigurationException)

Example 5 with BackoffIdleStrategy

use of org.agrona.concurrent.BackoffIdleStrategy in project Aeron by real-logic.

the class MultipleSubscribersWithFragmentAssembly method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    System.out.format("Subscribing to %s on stream ID %d and stream ID %d%n", CHANNEL, STREAM_ID_1, STREAM_ID_2);
    final Aeron.Context ctx = new Aeron.Context().availableImageHandler(MultipleSubscribersWithFragmentAssembly::eventAvailableImage).unavailableImageHandler(MultipleSubscribersWithFragmentAssembly::eventUnavailableImage);
    final FragmentAssembler assembler1 = new FragmentAssembler(reassembledMessage1(STREAM_ID_1));
    final FragmentAssembler assembler2 = new FragmentAssembler(reassembledMessage2(STREAM_ID_2));
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    try (Aeron aeron = Aeron.connect(ctx);
        Subscription subscription1 = aeron.addSubscription(CHANNEL, STREAM_ID_1);
        Subscription subscription2 = aeron.addSubscription(CHANNEL, STREAM_ID_2)) {
        final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
        int idleCount = 0;
        while (running.get()) {
            final int fragmentsRead1 = subscription1.poll(assembler1, FRAGMENT_COUNT_LIMIT);
            final int fragmentsRead2 = subscription2.poll(assembler2, FRAGMENT_COUNT_LIMIT);
            if ((fragmentsRead1 + fragmentsRead2) == 0) {
                idleStrategy.idle(idleCount++);
            } else {
                idleCount = 0;
            }
        }
        System.out.println("Shutting down...");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) IdleStrategy(org.agrona.concurrent.IdleStrategy) BackoffIdleStrategy(org.agrona.concurrent.BackoffIdleStrategy) Subscription(io.aeron.Subscription) Aeron(io.aeron.Aeron) FragmentAssembler(io.aeron.FragmentAssembler)

Aggregations

BackoffIdleStrategy (org.agrona.concurrent.BackoffIdleStrategy)8 IdleStrategy (org.agrona.concurrent.IdleStrategy)7 Subscription (io.aeron.Subscription)5 Aeron (io.aeron.Aeron)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FragmentAssembler (io.aeron.FragmentAssembler)2 ConfigurationException (io.aeron.driver.exceptions.ConfigurationException)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 TimeUnit (java.util.concurrent.TimeUnit)2 ControllableIdleStrategy (org.agrona.concurrent.ControllableIdleStrategy)2 SigInt (org.agrona.concurrent.SigInt)2 RecordingEventsAdapter (io.aeron.archive.client.RecordingEventsAdapter)1 MediaDriver (io.aeron.driver.MediaDriver)1 IOException (java.io.IOException)1 NoOpIdleStrategy (org.agrona.concurrent.NoOpIdleStrategy)1