Exemplo n.º 1
0
        public bool ScheduleTimer(long correlationId, long deadlineMs)
        {
            int length = MessageHeaderEncoder.ENCODED_LENGTH + ScheduleTimerEncoder.BLOCK_LENGTH;

            int attempts = SEND_ATTEMPTS;

            do
            {
                long result = _publication.TryClaim(length, _bufferClaim);
                if (result > 0)
                {
                    _scheduleTimerEncoder
                    .WrapAndApplyHeader(_bufferClaim.Buffer, _bufferClaim.Offset, _messageHeaderEncoder)
                    .CorrelationId(correlationId)
                    .Deadline(deadlineMs);

                    _bufferClaim.Commit();

                    return(true);
                }

                CheckResult(result);
            } while (--attempts > 0);

            return(false);
        }
Exemplo n.º 2
0
        public void ScheduleTimer(long correlationId, long deadlineMs)
        {
            int length = MessageHeaderEncoder.ENCODED_LENGTH + ScheduleTimerEncoder.BLOCK_LENGTH;

            int attempts = SEND_ATTEMPTS;

            do
            {
                long result = _publication.TryClaim(length, _bufferClaim);
                if (result > 0)
                {
                    _scheduleTimerEncoder
                    .WrapAndApplyHeader(_bufferClaim.Buffer, _bufferClaim.Offset, _messageHeaderEncoder)
                    .CorrelationId(correlationId)
                    .Deadline(deadlineMs);

                    _bufferClaim.Commit();

                    return;
                }

                CheckResult(result);
            } while (--attempts > 0);

            throw new InvalidOperationException("Failed to schedule timer");
        }
Exemplo n.º 3
0
        public void ShouldClaimRegionForZeroCopyEncoding()
        {
            int         headerLength       = _defaultHeader.Capacity;
            const int   msgLength          = 20;
            int         frameLength        = msgLength + headerLength;
            int         alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            const int   tail        = 0;
            BufferClaim bufferClaim = new BufferClaim();

            A.CallTo(() => _termBuffer.PutIntOrdered(A <int> ._, A <int> ._));

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

            Assert.That(_termAppender.Claim(_headerWriter, msgLength, bufferClaim), Is.EqualTo((long)alignedFrameLength));

            Assert.That(bufferClaim.Offset, Is.EqualTo(tail + headerLength));
            Assert.That(bufferClaim.Length, Is.EqualTo(msgLength));

            // Map flyweight or encode to buffer directly then call commit() when done
            bufferClaim.Commit();


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

            A.CallTo(() => _headerWriter.Write(_termBuffer, tail, frameLength, TermID)).MustHaveHappened();
        }
            public void Run()
            {
                var  publication       = Publication;
                var  bufferClaim       = new BufferClaim();
                long backPressureCount = 0;
                long totalMessageCount = 0;

                while (Running.Get())
                {
                    for (var i = 0; i < BurstLength; i++)
                    {
                        while (publication.TryClaim(MessageLength, bufferClaim) <= 0)
                        {
                            ++backPressureCount;
                            if (!Running.Get())
                            {
                                break;
                            }
                        }

                        var offset = bufferClaim.Offset;
                        bufferClaim.Buffer.PutInt(offset, i); // Example field write
                        // Real app would write whatever fields are required via a flyweight like SBE

                        bufferClaim.Commit();

                        ++totalMessageCount;
                    }
                }


                var backPressureRatio = backPressureCount / (double)totalMessageCount;

                Console.WriteLine($"Publisher back pressure ratio: {backPressureRatio}");
            }
Exemplo n.º 5
0
        public void MarkSnapshot(
            long snapshotTypeId,
            long logPosition,
            long leadershipTermId,
            int snapshotIndex,
            SnapshotMark snapshotMark,
            ClusterTimeUnit timeUnit,
            int appVersion)
        {
            idleStrategy.Reset();
            while (true)
            {
                long result = publication.TryClaim(ENCODED_MARKER_LENGTH, bufferClaim);
                if (result > 0)
                {
                    snapshotMarkerEncoder
                    .WrapAndApplyHeader(bufferClaim.Buffer, bufferClaim.Offset, messageHeaderEncoder)
                    .TypeId(snapshotTypeId)
                    .LogPosition(logPosition)
                    .LeadershipTermId(leadershipTermId)
                    .Index(snapshotIndex)
                    .Mark(snapshotMark)
                    .TimeUnit(timeUnit)
                    .AppVersion(appVersion);

                    bufferClaim.Commit();
                    break;
                }

                CheckResultAndIdle(result);
            }
        }
Exemplo n.º 6
0
        public void ShouldClaimRegionForZeroCopyEncoding()
        {
            int         headerLength       = _defaultHeader.Capacity;
            const int   msgLength          = 20;
            int         frameLength        = msgLength + headerLength;
            int         alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            const int   tail        = 0;
            BufferClaim bufferClaim = new BufferClaim();

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, alignedFrameLength))
            .Returns(TermAppender.Pack(TermID, tail));

            Assert.That(_termAppender.Claim(_headerWriter, msgLength, bufferClaim), Is.EqualTo((long)alignedFrameLength));

            Assert.That(bufferClaim.Offset, Is.EqualTo(tail + headerLength));
            Assert.That(bufferClaim.Length, Is.EqualTo(msgLength));

            // Map flyweight or encode to buffer directly then call commit() when done
            bufferClaim.Commit();

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, alignedFrameLength)).MustHaveHappened()
            .Then(A.CallTo(() => _headerWriter.Write(_termBuffer, tail, frameLength, TermID)).MustHaveHappened());
        }
Exemplo n.º 7
0
            public void Run()
            {
                var publication = Publication;
                var bufferClaim = new BufferClaim();
                long backPressureCount = 0;
                long totalMessageCount = 0;

                while (Running.Get())
                {
                    for (var i = 0; i < BurstLength; i++)
                    {
                        while (publication.TryClaim(MessageLength, bufferClaim) <= 0)
                        {
                            ++backPressureCount;
                            if (!Running.Get())
                            {
                                break;
                            }
                        }

                        var offset = bufferClaim.Offset;
                        bufferClaim.Buffer.PutInt(offset, i); // Example field write
                        // Real app would write whatever fields are required via a flyweight like SBE

                        bufferClaim.Commit();

                        ++totalMessageCount;
                    }
                }


                var backPressureRatio = backPressureCount/(double) totalMessageCount;
                Console.WriteLine($"Publisher back pressure ratio: {backPressureRatio}");
            }