Java Code Examples for org.agrona.concurrent.status.AtomicCounter#get()

The following examples show how to use org.agrona.concurrent.status.AtomicCounter#get() . 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: Tests.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void awaitValue(final AtomicCounter counter, final long value)
{
    long counterValue;
    while ((counterValue = counter.get()) < value)
    {
        Thread.yield();
        if (Thread.interrupted())
        {
            unexpectedInterruptStackTrace("awaiting=" + value + " counter=" + counterValue);
            fail("unexpected interrupt");
        }

        if (counter.isClosed())
        {
            unexpectedInterruptStackTrace("awaiting=" + value + " counter=" + counterValue);
        }
    }
}
 
Example 2
Source File: ClusterControl.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Get the {@link ToggleState} for a given control toggle.
 *
 * @param controlToggle to get the current state for.
 * @return the state for the current control toggle.
 * @throws ClusterException if the counter is not one of the valid values.
 */
public static ToggleState get(final AtomicCounter controlToggle)
{
    if (controlToggle.isClosed())
    {
        throw new ClusterException("counter is closed");
    }

    final long toggleValue = controlToggle.get();
    if (toggleValue < 0 || toggleValue > (STATES.length - 1))
    {
        throw new ClusterException("invalid toggle value: " + toggleValue);
    }

    return STATES[(int)toggleValue];
}
 
Example 3
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(10)
public void shouldTakeMultipleSnapshots()
{
    final AtomicLong serviceMsgCount = new AtomicLong(0);
    launchService(serviceMsgCount);
    connectClient();

    final AtomicCounter controlToggle = getControlToggle();

    for (int i = 0; i < 3; i++)
    {
        assertTrue(ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle));

        while (controlToggle.get() != ClusterControl.ToggleState.NEUTRAL.code())
        {
            Tests.sleep(1, "snapshot ", i);
        }
    }

    assertEquals(3L, clusteredMediaDriver.consensusModule().context().snapshotCounter().get());

    ClusterTests.failOnClusterError();
}
 
Example 4
Source File: SingleNodeCluster.java    From aeron with Apache License 2.0 6 votes vote down vote up
void takeSnapshot()
{
    final AtomicCounter snapshotCounter = clusteredMediaDriver.consensusModule().context().snapshotCounter();
    final long snapshotCount = snapshotCounter.get();

    final AtomicCounter controlToggle = ClusterControl.findControlToggle(
        clusteredMediaDriver.mediaDriver().context().countersManager(),
        clusteredMediaDriver.consensusModule().context().clusterId());
    ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle);

    idleStrategy.reset();
    while (snapshotCounter.get() <= snapshotCount)
    {
        idleStrategy.idle();
    }
}
 
Example 5
Source File: ClusterControl.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Toggle the control counter to trigger the requested {@link ToggleState}.
 * <p>
 * This action is thread safe and will succeed if the toggle is in the {@link ToggleState#NEUTRAL} state,
 * or if toggle is {@link ToggleState#SUSPEND} and requested state is {@link ToggleState#RESUME}.
 *
 * @param controlToggle to change to the trigger state.
 * @return true if the counter toggles or false if it is in a state other than {@link ToggleState#NEUTRAL}.
 */
public final boolean toggle(final AtomicCounter controlToggle)
{
    if (code() == RESUME.code() && controlToggle.get() == SUSPEND.code())
    {
        return controlToggle.compareAndSet(SUSPEND.code(), RESUME.code());
    }

    return controlToggle.compareAndSet(NEUTRAL.code(), code());
}
 
Example 6
Source File: TestCluster.java    From aeron with Apache License 2.0 5 votes vote down vote up
void awaitNeutralControlToggle(final TestNode leaderNode)
{
    final AtomicCounter controlToggle = getControlToggle(leaderNode);
    while (controlToggle.get() != ClusterControl.ToggleState.NEUTRAL.code())
    {
        Tests.yield();
    }
}
 
Example 7
Source File: ConsensusModule.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link State} encoded in an {@link AtomicCounter}.
 *
 * @param counter to get the current state for.
 * @return the state for the {@link ConsensusModule}.
 * @throws ClusterException if the counter is not one of the valid values.
 */
public static State get(final AtomicCounter counter)
{
    final long code = counter.get();
    return get((int)code);
}