|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using DurableTask.AzureStorage; |
| 6 | +using Microsoft.Azure.WebJobs.Host.Scale; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | + |
| 9 | +namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.FunctionsScale.AzureStorage |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Azure Storage backend implementation of the scalability provider for Durable Functions scaling decisions. |
| 13 | + /// </summary> |
| 14 | + public class AzureStorageScalabilityProvider : ScalabilityProvider |
| 15 | + { |
| 16 | + private readonly StorageAccountClientProvider storageAccountClientProvider; |
| 17 | + private readonly string connectionName; |
| 18 | + private readonly ILogger logger; |
| 19 | + |
| 20 | + private readonly object initLock = new object(); |
| 21 | + |
| 22 | + private DurableTaskMetricsProvider singletonDurableTaskMetricsProvider; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="AzureStorageScalabilityProvider"/> class. |
| 26 | + /// </summary> |
| 27 | + /// <param name="storageAccountClientProvider"> |
| 28 | + /// Provides Azure Storage clients using resolved configuration, including |
| 29 | + /// connection strings or token-based credentials.</param> |
| 30 | + /// <param name="connectionName">The name of the storage connection used to resolve host configuration.</param> |
| 31 | + /// <param name="logger">The logger instance used for diagnostics and telemetry.</param> |
| 32 | + public AzureStorageScalabilityProvider( |
| 33 | + StorageAccountClientProvider storageAccountClientProvider, |
| 34 | + string connectionName, |
| 35 | + ILogger logger) |
| 36 | + : base("AzureStorage", connectionName) |
| 37 | + { |
| 38 | + this.storageAccountClientProvider = storageAccountClientProvider ?? throw new ArgumentNullException(nameof(storageAccountClientProvider)); |
| 39 | + this.connectionName = connectionName; |
| 40 | + this.logger = logger; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets the app setting containing the Azure Storage connection string. |
| 45 | + /// </summary> |
| 46 | + public override string ConnectionName => this.connectionName; |
| 47 | + |
| 48 | + /// <inheritdoc/> |
| 49 | + /// Note: ScaleMonitor is not used in prod. Can be cleaned in future. |
| 50 | + public override bool TryGetScaleMonitor( |
| 51 | + string functionId, |
| 52 | + string functionName, |
| 53 | + string hubName, |
| 54 | + string targetConnectionName, |
| 55 | + out IScaleMonitor scaleMonitor) |
| 56 | + { |
| 57 | + lock (this.initLock) |
| 58 | + { |
| 59 | + if (this.singletonDurableTaskMetricsProvider == null) |
| 60 | + { |
| 61 | + this.singletonDurableTaskMetricsProvider = this.GetMetricsProvider( |
| 62 | + hubName, |
| 63 | + this.storageAccountClientProvider, |
| 64 | + this.logger); |
| 65 | + } |
| 66 | + |
| 67 | + scaleMonitor = new DurableTaskScaleMonitor(functionId, hubName, this.logger, this.singletonDurableTaskMetricsProvider); |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <inheritdoc/> |
| 73 | + public override bool TryGetTargetScaler( |
| 74 | + string functionId, |
| 75 | + string functionName, |
| 76 | + string hubName, |
| 77 | + string targetConnectionName, |
| 78 | + out ITargetScaler targetScaler) |
| 79 | + { |
| 80 | + lock (this.initLock) |
| 81 | + { |
| 82 | + if (this.singletonDurableTaskMetricsProvider == null) |
| 83 | + { |
| 84 | + this.singletonDurableTaskMetricsProvider = this.GetMetricsProvider( |
| 85 | + hubName, |
| 86 | + this.storageAccountClientProvider, |
| 87 | + this.logger); |
| 88 | + } |
| 89 | + |
| 90 | + targetScaler = new DurableTaskTargetScaler(functionId, this.singletonDurableTaskMetricsProvider, this, this.logger); |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + internal DurableTaskMetricsProvider GetMetricsProvider( |
| 96 | + string hubName, |
| 97 | + StorageAccountClientProvider clientProvider, |
| 98 | + ILogger metricsLogger) |
| 99 | + { |
| 100 | + return new DurableTaskMetricsProvider(hubName, metricsLogger, performanceMonitor: null, clientProvider); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments