@@ -35,6 +35,16 @@ internal class AzureStorageDurabilityProvider : DurabilityProvider
35
35
private readonly JObject storageOptionsJson ;
36
36
private readonly ILogger logger ;
37
37
38
+ private readonly object initLock = new object ( ) ;
39
+
40
+ #if ! FUNCTIONS_V1
41
+ private DurableTaskScaleMonitor singletonScaleMonitor ;
42
+ #endif
43
+
44
+ #if FUNCTIONS_V3_OR_GREATER
45
+ private DurableTaskTargetScaler singletonTargetScaler ;
46
+ #endif
47
+
38
48
public AzureStorageDurabilityProvider (
39
49
AzureStorageOrchestrationService service ,
40
50
IStorageAccountProvider storageAccountProvider ,
@@ -226,12 +236,11 @@ internal static OrchestrationInstanceStatusQueryCondition ConvertWebjobsDurableC
226
236
#if ! FUNCTIONS_V1
227
237
228
238
internal DurableTaskMetricsProvider GetMetricsProvider (
229
- string functionName ,
230
239
string hubName ,
231
240
CloudStorageAccount storageAccount ,
232
241
ILogger logger )
233
242
{
234
- return new DurableTaskMetricsProvider ( functionName , hubName , logger , performanceMonitor : null , storageAccount ) ;
243
+ return new DurableTaskMetricsProvider ( hubName , logger , performanceMonitor : null , storageAccount ) ;
235
244
}
236
245
237
246
/// <inheritdoc/>
@@ -242,16 +251,22 @@ public override bool TryGetScaleMonitor(
242
251
string connectionName ,
243
252
out IScaleMonitor scaleMonitor )
244
253
{
245
- CloudStorageAccount storageAccount = this . storageAccountProvider . GetStorageAccountDetails ( connectionName ) . ToCloudStorageAccount ( ) ;
246
- DurableTaskMetricsProvider metricsProvider = this . GetMetricsProvider ( functionName , hubName , storageAccount , this . logger ) ;
247
- scaleMonitor = new DurableTaskScaleMonitor (
248
- functionId ,
249
- functionName ,
250
- hubName ,
251
- storageAccount ,
252
- this . logger ,
253
- metricsProvider ) ;
254
- return true ;
254
+ lock ( this . initLock )
255
+ {
256
+ if ( this . singletonScaleMonitor == null )
257
+ {
258
+ CloudStorageAccount storageAccount = this . storageAccountProvider . GetStorageAccountDetails ( connectionName ) . ToCloudStorageAccount ( ) ;
259
+ DurableTaskMetricsProvider metricsProvider = this . GetMetricsProvider ( hubName , storageAccount , this . logger ) ;
260
+ this . singletonScaleMonitor = new DurableTaskScaleMonitor (
261
+ hubName ,
262
+ storageAccount ,
263
+ this . logger ,
264
+ metricsProvider ) ;
265
+ }
266
+
267
+ scaleMonitor = this . singletonScaleMonitor ;
268
+ return true ;
269
+ }
255
270
}
256
271
257
272
#endif
@@ -263,11 +278,23 @@ public override bool TryGetTargetScaler(
263
278
string connectionName ,
264
279
out ITargetScaler targetScaler )
265
280
{
266
- // This is only called by the ScaleController, it doesn't run in the Functions Host process.
267
- CloudStorageAccount storageAccount = this . storageAccountProvider . GetStorageAccountDetails ( connectionName ) . ToCloudStorageAccount ( ) ;
268
- DurableTaskMetricsProvider metricsProvider = this . GetMetricsProvider ( functionName , hubName , storageAccount , this . logger ) ;
269
- targetScaler = new DurableTaskTargetScaler ( functionId , metricsProvider , this , this . logger ) ;
270
- return true ;
281
+ lock ( this . initLock )
282
+ {
283
+ if ( this . singletonTargetScaler == null )
284
+ {
285
+ // This is only called by the ScaleController, it doesn't run in the Functions Host process.
286
+ CloudStorageAccount storageAccount = this . storageAccountProvider . GetStorageAccountDetails ( connectionName ) . ToCloudStorageAccount ( ) ;
287
+ DurableTaskMetricsProvider metricsProvider = this . GetMetricsProvider ( hubName , storageAccount , this . logger ) ;
288
+
289
+ // Scalers in Durable Functions are shared for all functions in the same task hub.
290
+ // So instead of using a function ID, we use the task hub name as the basis for the descriptor ID.
291
+ string id = $ "DurableTask-AzureStorage:{ hubName ?? "default" } ";
292
+ this . singletonTargetScaler = new DurableTaskTargetScaler ( id , metricsProvider , this , this . logger ) ;
293
+ }
294
+
295
+ targetScaler = this . singletonTargetScaler ;
296
+ return true ;
297
+ }
271
298
}
272
299
#endif
273
300
}
0 commit comments