public void starts_low()
 {
     using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
     {
         Assert.Equal(10, sut.AvailableDegreesOfParallelism);
     }
 }
Exemplo n.º 2
0
        public SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ILogger <SubscriptionReceiver> logger)
        {
            this.settings          = settings;
            this.topic             = topic;
            this.subscription      = subscription;
            this.processInParallel = processInParallel;
            this.logger            = logger;

            this.client = new SubscriptionClient(settings.ConnectionString, topic, subscription);
            if (this.processInParallel)
            {
                this.client.PrefetchCount = 18;
            }
            else
            {
                this.client.PrefetchCount = 14;
            }

            dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 100,
                    minDegreeOfParallelism: 50,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 8000);

            receiveRetryPolicy = Policy.Handle <Exception>().WaitAndRetryAsync(3, (r) => TimeSpan.FromMilliseconds(900),//, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(1));
                                                                               (ex, ts, attemps, cc) =>
            {
                this.dynamicThrottling.Penalize();
                logger.LogWarning($"An error occurred in attempt number {attemps} to receive a message from subscription {subscription}: {ex.Message}");
            });
        }
Exemplo n.º 3
0
 public void starts_low()
 {
     using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
     {
         Assert.Equal(10, sut.AvailableDegreesOfParallelism);
     }
 }
Exemplo n.º 4
0
 private static void IncreaseDegreesOfParallelism(DynamicThrottling sut)
 {
     for (int i = 0; i < 10; i++)
     {
         // increase degrees to avoid being in the minimum boundary
         sut.NotifyWorkStarted();
         sut.NotifyWorkCompleted();
     }
 }
        public void increases_on_completed_work()
        {
            using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) {
                sut.NotifyWorkStarted();
                var startingValue = sut.AvailableDegreesOfParallelism;

                sut.NotifyWorkCompleted();

                Assert.True(startingValue < sut.AvailableDegreesOfParallelism);
            }
        }
        public void increases_on_completed_work()
        {
            using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
            {
                sut.NotifyWorkStarted();
                var startingValue = sut.AvailableDegreesOfParallelism;

                sut.NotifyWorkCompleted();

                Assert.True(startingValue < sut.AvailableDegreesOfParallelism);
            }
        }
        public void decreases_on_penalize()
        {
            using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000)) {
                IncreaseDegreesOfParallelism(sut);

                sut.NotifyWorkStarted();
                var startingValue = sut.AvailableDegreesOfParallelism;
                sut.Penalize();

                Assert.True(startingValue > sut.AvailableDegreesOfParallelism);
            }
        }
        public void decreases_on_penalize()
        {
            using (var sut = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
            {
                IncreaseDegreesOfParallelism(sut);

                sut.NotifyWorkStarted();
                var startingValue = sut.AvailableDegreesOfParallelism;
                sut.Penalize();

                Assert.True(startingValue > sut.AvailableDegreesOfParallelism);
            }
        }
Exemplo n.º 9
0
        public void penalize_decreases_less_than_completed_with_error()
        {
            using (var sut1 = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
                using (var sut2 = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
                {
                    IncreaseDegreesOfParallelism(sut1);
                    IncreaseDegreesOfParallelism(sut2);

                    sut1.NotifyWorkStarted();
                    sut2.NotifyWorkStarted();

                    sut1.Penalize();
                    sut2.NotifyWorkCompletedWithError();

                    Assert.True(sut1.AvailableDegreesOfParallelism > sut2.AvailableDegreesOfParallelism);
                }
        }
        public void penalize_decreases_less_than_completed_with_error()
        {
            using (var sut1 = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
            using (var sut2 = new DynamicThrottling(100, 10, 3, 5, 1, 8000))
            {
                IncreaseDegreesOfParallelism(sut1);
                IncreaseDegreesOfParallelism(sut2);

                sut1.NotifyWorkStarted();
                sut2.NotifyWorkStarted();

                sut1.Penalize();
                sut2.NotifyWorkCompletedWithError();

                Assert.True(sut1.AvailableDegreesOfParallelism > sut2.AvailableDegreesOfParallelism);
            }
        }
Exemplo n.º 11
0
        public EventStoreBusPublisher(IMessageSender sender, IPendingEventsQueue queue, IEventStoreBusPublisherInstrumentation instrumentation)
        {
            this.sender          = sender;
            this.queue           = queue;
            this.instrumentation = instrumentation;

            this.enqueuedKeys      = new BlockingCollection <string>();
            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 230,
                    minDegreeOfParallelism: 30,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 10,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 8000);
            this.queue.Retrying  += (s, e) => this.dynamicThrottling.Penalize();
            this.sender.Retrying += (s, e) => this.dynamicThrottling.Penalize();
        }
Exemplo n.º 12
0
        public EventStoreBusPublisher(IMessageSender sender, IPendingEventsQueue queue, IEventStoreBusPublisherInstrumentation instrumentation)
        {
            this.sender          = sender;
            this.queue           = queue;
            this.instrumentation = instrumentation;

            enqueuedKeys      = new BlockingCollection <string>();
            dynamicThrottling =
                new DynamicThrottling(
                    230,
                    30,
                    3,
                    10,
                    1,
                    8000);
            this.queue.Retrying  += (s, e) => dynamicThrottling.Penalize();
            this.sender.Retrying += (s, e) => dynamicThrottling.Penalize();
        }
Exemplo n.º 13
0
        protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ISubscriptionReceiverInstrumentation instrumentation, int maxNumberRetry, RetryStrategy backgroundRetryStrategy)
        {
            this.settings          = settings;
            this.topic             = topic;
            this.subscription      = subscription;
            this.processInParallel = processInParallel;
            this.instrumentation   = instrumentation;

            var messagingFactory = MessagingFactory.CreateFromConnectionString(this.settings.ConnectionString);

            this.client = messagingFactory.CreateSubscriptionClient(topic, subscription);
            if (this.processInParallel)
            {
                this.client.PrefetchCount = 500;
            }
            else
            {
                this.client.PrefetchCount = 100;
            }

            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 10000,
                    minDegreeOfParallelism: 50,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 8000);

            this.retryStrategy  = backgroundRetryStrategy;
            this.maxNumberRetry = maxNumberRetry;

            this.receiveRetryPolicy           = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.receiveRetryPolicy.Retrying += (s, e) =>
            {
                this.dynamicThrottling.Penalize();
                Trace.TraceWarning(
                    "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}",
                    e.LastException.Message,
                    e.CurrentRetryCount,
                    this.subscription);
            };
        }
Exemplo n.º 14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class,
        /// automatically creating the topic and subscription if they don't exist.
        /// </summary>
        protected SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ISessionSubscriptionReceiverInstrumentation instrumentation, RetryStrategy backgroundRetryStrategy)
        {
            this.settings     = settings;
            this.topic        = topic;
            this.subscription = subscription;
            this.requiresSequentialProcessing = requiresSequentialProcessing;
            this.instrumentation = instrumentation;

            this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            this.serviceUri    = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);

            var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider);

            this.client = messagingFactory.CreateSubscriptionClient(topic, subscription);
            if (this.requiresSequentialProcessing)
            {
                this.client.PrefetchCount = 10;
            }
            else
            {
                this.client.PrefetchCount = 15;
            }

            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 160,
                    minDegreeOfParallelism: 30,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 10000);
            this.receiveRetryPolicy           = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.receiveRetryPolicy.Retrying += (s, e) =>
            {
                this.dynamicThrottling.Penalize();
                Trace.TraceWarning(
                    "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}",
                    e.LastException.Message,
                    e.CurrentRetryCount,
                    this.subscription);
            };
        }
Exemplo n.º 15
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="SubscriptionReceiver" /> class,
        ///     automatically creating the topic and subscription if they don't exist.
        /// </summary>
        protected SubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool processInParallel, ISubscriptionReceiverInstrumentation instrumentation,
                                       RetryStrategy backgroundRetryStrategy)
        {
            this.settings          = settings;
            this.topic             = topic;
            this.subscription      = subscription;
            this.processInParallel = processInParallel;
            this.instrumentation   = instrumentation;

            tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            serviceUri    = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);

            var messagingFactory = MessagingFactory.Create(serviceUri, tokenProvider);

            client = messagingFactory.CreateSubscriptionClient(topic, subscription);
            if (this.processInParallel)
            {
                client.PrefetchCount = 18;
            }
            else
            {
                client.PrefetchCount = 14;
            }

            dynamicThrottling =
                new DynamicThrottling(
                    100,
                    50,
                    3,
                    5,
                    1,
                    8000);
            receiveRetryPolicy           = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            receiveRetryPolicy.Retrying += (s, e) => {
                dynamicThrottling.Penalize();
                Trace.TraceWarning(
                    "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}",
                    e.LastException.Message,
                    e.CurrentRetryCount,
                    this.subscription);
            };
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class,
        /// automatically creating the topic and subscription if they don't exist.
        /// </summary>
        public SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ILogger <SessionSubscriptionReceiver> logger)
        {
            this.settings     = settings;
            this.topic        = topic;
            this.subscription = subscription;
            this.requiresSequentialProcessing = requiresSequentialProcessing;
            this.logger = logger;

            client = new SubscriptionClient(settings.ConnectionString, topic, subscription, ReceiveMode.PeekLock, Microsoft.Azure.ServiceBus.RetryPolicy.Default);

            if (this.requiresSequentialProcessing)
            {
                this.client.PrefetchCount = 10;
            }
            else
            {
                this.client.PrefetchCount = 15;
            }

            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 160,
                    minDegreeOfParallelism: 30,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 10000);

            this.receiveRetryPolicy = Policy.Handle <Exception>()
                                      .WaitAndRetryAsync(10, retry => TimeSpan.FromSeconds(Math.Pow(2, retry)),
                                                         (ex, ts, attempt, context) =>
            {
                this.dynamicThrottling.Penalize();
                logger.LogWarning($"An error occurred in attempt number {attempt} to receive a message from subscription {subscription}: {ex.Message}");
            });
        }
 private static void IncreaseDegreesOfParallelism(DynamicThrottling sut)
 {
     for (int i = 0; i < 10; i++)
     {
         // increase degrees to avoid being in the minimum boundary
         sut.NotifyWorkStarted();
         sut.NotifyWorkCompleted();
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionSubscriptionReceiver"/> class, 
        /// automatically creating the topic and subscription if they don't exist.
        /// </summary>
        protected SessionSubscriptionReceiver(ServiceBusSettings settings, string topic, string subscription, bool requiresSequentialProcessing, ISessionSubscriptionReceiverInstrumentation instrumentation, RetryStrategy backgroundRetryStrategy)
        {
            this.settings = settings;
            this.topic = topic;
            this.subscription = subscription;
            this.requiresSequentialProcessing = requiresSequentialProcessing;
            this.instrumentation = instrumentation;

            this.tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey);
            this.serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath);

            var messagingFactory = MessagingFactory.Create(this.serviceUri, tokenProvider);
            this.client = messagingFactory.CreateSubscriptionClient(topic, subscription);
            if (this.requiresSequentialProcessing)
            {
                this.client.PrefetchCount = 10;
            }
            else
            {
                this.client.PrefetchCount = 15;
            }

            this.dynamicThrottling =
                new DynamicThrottling(
                    maxDegreeOfParallelism: 160,
                    minDegreeOfParallelism: 30,
                    penaltyAmount: 3,
                    workFailedPenaltyAmount: 5,
                    workCompletedParallelismGain: 1,
                    intervalForRestoringDegreeOfParallelism: 10000);
            this.receiveRetryPolicy = new RetryPolicy<ServiceBusTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.receiveRetryPolicy.Retrying += (s, e) =>
                {
                    this.dynamicThrottling.Penalize();
                    Trace.TraceWarning(
                        "An error occurred in attempt number {1} to receive a message from subscription {2}: {0}",
                        e.LastException.Message,
                        e.CurrentRetryCount,
                        this.subscription);
                };
        }