예제 #1
0
        public static Counter Allocate(Aeron.Aeron aeron, UnsafeBuffer tempBuffer, long recordingId,
                                       long controlSessionId,
                                       long correlationId, int sessionId, int streamId, string strippedChannel)
        {
            tempBuffer.PutLong(RECORDING_ID_OFFSET, recordingId);
            tempBuffer.PutLong(CONTROL_SESSION_ID_OFFSET, controlSessionId);
            tempBuffer.PutLong(CORRELATION_ID_OFFSET, correlationId);
            tempBuffer.PutInt(SESSION_ID_OFFSET, sessionId);
            tempBuffer.PutInt(STREAM_ID_OFFSET, streamId);

            int labelLength = 0;

            labelLength += tempBuffer.PutStringWithoutLengthAscii(KEY_LENGTH, NAME + ": ");
            labelLength += tempBuffer.PutLongAscii(KEY_LENGTH + labelLength, recordingId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(KEY_LENGTH + labelLength, " ");
            labelLength += tempBuffer.PutIntAscii(KEY_LENGTH + labelLength, sessionId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(KEY_LENGTH + labelLength, " ");
            labelLength += tempBuffer.PutIntAscii(KEY_LENGTH + labelLength, streamId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(KEY_LENGTH + labelLength, " ");
            labelLength += tempBuffer.PutStringWithoutLengthAscii(KEY_LENGTH + labelLength, strippedChannel, 0,
                                                                  CountersReader.MAX_LABEL_LENGTH - labelLength);

            return(aeron.AddCounter(RECORDING_POSITION_TYPE_ID, tempBuffer, 0, KEY_LENGTH, tempBuffer, KEY_LENGTH,
                                    labelLength));
        }
예제 #2
0
        public long AppendUnfragmentedMessage(HeaderWriter header, UnsafeBuffer srcBuffer, int srcOffset, int length, ReservedValueSupplier reservedValueSupplier)
#endif
        {
            int  frameLength   = length + DataHeaderFlyweight.HEADER_LENGTH;
            int  alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            long rawTail       = GetAndAddRawTail(alignedLength);
            long termOffset    = rawTail & 0xFFFFFFFFL;

            UnsafeBuffer termBuffer = _termBuffer;
            int          termLength = termBuffer.Capacity;

            long resultingOffset = termOffset + alignedLength;

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, LogBufferDescriptor.TermId(rawTail));
            }
            else
            {
                int offset = (int)termOffset;
                header.Write(termBuffer, offset, frameLength, LogBufferDescriptor.TermId(rawTail));
                termBuffer.PutBytes(offset + DataHeaderFlyweight.HEADER_LENGTH, srcBuffer, srcOffset, length);

                if (null != reservedValueSupplier)
                {
                    long reservedValue = reservedValueSupplier(termBuffer, offset, frameLength);
                    termBuffer.PutLong(offset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue);
                }

                FrameDescriptor.FrameLengthOrdered(termBuffer, offset, frameLength);
            }

            return(resultingOffset);
        }
예제 #3
0
        public int AppendFragmentedMessage(HeaderWriter header, IDirectBuffer srcBuffer, int srcOffset, int length,
                                           int maxPayloadLength, ReservedValueSupplier reservedValueSupplier, int activeTermId)
        {
            int          numMaxPayloads   = length / maxPayloadLength;
            int          remainingPayload = length % maxPayloadLength;
            int          lastFrameLength  = remainingPayload > 0 ? BitUtil.Align(remainingPayload + DataHeaderFlyweight.HEADER_LENGTH, FrameDescriptor.FRAME_ALIGNMENT) : 0;
            int          requiredLength   = (numMaxPayloads * (maxPayloadLength + DataHeaderFlyweight.HEADER_LENGTH)) + lastFrameLength;
            long         rawTail          = GetAndAddRawTail(requiredLength);
            int          termId           = LogBufferDescriptor.TermId(rawTail);
            long         termOffset       = rawTail & 0xFFFFFFFFL;
            UnsafeBuffer termBuffer       = _termBuffer;
            int          termLength       = termBuffer.Capacity;

            CheckTerm(activeTermId, termId);

            long resultingOffset = termOffset + requiredLength;

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                int  frameOffset = (int)termOffset;
                byte flags       = FrameDescriptor.BEGIN_FRAG_FLAG;
                int  remaining   = length;

                do
                {
                    int bytesToWrite  = Math.Min(remaining, maxPayloadLength);
                    int frameLength   = bytesToWrite + DataHeaderFlyweight.HEADER_LENGTH;
                    int alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);

                    header.Write(termBuffer, frameOffset, frameLength, termId);
                    termBuffer.PutBytes(frameOffset + DataHeaderFlyweight.HEADER_LENGTH, srcBuffer,
                                        srcOffset + (length - remaining), bytesToWrite);

                    if (remaining <= maxPayloadLength)
                    {
                        flags |= FrameDescriptor.END_FRAG_FLAG;
                    }

                    FrameDescriptor.FrameFlags(termBuffer, frameOffset, flags);

                    if (null != reservedValueSupplier)
                    {
                        long reservedValue = reservedValueSupplier(termBuffer, frameOffset, frameLength);
                        termBuffer.PutLong(frameOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                    }

                    FrameDescriptor.FrameLengthOrdered(termBuffer, frameOffset, frameLength);

                    flags        = 0;
                    frameOffset += alignedLength;
                    remaining   -= bytesToWrite;
                } while (remaining > 0);
            }

            return((int)resultingOffset);
        }
예제 #4
0
        public static Counter Allocate(Aeron.Aeron aeron, UnsafeBuffer tempBuffer, long recordingId,
                                       int sessionId, int streamId, string strippedChannel, string sourceIdentity)
        {
            tempBuffer.PutLong(RECORDING_ID_OFFSET, recordingId);
            tempBuffer.PutInt(SESSION_ID_OFFSET, sessionId);

            var sourceIdentityLength = Math.Min(sourceIdentity.Length, CountersReader.MAX_KEY_LENGTH - SOURCE_IDENTITY_OFFSET);

            tempBuffer.PutStringAscii(SOURCE_IDENTITY_LENGTH_OFFSET, sourceIdentity);
            var keyLength = SOURCE_IDENTITY_OFFSET + sourceIdentityLength;

            int labelLength = 0;

            labelLength += tempBuffer.PutStringWithoutLengthAscii(keyLength, NAME + ": ");
            labelLength += tempBuffer.PutLongAscii(keyLength + labelLength, recordingId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(keyLength + labelLength, " ");
            labelLength += tempBuffer.PutIntAscii(keyLength + labelLength, sessionId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(keyLength + labelLength, " ");
            labelLength += tempBuffer.PutIntAscii(keyLength + labelLength, streamId);
            labelLength += tempBuffer.PutStringWithoutLengthAscii(keyLength + labelLength, " ");
            labelLength += tempBuffer.PutStringWithoutLengthAscii(
                keyLength + labelLength, strippedChannel, 0, CountersReader.MAX_LABEL_LENGTH - labelLength);

            return(aeron.AddCounter(
                       RECORDING_POSITION_TYPE_ID, tempBuffer, 0, keyLength, tempBuffer, keyLength, labelLength));
        }
예제 #5
0
        public void ShouldStoreRawData()
        {
            const int  typeIdOne = 333;
            const long keyOne    = 777L;

            var keyOneBuffer = new UnsafeBuffer(new byte[8]);

            keyOneBuffer.PutLong(0, keyOne);
            var labelOneBuffer = new UnsafeBuffer(Encoding.ASCII.GetBytes("Test Label One"));

            const int  typeIdTwo    = 222;
            const long keyTwo       = 444;
            var        keyTwoBuffer = new UnsafeBuffer(new byte[8]);

            keyTwoBuffer.PutLong(0, keyTwo);
            var labelTwoBuffer = new UnsafeBuffer(Encoding.ASCII.GetBytes("Test Label Two"));

            int counterIdOne = _manager.Allocate(
                typeIdOne, keyOneBuffer, 0, keyOneBuffer.Capacity, labelOneBuffer, 0, labelOneBuffer.Capacity);

            int counterIdTwo = _manager.Allocate(
                typeIdTwo, keyTwoBuffer, 0, keyTwoBuffer.Capacity, labelTwoBuffer, 0, labelTwoBuffer.Capacity);

            _manager.ForEach(_metaData);

            A.CallTo(() => _metaData(counterIdOne, typeIdOne,
                                     A <IDirectBuffer> .That.Matches(d => d.GetLong(0) == keyOne), "Test Label One")).MustHaveHappened()
            .Then(A.CallTo(() => _metaData(counterIdTwo, typeIdTwo,
                                           A <IDirectBuffer> .That.Matches(d => d.GetLong(0) == keyTwo), "Test Label Two")).MustHaveHappened());
        }
예제 #6
0
 public static void FillMetaData(UnsafeBuffer cncMetaDataBuffer, int toDriverBufferLength, int toClientsBufferLength, int counterMetaDataBufferLength, int counterValuesBufferLength, long clientLivenessTimeout, int errorLogBufferLength)
 {
     cncMetaDataBuffer.PutInt(ToDriverBufferLengthOffset(0), toDriverBufferLength);
     cncMetaDataBuffer.PutInt(ToClientsBufferLengthOffset(0), toClientsBufferLength);
     cncMetaDataBuffer.PutInt(CountersMetaDataBufferLengthOffset(0), counterMetaDataBufferLength);
     cncMetaDataBuffer.PutInt(CountersValuesBufferLengthOffset(0), counterValuesBufferLength);
     cncMetaDataBuffer.PutInt(ErrorLogBufferLengthOffset(0), errorLogBufferLength);
     cncMetaDataBuffer.PutLong(ClientLivenessTimeoutOffset(0), clientLivenessTimeout);
 }
예제 #7
0
 /// <summary>
 /// Fill the CnC file with metadata to define its sections.
 /// </summary>
 /// <param name="cncMetaDataBuffer">           that wraps the metadata section of the CnC file. </param>
 /// <param name="toDriverBufferLength">        for sending commands to the driver. </param>
 /// <param name="toClientsBufferLength">       for broadcasting events to the clients. </param>
 /// <param name="counterMetaDataBufferLength"> buffer length for counters metadata. </param>
 /// <param name="counterValuesBufferLength">   buffer length for counter values. </param>
 /// <param name="clientLivenessTimeoutNs">     timeout value in nanoseconds for client liveness and inter-service interval. </param>
 /// <param name="errorLogBufferLength">        for recording the distinct error log. </param>
 /// <param name="startTimestampMs">            epoch at which the driver started. </param>
 /// <param name="pid">                         for the process hosting the driver. </param>
 public static void FillMetaData(
     UnsafeBuffer cncMetaDataBuffer,
     int toDriverBufferLength,
     int toClientsBufferLength,
     int counterMetaDataBufferLength,
     int counterValuesBufferLength,
     long clientLivenessTimeoutNs,
     int errorLogBufferLength,
     long startTimestampMs,
     long pid)
 {
     cncMetaDataBuffer.PutInt(ToDriverBufferLengthOffset(0), toDriverBufferLength);
     cncMetaDataBuffer.PutInt(ToClientsBufferLengthOffset(0), toClientsBufferLength);
     cncMetaDataBuffer.PutInt(CountersMetaDataBufferLengthOffset(0), counterMetaDataBufferLength);
     cncMetaDataBuffer.PutInt(CountersValuesBufferLengthOffset(0), counterValuesBufferLength);
     cncMetaDataBuffer.PutInt(ErrorLogBufferLengthOffset(0), errorLogBufferLength);
     cncMetaDataBuffer.PutLong(ClientLivenessTimeoutOffset(0), clientLivenessTimeoutNs);
     cncMetaDataBuffer.PutLong(StartTimestampOffset(0), startTimestampMs);
     cncMetaDataBuffer.PutLong(PidOffset(0), pid);
 }
예제 #8
0
        public void SharedBuffer()
        {
            var bb  = new byte[1024];
            var ub1 = new UnsafeBuffer(bb, 0, 512);
            var ub2 = new UnsafeBuffer(bb, 512, 512);

            ub1.PutLong(Index, LongValue);
            ub2.PutLong(Index, 9876543210L);

            Assert.That(ub1.GetLong(Index), Is.EqualTo(LongValue));
        }
예제 #9
0
        public void ShouldAppendFrameToEmptyLog()
        {
            int          headerLength       = _defaultHeader.Capacity;
            UnsafeBuffer buffer             = new UnsafeBuffer(new byte[128]);
            const int    msgLength          = 20;
            int          frameLength        = msgLength + headerLength;
            int          alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            const int    tail = 0;

            _logMetaDataBuffer.PutLong(TermTailCounterOffset, LogBufferDescriptor.PackTail(TermID, tail));

            Assert.That(_termAppender.AppendUnfragmentedMessage(_headerWriter, buffer, 0, msgLength, RVS), Is.EqualTo((long)alignedFrameLength));

            Assert.AreEqual(LogBufferDescriptor.RawTailVolatile(_logMetaDataBuffer, PartionIndex), LogBufferDescriptor.PackTail(TermID, tail + alignedFrameLength));

            A.CallTo(() => _headerWriter.Write(_termBuffer, tail, frameLength, TermID)).MustHaveHappened()
            .Then(A.CallTo(() => _termBuffer.PutBytes(headerLength, buffer, 0, msgLength)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutLong(tail + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, RV)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(tail, frameLength)).MustHaveHappened());
        }
예제 #10
0
        public void Write(UnsafeBuffer termBuffer, int offset, int length, int termId)
        {
            var lengthVersionFlagsType = _versionFlagsType | (-length & 0xFFFFFFFFL);
            var termOffsetSessionId    = _sessionId | (uint)offset;
            var streamAndTermIds       = _streamId | ((long)termId << 32);

            termBuffer.PutLongOrdered(offset + HeaderFlyweight.FRAME_LENGTH_FIELD_OFFSET, lengthVersionFlagsType);

            termBuffer.PutLongOrdered(offset + DataHeaderFlyweight.TERM_OFFSET_FIELD_OFFSET, termOffsetSessionId);
            termBuffer.PutLong(offset + DataHeaderFlyweight.STREAM_ID_FIELD_OFFSET, streamAndTermIds);
        }
예제 #11
0
        /// <summary>
        /// Commit the position reached in a leadership term before a clean shutdown.
        /// </summary>
        /// <param name="leadershipTermId"> for committing the term position reached. </param>
        /// <param name="termPosition">     reached in the leadership term. </param>
        public void CommitLeadershipTermPosition(long leadershipTermId, long termPosition)
        {
            int index = -1;

            for (int i = 0, size = entries.Count; i < size; i++)
            {
                Entry entry = entries[i];
                if (entry.leadershipTermId == leadershipTermId && entry.type == ENTRY_TYPE_TERM)
                {
                    index = entry.entryIndex;
                    break;
                }
            }

            if (-1 == index)
            {
                throw new ArgumentException("Unknown leadershipTermId: " + leadershipTermId);
            }

            buffer.PutLong(0, termPosition);
            long filePosition = (index * ENTRY_LENGTH) + TERM_POSITION_OFFSET;

            using (var fileChannel = new FileStream(indexFile.FullName, FileMode.Append, FileAccess.Write,
                                                    FileShare.ReadWrite, BitUtil.SIZE_OF_LONG, FileOptions.WriteThrough))
            {
                fileChannel.Position = filePosition;
                fileChannel.Write(byteBuffer, 0, BitUtil.SIZE_OF_LONG); // Check
            }
        }
예제 #12
0
        /// <summary>
        /// Return an initialised default Data Frame Header.
        /// </summary>
        /// <param name="sessionId"> for the header </param>
        /// <param name="streamId">  for the header </param>
        /// <param name="termId">    for the header </param>
        /// <returns> byte array containing the header </returns>
        public static UnsafeBuffer CreateDefaultHeader(int sessionId, int streamId, int termId)
        {
            var buffer = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(HEADER_LENGTH, FrameDescriptor.FRAME_ALIGNMENT));

            buffer.PutByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
            buffer.PutByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS);
            buffer.PutShort(TYPE_FIELD_OFFSET, HDR_TYPE_DATA);
            buffer.PutInt(SESSION_ID_FIELD_OFFSET, sessionId);
            buffer.PutInt(STREAM_ID_FIELD_OFFSET, streamId);
            buffer.PutInt(TERM_ID_FIELD_OFFSET, termId);
            buffer.PutLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);

            return(buffer);
        }
예제 #13
0
        /// <summary>
        /// Return an initialised default Data Frame Header.
        /// </summary>
        /// <param name="sessionId"> for the header </param>
        /// <param name="streamId">  for the header </param>
        /// <param name="termId">    for the header </param>
        /// <returns> byte array containing the header </returns>
        public static UnsafeBuffer CreateDefaultHeader(int sessionId, int streamId, int termId)
        {
            var buffer = new UnsafeBuffer(new byte[HEADER_LENGTH]);

            buffer.PutByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
            buffer.PutByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS);
            buffer.PutShort(TYPE_FIELD_OFFSET, HDR_TYPE_DATA);
            buffer.PutInt(SESSION_ID_FIELD_OFFSET, sessionId);
            buffer.PutInt(STREAM_ID_FIELD_OFFSET, streamId);
            buffer.PutInt(TERM_ID_FIELD_OFFSET, termId);
            buffer.PutLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);

            return(buffer);
        }
예제 #14
0
        public int AppendUnfragmentedMessage(
            HeaderWriter header,
            DirectBufferVector[] vectors,
            int length,
            ReservedValueSupplier reservedValueSupplier,
            int activeTermId)
        {
            int          frameLength   = length + DataHeaderFlyweight.HEADER_LENGTH;
            int          alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            UnsafeBuffer termBuffer    = _termBuffer;
            int          termLength    = termBuffer.Capacity;

            long rawTail    = GetAndAddRawTail(alignedLength);
            int  termId     = LogBufferDescriptor.TermId(rawTail);
            long termOffset = rawTail & 0xFFFFFFFFL;


            CheckTerm(activeTermId, termId);

            long resultingOffset = termOffset + alignedLength;

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                int frameOffset = (int)termOffset;
                header.Write(termBuffer, frameOffset, frameLength, LogBufferDescriptor.TermId(rawTail));

                int offset = frameOffset + DataHeaderFlyweight.HEADER_LENGTH;

                foreach (var vector in vectors)
                {
                    termBuffer.PutBytes(offset, vector.buffer, vector.offset, vector.length);
                    offset += vector.length;
                }

                if (null != reservedValueSupplier)
                {
                    long reservedValue = reservedValueSupplier(termBuffer, frameOffset, frameLength);
                    termBuffer.PutLong(frameOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                }

                FrameDescriptor.FrameLengthOrdered(termBuffer, frameOffset, frameLength);
            }

            return((int)resultingOffset);
        }
예제 #15
0
        private static void RoundTripMessages(UnsafeBuffer buffer,
                                              IFragmentHandler fragmentHandler, Publication publication, Subscription subscription, int count)
        {
            for (var i = 0; i < count; i++)
            {
                do
                {
                    buffer.PutLong(0, Stopwatch.GetTimestamp());
                } while (publication.Offer(buffer, 0, MessageLength) < 0L);

                PollingIdleStrategy.Reset();
                while (subscription.Poll(fragmentHandler, FragmentCountLimit) <= 0)
                {
                    PollingIdleStrategy.Idle();
                }
            }
        }
예제 #16
0
        public int AppendUnfragmentedMessage(
            int termId,
            int termOffset,
            HeaderWriter header,
            DirectBufferVector[] vectors,
            int length,
            ReservedValueSupplier reservedValueSupplier)
#endif
        {
            int          frameLength   = length + DataHeaderFlyweight.HEADER_LENGTH;
            int          alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            UnsafeBuffer termBuffer    = _termBuffer;
            int          termLength    = termBuffer.Capacity;

            int resultingOffset = termOffset + alignedLength;

            PutRawTailOrdered(termId, resultingOffset);

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                header.Write(termBuffer, termOffset, frameLength, termId);

                var offset = termOffset + DataHeaderFlyweight.HEADER_LENGTH;

                foreach (var vector in vectors)
                {
                    termBuffer.PutBytes(offset, vector.buffer, vector.offset, vector.length);
                    offset += vector.length;
                }

                if (null != reservedValueSupplier)
                {
                    long reservedValue = reservedValueSupplier(termBuffer, termOffset, frameLength);
                    termBuffer.PutLong(termOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                }

                FrameDescriptor.FrameLengthOrdered(termBuffer, termOffset, frameLength);
            }

            return(resultingOffset);
        }
예제 #17
0
        /// <summary>
        /// Append an unfragmented message to the the term buffer.
        /// </summary>
        /// <param name="termId">                for the current term. </param>
        /// <param name="termOffset">            in the term at which to append. </param>
        /// <param name="header">                for writing the default header. </param>
        /// <param name="bufferOne">             containing the first part of the message. </param>
        /// <param name="offsetOne">             at which the first part of the message begins. </param>
        /// <param name="lengthOne">             of the first part of the message. </param>
        /// <param name="bufferTwo">             containing the second part of the message. </param>
        /// <param name="offsetTwo">             at which the second part of the message begins. </param>
        /// <param name="lengthTwo">             of the second part of the message. </param>
        /// <param name="reservedValueSupplier"> <seealso cref="ReservedValueSupplier"/> for the frame. </param>
        /// <returns> the resulting offset of the term after the append on success otherwise <seealso cref="FAILED"/>. </returns>
        public int AppendUnfragmentedMessage(
            int termId,
            int termOffset,
            HeaderWriter header,
            IDirectBuffer bufferOne,
            int offsetOne,
            int lengthOne,
            IDirectBuffer bufferTwo,
            int offsetTwo,
            int lengthTwo,
            ReservedValueSupplier reservedValueSupplier)
        {
            int          frameLength   = lengthOne + lengthTwo + DataHeaderFlyweight.HEADER_LENGTH;
            int          alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            UnsafeBuffer termBuffer    = _termBuffer;
            int          termLength    = termBuffer.Capacity;

            int resultingOffset = termOffset + alignedLength;

            PutRawTailOrdered(termId, resultingOffset);

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                header.Write(termBuffer, termOffset, frameLength, termId);
                termBuffer.PutBytes(termOffset + DataHeaderFlyweight.HEADER_LENGTH, bufferOne, offsetOne, lengthOne);
                termBuffer.PutBytes(termOffset + DataHeaderFlyweight.HEADER_LENGTH + lengthOne, bufferTwo, offsetTwo, lengthTwo);

                if (null != reservedValueSupplier)
                {
                    long reservedValue = reservedValueSupplier(termBuffer, termOffset, frameLength);
                    termBuffer.PutLong(termOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                }

                FrameDescriptor.FrameLengthOrdered(termBuffer, termOffset, frameLength);
            }

            return(resultingOffset);
        }
예제 #18
0
        private void Append(int entryType, long recordingId, long leadershipTermId, long logPosition, long termPosition, long timestamp, int votedForMemberId)
        {
            buffer.PutLong(RECORDING_ID_OFFSET, recordingId, ByteOrder.LittleEndian);
            buffer.PutLong(TERM_BASE_LOG_POSITION_OFFSET, logPosition, ByteOrder.LittleEndian);
            buffer.PutLong(LEADERSHIP_TERM_ID_OFFSET, leadershipTermId, ByteOrder.LittleEndian);
            buffer.PutLong(TIMESTAMP_OFFSET, timestamp, ByteOrder.LittleEndian);
            buffer.PutLong(TERM_POSITION_OFFSET, termPosition, ByteOrder.LittleEndian);
            buffer.PutInt(MEMBER_ID_VOTE_OFFSET, votedForMemberId, ByteOrder.LittleEndian);
            buffer.PutInt(ENTRY_TYPE_OFFSET, entryType, ByteOrder.LittleEndian);

            using (var fileChannel = new FileStream(logFile.FullName, FileMode.Append, FileAccess.Write,
                                                    FileShare.ReadWrite, 4096, FileOptions.WriteThrough))
            {
                fileChannel.Write(byteBuffer, 0, ENTRY_LENGTH);
            }

            entries.Add(new Entry(recordingId, leadershipTermId, logPosition, AeronArchive.NULL_POSITION, timestamp, votedForMemberId, entryType, nextEntryIndex++));
        }
예제 #19
0
 /// <summary>
 ///     Set the raw value of the tail for the given partition.
 /// </summary>
 /// <param name="metaDataBuffer"> containing the tail counters. </param>
 /// <param name="partitionIndex">    for the tail counter. </param>
 /// <param name="rawTail">           to be stored </param>
 public static void RawTail(UnsafeBuffer metaDataBuffer, int partitionIndex, long rawTail)
 {
     metaDataBuffer.PutLong(TERM_TAIL_COUNTERS_OFFSET + BitUtil.SIZE_OF_LONG * partitionIndex, rawTail);
 }
예제 #20
0
 public static void InitialiseTailWithTermId(UnsafeBuffer logMetaData, int partitionIndex, int termId)
 {
     logMetaData.PutLong(TERM_TAIL_COUNTERS_OFFSET + partitionIndex * BitUtil.SIZE_OF_LONG, PackTail(termId, 0));
 }
예제 #21
0
 public static void CorrelationId(UnsafeBuffer metaDataBuffer, long id)
 {
     metaDataBuffer.PutLong(LOG_CORRELATION_ID_OFFSET, id);
 }
예제 #22
0
 /// <summary>
 /// Set the initial value for the termId in the upper bits of the tail counter.
 /// </summary>
 /// <param name="termMetaData">  contain the tail counter. </param>
 /// <param name="initialTermId"> to be set. </param>
 public static void InitialiseTailWithTermId(UnsafeBuffer termMetaData, int initialTermId)
 {
     termMetaData.PutLong(TERM_TAIL_COUNTER_OFFSET, ((long)initialTermId) << 32);
 }
예제 #23
0
        public static void Main()
        {
            ComputerSpecifications.Dump();


            var reporter            = new RateReporter(1000, PrintRate);
            var rateReporterHandler = SamplesUtil.RateReporterHandler(reporter);
            var context             = new Aeron.Context();

            var running = new AtomicBoolean(true);

            var reportThread    = new Thread(reporter.Run);
            var subscribeThread = new Thread(subscription => SamplesUtil.SubscriberLoop(rateReporterHandler, FragmentCountLimit, running)((Subscription)subscription));

            using (var aeron = Aeron.Connect(context))
                using (var publication = aeron.AddPublication(Channel, StreamID))
                    using (var subscription = aeron.AddSubscription(Channel, StreamID))
                        using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                            using (var buffer = new UnsafeBuffer(byteBuffer))
                            {
                                reportThread.Start();
                                subscribeThread.Start(subscription);

                                do
                                {
                                    Console.WriteLine("Streaming {0:G} messages of size {1:G} bytes to {2} on stream Id {3}", NumberOfMessages, MessageLength, Channel, StreamID);

                                    _printingActive = true;

                                    long backPressureCount = 0;
                                    for (long i = 0; i < NumberOfMessages; i++)
                                    {
                                        buffer.PutLong(0, i);

                                        OfferIdleStrategy.Reset();
                                        while (publication.Offer(buffer, 0, buffer.Capacity) < 0)
                                        {
                                            OfferIdleStrategy.Idle();
                                            backPressureCount++;
                                        }
                                    }

                                    Console.WriteLine("Done streaming. backPressureRatio=" + (double)backPressureCount / NumberOfMessages);

                                    if (0 < LingerTimeoutMs)
                                    {
                                        Console.WriteLine("Lingering for " + LingerTimeoutMs + " milliseconds...");
                                        Thread.Sleep((int)LingerTimeoutMs);
                                    }

                                    _printingActive = false;
                                } while (Console.ReadLine() != "x");

                                reporter.Halt();
                                running.Set(false);

                                if (!subscribeThread.Join(5000))
                                {
                                    Console.WriteLine("Warning: not all tasks completed promptly");
                                }
                            }
        }
예제 #24
0
 /// <summary>
 /// Write the provided value into the reserved space at the end of the data frame header.
 ///
 /// Note: The value will be written in <seealso cref="ByteOrder.LittleEndian"/> format.
 /// </summary>
 /// <param name="value"> to be stored in the reserve space at the end of a data frame header. </param>
 /// <returns> this for fluent API semantics. </returns>
 /// <seealso cref="DataHeaderFlyweight" />
 public BufferClaim ReservedValue(long value)
 {
     _buffer.PutLong(DataHeaderFlyweight.RESERVED_VALUE_OFFSET, value);
     return(this);
 }
예제 #25
0
 public void TailTermId(int termId)
 {
     _metaDataBuffer.PutLong((int)tailAddressOffset, ((long)termId) << 32);
 }
예제 #26
0
 /// <summary>
 /// Set the value of the term id into the tail counter.
 /// </summary>
 /// <param name="termId"> for the tail counter </param>
 public void TermId(int termId)
 {
     _metaDataBuffer.PutLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, ((long)termId) << 32);
 }
예제 #27
0
 public override void Set(long value)
 {
     _buffer.PutLong(_offset, value);
 }
예제 #28
0
        public int AppendFragmentedMessage(
            int termId,
            int termOffset,
            HeaderWriter header,
            DirectBufferVector[] vectors,
            int length,
            int maxPayloadLength,
            ReservedValueSupplier reservedValueSupplier)
        {
            int numMaxPayloads   = length / maxPayloadLength;
            int remainingPayload = length % maxPayloadLength;
            int lastFrameLength  = remainingPayload > 0
                ? BitUtil.Align(remainingPayload + DataHeaderFlyweight.HEADER_LENGTH, FrameDescriptor.FRAME_ALIGNMENT)
                : 0;
            int requiredLength = (numMaxPayloads * (maxPayloadLength + DataHeaderFlyweight.HEADER_LENGTH)) +
                                 lastFrameLength;
            UnsafeBuffer termBuffer = _termBuffer;
            int          termLength = termBuffer.Capacity;

            int resultingOffset = termOffset + requiredLength;

            PutRawTailOrdered(termId, resultingOffset);

            if (resultingOffset > termLength)
            {
                resultingOffset = HandleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
            }
            else
            {
                int  frameOffset  = termOffset;
                byte flags        = FrameDescriptor.BEGIN_FRAG_FLAG;
                int  remaining    = length;
                int  vectorIndex  = 0;
                int  vectorOffset = 0;

                do
                {
                    int bytesToWrite  = Math.Min(remaining, maxPayloadLength);
                    int frameLength   = bytesToWrite + DataHeaderFlyweight.HEADER_LENGTH;
                    int alignedLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);

                    header.Write(termBuffer, frameOffset, frameLength, termId);

                    int bytesWritten  = 0;
                    int payloadOffset = frameOffset + DataHeaderFlyweight.HEADER_LENGTH;

                    do
                    {
                        var vector          = vectors[vectorIndex];
                        int vectorRemaining = vector.length - vectorOffset;
                        int numBytes        = Math.Min(bytesToWrite - bytesWritten, vectorRemaining);

                        termBuffer.PutBytes(payloadOffset, vector.buffer, vector.offset + vectorOffset, numBytes);

                        bytesWritten  += numBytes;
                        payloadOffset += numBytes;
                        vectorOffset  += numBytes;

                        if (vectorRemaining <= numBytes)
                        {
                            vectorIndex++;
                            vectorOffset = 0;
                        }
                    } while (bytesWritten < bytesToWrite);

                    if (remaining <= maxPayloadLength)
                    {
                        flags |= FrameDescriptor.END_FRAG_FLAG;
                    }

                    FrameDescriptor.FrameFlags(termBuffer, frameOffset, flags);

                    if (null != reservedValueSupplier)
                    {
                        long reservedValue = reservedValueSupplier(termBuffer, frameOffset, frameLength);
                        termBuffer.PutLong(frameOffset + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, reservedValue, ByteOrder.LittleEndian);
                    }

                    FrameDescriptor.FrameLengthOrdered(termBuffer, frameOffset, frameLength);

                    flags        = 0;
                    frameOffset += alignedLength;
                    remaining   -= bytesToWrite;
                } while (remaining > 0);
            }

            return(resultingOffset);
        }
예제 #29
0
        public static void Main()
        {
            if (MessageLength < BitUtil.SIZE_OF_LONG)
            {
                throw new ArgumentException($"Message length must be at least {BitUtil.SIZE_OF_LONG:D} bytes");
            }

            ComputerSpecifications.Dump();

            var context  = new Aeron.Context();
            var reporter = new RateReporter(1000, PrintRate);

            _reporterThread = new Thread(_ => reporter.Run());
            _reporterThread.Start();

            // Connect to media driver and add publication to send messages on the configured channel and stream ID.
            // The Aeron and Publication classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(context))
                using (var publication = aeron.AddPublication(Channel, StreamID))
                    using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                        using (var buffer = new UnsafeBuffer(byteBuffer))
                        {
                            do
                            {
                                _printingActive = true;

                                Console.WriteLine($"Streaming {NumberOfMessages} messages of {(RandomMessageLength ? " random" : "")} size {MessageLength} bytes to {Channel} on stream Id {StreamID}");

                                long backPressureCount = 0;

                                for (long i = 0; i < NumberOfMessages; i++)
                                {
                                    var length = LengthGenerator.AsInt;

                                    buffer.PutLong(0, i);
                                    OfferIdleStrategy.Reset();
                                    while (publication.Offer(buffer, 0, length) < 0L)
                                    {
                                        // The offer failed, which is usually due to the publication
                                        // being temporarily blocked.  Retry the offer after a short
                                        // spin/yield/sleep, depending on the chosen IdleStrategy.
                                        backPressureCount++;
                                        OfferIdleStrategy.Idle();
                                    }

                                    reporter.OnMessage(1, length);
                                }

                                Console.WriteLine("Done streaming. Back pressure ratio " + (double)backPressureCount / NumberOfMessages);

                                if (0 < LingerTimeoutMs)
                                {
                                    Console.WriteLine("Lingering for " + LingerTimeoutMs + " milliseconds...");
                                    Thread.Sleep((int)LingerTimeoutMs);
                                }

                                _printingActive = false;

                                Console.WriteLine("Execute again?");
                            } while (Console.ReadLine() == "y");
                        }

            reporter.Halt();
            _reporterThread.Join();
        }