Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -176,9 +176,9 @@ 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?.ThrowIfOrchestratorFunctionIsMissingOrDisabled(orchestratorFunctionName);
}

if (string.IsNullOrEmpty(instanceId))
Expand Down Expand Up @@ -361,15 +361,21 @@ 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);
return string.Equals(client.TaskHubName, taskHubName, StringComparison.OrdinalIgnoreCase);
}

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 @@ -1257,6 +1263,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?.ThrowIfOrchestratorFunctionIsMissingOrDisabled(state.Name);
}

JToken input = ParseToJToken(state.Input);

string newInstanceId = null;
Expand Down
13 changes: 13 additions & 0 deletions src/WebJobs.Extensions.DurableTask/DurableTaskExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,19 @@ internal void ThrowIfFunctionDoesNotExist(string name, FunctionType functionType
}
}

internal void ThrowIfOrchestratorFunctionIsMissingOrDisabled(string name)
{
RegisteredFunctionInfo info = this.GetOrchestratorInfo(new FunctionName(name));

// A null registration means the function was indexed but disabled, so its listener was
// never created. IsDeregistered is deliberately allowed because it represents transient,
// instance-local listener shutdown; the durable start can run on another or restarted host.
if (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.ThrowIfOrchestratorFunctionIsMissingOrDisabled(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.ThrowIfOrchestratorFunctionIsMissingOrDisabled(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
26 changes: 26 additions & 0 deletions test/FunctionsV2/Tests/Unit/DisabledFunctionDispatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ public void GetObject_ActiveActivity_ReturnsRealActivityShim()
Assert.IsType<TaskActivityShim>(shim);
}

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

extension.ThrowIfOrchestratorFunctionIsMissingOrDisabled("ActiveOrchestrator");
}

[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
122 changes: 121 additions & 1 deletion test/FunctionsV2/Tests/Unit/DurableClientBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,122 @@ public async Task StartNewAsync_InvalidInstanceId_ThrowsException(string instanc
await Assert.ThrowsAnyAsync<ArgumentException>(async () => await durableClient.StartNewAsync("anyOrchestratorFunction", instanceId, new { message = "any obj" }));
}

[Theory]
[InlineData(null)]
[InlineData("durabletaskhub")]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task StartNewAsync_DisabledOrchestrator_ThrowsException(string taskHub)
{
var orchestrationServiceClientMock = new Mock<IOrchestrationServiceClient>();
orchestrationServiceClientMock
.Setup(x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()))
.Returns(Task.CompletedTask);
var storageProvider = new DurabilityProvider(
"test",
new Mock<IOrchestrationService>().Object,
orchestrationServiceClientMock.Object,
TestConstants.ConnectionName);
var durableExtension = GetDurableTaskConfig();
durableExtension.RegisterOrchestrator(new FunctionName("DisabledOrchestrator"), orchestratorInfo: null);
var durableClient = (IDurableOrchestrationClient)new DurableClient(
storageProvider,
durableExtension,
durableExtension.HttpApiHandler,
new DurableClientAttribute { TaskHub = taskHub });

ArgumentException exception = await Assert.ThrowsAnyAsync<ArgumentException>(
() => durableClient.StartNewAsync("DisabledOrchestrator"));

Assert.Contains("doesn't exist, is disabled, or is not an orchestrator function", exception.Message);
orchestrationServiceClientMock.Verify(
x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()),
Times.Never());
}

[Theory]
[InlineData(null)]
[InlineData("durabletaskhub")]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task RestartAsync_DisabledOrchestrator_ThrowsException(string taskHub)
{
const string InstanceId = "completed-instance";
const string FunctionName = "DisabledOrchestrator";
var orchestrationServiceClientMock = new Mock<IOrchestrationServiceClient>();
orchestrationServiceClientMock
.Setup(x => x.GetOrchestrationStateAsync(InstanceId, false))
.ReturnsAsync(
new List<OrchestrationState>
{
new OrchestrationState
{
Name = FunctionName,
Input = "null",
OrchestrationInstance = new OrchestrationInstance { InstanceId = InstanceId },
OrchestrationStatus = OrchestrationStatus.Completed,
},
});
orchestrationServiceClientMock
.Setup(x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()))
.Returns(Task.CompletedTask);
var storageProvider = new DurabilityProvider(
"test",
new Mock<IOrchestrationService>().Object,
orchestrationServiceClientMock.Object,
TestConstants.ConnectionName);
var durableExtension = GetDurableTaskConfig();
durableExtension.RegisterOrchestrator(new FunctionName(FunctionName), orchestratorInfo: null);
var durableClient = (IDurableOrchestrationClient)new DurableClient(
storageProvider,
durableExtension,
durableExtension.HttpApiHandler,
new DurableClientAttribute { TaskHub = taskHub });

ArgumentException exception = await Assert.ThrowsAnyAsync<ArgumentException>(
() => durableClient.RestartAsync(InstanceId, restartWithNewInstanceId: false));

Assert.Contains("doesn't exist, is disabled, or is not an orchestrator function", exception.Message);
orchestrationServiceClientMock.Verify(
x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()),
Times.Never());
}

[Fact]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
public async Task StartNewAsync_DeregisteredOrchestrator_SchedulesInstance()
{
const string FunctionName = "DrainingOrchestrator";
const string InstanceId = "new-instance";
var orchestrationServiceClientMock = new Mock<IOrchestrationServiceClient>();
orchestrationServiceClientMock
.Setup(x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()))
.Returns(Task.CompletedTask);
var storageProvider = new DurabilityProvider(
"test",
new Mock<IOrchestrationService>().Object,
orchestrationServiceClientMock.Object,
TestConstants.ConnectionName);
var durableExtension = GetDurableTaskConfig();
var functionName = new FunctionName(FunctionName);
durableExtension.RegisterOrchestrator(
functionName,
new RegisteredFunctionInfo(executor: null, isOutOfProc: true));
durableExtension.DeregisterOrchestrator(functionName);
var durableClient = (IDurableOrchestrationClient)new DurableClient(
storageProvider,
durableExtension,
durableExtension.HttpApiHandler,
new DurableClientAttribute { });

string actualInstanceId = await durableClient.StartNewAsync(
FunctionName,
InstanceId);

Assert.Equal(InstanceId, actualInstanceId);
orchestrationServiceClientMock.Verify(
x => x.CreateTaskOrchestrationAsync(It.IsAny<TaskMessage>(), It.IsAny<OrchestrationStatus[]>()),
Times.Once());
}

[Theory]
[Trait("Category", PlatformSpecificHelpers.TestCategory)]
[InlineData("@invalid")]
Expand All @@ -64,7 +180,11 @@ public async Task SignalEntity_InvalidEntityKey_ThrowsException(string entityKey
{
var orchestrationServiceClientMock = new Mock<IOrchestrationServiceClient>();
orchestrationServiceClientMock.Setup(x => x.GetOrchestrationStateAsync(It.IsAny<string>(), It.IsAny<bool>())).ReturnsAsync(GetInvalidInstanceState());
var storageProvider = new DurabilityProvider("test", new Mock<IOrchestrationService>().Object, orchestrationServiceClientMock.Object, "test");
var storageProvider = new DurabilityProvider(
"test",
new Mock<IOrchestrationService>().Object,
orchestrationServiceClientMock.Object,
TestConstants.ConnectionName);
var durableExtension = GetDurableTaskConfig();
var durableClient = (IDurableEntityClient)new DurableClient(storageProvider, durableExtension, durableExtension.HttpApiHandler, new DurableClientAttribute { });

Expand Down
Loading
Loading