Class CountersManager

  • Direct Known Subclasses:
    ConcurrentCountersManager

    public class CountersManager
    extends CountersReader
    Manages the allocation and freeing of counters that are normally stored in a memory-mapped file.

    This class in not threadsafe. Counters should be centrally managed.

    Values Buffer

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                        Counter Value                          |
      |                                                               |
      +---------------------------------------------------------------+
      |                       Registration Id                         |
      |                                                               |
      +---------------------------------------------------------------+
      |                          Owner Id                             |
      |                                                               |
      +---------------------------------------------------------------+
      |                        Reference Id                           |
      |                                                               |
      +---------------------------------------------------------------+
      |                     96 bytes of padding                     ...
     ...                                                              |
      +---------------------------------------------------------------+
      |                   Repeats to end of buffer                   ...
      |                                                               |
     ...                                                              |
      +---------------------------------------------------------------+
     

    Meta Data Buffer

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                        Record State                           |
      +---------------------------------------------------------------+
      |                          Type Id                              |
      +---------------------------------------------------------------+
      |                 Free-for-reuse Deadline (ms)                  |
      |                                                               |
      +---------------------------------------------------------------+
      |                      112 bytes for key                       ...
     ...                                                              |
      +-+-------------------------------------------------------------+
      |R|                      Label Length                           |
      +-+-------------------------------------------------------------+
      |                     380 bytes of Label                       ...
     ...                                                              |
      +---------------------------------------------------------------+
      |                   Repeats to end of buffer                   ...
      |                                                               |
     ...                                                              |
      +---------------------------------------------------------------+
     
    • Field Detail

      • freeToReuseTimeoutMs

        private final long freeToReuseTimeoutMs
      • highWaterMarkId

        private int highWaterMarkId
    • Constructor Detail

      • CountersManager

        public CountersManager​(AtomicBuffer metaDataBuffer,
                               AtomicBuffer valuesBuffer,
                               java.nio.charset.Charset labelCharset,
                               EpochClock epochClock,
                               long freeToReuseTimeoutMs)
        Create a new counter manager over two buffers.
        Parameters:
        metaDataBuffer - containing the types, keys, and labels for the counters.
        valuesBuffer - containing the values of the counters themselves.
        labelCharset - for the label encoding.
        epochClock - to use for determining time for keep counter from being reused after being freed.
        freeToReuseTimeoutMs - timeout (in milliseconds) to keep counter from being reused after being freed.
      • CountersManager

        public CountersManager​(AtomicBuffer metaDataBuffer,
                               AtomicBuffer valuesBuffer,
                               java.nio.charset.Charset labelCharset)
        Create a new counter manager over two buffers.
        Parameters:
        metaDataBuffer - containing the types, keys, and labels for the counters.
        valuesBuffer - containing the values of the counters themselves.
        labelCharset - for the label encoding.
      • CountersManager

        public CountersManager​(AtomicBuffer metaDataBuffer,
                               AtomicBuffer valuesBuffer)
        Create a new counter manager over two buffers.
        Parameters:
        metaDataBuffer - containing the types, keys, and labels for the counters.
        valuesBuffer - containing the values of the counters themselves.
    • Method Detail

      • capacity

        public int capacity()
        Capacity as a count of counters which can be allocated.
        Returns:
        capacity as a count of counters which can be allocated.
      • available

        public int available()
        Number of counters available to be allocated which is a function of capacity() minus the number already allocated.
        Returns:
        the number of counter available to be allocated.
      • allocate

        public int allocate​(java.lang.String label)
        Allocate a new counter with a given label with a default type of CountersReader.DEFAULT_TYPE_ID.
        Parameters:
        label - to describe the counter.
        Returns:
        the id allocated for the counter.
      • allocate

        public int allocate​(java.lang.String label,
                            int typeId)
        Allocate a new counter with a given label and type.
        Parameters:
        label - to describe the counter.
        typeId - for the type of counter.
        Returns:
        the id allocated for the counter.
      • allocate

        public int allocate​(java.lang.String label,
                            int typeId,
                            java.util.function.Consumer<MutableDirectBuffer> keyFunc)
        Allocate a new counter with a given label.

        The key function will be called with a buffer with the exact length of available key space in the record for the user to store what they want for the key. No offset is required.

        Parameters:
        label - to describe the counter.
        typeId - for the type of counter.
        keyFunc - for setting the key value for the counter.
        Returns:
        the id allocated for the counter.
      • allocate

        public int allocate​(int typeId,
                            DirectBuffer keyBuffer,
                            int keyOffset,
                            int keyLength,
                            DirectBuffer labelBuffer,
                            int labelOffset,
                            int labelLength)
        Allocate a counter with the minimum of allocation by allowing the label a key to be provided and copied.

        If the keyBuffer is null then a copy of the key is not attempted.

        Parameters:
        typeId - for the counter.
        keyBuffer - containing the optional key for the counter.
        keyOffset - within the keyBuffer at which the key begins.
        keyLength - of the key in the keyBuffer.
        labelBuffer - containing the mandatory label for the counter.
        labelOffset - within the labelBuffer at which the label begins.
        labelLength - of the label in the labelBuffer.
        Returns:
        the id allocated for the counter.
      • newCounter

        public AtomicCounter newCounter​(java.lang.String label,
                                        int typeId)
        Allocate a counter record and wrap it with a new AtomicCounter for use.
        Parameters:
        label - to describe the counter.
        typeId - for the type of counter.
        Returns:
        a newly allocated AtomicCounter
      • newCounter

        public AtomicCounter newCounter​(java.lang.String label,
                                        int typeId,
                                        java.util.function.Consumer<MutableDirectBuffer> keyFunc)
        Allocate a counter record and wrap it with a new AtomicCounter for use.
        Parameters:
        label - to describe the counter.
        typeId - for the type of counter.
        keyFunc - for setting the key value for the counter.
        Returns:
        a newly allocated AtomicCounter
      • newCounter

        public AtomicCounter newCounter​(int typeId,
                                        DirectBuffer keyBuffer,
                                        int keyOffset,
                                        int keyLength,
                                        DirectBuffer labelBuffer,
                                        int labelOffset,
                                        int labelLength)
        Allocate a counter record and wrap it with a new AtomicCounter for use.

        If the keyBuffer is null then a copy of the key is not attempted.

        Parameters:
        typeId - for the counter.
        keyBuffer - containing the optional key for the counter.
        keyOffset - within the keyBuffer at which the key begins.
        keyLength - of the key in the keyBuffer.
        labelBuffer - containing the mandatory label for the counter.
        labelOffset - within the labelBuffer at which the label begins.
        labelLength - of the label in the labelBuffer.
        Returns:
        the id allocated for the counter.
      • free

        public void free​(int counterId)
        Free the counter identified by counterId.
        Parameters:
        counterId - the counter to freed
      • setCounterValue

        public void setCounterValue​(int counterId,
                                    long value)
        Set an AtomicCounter value based for a counter id with ordered memory ordering.
        Parameters:
        counterId - to be set.
        value - to set for the counter.
      • setCounterRegistrationId

        public void setCounterRegistrationId​(int counterId,
                                             long registrationId)
        Set an AtomicCounter registration id for a counter id with ordered memory ordering.
        Parameters:
        counterId - to be set.
        registrationId - to set for the counter.
      • setCounterOwnerId

        public void setCounterOwnerId​(int counterId,
                                      long ownerId)
        Set an AtomicCounter owner id for a counter id.
        Parameters:
        counterId - to be set.
        ownerId - to set for the counter.
      • setCounterReferenceId

        public void setCounterReferenceId​(int counterId,
                                          long referenceId)
        Set an AtomicCounter reference id for a counter id.
        Parameters:
        counterId - to be set.
        referenceId - to set for the counter.
      • setCounterLabel

        public void setCounterLabel​(int counterId,
                                    java.lang.String label)
        Set an AtomicCounter label by counter id.
        Parameters:
        counterId - to be set.
        label - to set for the counter.
      • setCounterKey

        public void setCounterKey​(int counterId,
                                  java.util.function.Consumer<MutableDirectBuffer> keyFunc)
        Set an AtomicCounter key by on counter id, using a consumer callback to update the key metadata buffer.
        Parameters:
        counterId - to be set.
        keyFunc - callback used to set the key.
      • setCounterKey

        public void setCounterKey​(int counterId,
                                  DirectBuffer keyBuffer,
                                  int offset,
                                  int length)
        Set an AtomicCounter key by on counter id, copying the key metadata from the supplied buffer.
        Parameters:
        counterId - to be set.
        keyBuffer - containing the updated key.
        offset - offset into buffer.
        length - length of data to copy.
      • appendToLabel

        public void appendToLabel​(int counterId,
                                  java.lang.String label)
        Set an AtomicCounter label based on counter id.
        Parameters:
        counterId - to be set.
        label - to set for the counter.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • nextCounterId

        private int nextCounterId()
      • putLabel

        private void putLabel​(int recordOffset,
                              java.lang.String label)
      • appendLabel

        private void appendLabel​(int recordOffset,
                                 java.lang.String suffix)
      • checkCountersCapacity

        private void checkCountersCapacity​(int counterId)