コード例 #1
0
ファイル: ErrorReader.cs プロジェクト: zhangbo27/Aeron.NET
        /// <summary>
        /// Read all the errors in a log since a given timestamp.
        /// </summary>
        /// <param name="buffer">         containing the <seealso cref="DistinctErrorLog"/>. </param>
        /// <param name="consumer">       to be called for each exception encountered. </param>
        /// <param name="sinceTimestamp"> for filtering errors that have been recorded since this time. </param>
        /// <returns> the number of entries that has been read. </returns>
        public static int Read(IAtomicBuffer buffer, ErrorConsumer consumer, long sinceTimestamp)
        {
            int entries  = 0;
            int offset   = 0;
            int capacity = buffer.Capacity;

            while (offset < capacity)
            {
                int length = buffer.GetIntVolatile(offset + DistinctErrorLog.LengthOffset);
                if (0 == length)
                {
                    break;
                }

                long lastObservationTimestamp = buffer.GetLongVolatile(offset + DistinctErrorLog.LastObservationTimestampOffset);
                if (lastObservationTimestamp >= sinceTimestamp)
                {
                    ++entries;

                    consumer(
                        buffer.GetInt(offset + DistinctErrorLog.ObservationCountOffset),
                        buffer.GetLong(offset + DistinctErrorLog.FirstObservationTimestampOffset),
                        lastObservationTimestamp,
                        buffer.GetStringWithoutLengthUtf8(offset + DistinctErrorLog.EncodedErrorOffset, length - DistinctErrorLog.EncodedErrorOffset));
                }

                offset += BitUtil.Align(length, DistinctErrorLog.RecordAlignment);
            }

            return(entries);
        }
コード例 #2
0
        /// <summary>
        /// Read all the errors in a log since a given timestamp.
        /// </summary>
        /// <param name="buffer">         containing the <seealso cref="DistinctErrorLog"/>. </param>
        /// <param name="consumer">       to be called for each exception encountered. </param>
        /// <param name="sinceTimestamp"> for filtering errors that have been recorded since this time. </param>
        /// <returns> the number of entries that has been read. </returns>
        public static int Read(IAtomicBuffer buffer, ErrorConsumer consumer, long sinceTimestamp)
        {
            int entries = 0;
            int offset = 0;
            int capacity = buffer.Capacity;

            while (offset < capacity)
            {
                int length = buffer.GetIntVolatile(offset + DistinctErrorLog.LengthOffset);
                if (0 == length)
                {
                    break;
                }

                long lastObservationTimestamp = buffer.GetLongVolatile(offset + DistinctErrorLog.LastObservationTimestampOffset);
                if (lastObservationTimestamp >= sinceTimestamp)
                {
                    ++entries;

                    consumer(
                        buffer.GetInt(offset + DistinctErrorLog.ObservationCountOffset), 
                        buffer.GetLong(offset + DistinctErrorLog.FirstObservationTimestampOffset), 
                        lastObservationTimestamp, 
                        buffer.GetStringUtf8(offset + DistinctErrorLog.EncodedErrorOffset, 
                        length - DistinctErrorLog.EncodedErrorOffset));
                }

                offset += BitUtil.Align(length, DistinctErrorLog.RecordAlignment);
            }

            return entries;
        }
コード例 #3
0
        private static bool ScanBackToConfirmStillZeroed(IAtomicBuffer buffer, int from, int limit)
        {
            var i        = from - RecordDescriptor.Alignment;
            var allZeros = true;

            while (i >= limit)
            {
                if (0 != buffer.GetIntVolatile(i))
                {
                    allZeros = false;
                    break;
                }

                i -= RecordDescriptor.Alignment;
            }

            return(allZeros);
        }
コード例 #4
0
        public void ShouldNotReadSingleMessagePartWayThroughWriting()
        {
            const long head      = 0L;
            var        headIndex = (int)head;

            A.CallTo(() => _buffer.GetLong(HeadCounterIndex)).Returns(head);
            A.CallTo(() => _buffer.GetIntVolatile(RecordDescriptor.LengthOffset(headIndex))).Returns(0);

            var times = new int[1];

            MessageHandler handler      = (msgTypeId, buffer, index, length) => times[0]++;
            var            messagesRead = _ringBuffer.Read(handler);

            Assert.AreEqual(messagesRead, 0);
            Assert.AreEqual(times[0], 0);

            A.CallTo(() => _buffer.GetLongVolatile(headIndex)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => _buffer.SetMemory(headIndex, 0, 0)).MustNotHaveHappened();
            A.CallTo(() => _buffer.PutLongOrdered(HeadCounterIndex, headIndex)).MustNotHaveHappened();
        }
コード例 #5
0
        public void ShouldReadFirstMessage()
        {
            const int offset               = 0;
            int       limit                = _termBuffer.Capacity;
            const int messageLength        = 50;
            int       alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
            .Returns(messageLength);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.That(newOffset, Is.EqualTo(alignedMessageLength));
        }
コード例 #6
0
        public void ShouldReadFirstMessage()
        {
            const int msgLength   = 1;
            int       frameLength = HEADER_LENGTH + msgLength;
            const int termOffset  = 0;

            A.CallTo(() => termBuffer.GetIntVolatile(0))
            .Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(0)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            long readOutcome = TermReader.Read(termBuffer, termOffset, handler, int.MaxValue, header, errorHandler);

            Assert.That(TermReader.FragmentsRead(readOutcome), Is.EqualTo(1));

            A.CallTo(() => termBuffer.GetIntVolatile(0)).MustHaveHappened();
            A.CallTo(() => handler(termBuffer, HEADER_LENGTH, msgLength, A <Header> ._)).MustHaveHappened();
        }
コード例 #7
0
        public static int FrameLengthVolatile(IAtomicBuffer buffer, int termOffset)
        {
            int frameLength = buffer.GetIntVolatile(termOffset);

            return(frameLength);
        }
コード例 #8
0
        public void ShouldGetIntVolatileFromNativeBuffer(IAtomicBuffer buffer)
        {
            Marshal.WriteInt32(buffer.BufferPointer, Index, IntValue);

            Assert.That(buffer.GetIntVolatile(Index), Is.EqualTo(IntValue));
        }
コード例 #9
0
        private static bool ScanBackToConfirmStillZeroed(IAtomicBuffer buffer, int from, int limit)
        {
            var i = from - RecordDescriptor.Alignment;
            var allZeros = true;
            while (i >= limit)
            {
                if (0 != buffer.GetIntVolatile(i))
                {
                    allZeros = false;
                    break;
                }

                i -= RecordDescriptor.Alignment;
            }

            return allZeros;
        }
コード例 #10
0
        /// <summary>
        /// Get the length of a frame from the header as a volatile read.
        /// </summary>
        /// <param name="buffer">     containing the frame. </param>
        /// <param name="termOffset"> at which a frame begins. </param>
        /// <returns> the value for the frame length. </returns>
        public static int FrameLengthVolatile(IAtomicBuffer buffer, int termOffset)
        {
            int frameLength = buffer.GetIntVolatile(termOffset);

            return frameLength;
        }
コード例 #11
0
        public void ShouldFailToReadFirstMessageBecauseOfLimit()
        {
            const int offset = 0;
            const int messageLength = 50;
            int alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);
            int limit = alignedMessageLength - 1;

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
                .Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(offset)))
                .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.AreEqual(offset, newOffset);
        }
コード例 #12
0
 /// <summary>
 /// Has the error buffer any recorded errors?
 /// </summary>
 /// <param name="buffer"> containing the <seealso cref="DistinctErrorLog"/>. </param>
 /// <returns> true if there is at least one error. </returns>
 public static bool HasErrors(IAtomicBuffer buffer)
 {
     return(0 != buffer.GetIntVolatile(DistinctErrorLog.LengthOffset));
 }