Beispiel #1
0
        private int AwaitRecoveryCounter(CountersReader counters)
        {
            idleStrategy.Reset();
            int counterId = RecoveryState.FindCounterId(counters);

            while (CountersReader.NULL_COUNTER_ID == counterId)
            {
                CheckInterruptedStatus();
                idleStrategy.Idle();

                counterId = RecoveryState.FindCounterId(counters);
            }

            return(counterId);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        private void AwaitActiveLog()
        {
            idleStrategy.Reset();

            while (null == _activeLogEvent)
            {
                _serviceAdapter.Poll();
                CheckInterruptedStatus();
                idleStrategy.Idle();
            }
        }
Beispiel #4
0
        public static void PingHandler(Publication pongPublication, UnsafeBuffer buffer, int offset, int length)
        {
            if (pongPublication.Offer(buffer, offset, length) > 0L)
            {
                return;
            }

            PingHandlerIdleStrategy.Reset();

            while (pongPublication.Offer(buffer, offset, length) < 0L)
            {
                PingHandlerIdleStrategy.Idle();
            }
        }
Beispiel #5
0
        private bool Offer(int length)
        {
            retryIdleStrategy.Reset();

            int attempts = retryAttempts;

            while (true)
            {
                long result;
                if ((result = publication.Offer(buffer, 0, MessageHeaderEncoder.ENCODED_LENGTH + length)) > 0)
                {
                    return(true);
                }

                if (result == Publication.CLOSED)
                {
                    throw new System.InvalidOperationException("connection to the archive has been closed");
                }

                if (result == Publication.NOT_CONNECTED)
                {
                    throw new System.InvalidOperationException("connection to the archive is no longer available");
                }

                if (result == Publication.MAX_POSITION_EXCEEDED)
                {
                    throw new System.InvalidOperationException("publication failed due to max position being reached");
                }

                if (--attempts <= 0)
                {
                    return(false);
                }

                retryIdleStrategy.Idle();
            }
        }
Beispiel #6
0
        private void AwaitResponse(long correlationId)
        {
            _driverException = null;
            var nowNs      = _nanoClock.NanoTime();
            var deadlineNs = nowNs + _driverTimeoutNs;

            CheckTimeouts(nowNs);

            _awaitingIdleStrategy.Reset();
            do
            {
                if (null == _driverAgentInvoker)
                {
                    _awaitingIdleStrategy.Idle();
                }
                else
                {
                    _driverAgentInvoker.Invoke();
                }

                Service(correlationId);

                if (_driverEventsAdapter.ReceivedCorrelationId == correlationId)
                {
                    _stashedChannel = null;

                    RegistrationException ex = _driverException;
                    if (null != _driverException)
                    {
                        _driverException = null;
                        throw ex;
                    }

                    return;
                }

                try
                {
                    Thread.Sleep(1);
                }
                catch (ThreadInterruptedException)
                {
                    _isTerminating = true;
                    throw new AgentTerminationException("thread interrupted");
                }
            } while (deadlineNs - _nanoClock.NanoTime() > 0);

            throw new DriverTimeoutException("no response from MediaDriver within (ms):" + _driverTimeoutMs);
        }
Beispiel #7
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();
                }
            }
        }
Beispiel #8
0
        private static void Publish <T>(T message,
                                        Publication publication,
                                        IIdleStrategy offerIdleStrategy,
                                        IMutableDirectBuffer buffer)
        {
            var serTm  = Util.Serialize(message);
            var length = serTm.Length;

            buffer.PutBytes(0, serTm);

            offerIdleStrategy.Reset();
            while (!Offer(publication, buffer, length))
            {
                // 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);
        }
Beispiel #9
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();
        }