forked from Azure/azure-webjobs-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic Concurrency support (Azure#2720)
- Loading branch information
Showing
83 changed files
with
6,916 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace TestChildProcess | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Child process started"); | ||
Console.ReadLine(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Microsoft.Azure.WebJobs.Host.Storage/BlobStorageConcurrencyStatusRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Storage; | ||
using Microsoft.Azure.Storage.Blob; | ||
using Microsoft.Azure.WebJobs.Host.Executors; | ||
using Microsoft.Azure.WebJobs.Host.Scale; | ||
using Microsoft.Azure.WebJobs.Logging; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host | ||
{ | ||
internal class BlobStorageConcurrencyStatusRepository : IConcurrencyStatusRepository | ||
{ | ||
private const string HostContainerName = "azure-webjobs-hosts"; | ||
private readonly IHostIdProvider _hostIdProvider; | ||
private readonly IConfiguration _configuration; | ||
private readonly ILogger _logger; | ||
private CloudBlobContainer? _blobContainer; | ||
|
||
public BlobStorageConcurrencyStatusRepository(IConfiguration configuration, IHostIdProvider hostIdProvider, ILoggerFactory loggerFactory) | ||
{ | ||
_configuration = configuration; | ||
_hostIdProvider = hostIdProvider; | ||
_logger = loggerFactory.CreateLogger(LogCategories.Concurrency); | ||
} | ||
|
||
public async Task<HostConcurrencySnapshot?> ReadAsync(CancellationToken cancellationToken) | ||
{ | ||
string blobPath = await GetBlobPathAsync(cancellationToken); | ||
|
||
try | ||
{ | ||
CloudBlobContainer? container = await GetContainerAsync(cancellationToken); | ||
if (container != null) | ||
{ | ||
CloudBlockBlob blob = container.GetBlockBlobReference(blobPath); | ||
string content = await blob.DownloadTextAsync(cancellationToken); | ||
if (!string.IsNullOrEmpty(content)) | ||
{ | ||
var result = JsonConvert.DeserializeObject<HostConcurrencySnapshot>(content); | ||
return result; | ||
} | ||
} | ||
} | ||
catch (StorageException stex) when (stex.RequestInformation?.HttpStatusCode == 404) | ||
{ | ||
return null; | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, $"Error reading snapshot blob {blobPath}"); | ||
throw e; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task WriteAsync(HostConcurrencySnapshot snapshot, CancellationToken cancellationToken) | ||
{ | ||
string blobPath = await GetBlobPathAsync(cancellationToken); | ||
|
||
try | ||
{ | ||
CloudBlobContainer? container = await GetContainerAsync(cancellationToken); | ||
if (container != null) | ||
{ | ||
CloudBlockBlob blob = container.GetBlockBlobReference(blobPath); | ||
|
||
using (StreamWriter writer = new StreamWriter(await blob.OpenWriteAsync(cancellationToken))) | ||
{ | ||
var content = JsonConvert.SerializeObject(snapshot); | ||
await writer.WriteAsync(content); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, $"Error writing snapshot blob {blobPath}"); | ||
throw e; | ||
} | ||
} | ||
|
||
internal async Task<CloudBlobContainer?> GetContainerAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_blobContainer == null) | ||
{ | ||
string storageConnectionString = _configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage); | ||
if (!string.IsNullOrEmpty(storageConnectionString) && CloudStorageAccount.TryParse(storageConnectionString, out CloudStorageAccount account)) | ||
{ | ||
var client = account.CreateCloudBlobClient(); | ||
_blobContainer = client.GetContainerReference(HostContainerName); | ||
|
||
await _blobContainer.CreateIfNotExistsAsync(cancellationToken); | ||
} | ||
} | ||
|
||
return _blobContainer; | ||
} | ||
|
||
internal async Task<string> GetBlobPathAsync(CancellationToken cancellationToken) | ||
{ | ||
string hostId = await _hostIdProvider.GetHostIdAsync(cancellationToken); | ||
return $"concurrency/{hostId}/concurrencyStatus.json"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/Microsoft.Azure.WebJobs.Host/Config/ConcurrencyOptionsSetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Azure.WebJobs.Host.Scale; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Config | ||
{ | ||
internal class ConcurrencyOptionsSetup : IConfigureOptions<ConcurrencyOptions> | ||
{ | ||
private const int BytesPerGB = 1024 * 1024 * 1024; | ||
|
||
public void Configure(ConcurrencyOptions options) | ||
{ | ||
// TODO: Once Memory monitoring is public add this back. | ||
// For now, the memory throttle is internal only for testing. | ||
// https://github.com/Azure/azure-webjobs-sdk/issues/2733 | ||
//ConfigureMemoryOptions(options); | ||
} | ||
|
||
internal static void ConfigureMemoryOptions(ConcurrencyOptions options) | ||
{ | ||
string sku = Utility.GetWebsiteSku(); | ||
int numCores = Utility.GetEffectiveCoresCount(); | ||
ConfigureMemoryOptions(options, sku, numCores); | ||
} | ||
|
||
internal static void ConfigureMemoryOptions(ConcurrencyOptions options, string sku, int numCores) | ||
{ | ||
long memoryLimitBytes = GetMemoryLimitBytes(sku, numCores); | ||
if (memoryLimitBytes > 0) | ||
{ | ||
// if we're able to determine the memory limit, apply it | ||
options.TotalAvailableMemoryBytes = memoryLimitBytes; | ||
} | ||
} | ||
|
||
internal static long GetMemoryLimitBytes(string sku, int numCores) | ||
{ | ||
if (!string.IsNullOrEmpty(sku)) | ||
{ | ||
float memoryGBPerCore = GetMemoryGBPerCore(sku); | ||
|
||
if (memoryGBPerCore > 0) | ||
{ | ||
double memoryLimitBytes = memoryGBPerCore * numCores * BytesPerGB; | ||
|
||
if (string.Equals(sku, "IsolatedV2", StringComparison.OrdinalIgnoreCase) && numCores == 8) | ||
{ | ||
// special case for upper tier IsolatedV2 where GB per Core | ||
// isn't cleanly linear | ||
memoryLimitBytes = (float)23 * BytesPerGB; | ||
} | ||
|
||
return (long)memoryLimitBytes; | ||
} | ||
} | ||
|
||
// unable to determine memory limit | ||
return -1; | ||
} | ||
|
||
internal static float GetMemoryGBPerCore(string sku) | ||
{ | ||
if (string.IsNullOrEmpty(sku)) | ||
{ | ||
return -1; | ||
} | ||
|
||
// These memory allowances are based on published limits: | ||
// Dynamic SKU: https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale#service-limits | ||
// Premium SKU: https://docs.microsoft.com/en-us/azure/azure-functions/functions-premium-plan?tabs=portal#available-instance-skus | ||
// Dedicated SKUs: https://azure.microsoft.com/en-us/pricing/details/app-service/windows/ | ||
switch (sku.ToLower()) | ||
{ | ||
case "free": | ||
case "shared": | ||
return 1; | ||
case "dynamic": | ||
return 1.5F; | ||
case "basic": | ||
case "standard": | ||
return 1.75F; | ||
case "premiumv2": | ||
case "isolated": | ||
case "elasticpremium": | ||
return 3.5F; | ||
case "premiumv3": | ||
case "isolatedv2": | ||
return 4; | ||
default: | ||
return -1; | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.Azure.WebJobs.Host/Config/PrimaryHostCoordinatorOptionsSetup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Azure.WebJobs.Host.Scale; | ||
using Microsoft.Azure.WebJobs.Hosting; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Config | ||
{ | ||
internal class PrimaryHostCoordinatorOptionsSetup : IConfigureOptions<PrimaryHostCoordinatorOptions> | ||
{ | ||
private readonly IOptions<ConcurrencyOptions> _concurrencyOptions; | ||
|
||
public PrimaryHostCoordinatorOptionsSetup(IOptions<ConcurrencyOptions> concurrencyOptions) | ||
{ | ||
_concurrencyOptions = concurrencyOptions; | ||
} | ||
|
||
public void Configure(PrimaryHostCoordinatorOptions options) | ||
{ | ||
// in most WebJobs SDK scenarios, primary host coordination is not needed | ||
// however, some features require it | ||
if (_concurrencyOptions.Value.DynamicConcurrencyEnabled) | ||
{ | ||
options.Enabled = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.