Пример #1
0
        internal virtual async Task <bool> UpdateQueueMetrics()
        {
            CloudQueue workItemQueue = AzureStorageOrchestrationService.GetWorkItemQueue(this.storageAccount, this.taskHub);

            CloudQueue[] controlQueues = await AzureStorageOrchestrationService.GetControlQueuesAsync(
                this.storageAccount,
                this.taskHub,
                defaultPartitionCount : AzureStorageOrchestrationServiceSettings.DefaultPartitionCount);

            Task <QueueMetric>         workItemMetricTask      = GetQueueMetricsAsync(workItemQueue);
            List <Task <QueueMetric> > controlQueueMetricTasks = controlQueues.Select(GetQueueMetricsAsync).ToList();

            var tasks = new List <Task>(controlQueueMetricTasks.Count + 1);

            tasks.Add(workItemMetricTask);
            tasks.AddRange(controlQueueMetricTasks);

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (StorageException e) when(e.RequestInformation?.HttpStatusCode == 404)
            {
                // The queues are not yet provisioned.
                AnalyticsEventSource.Log.MonitorWarning(
                    this.storageAccount.Credentials.AccountName,
                    this.taskHub,
                    $"Task hub has not been provisioned: {e.RequestInformation.ExtendedErrorInformation?.ErrorMessage}");
                return(false);
            }

            QueueMetric workItemQueueMetric = workItemMetricTask.Result;

            this.WorkItemQueueLatencies.Add((int)workItemQueueMetric.Latency.TotalMilliseconds);

            int i;

            for (i = 0; i < controlQueueMetricTasks.Count; i++)
            {
                QueueMetric controlQueueMetric = controlQueueMetricTasks[i].Result;
                if (i >= this.ControlQueueLatencies.Count)
                {
                    this.ControlQueueLatencies.Add(new QueueMetricHistory(QueueLengthSampleSize));
                }

                this.ControlQueueLatencies[i].Add((int)controlQueueMetric.Latency.TotalMilliseconds);
            }

            // Handle the case where the number of control queues has been reduced since we last checked.
            while (i < this.ControlQueueLatencies.Count && this.ControlQueueLatencies.Count > 0)
            {
                this.ControlQueueLatencies.RemoveAt(this.ControlQueueLatencies.Count - 1);
            }

            this.currentPartitionCount      = controlQueues.Length;
            this.currentWorkItemQueueLength = workItemQueueMetric.Length;
            this.currentControlQueueLengths = controlQueueMetricTasks.Select(t => t.Result.Length).ToArray();

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Gets the scale-related status of the work-item queue.
        /// </summary>
        /// <returns>The approximate number of messages in the work-item queue.</returns>
        protected virtual async Task <WorkItemQueueData> GetWorkItemQueueStatusAsync()
        {
            CloudQueue workItemQueue = AzureStorageOrchestrationService.GetWorkItemQueue(this.storageAccount, this.taskHub);

            DateTimeOffset now = DateTimeOffset.Now;

            Task fetchTask = workItemQueue.FetchAttributesAsync();
            Task <CloudQueueMessage> peekTask = workItemQueue.PeekMessageAsync();
            await Task.WhenAll(fetchTask, peekTask);

            int      queueLength = workItemQueue.ApproximateMessageCount.GetValueOrDefault(0);
            TimeSpan age         = now.Subtract((peekTask.Result?.InsertionTime).GetValueOrDefault(now));

            if (age < TimeSpan.Zero)
            {
                age = TimeSpan.Zero;
            }

            return(new WorkItemQueueData
            {
                QueueLength = queueLength,
                FirstMessageAge = age,
            });
        }