Skip to content
Open
24 changes: 14 additions & 10 deletions eng/ci/templates/public/windows-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ jobs:
testTargetFramework: net10.0
variables:
AzureWebJobsStorage: UseDevelopmentStorage=true
SecondaryStorage: 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:11000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:11001/devstoreaccount1;TableEndpoint=http://127.0.0.1:11002/devstoreaccount1;'
steps:
- checkout: self
clean: true
Expand Down Expand Up @@ -198,16 +199,19 @@ jobs:
if ($LASTEXITCODE) { exit $LASTEXITCODE }

$azurite = Resolve-Path 'node_modules/.bin/azurite.cmd'
Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--blobPort','10000','--queuePort','10001','--tablePort','10002'
for ($i = 1; $i -le 30; $i++) {
try {
$tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', 10000)
$tcp.Dispose()
Write-Host 'Azurite is ready'
break
} catch {
if ($i -eq 30) { throw 'Azurite did not start within 30 seconds' }
Start-Sleep -Seconds 1
Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-primary','--blobPort','10000','--queuePort','10001','--tablePort','10002'
Start-Process $azurite -ArgumentList '--silent','--skipApiVersionCheck','--location','.azurite-secondary','--blobPort','11000','--queuePort','11001','--tablePort','11002'
foreach ($port in 10000, 10001, 10002, 11000, 11001, 11002) {
for ($i = 1; $i -le 30; $i++) {
try {
$tcp = [System.Net.Sockets.TcpClient]::new('127.0.0.1', $port)
$tcp.Dispose()
Write-Host "Azurite on port $port is ready"
break
} catch {
if ($i -eq 30) { throw "Azurite on port $port did not start within 30 seconds" }
Start-Sleep -Seconds 1
}
}
}
displayName: Start Azurite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public AzureStorageDurabilityProvider(

public override string EventSourceName { get; set; } = "DurableTask-AzureStorage";

internal override bool TaskHubNameMatches(string taskHubName, string otherTaskHubName)
{
return string.Equals(taskHubName, otherTaskHubName, StringComparison.OrdinalIgnoreCase);
}

/// <inheritdoc/>
public async override Task<IList<OrchestrationState>> GetAllOrchestrationStates(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private bool CheckStatusBeforeRaiseEvent

private IDurableClient GetDurableClient(string taskHubName, string connectionName)
{
if (string.Equals(this.TaskHubName, taskHubName, StringComparison.OrdinalIgnoreCase)
if (this.DurabilityProvider.TaskHubNameMatches(this.TaskHubName, taskHubName)
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
&& string.Equals(this.attribute.ConnectionName, connectionName, StringComparison.OrdinalIgnoreCase))
Comment on lines +107 to 108
{
return this;
Expand Down Expand Up @@ -358,7 +358,8 @@ private async Task SignalEntityAsyncInternal(DurableClient durableClient, string

private bool ClientReferencesCurrentApp(DurableClient client)
{
return !client.attribute.ExternalClient &&
return this.config != null &&
!client.attribute.ExternalClient &&
this.TaskHubMatchesCurrentApp(client) &&
this.ConnectionNameMatchesCurrentApp(client);
}
Expand All @@ -374,12 +375,12 @@ internal void ThrowIfOrchestratorFunctionIsDisabled(string name)
private bool TaskHubMatchesCurrentApp(DurableClient client)
{
var taskHubName = this.durableTaskOptions.HubName;
return client.TaskHubName.Equals(taskHubName);
return this.config.DefaultDurabilityProvider.TaskHubNameMatches(client.TaskHubName, taskHubName);
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
}

private bool ConnectionNameMatchesCurrentApp(DurableClient client)
{
return this.DurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
return this.config.DefaultDurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.ContextImplementations
/// </summary>
public class DurableClientFactory : IDurableClientFactory, IDisposable
{
// Creating client objects is expensive, so we cache them when the attributes match.
// Note that DurableClientAttribute defines a custom equality comparer.
private readonly ConcurrentDictionary<DurableClientAttribute, DurableClient> cachedClients =
new ConcurrentDictionary<DurableClientAttribute, DurableClient>();
// Creating client objects is expensive, so we cache them when their semantic configuration matches.
private readonly ConcurrentDictionary<DurableClientCacheKey, DurableClient> cachedClients =
new ConcurrentDictionary<DurableClientCacheKey, DurableClient>();

private readonly ConcurrentDictionary<DurableClientAttribute, HttpApiHandler> cachedHttpListeners =
new ConcurrentDictionary<DurableClientAttribute, HttpApiHandler>();
private readonly ConcurrentDictionary<DurableClientCacheKey, HttpApiHandler> cachedHttpListeners =
new ConcurrentDictionary<DurableClientCacheKey, HttpApiHandler>();

private readonly DurableClientOptions defaultDurableClientOptions;
private readonly DurableTaskOptions durableTaskOptions;
Expand Down Expand Up @@ -76,9 +75,10 @@ public IDurableClient CreateClient(DurableClientOptions durableClientOptions)

DurableClientAttribute attribute = new DurableClientAttribute(durableClientOptions);

var cacheKey = new DurableClientCacheKey(attribute);
DurableClient client = this.cachedClients.GetOrAdd(
attribute,
attr =>
cacheKey,
_ =>
{
DurabilityProvider innerClient = this.durabilityProviderFactory.GetDurabilityProvider(attribute);
return new DurableClient(innerClient, null, attribute, this.MessageDataConverter, this.TraceHelper, this.durableTaskOptions, this);
Expand Down
11 changes: 11 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,17 @@ internal virtual bool ConnectionNameMatches(DurabilityProvider durabilityProvide
return this.ConnectionName.Equals(durabilityProvider.ConnectionName);
}

/// <summary>
/// Returns true if two task hub names identify the same task hub for this durability provider.
/// </summary>
/// <param name="taskHubName">The first task hub name.</param>
/// <param name="otherTaskHubName">The second task hub name.</param>
/// <returns>A boolean indicating whether the task hub names match.</returns>
internal virtual bool TaskHubNameMatches(string taskHubName, string otherTaskHubName)
{
return string.Equals(taskHubName, otherTaskHubName, StringComparison.Ordinal);
}

/// <summary>
/// Tries to obtain a scale monitor for autoscaling.
/// </summary>
Expand Down
11 changes: 8 additions & 3 deletions src/WebJobs.Extensions.DurableTask/DurableClientAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,14 @@ public override int GetHashCode()
{
unchecked
{
return
this.TaskHub?.GetHashCode() ?? 0 +
this.ConnectionName?.GetHashCode() ?? 0;
int hashCode = 17;
hashCode = (hashCode * 31) + (this.TaskHub == null
? 0
: StringComparer.OrdinalIgnoreCase.GetHashCode(this.TaskHub));
hashCode = (hashCode * 31) + (this.ConnectionName == null
? 0
: StringComparer.OrdinalIgnoreCase.GetHashCode(this.ConnectionName));
return hashCode;
}
}

Expand Down
53 changes: 53 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurableClientCacheKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask
{
internal readonly struct DurableClientCacheKey : IEquatable<DurableClientCacheKey>
{
private readonly string taskHub;
private readonly string connectionName;
private readonly bool externalClient;
private readonly bool durableRequiresGrpc;

public DurableClientCacheKey(DurableClientAttribute attribute)
{
this.taskHub = attribute.TaskHub;
this.connectionName = attribute.ConnectionName;
this.externalClient = attribute.ExternalClient;
this.durableRequiresGrpc = attribute.DurableRequiresGrpc;
}

public bool Equals(DurableClientCacheKey other)
{
return string.Equals(this.taskHub, other.taskHub, StringComparison.Ordinal)
&& string.Equals(this.connectionName, other.connectionName, StringComparison.Ordinal)
&& this.externalClient == other.externalClient
&& this.durableRequiresGrpc == other.durableRequiresGrpc;
}

public override bool Equals(object obj)
{
return obj is DurableClientCacheKey other && this.Equals(other);
}

public override int GetHashCode()
{
unchecked
{
int hashCode = 17;
hashCode = (hashCode * 31) + (this.taskHub == null
? 0
: StringComparer.Ordinal.GetHashCode(this.taskHub));
hashCode = (hashCode * 31) + (this.connectionName == null
? 0
: StringComparer.Ordinal.GetHashCode(this.connectionName));
hashCode = (hashCode * 31) + this.externalClient.GetHashCode();
hashCode = (hashCode * 31) + this.durableRequiresGrpc.GetHashCode();
return hashCode;
}
}
}
}
14 changes: 7 additions & 7 deletions src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public class DurableTaskExtension :

internal static readonly string LoggerCategoryName = LogCategories.CreateTriggerCategory("DurableTask");

// Creating client objects is expensive, so we cache them when the attributes match.
// Note that DurableClientAttribute defines a custom equality comparer.
private readonly ConcurrentDictionary<DurableClientAttribute, DurableClient> cachedClients =
new ConcurrentDictionary<DurableClientAttribute, DurableClient>();
// Creating client objects is expensive, so we cache them when their semantic configuration matches.
private readonly ConcurrentDictionary<DurableClientCacheKey, DurableClient> cachedClients =
new ConcurrentDictionary<DurableClientCacheKey, DurableClient>();

private readonly ConcurrentDictionary<FunctionName, RegisteredFunctionInfo> knownOrchestrators =
new ConcurrentDictionary<FunctionName, RegisteredFunctionInfo>();
Expand Down Expand Up @@ -1379,12 +1378,13 @@ protected internal virtual IDurableClient GetClient(DurableClientAttribute attri
// We must ensure the TaskHubWorker exists so that we know we have started the appropriate server.
_ = this.EnsureTaskHubWorker();

var cacheKey = new DurableClientCacheKey(attribute);
DurableClient client = this.cachedClients.GetOrAdd(
attribute,
attr =>
cacheKey,
_ =>
{
DurabilityProvider innerClient = this.durabilityProviderFactory.GetDurabilityProvider(attribute);
return new DurableClient(innerClient, this, this.HttpApiHandler, attr);
return new DurableClient(innerClient, this, this.HttpApiHandler, attribute);
});

return client;
Expand Down
19 changes: 19 additions & 0 deletions test/FunctionsV2/FunctionApp/ClientFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public static async Task StartFunctionWithTaskHub(
instanceCreationTime);
}

// NOTE: The TestTaskHub and SecondaryStorage app setting names must exist for indexing.
[NoAutomaticTrigger]
public static async Task StartFunctionWithConnection(
[DurableClient(TaskHub = "%TestTaskHub%", ConnectionName = "SecondaryStorage")] IDurableClient client,
string functionName,
string instanceId,
object input,
TestDurableClient[] clientRef)
{
DateTime instanceCreationTime = DateTime.UtcNow;

instanceId = await client.StartNewAsync(functionName, instanceId, input);
clientRef[0] = new TestDurableClient(
client,
functionName,
instanceId,
instanceCreationTime);
}

[NoAutomaticTrigger]
public static void GetEntityClient(
[DurableClient] IDurableClient client,
Expand Down
14 changes: 14 additions & 0 deletions test/FunctionsV2/FunctionApp/CurrentAppDetectionFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests
{
internal static class CurrentAppDetectionFunctions
{
public static string TargetOnlyOrchestrator(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
return $"target:{context.GetInput<string>()}";
}
}
}
26 changes: 26 additions & 0 deletions test/FunctionsV2/FunctionApp/HostedClientCacheFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests
{
internal static class HostedClientCacheFunctions
{
[NoAutomaticTrigger]
public static void CaptureInternalClient(
string hub,
IDurableClient[] clients,
[DurableClient(TaskHub = "{hub}")] IDurableClient client)
{
clients[0] = client;
}

[NoAutomaticTrigger]
public static void CaptureExternalClient(
string hub,
IDurableClient[] clients,
[DurableClient(TaskHub = "{hub}", ExternalClient = true)] IDurableClient client)
{
clients[0] = client;
}
}
}
Loading
Loading