Beispiel #1
0
        public static MappedByteBuffer MapNewOrExistingCncFile(FileInfo cncFile, bool shouldPreExist,
                                                               int versionFieldOffset, int timestampFieldOffset, long totalFileLength, long timeoutMs,
                                                               IEpochClock epochClock, Action <int> versionCheck, Action <string> logger)
        {
            MappedByteBuffer cncByteBuffer = null;

            try
            {
                cncByteBuffer = IoUtil.MapNewOrExixtingFile(cncFile, totalFileLength);

                UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

                if (shouldPreExist)
                {
                    int cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset);

                    if (null != logger)
                    {
                        logger("INFO: CnC file exists: " + cncFile);
                    }

                    versionCheck(cncVersion);

                    long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
                    long now          = epochClock.Time();
                    long timestampAge = now - timestamp;

                    if (null != logger)
                    {
                        logger("INFO: heartbeat is (ms): " + timestampAge);
                    }

                    if (timestampAge < timeoutMs)
                    {
                        throw new System.InvalidOperationException("Active CnC file detected");
                    }
                }
            }
            catch (Exception)
            {
                if (null != cncByteBuffer)
                {
                    IoUtil.Unmap(cncByteBuffer);
                }

                throw;
            }

            return(cncByteBuffer);
        }
Beispiel #2
0
        public static MappedByteBuffer MapExistingCncFile(FileInfo cncFile, int versionFieldOffset,
                                                          int timestampFieldOffset, long timeoutMs, IEpochClock epochClock, Action <int> versionCheck,
                                                          Action <string> logger)
        {
            long startTimeMs = epochClock.Time();

            while (true)
            {
                while (!cncFile.Exists)
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("CnC file not found: " + cncFile.FullName);
                    }

                    Sleep(16);
                }

                MappedByteBuffer cncByteBuffer = MapExistingFile(cncFile, logger);
                UnsafeBuffer     cncBuffer     = new UnsafeBuffer(cncByteBuffer);

                int cncVersion;
                while (0 == (cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset)))
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("CnC file is created but not initialised.");
                    }

                    Sleep(1);
                }

                versionCheck(cncVersion);

                while (0 == cncBuffer.GetLongVolatile(timestampFieldOffset))
                {
                    if (epochClock.Time() > (startTimeMs + timeoutMs))
                    {
                        throw new InvalidOperationException("No non-0 timestamp detected.");
                    }

                    Sleep(1);
                }

                return(cncByteBuffer);
            }
        }
Beispiel #3
0
        public static bool IsActive(MappedByteBuffer cncByteBuffer, IEpochClock epochClock, long timeoutMs,
                                    int versionFieldOffset, int timestampFieldOffset, Action <int> versionCheck, Action <string> logger)
        {
            if (null == cncByteBuffer)
            {
                return(false);
            }

            UnsafeBuffer cncBuffer = new UnsafeBuffer(cncByteBuffer);

            long startTimeMs = epochClock.Time();
            int  cncVersion;

            while (0 == (cncVersion = cncBuffer.GetIntVolatile(versionFieldOffset)))
            {
                if (epochClock.Time() > (startTimeMs + timeoutMs))
                {
                    throw new System.InvalidOperationException("CnC file is created but not initialised.");
                }

                Sleep(1);
            }

            versionCheck(cncVersion);

            long timestamp    = cncBuffer.GetLongVolatile(timestampFieldOffset);
            long now          = epochClock.Time();
            long timestampAge = now - timestamp;

            if (null != logger)
            {
                logger("INFO: heartbeat is (ms): " + timestampAge);
            }

            return(timestampAge <= timeoutMs);
        }
Beispiel #4
0
        public static long RawTailVolatile(UnsafeBuffer metaDataBuffer)
        {
            var partitionIndex = IndexByTermCount(ActiveTermCount(metaDataBuffer));

            return(metaDataBuffer.GetLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex));
        }
Beispiel #5
0
 public static long RawTailVolatile(UnsafeBuffer metaDataBuffer, int partitionIndex)
 {
     return(metaDataBuffer.GetLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex));
 }
Beispiel #6
0
 /// <summary>
 ///     Get the value of the end of stream position.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the metadata. </param>
 /// <returns> the value of end of stream position </returns>
 public static long EndOfStreamPosition(UnsafeBuffer metaDataBuffer)
 {
     return(metaDataBuffer.GetLongVolatile(LOG_END_OF_STREAM_POSITION_OFFSET));
 }
Beispiel #7
0
 /// <summary>
 /// Get the current value of a candidate term id if a vote is placed in an election.
 /// </summary>
 /// <returns> the current candidate term id within an election after voting or <seealso cref="Aeron#NULL_VALUE"/> if
 /// no voting phase of an election is currently active. </returns>
 public long CandidateTermId()
 {
     return(buffer.GetLongVolatile(MarkFileHeaderDecoder.CandidateTermIdEncodingOffset()));
 }
 public long GetVolatile()
 {
     return(_buffer.GetLongVolatile(_offset));
 }
Beispiel #9
0
 public long RawTailVolatile()
 {
     return(_metaDataBuffer.GetLongVolatile((int)_tailAddressOffset));
 }
Beispiel #10
0
        /// <summary>
        /// Get the current tail value in a volatile memory ordering fashion. If raw tail is greater than
        /// <seealso cref="TermBuffer()"/>.<seealso cref="IDirectBuffer.Capacity"/> then capacity will be returned.
        /// </summary>
        /// <returns> the current tail value. </returns>
        public int TailOffsetVolatile()
        {
            long tail = _metaDataBuffer.GetLongVolatile(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET) & 0xFFFFFFFFL;

            return((int)Math.Min(tail, _termBuffer.Capacity));
        }
Beispiel #11
0
 public virtual long TimestampVolatile()
 {
     return(buffer.GetLongVolatile(timestampFieldOffset));
 }
Beispiel #12
0
 /// <summary>
 /// Get the value of the time of last SM in <seealso cref="System#currentTimeMillis()"/>.
 /// </summary>
 /// <param name="logMetaDataBuffer"> containing the meta data. </param>
 /// <returns> the value of time of last SM </returns>
 public static long TimeOfLastStatusMessage(UnsafeBuffer logMetaDataBuffer)
 {
     return(logMetaDataBuffer.GetLongVolatile(LOG_TIME_OF_LAST_SM_OFFSET));
 }
        public static long RawTailVolatile(UnsafeBuffer logMetaDataBuffer)
        {
            int partitionIndex = ActivePartitionIndex(logMetaDataBuffer);

            return(logMetaDataBuffer.GetLongVolatile(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex));
        }