Java Code Examples for org.agrona.concurrent.status.CountersReader#getCounterState()

The following examples show how to use org.agrona.concurrent.status.CountersReader#getCounterState() . 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: RecordingPos.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for a stream based on the session id.
 *
 * @param countersReader to search within.
 * @param sessionId      for the active recording.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterIdBySession(final CountersReader countersReader, final int sessionId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == RECORDING_POSITION_TYPE_ID)
        {
            if (buffer.getInt(CountersReader.metaDataOffset(i) + KEY_OFFSET + SESSION_ID_OFFSET) == sessionId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example 2
Source File: RecordingPos.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for a stream based on the recording id.
 *
 * @param countersReader to search within.
 * @param recordingId    for the active recording.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterIdByRecording(final CountersReader countersReader, final long recordingId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == RECORDING_POSITION_TYPE_ID)
        {
            if (buffer.getLong(CountersReader.metaDataOffset(i) + KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example 3
Source File: IndexedReplicatedRecording.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void awaitPosition(final CountersReader counters, final int counterId, final long position)
    throws InterruptedException
{
    while (counters.getCounterValue(counterId) < position)
    {
        if (counters.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED)
        {
            throw new IllegalStateException("count not active: " + counterId);
        }

        Thread.yield();
        if (Thread.interrupted())
        {
            throw new InterruptedException();
        }
    }
}
 
Example 4
Source File: RecoveryState.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for recovery state.
 *
 * @param counters  to search within.
 * @param clusterId to constrain the search.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterId(final CountersReader counters, final int clusterId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    for (int i = 0, size = counters.maxCounterId(); i < size; i++)
    {
        if (counters.getCounterState(i) == RECORD_ALLOCATED &&
            counters.getCounterTypeId(i) == RECOVERY_STATE_TYPE_ID)
        {
            if (buffer.getInt(CountersReader.metaDataOffset(i) + KEY_OFFSET + CLUSTER_ID_OFFSET) == clusterId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example 5
Source File: HeartbeatTimestamp.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for a heartbeat timestamp.
 *
 * @param countersReader to search within.
 * @param counterTypeId  to match on.
 * @param registrationId for the active client.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterIdByRegistrationId(
    final CountersReader countersReader, final int counterTypeId, final long registrationId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == counterTypeId)
        {
            final int recordOffset = CountersReader.metaDataOffset(i);

            if (buffer.getLong(recordOffset + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example 6
Source File: ReadableCounter.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a view of an existing counter.
 *
 * @param countersReader for getting access to the buffers.
 * @param registrationId assigned by the driver for the counter or {@link Aeron#NULL_VALUE} if not known.
 * @param counterId      for the counter to be viewed.
 * @throws IllegalStateException if the id has for the counter has not been allocated.
 */
public ReadableCounter(final CountersReader countersReader, final long registrationId, final int counterId)
{
    final int counterState = countersReader.getCounterState(counterId);
    if (counterState != CountersReader.RECORD_ALLOCATED)
    {
        throw new IllegalStateException("Counter not allocated: id=" + counterId + " state=" + counterState);
    }

    this.countersReader = countersReader;
    this.counterId = counterId;
    this.registrationId = registrationId;

    final AtomicBuffer valuesBuffer = countersReader.valuesBuffer();
    final int counterOffset = CountersReader.counterOffset(counterId);
    valuesBuffer.boundsCheck(counterOffset, SIZE_OF_LONG);

    this.buffer = valuesBuffer.byteArray();
    this.addressOffset = valuesBuffer.addressOffset() + counterOffset;
}
 
Example 7
Source File: RecoveryState.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Get the recording id of the snapshot for a service.
 *
 * @param counters  to search within.
 * @param counterId for the active recovery counter.
 * @param serviceId for the snapshot required.
 * @return the count of replay terms if found otherwise {@link Aeron#NULL_VALUE}.
 */
public static long getSnapshotRecordingId(final CountersReader counters, final int counterId, final int serviceId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    if (counters.getCounterState(counterId) == RECORD_ALLOCATED &&
        counters.getCounterTypeId(counterId) == RECOVERY_STATE_TYPE_ID)
    {
        final int recordOffset = CountersReader.metaDataOffset(counterId);

        final int serviceCount = buffer.getInt(recordOffset + KEY_OFFSET + SERVICE_COUNT_OFFSET);
        if (serviceId < 0 || serviceId >= serviceCount)
        {
            throw new ClusterException("invalid serviceId " + serviceId + " for count of " + serviceCount);
        }

        return buffer.getLong(
            recordOffset + KEY_OFFSET + SNAPSHOT_RECORDING_IDS_OFFSET + (serviceId * SIZE_OF_LONG));
    }

    throw new ClusterException("active counter not found " + counterId);
}
 
Example 8
Source File: ClusterCounters.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the counter id for a type of counter in a cluster.
 *
 * @param counters  to search within.
 * @param typeId    of the counter.
 * @param clusterId to which the allocated counter belongs.
 * @return the matching counter id or {@link Aeron#NULL_VALUE} if not found.
 */
public static int find(final CountersReader counters, final int typeId, final int clusterId)
{
    final AtomicBuffer buffer = counters.metaDataBuffer();

    for (int i = 0, size = counters.maxCounterId(); i < size; i++)
    {
        final int recordOffset = CountersReader.metaDataOffset(i);

        if (counters.getCounterState(i) == RECORD_ALLOCATED &&
            counters.getCounterTypeId(i) == typeId &&
            buffer.getInt(recordOffset + KEY_OFFSET) == clusterId)
        {
            return i;
        }
    }

    return Aeron.NULL_VALUE;
}
 
Example 9
Source File: ArchiveCreator.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void awaitPosition(final CountersReader counters, final int counterId, final long position)
{
    while (counters.getCounterValue(counterId) < position)
    {
        if (counters.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED)
        {
            throw new IllegalStateException("count not active: " + counterId);
        }

        Thread.yield();
        checkInterruptStatus();
    }
}
 
Example 10
Source File: Counter.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a read-write view of an existing counter.
 *
 * @param countersReader for getting access to the buffers.
 * @param registrationId assigned by the driver for the counter or {@link Aeron#NULL_VALUE} if not known.
 * @param counterId      for the counter to be viewed.
 * @throws AeronException if the id has for the counter has not been allocated.
 */
public Counter(final CountersReader countersReader, final long registrationId, final int counterId)
{
    super(countersReader.valuesBuffer(), counterId);

    if (countersReader.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED)
    {
        throw new AeronException("Counter id is not allocated: " + counterId);
    }

    this.registrationId = registrationId;
    this.clientConductor = null;
}
 
Example 11
Source File: HeartbeatTimestamp.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Is the counter active for usage? Checks to see if reclaimed or reused and matches registration id.
 *
 * @param countersReader to search within.
 * @param counterId      to test.
 * @param counterTypeId  to validate type.
 * @param registrationId for the entity.
 * @return true if still valid otherwise false.
 */
public static boolean isActive(
    final CountersReader countersReader, final int counterId, final int counterTypeId, final long registrationId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();
    final int recordOffset = CountersReader.metaDataOffset(counterId);

    return countersReader.getCounterTypeId(counterId) == counterTypeId &&
        buffer.getLong(recordOffset + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId &&
        countersReader.getCounterState(counterId) == RECORD_ALLOCATED;
}
 
Example 12
Source File: LocalSocketAddressStatus.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Find the list of currently bound local sockets.
 *
 * @param countersReader  for the connected driver.
 * @param channelStatus   value for the channel which aggregates the transports.
 * @param channelStatusId identity of the counter for the channel which aggregates the transports.
 * @return the list of active bound local socket addresses.
 */
public static List<String> findAddresses(
    final CountersReader countersReader, final long channelStatus, final int channelStatusId)
{
    if (channelStatus != ChannelEndpointStatus.ACTIVE)
    {
        return Collections.emptyList();
    }

    final ArrayList<String> bindings = new ArrayList<>(2);
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID)
        {
            final int recordOffset = CountersReader.metaDataOffset(i);
            final int keyIndex = recordOffset + CountersReader.KEY_OFFSET;

            if (channelStatusId == buffer.getInt(keyIndex + CHANNEL_STATUS_ID_OFFSET) &&
                ChannelEndpointStatus.ACTIVE == countersReader.getCounterValue(i))
            {
                final int length = buffer.getInt(keyIndex + LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET);
                if (length > 0)
                {
                    bindings.add(buffer.getStringWithoutLengthAscii(
                        keyIndex + LOCAL_SOCKET_ADDRESS_STRING_OFFSET, length));
                }
            }
        }
    }

    return bindings;
}
 
Example 13
Source File: RecoveryState.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the timestamp at the beginning of recovery. {@link Aeron#NULL_VALUE} if no snapshot for recovery.
 *
 * @param counters  to search within.
 * @param counterId for the active recovery counter.
 * @return the timestamp if found otherwise {@link Aeron#NULL_VALUE}.
 */
public static long getTimestamp(final CountersReader counters, final int counterId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    if (counters.getCounterState(counterId) == RECORD_ALLOCATED &&
        counters.getCounterTypeId(counterId) == RECOVERY_STATE_TYPE_ID)
    {
        return buffer.getLong(CountersReader.metaDataOffset(counterId) + KEY_OFFSET + TIMESTAMP_OFFSET);
    }

    return NULL_VALUE;
}
 
Example 14
Source File: RecoveryState.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the position at which the snapshot was taken. {@link Aeron#NULL_VALUE} if no snapshot for recovery.
 *
 * @param counters  to search within.
 * @param counterId for the active recovery counter.
 * @return the log position if found otherwise {@link Aeron#NULL_VALUE}.
 */
public static long getLogPosition(final CountersReader counters, final int counterId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    if (counters.getCounterState(counterId) == RECORD_ALLOCATED &&
        counters.getCounterTypeId(counterId) == RECOVERY_STATE_TYPE_ID)
    {
        return buffer.getLong(CountersReader.metaDataOffset(counterId) + KEY_OFFSET + LOG_POSITION_OFFSET);
    }

    return NULL_VALUE;
}
 
Example 15
Source File: RecoveryState.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the leadership term id for the snapshot state. {@link Aeron#NULL_VALUE} if no snapshot for recovery.
 *
 * @param counters  to search within.
 * @param counterId for the active recovery counter.
 * @return the leadership term id if found otherwise {@link Aeron#NULL_VALUE}.
 */
public static long getLeadershipTermId(final CountersReader counters, final int counterId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    if (counters.getCounterState(counterId) == RECORD_ALLOCATED &&
        counters.getCounterTypeId(counterId) == RECOVERY_STATE_TYPE_ID)
    {
        return buffer.getLong(CountersReader.metaDataOffset(counterId) + KEY_OFFSET + LEADERSHIP_TERM_ID_OFFSET);
    }

    return NULL_VALUE;
}
 
Example 16
Source File: Common.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void awaitPosition(final CountersReader counters, final int counterId, final long position)
{
    while (counters.getCounterValue(counterId) < position)
    {
        if (counters.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED)
        {
            throw new IllegalStateException("count not active: " + counterId);
        }

        Tests.yield();
    }
}
 
Example 17
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Is the recording counter still active.
 *
 * @param counters    to search within.
 * @param counterId   to search for.
 * @param recordingId to confirm it is still the same value.
 * @return true if the counter is still active otherwise false.
 */
public static boolean isActive(final CountersReader counters, final int counterId, final long recordingId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();
    final int recordOffset = CountersReader.metaDataOffset(counterId);

    return counters.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID &&
        buffer.getLong(recordOffset + KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId &&
        counters.getCounterState(counterId) == RECORD_ALLOCATED;
}
 
Example 18
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the {@link Image#sourceIdentity()} for the recording.
 *
 * @param counters  to search within.
 * @param counterId for the active recording.
 * @return {@link Image#sourceIdentity()} for the recording or null if not found.
 */
public static String getSourceIdentity(final CountersReader counters, final int counterId)
{
    final DirectBuffer buffer = counters.metaDataBuffer();

    if (counters.getCounterState(counterId) == RECORD_ALLOCATED &&
        counters.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID)
    {
        final int recordOffset = CountersReader.metaDataOffset(counterId);
        return buffer.getStringAscii(recordOffset + KEY_OFFSET + SOURCE_IDENTITY_LENGTH_OFFSET);
    }

    return null;
}
 
Example 19
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Get the recording id for a given counter id.
 *
 * @param countersReader to search within.
 * @param counterId      for the active recording.
 * @return the counter id if found otherwise {@link #NULL_RECORDING_ID}.
 */
public static long getRecordingId(final CountersReader countersReader, final int counterId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    if (countersReader.getCounterState(counterId) == RECORD_ALLOCATED &&
        countersReader.getCounterTypeId(counterId) == RECORDING_POSITION_TYPE_ID)
    {
        return buffer.getLong(CountersReader.metaDataOffset(counterId) + KEY_OFFSET + RECORDING_ID_OFFSET);
    }

    return NULL_RECORDING_ID;
}