github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/archive/recording_pos_counter.go (about)

     1  // See: ~aeron/aeron-archive/src/main/java/io/aeron/archive/status/RecordingPos.Java
     2  // for which the below is a subset
     3  //
     4  // The position a recording has reached when being archived.
     5  //
     6  // Key has the following layout:
     7  //
     8  //   0                   1                   2                   3
     9  //   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
    10  //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    11  //  |                        Recording ID                           |
    12  //  |                                                               |
    13  //  +---------------------------------------------------------------+
    14  //  |                         Session ID                            |
    15  //  +---------------------------------------------------------------+
    16  //  |                Source Identity for the Image                  |
    17  //  |                                                              ...
    18  // ...                                                              |
    19  //  +---------------------------------------------------------------+
    20  
    21  package archive
    22  
    23  import (
    24  	"github.com/lirm/aeron-go/aeron/atomic"
    25  	"github.com/lirm/aeron-go/aeron/counters"
    26  )
    27  
    28  const RECORDING_POSITION_COUNTER_TYPE_ID = 100
    29  const NULL_RECORDING_ID = -1
    30  
    31  const (
    32  	RECORDING_ID_OFFSET           = 0
    33  	SESSION_ID_OFFSET             = RECORDING_ID_OFFSET + 8
    34  	SOURCE_IDENTITY_LENGTH_OFFSET = SESSION_ID_OFFSET + 4
    35  	SOURCE_IDENTITY_OFFSET        = SOURCE_IDENTITY_LENGTH_OFFSET + 4
    36  )
    37  
    38  // FindCounterIdByRecording finds the active counter id for a stream based on the recording id.
    39  // Returns the counter id if found otherwise NullCounterId
    40  func FindCounterIdByRecording(countersReader *counters.Reader, recordingId int64) int32 {
    41  	return countersReader.FindCounter(RECORDING_POSITION_COUNTER_TYPE_ID, func(keyBuffer *atomic.Buffer) bool {
    42  		return keyBuffer.GetInt64(RECORDING_ID_OFFSET) == recordingId
    43  	})
    44  }
    45  
    46  // FindCounterIdBySession finds the active counterID for a stream based on the session id.
    47  // Returns the counter id if found otherwise NullCounterId
    48  func FindCounterIdBySession(countersReader *counters.Reader, sessionId int32) int32 {
    49  	return countersReader.FindCounter(RECORDING_POSITION_COUNTER_TYPE_ID, func(keyBuffer *atomic.Buffer) bool {
    50  		return keyBuffer.GetInt32(SESSION_ID_OFFSET) == sessionId
    51  	})
    52  }
    53  
    54  func GetRecordingId(countersReader *counters.Reader, counterId int32) int64 {
    55  	if countersReader.GetCounterTypeId(counterId) != RECORDING_POSITION_COUNTER_TYPE_ID {
    56  		return NULL_RECORDING_ID
    57  	}
    58  	recordingId, err := countersReader.GetKeyPartInt64(counterId, RECORDING_ID_OFFSET)
    59  	if err != nil {
    60  		return NULL_RECORDING_ID
    61  	}
    62  	return recordingId
    63  }
    64  
    65  // GetSourceIdentity returns the source identity for the recording
    66  func GetSourceIdentity(countersReader *counters.Reader, counterId int32) string {
    67  	if countersReader.GetCounterTypeId(counterId) != RECORDING_POSITION_COUNTER_TYPE_ID {
    68  		return ""
    69  	}
    70  	identity, err := countersReader.GetKeyPartString(counterId, SOURCE_IDENTITY_LENGTH_OFFSET)
    71  	if err != nil {
    72  		return ""
    73  	}
    74  	return identity
    75  }
    76  
    77  // IsRecordingActive tells us if the recording counter is still active?
    78  func IsRecordingActive(countersReader *counters.Reader, counterId int32, recordingId int64) bool {
    79  	recId, err := countersReader.GetKeyPartInt64(counterId, RECORDING_ID_OFFSET)
    80  	return err == nil && recId == recordingId
    81  }