Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ internal class DurableClient : IDurableClient,
#pragma warning restore 618
{
private const int MaxInstanceIdLength = 256;
private const string AzureStorageProviderName = "Azure Storage";
private const string AzureManagedProviderName = "azureManaged";

private static readonly JValue NullJValue = JValue.CreateNull();
private static readonly OrchestrationRuntimeStatus[] RunningStatus
Expand Down Expand Up @@ -176,9 +178,10 @@ async Task<IActionResult> IDurableOrchestrationClient.WaitForCompletionOrCreateC
/// <inheritdoc />
async Task<string> IDurableOrchestrationClient.StartNewAsync<T>(string orchestratorFunctionName, string instanceId, T input)
{
if (this.ClientReferencesCurrentApp(this))
if (this.ReferencesCurrentApp())
{
this.config?.ThrowIfFunctionDoesNotExist(orchestratorFunctionName, FunctionType.Orchestrator);
this.config?.ThrowIfOrchestratorFunctionIsDisabled(orchestratorFunctionName);
}

if (string.IsNullOrEmpty(instanceId))
Expand Down Expand Up @@ -361,15 +364,31 @@ private bool ClientReferencesCurrentApp(DurableClient client)
this.ConnectionNameMatchesCurrentApp(client);
}

internal bool ReferencesCurrentApp()
{
return this.ClientReferencesCurrentApp(this);
}

private bool TaskHubMatchesCurrentApp(DurableClient client)
{
var taskHubName = this.durableTaskOptions.HubName;
return client.TaskHubName.Equals(taskHubName);
StringComparison comparison = UsesCaseInsensitiveTaskHubNames(this.config?.DefaultDurabilityProvider)
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
return string.Equals(client.TaskHubName, taskHubName, comparison);
}

private static bool UsesCaseInsensitiveTaskHubNames(DurabilityProvider provider)
{
return provider != null &&
(string.Equals(provider.Name, AzureStorageProviderName, StringComparison.OrdinalIgnoreCase) ||
string.Equals(provider.Name, AzureManagedProviderName, StringComparison.OrdinalIgnoreCase));
Comment thread
bachuv marked this conversation as resolved.
Resolved
}

private bool ConnectionNameMatchesCurrentApp(DurableClient client)
{
return this.DurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
return this.config != null &&
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
this.config.DefaultDurabilityProvider.ConnectionNameMatches(client.DurabilityProvider);
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
}

/// <inheritdoc />
Expand Down Expand Up @@ -1262,6 +1281,11 @@ async Task<string> IDurableOrchestrationClient.RestartAsync(string instanceId, b
// GetOrchestrationInstanceStateAsync will throw ArgumentException if the provided instanceid is not found.
OrchestrationState state = await this.GetOrchestrationInstanceStateAsync(instanceId);

if (this.ReferencesCurrentApp())
{
this.config?.ThrowIfOrchestratorFunctionIsDisabled(state.Name);
}

JToken input = ParseToJToken(state.Input);

string newInstanceId = null;
Expand Down
2 changes: 2 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurabilityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public DurabilityProvider(string storageProviderName, IOrchestrationService serv
/// </summary>
public virtual string ConnectionName => this.connectionName;

internal string Name => this.name;

/// <summary>
/// Specifies whether the durability provider supports Durable Entities.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,18 @@ internal void ThrowIfFunctionDoesNotExist(string name, FunctionType functionType
}
}

internal void ThrowIfOrchestratorFunctionIsDisabled(string name)
{
// A null registration means the function was indexed but disabled, so its listener was
// never created. A missing registration and IsDeregistered are deliberately allowed:
// the target may exist in another app, or its listener may be restarting on this host.
Comment thread
YunchuWang marked this conversation as resolved.
Resolved
if (this.knownOrchestrators.TryGetValue(new FunctionName(name), out RegisteredFunctionInfo info) &&
info == null)
{
throw new OrchestratorFunctionUnavailableException(this.GetInvalidOrchestratorFunctionMessage(name));
}
}

private static bool IsDurableHttpTask(string functionName)
{
return string.Equals(functionName, HttpOptions.HttpTaskActivityReservedName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 sealed class OrchestratorFunctionUnavailableException : ArgumentException
{
internal OrchestratorFunctionUnavailableException(string message)
: base(message)
{
}
}
}
9 changes: 9 additions & 0 deletions src/WebJobs.Extensions.DurableTask/HttpApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ private async Task<HttpResponseMessage> HandleStartOrchestratorRequestAsync(

if (client is DurableClient durableClient)
{
if (durableClient.ReferencesCurrentApp())
{
this.config.ThrowIfOrchestratorFunctionIsDisabled(functionName);
}

var instance = new OrchestrationInstance
{
InstanceId = id,
Expand Down Expand Up @@ -1100,6 +1105,10 @@ private async Task<HttpResponseMessage> HandleRestartInstanceRequestAsync(
return client.CreateCheckStatusResponse(request, newInstanceId);
}
}
catch (OrchestratorFunctionUnavailableException e)
{
return request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message, e);
}
catch (ArgumentException e)
{
return request.CreateErrorResponse(HttpStatusCode.BadRequest, "InstanceId does not match a valid orchestration instance.", e);
Expand Down
9 changes: 9 additions & 0 deletions src/WebJobs.Extensions.DurableTask/TaskHubGrpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public override Task<Empty> Hello(Empty request, ServerCallContext context)
{
try
{
if (this.GetClient(context) is DurableClient durableClient && durableClient.ReferencesCurrentApp())
{
this.extension.ThrowIfOrchestratorFunctionIsDisabled(request.Name);
}

List<OrchestrationStatus> allStatuses = System.Enum
.GetValues<OrchestrationStatus>()
.ToList();
Expand Down Expand Up @@ -521,6 +526,10 @@ await this.GetDurabilityProvider(context).CreateTaskOrchestrationAsync(
string newInstanceId = await this.GetClient(context).RestartAsync(request.InstanceId, request.RestartWithNewInstanceId);
return new P.RestartInstanceResponse { InstanceId = newInstanceId };
}
catch (OrchestratorFunctionUnavailableException ex)
{
throw new RpcException(new Status(StatusCode.InvalidArgument, ex.Message));
}
catch (ArgumentException ex)
{
// Thrown when th instanceId is not found.
Expand Down
6 changes: 6 additions & 0 deletions test/FunctionsV2/FunctionApp/TestOrchestrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ internal static class TestOrchestrations

public static bool SayHelloWithActivityForRewindShouldFail { get; set; } = true;

[Disable]
public static string DisabledOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext ctx)
{
return nameof(DisabledOrchestrator);
}

public static string SayHelloInline([OrchestrationTrigger] IDurableOrchestrationContext ctx)
{
string input = ctx.GetInput<string>();
Expand Down
48 changes: 48 additions & 0 deletions test/FunctionsV2/Tests/E2E/StatusAndQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,54 @@ public async Task StartOrchestration_OnUnregisteredOrchestrator(bool extendedSes
}
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task DisabledOrchestrator_CaseVariantTaskHub_IsRejectedWithoutPersistingInstance()
{
const string InstanceId = "disabled-case-variant-instance";
string taskHubName = TestHelpers.GetTaskHubNameFromTestName(
nameof(this.DisabledOrchestrator_CaseVariantTaskHub_IsRejectedWithoutPersistingInstance),
enableExtendedSessions: false).ToLowerInvariant();
string hostTaskHubName = char.ToUpperInvariant(taskHubName[0]) + taskHubName.Substring(1);
var nameResolver = new SimpleNameResolver(
new Dictionary<string, string>
{
{ "TestTaskHub", taskHubName },
});

using (ITestHost host = TestHelpers.GetJobHost(
this.loggerProvider,
nameof(this.DisabledOrchestrator_CaseVariantTaskHub_IsRejectedWithoutPersistingInstance),
enableExtendedSessions: false,
nameResolver: nameResolver,
storageProviderType: TestHelpers.AzureStorageProviderType,
exactTaskHubName: hostTaskHubName))
{
await host.StartAsync();

Exception exception = await Record.ExceptionAsync(
() => host.StartOrchestratorAsync(
nameof(TestOrchestrations.DisabledOrchestrator),
input: null,
this.output,
instanceId: InstanceId,
useTaskHubFromAppSettings: true));

IDurableOrchestrationClient defaultClient =
await host.GetOrchestrationClientBindingTest(this.output);
Assert.Null(await defaultClient.GetStatusAsync(InstanceId));

FunctionInvocationException invocationException =
Assert.IsType<FunctionInvocationException>(exception);
Assert.NotNull(invocationException.InnerException);
Assert.Contains(
"doesn't exist, is disabled, or is not an orchestrator function",
invocationException.InnerException.ToString());

await host.StopAsync();
}
}

/// <summary>
/// End-to-end test which creates an external client that calls a non-existent orchestrator function.
/// </summary>
Expand Down
35 changes: 35 additions & 0 deletions test/FunctionsV2/Tests/Unit/DisabledFunctionDispatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ public void GetObject_ActiveActivity_ReturnsRealActivityShim()
Assert.IsType<TaskActivityShim>(shim);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void ThrowIfOrchestratorFunctionIsDisabled_ActiveOutOfProcOrchestrator_DoesNotThrow()
{
var extension = CreateExtension();
var mockExecutor = new Mock<ITriggeredFunctionExecutor>();
extension.RegisterOrchestrator(
new FunctionName("ActiveOrchestrator"),
new RegisteredFunctionInfo(mockExecutor.Object, isOutOfProc: true));

extension.ThrowIfOrchestratorFunctionIsDisabled("ActiveOrchestrator");
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void ThrowIfOrchestratorFunctionIsDisabled_MissingOrchestrator_DoesNotThrow()
{
var extension = CreateExtension();

extension.ThrowIfOrchestratorFunctionIsDisabled("MissingOrchestrator");
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void ThrowIfFunctionDoesNotExist_DisabledOrchestrator_DoesNotBreakReplay()
{
// Deterministic orchestration calls use this helper while replaying history. A disabled
// orchestrator remains a known function, so availability checks belong only at new-start
// entry points and must not make existing sub-orchestration history fail to replay.
var extension = CreateExtension();
extension.RegisterOrchestrator(new FunctionName("DisabledOrchestrator"), orchestratorInfo: null);

extension.ThrowIfFunctionDoesNotExist("DisabledOrchestrator", FunctionType.Orchestrator);
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public void DisabledEntity_IsTreatedAsUnavailableByClassicDispatch()
Expand Down
Loading
Loading