org.agrona.concurrent.IdleStrategy Java Examples

The following examples show how to use org.agrona.concurrent.IdleStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BenchmarkConfiguration.java    From artio with Apache License 2.0 6 votes vote down vote up
static IdleStrategy idleStrategy()
{
    final String strategyName = System.getProperty("fix.benchmark.engine_idle", "");
    switch (strategyName)
    {
        case "noop":
            return new NoOpIdleStrategy();

        case "yield":
            return new YieldingIdleStrategy();

        default:
        case "backoff":
            return backoffIdleStrategy();
    }
}
 
Example #2
Source File: ReplayIndexTest.java    From artio with Apache License 2.0 6 votes vote down vote up
private int query(
    final long sessionId,
    final int beginSequenceNumber,
    final int beginSequenceIndex,
    final int endSequenceNumber,
    final int endSequenceIndex)
{
    final ReplayOperation operation = query.query(
        sessionId,
        beginSequenceNumber,
        beginSequenceIndex,
        endSequenceNumber,
        endSequenceIndex,
        REPLAY,
        new FixMessageTracker(REPLAY, mockHandler, sessionId));

    final IdleStrategy idleStrategy = CommonConfiguration.backoffIdleStrategy();
    while (!operation.attemptReplay())
    {
        idleStrategy.idle();
    }
    idleStrategy.reset();

    return operation.replayedMessages();
}
 
Example #3
Source File: ReplayerSession.java    From artio with Apache License 2.0 6 votes vote down vote up
protected ReplayerSession(
    final long connectionId,
    final BufferClaim bufferClaim,
    final IdleStrategy idleStrategy,
    final int maxClaimAttempts,
    final ExclusivePublication publication,
    final ReplayQuery replayQuery,
    final int beginSeqNo,
    final int endSeqNo,
    final long sessionId,
    final int sequenceIndex)
{
    this.connectionId = connectionId;
    this.bufferClaim = bufferClaim;
    this.idleStrategy = idleStrategy;
    this.maxClaimAttempts = maxClaimAttempts;
    this.publication = publication;
    this.replayQuery = replayQuery;
    this.beginSeqNo = beginSeqNo;
    this.endSeqNo = endSeqNo;
    this.sessionId = sessionId;
    this.sequenceIndex = sequenceIndex;
}
 
Example #4
Source File: ReplayQuery.java    From artio with Apache License 2.0 6 votes vote down vote up
public ReplayQuery(
    final String logFileDir,
    final int cacheNumSets,
    final int cacheSetSize,
    final ExistingBufferFactory indexBufferFactory,
    final int requiredStreamId,
    final IdleStrategy idleStrategy,
    final AeronArchive aeronArchive,
    final ErrorHandler errorHandler,
    final int archiveReplayStream)
{
    this.logFileDir = logFileDir;
    this.indexBufferFactory = indexBufferFactory;
    this.requiredStreamId = requiredStreamId;
    this.idleStrategy = idleStrategy;
    this.aeronArchive = aeronArchive;
    this.errorHandler = errorHandler;
    this.archiveReplayStream = archiveReplayStream;

    logFileDirFile = new File(logFileDir);
    fixSessionToIndex = new Long2ObjectCache<>(cacheNumSets, cacheSetSize, SessionQuery::close);
}
 
Example #5
Source File: AeronExclusiveIpcBenchmark.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
public void run()
{
    while (!subscription.isConnected())
    {
        Thread.yield();
    }

    final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
    while (true)
    {
        final int frameCount = subscription.poll(this, FRAGMENT_LIMIT);
        if (0 == frameCount)
        {
            if (!running.get())
            {
                break;
            }
        }

        idleStrategy.idle(frameCount);
    }
}
 
Example #6
Source File: AeronIpcBenchmark.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
public void run()
{
    while (!subscription.isConnected())
    {
        Thread.yield();
    }

    final IdleStrategy idleStrategy = new BusySpinIdleStrategy();
    while (true)
    {
        final int frameCount = subscription.poll(this, FRAGMENT_LIMIT);
        if (0 == frameCount)
        {
            if (!running.get())
            {
                break;
            }
        }

        idleStrategy.idle(frameCount);
    }
}
 
Example #7
Source File: ILinkReplayerSession.java    From artio with Apache License 2.0 6 votes vote down vote up
public ILinkReplayerSession(
    final long connectionId,
    final BufferClaim bufferClaim,
    final IdleStrategy idleStrategy,
    final int maxClaimAttempts,
    final ExclusivePublication publication,
    final ReplayQuery replayQuery,
    final int beginSeqNo,
    final int endSeqNo,
    final long sessionId)
{
    super(connectionId, bufferClaim, idleStrategy, maxClaimAttempts, publication, replayQuery, beginSeqNo, endSeqNo,
        sessionId, 0);

    state = State.REPLAYING;
}
 
Example #8
Source File: SamplesUtil.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return a reusable, parametrised event loop that calls and idler when no messages are received.
 *
 * @param fragmentHandler to be called back for each message.
 * @param limit           passed to {@link Subscription#poll(FragmentHandler, int)}.
 * @param running         indication for loop.
 * @param idleStrategy    to use for loop.
 * @return loop function.
 */
public static Consumer<Subscription> subscriberLoop(
    final FragmentHandler fragmentHandler,
    final int limit,
    final AtomicBoolean running,
    final IdleStrategy idleStrategy)
{
    return
        (subscription) ->
        {
            final FragmentAssembler assembler = new FragmentAssembler(fragmentHandler);
            while (running.get())
            {
                final int fragmentsRead = subscription.poll(assembler, limit);
                idleStrategy.idle(fragmentsRead);
            }
        };
}
 
Example #9
Source File: RecordingIdLookup.java    From artio with Apache License 2.0 5 votes vote down vote up
public RecordingIdLookup(
    final IdleStrategy archiverIdleStrategy,
    final CountersReader counters)
{
    this.archiverIdleStrategy = archiverIdleStrategy;
    this.counters = counters;
}
 
Example #10
Source File: FixBenchmarkServer.java    From artio with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args)
{
    final EngineConfiguration configuration = engineConfiguration();

    try (MediaDriver mediaDriver = newMediaDriver();
        FixEngine engine = FixEngine.launch(configuration);
        FixLibrary library = FixLibrary.connect(libraryConfiguration()))
    {
        final IdleStrategy idleStrategy = idleStrategy();
        System.out.printf("Using %s idle strategy%n", idleStrategy.getClass().getSimpleName());
        while (true)
        {
            final boolean notConnected = !library.isConnected();

            idleStrategy.idle(library.poll(10));

            if (notConnected && library.isConnected())
            {
                System.out.println("Connected");
                break;
            }
        }

        while (true)
        {
            idleStrategy.idle(library.poll(10));
        }
    }
}
 
Example #11
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 5 votes vote down vote up
RecordingCoordinator(
    final Aeron aeron,
    final AeronArchive archive,
    final EngineConfiguration configuration,
    final IdleStrategy archiverIdleStrategy,
    final ErrorHandler errorHandler)
{
    this.aeron = aeron;
    this.archive = archive;
    this.configuration = configuration;
    this.channel = configuration.libraryAeronChannel();

    recordingIdsFile = new File(configuration.logFileDir(), FILE_NAME);
    this.errorHandler = errorHandler;
    outboundLocation = channel.equals(IPC_CHANNEL) ? LOCAL : REMOTE;
    loadRecordingIdsFile();

    if (configuration.logAnyMessages())
    {
        counters = this.aeron.countersReader();
        framerInboundLookup = new RecordingIdLookup(archiverIdleStrategy, counters);
        framerOutboundLookup = new RecordingIdLookup(archiverIdleStrategy, counters);
        indexerInboundLookup = new RecordingIdLookup(archiverIdleStrategy, counters);
        indexerOutboundLookup = new RecordingIdLookup(archiverIdleStrategy, counters);
    }
    else
    {
        counters = null;
        framerInboundLookup = null;
        framerOutboundLookup = null;
        indexerInboundLookup = null;
        indexerOutboundLookup = null;
    }
}
 
Example #12
Source File: LibraryUtil.java    From artio with Apache License 2.0 5 votes vote down vote up
/**
 * Initiate a FIX session with a FIX acceptor. This method returns a reply object
 * wrapping the Session itself.
 *
 * @param library the library to attempt to initiate from.
 * @param configuration the configuration to use for the session.
 * @param attempts the number of times to attempt to connect to a gateway.
 * @param idleStrategy the {@link IdleStrategy} to use when polling.
 * @param fragmentLimit the number of messages to poll on the library before idling.
 * @return the session object for the session that you've initiated.
 * @throws IllegalStateException if you're trying to initiate two sessions at the same time or if there's a timeout talking to
 *         the {@link uk.co.real_logic.artio.engine.FixEngine}.
 *         This probably indicates that there's a problem in your code or that your engine isn't running.
 * @throws FixGatewayException
 *         if you're unable to connect to the accepting gateway.
 *         This probably indicates a configuration problem related to the external gateway.
 * @throws TimeoutException the connection has timed out <code>attempts</code> number of times.
 * @throws IllegalArgumentException if attempts is &lt; 1
 */
private static Session initiate(
    final FixLibrary library,
    final SessionConfiguration configuration,
    final int attempts,
    final IdleStrategy idleStrategy,
    final int fragmentLimit)
{
    if (attempts < 1)
    {
        throw new IllegalArgumentException("Attempts should be >= 1, but is " + attempts);
    }

    Reply<Session> reply = null;
    for (int i = 0; i < attempts; i++)
    {
        reply = library.initiate(configuration);
        while (reply.isExecuting())
        {
            idleStrategy.idle(library.poll(fragmentLimit));
        }

        if (reply.hasCompleted())
        {
            return reply.resultIfPresent();
        }
    }

    if (reply.hasTimedOut())
    {
        throw new TimeoutException("reply timeout", AeronException.Category.ERROR);
    }
    else
    {
        LangUtil.rethrowUnchecked(reply.error());
        // never executed:
        return null;
    }
}
 
Example #13
Source File: ClaimablePublication.java    From artio with Apache License 2.0 5 votes vote down vote up
ClaimablePublication(
    final int maxClaimAttempts,
    final IdleStrategy idleStrategy,
    final AtomicCounter fails,
    final ExclusivePublication dataPublication)
{
    this.maxClaimAttempts = maxClaimAttempts;
    this.idleStrategy = idleStrategy;
    this.fails = fails;
    dataPublication(dataPublication);
}
 
Example #14
Source File: Streams.java    From artio with Apache License 2.0 5 votes vote down vote up
public GatewayPublication gatewayPublication(
    final IdleStrategy idleStrategy, final ExclusivePublication dataPublication)
{
    return new GatewayPublication(
        dataPublication,
        failedPublications,
        idleStrategy,
        clock,
        maxClaimAttempts);
}
 
Example #15
Source File: Indexer.java    From artio with Apache License 2.0 5 votes vote down vote up
private void idle(final IdleStrategy idleStrategy, final AgentInvoker aeronInvoker, final int workCount)
{
    int totalWork = workCount;
    if (aeronInvoker != null)
    {
        totalWork += aeronInvoker.invoke();
    }

    idleStrategy.idle(totalWork);
}
 
Example #16
Source File: GatewayPublication.java    From artio with Apache License 2.0 5 votes vote down vote up
public GatewayPublication(
    final ExclusivePublication dataPublication,
    final AtomicCounter fails,
    final IdleStrategy idleStrategy,
    final Clock clock,
    final int maxClaimAttempts)
{
    super(maxClaimAttempts, idleStrategy, fails, dataPublication);
    this.clock = clock;
    this.maxPayloadLength = dataPublication.maxPayloadLength();
    this.maxInitialBodyLength = maxPayloadLength - FRAMED_MESSAGE_SIZE;
}
 
Example #17
Source File: LoadTestRig.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
void receive()
{
    final MessageTransceiver messageTransceiver = this.messageTransceiver;
    final IdleStrategy idleStrategy = configuration.receiveIdleStrategy();

    long sent = 0;
    long received = 0;
    while (true)
    {
        messageTransceiver.receive();

        final long receivedMessagesCount = receivedMessages;
        if (receivedMessagesCount != received)
        {
            received = receivedMessagesCount;
            idleStrategy.reset();
        }
        else
        {
            idleStrategy.idle();
        }

        if (0 == sent)
        {
            sent = sentMessages;
        }

        if (0 != sent && received >= sent)
        {
            break;
        }
    }
}
 
Example #18
Source File: AeronUtil.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Return a reusable, parameterized event
 * loop that calls and idler
 * when no messages are received
 *
 * @param fragmentHandler to be called back for each message.
 * @param limit           passed to {@link Subscription#poll(FragmentHandler, int)}
 * @param running         indication for loop
 * @param idleStrategy    to use for loop
 * @return loop function
 */
public static Consumer<Subscription> subscriberLoop(final FragmentHandler fragmentHandler, final int limit,
                final AtomicBoolean running, final IdleStrategy idleStrategy, final AtomicBoolean launched) {
    return (subscription) -> {
        try {
            while (running.get()) {
                idleStrategy.idle(subscription.poll(fragmentHandler, limit));
                launched.set(true);
            }
        } catch (final Exception ex) {
            LangUtil.rethrowUnchecked(ex);
        }
    };
}
 
Example #19
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static void pipeMessages(
    final Subscription subscription, final ExclusivePublication publication, final AtomicBoolean running)
{
    final IdleStrategy idleStrategy = idleStrategy();
    final BufferClaim bufferClaim = new BufferClaim();
    final FragmentHandler dataHandler =
        (buffer, offset, length, header) ->
        {
            long result;
            while ((result = publication.tryClaim(length, bufferClaim)) <= 0)
            {
                checkPublicationResult(result);
            }

            bufferClaim
                .flags(header.flags())
                .putBytes(buffer, offset, length)
                .commit();
        };

    final Image image = subscription.imageAtIndex(0);
    final int fragmentLimit = fragmentLimit();

    while (true)
    {
        final int fragmentsRead = image.poll(dataHandler, fragmentLimit);
        if (0 == fragmentsRead)
        {
            if (image.isClosed() || !running.get())
            {
                break;
            }
        }

        idleStrategy.idle(fragmentsRead);
    }
}
 
Example #20
Source File: MemoryOrderingTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;

    while (messageNum < NUM_MESSAGES && null == failedMessage)
    {
        idleStrategy.idle(subscription.poll(fragmentAssembler, FRAGMENT_COUNT_LIMIT));
    }
}
 
Example #21
Source File: SnapshotTaker.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a {@link SnapshotTaker} which will encode the snapshot to a publication.
 *
 * @param publication       into which the snapshot will be encoded.
 * @param idleStrategy      to call when the publication is back pressured.
 * @param aeronAgentInvoker to call when idling so it stays active.
 */
public SnapshotTaker(
    final ExclusivePublication publication, final IdleStrategy idleStrategy, final AgentInvoker aeronAgentInvoker)
{
    this.publication = publication;
    this.idleStrategy = idleStrategy;
    this.aeronAgentInvoker = aeronAgentInvoker;
}
 
Example #22
Source File: ClusterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static Thread startMessageThread(final TestCluster cluster, final long backoffIntervalNs)
{
    final Thread thread = new Thread(
        () ->
        {
            final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
            final AeronCluster client = cluster.client();
            final ExpandableArrayBuffer msgBuffer = cluster.msgBuffer();
            msgBuffer.putStringWithoutLengthAscii(0, HELLO_WORLD_MSG);

            while (!Thread.interrupted())
            {
                if (client.offer(msgBuffer, 0, HELLO_WORLD_MSG.length()) < 0)
                {
                    LockSupport.parkNanos(backoffIntervalNs);
                }

                idleStrategy.idle(client.pollEgress());
            }
        });

    thread.setDaemon(true);
    thread.setName("message-thread");
    thread.start();

    return thread;
}
 
Example #23
Source File: IndexedReplicatedRecording.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    while (!subscription.isConnected() || !publication.isConnected())
    {
        try
        {
            Thread.sleep(1);
        }
        catch (final InterruptedException ignore)
        {
            Thread.currentThread().interrupt();
            return;
        }
    }

    final Image image = subscription.imageBySessionId(sessionId);
    this.image = image;
    if (null == image)
    {
        throw new IllegalStateException("session not found");
    }

    lastMessagePosition = image.joinPosition();

    final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
    while (true)
    {
        final int fragments = image.controlledPoll(this, FRAGMENT_LIMIT);
        if (0 == fragments)
        {
            if (Thread.interrupted() || image.isClosed())
            {
                return;
            }
        }

        idleStrategy.idle(fragments);
    }
}
 
Example #24
Source File: ImageRateSubscriber.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    while (!subscription.isConnected())
    {
        Thread.yield();
    }

    final Image image = subscription.images().get(0);
    final ImageFragmentAssembler fragmentAssembler = new ImageFragmentAssembler(this::onFragment);
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final int fragmentLimit = this.fragmentLimit;

    long failedPolls = 0;
    long successfulPolls = 0;

    while (running.get())
    {
        final int fragmentsRead = image.poll(fragmentAssembler, fragmentLimit);
        if (0 == fragmentsRead)
        {
            ++failedPolls;
        }
        else
        {
            ++successfulPolls;
        }

        idleStrategy.idle(fragmentsRead);
    }

    final double failureRatio = failedPolls / (double)(successfulPolls + failedPolls);
    System.out.format("Subscriber poll failure ratio: %f%n", failureRatio);
}
 
Example #25
Source File: EmbeddedExclusiveBufferClaimIpcThroughput.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final AtomicBoolean running = this.running;
    final Publication publication = this.publication;
    final BufferClaim bufferClaim = new BufferClaim();
    long backPressureCount = 0;
    long totalMessageCount = 0;

    outputResults:
    while (running.get())
    {
        for (int i = 0; i < BURST_LENGTH; i++)
        {
            idleStrategy.reset();
            while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) <= 0)
            {
                ++backPressureCount;
                if (!running.get())
                {
                    break outputResults;
                }
                idleStrategy.idle();
            }

            final int offset = bufferClaim.offset();
            bufferClaim.buffer().putInt(offset, i); // Example field write
            // Real app would write whatever fields are required via a flyweight like SBE

            bufferClaim.commit();

            ++totalMessageCount;
        }
    }

    final double backPressureRatio = backPressureCount / (double)totalMessageCount;
    System.out.format("Publisher back pressure ratio: %f%n", backPressureRatio);
}
 
Example #26
Source File: EmbeddedBufferClaimIpcThroughput.java    From aeron with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final AtomicBoolean running = this.running;
    final Publication publication = this.publication;
    final BufferClaim bufferClaim = new BufferClaim();
    long backPressureCount = 0;
    long totalMessageCount = 0;

    outputResults:
    while (running.get())
    {
        for (int i = 0; i < BURST_LENGTH; i++)
        {
            idleStrategy.reset();
            while (publication.tryClaim(MESSAGE_LENGTH, bufferClaim) <= 0)
            {
                ++backPressureCount;
                if (!running.get())
                {
                    break outputResults;
                }
                idleStrategy.idle();
            }

            final int offset = bufferClaim.offset();
            bufferClaim.buffer().putInt(offset, i); // Example field write
            // Real app would write whatever fields are required via a flyweight like SBE

            bufferClaim.commit();

            ++totalMessageCount;
        }
    }

    final double backPressureRatio = backPressureCount / (double)totalMessageCount;
    System.out.format("Publisher back pressure ratio: %f%n", backPressureRatio);
}
 
Example #27
Source File: AeronUtil.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Return a reusable, parameterized event
 * loop that calls and idler
 * when no messages are received
 *
 * @param fragmentHandler to be called back for each message.
 * @param limit           passed to {@link Subscription#poll(FragmentHandler, int)}
 * @param running         indication for loop
 * @param idleStrategy    to use for loop
 * @return loop function
 */
public static Consumer<Subscription> subscriberLoop(final FragmentHandler fragmentHandler, final int limit,
                final AtomicBoolean running, final IdleStrategy idleStrategy, final AtomicBoolean launched) {
    return (subscription) -> {
        try {
            while (running.get()) {
                idleStrategy.idle(subscription.poll(fragmentHandler, limit));
                launched.set(true);
            }
        } catch (final Exception ex) {
            LangUtil.rethrowUnchecked(ex);
        }
    };
}
 
Example #28
Source File: EmbeddedRecordingThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public long streamMessagesForRecording()
{
    try (ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID))
    {
        final IdleStrategy idleStrategy = YieldingIdleStrategy.INSTANCE;
        while (!publication.isConnected())
        {
            idleStrategy.idle();
        }

        final long startNs = System.nanoTime();
        final UnsafeBuffer buffer = this.buffer;

        for (long i = 0; i < NUMBER_OF_MESSAGES; i++)
        {
            buffer.putLong(0, i);

            idleStrategy.reset();
            while (publication.offer(buffer, 0, MESSAGE_LENGTH) < 0)
            {
                idleStrategy.idle();
            }
        }

        final long stopPosition = publication.position();
        final CountersReader counters = aeron.countersReader();
        final int counterId = RecordingPos.findCounterIdBySession(counters, publication.sessionId());

        idleStrategy.reset();
        while (counters.getCounterValue(counterId) < stopPosition)
        {
            idleStrategy.idle();
        }

        final long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
        final double dataRate = (stopPosition * 1000.0d / durationMs) / MEGABYTE;
        final double recordingMb = stopPosition / MEGABYTE;
        final long msgRate = (NUMBER_OF_MESSAGES / durationMs) * 1000L;

        System.out.printf(
            "Recorded %.02f MB @ %.02f MB/s - %,d msg/sec - %d byte payload + 32 byte header%n",
            recordingMb, dataRate, msgRate, MESSAGE_LENGTH);

        return RecordingPos.getRecordingId(counters, counterId);
    }
}
 
Example #29
Source File: MultipleSubscribersWithFragmentAssembly.java    From aeron with Apache License 2.0 4 votes vote down vote up
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...");
    }
}
 
Example #30
Source File: EmbeddedExclusiveSpiedThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    loadPropertiesFiles(args);

    final RateReporter reporter = new RateReporter(
        TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final AtomicBoolean running = new AtomicBoolean(true);

    final MediaDriver.Context ctx = new MediaDriver.Context()
        .spiesSimulateConnection(true);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(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();
    }
}