-
Notifications
You must be signed in to change notification settings - Fork 54
Fix concurrent timer race condition in InMemoryOrchestrationService #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.DurableTask; | ||
| using Microsoft.DurableTask.Client; | ||
| using Microsoft.DurableTask.Testing; | ||
| using Microsoft.DurableTask.Worker; | ||
| using Xunit; | ||
|
|
||
| namespace InProcessTestHost.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Tests to verify that multiple orchestrations with identical timer FireAt timestamps | ||
| /// all complete correctly without any being dropped. | ||
| /// </summary> | ||
| public class ConcurrentTimerTests | ||
| { | ||
| [Fact] | ||
| // Test that multiple orchestrations with the same timer that fire at the same time | ||
| // can all complete correctly. | ||
| public async Task MultipleOrchestrations_WithSameTimerFireAt_AllComplete() | ||
| { | ||
| const int orchestrationCount = 10; | ||
| const string orchestratorName = "TimerOrchestrator"; | ||
|
|
||
| await using DurableTaskTestHost host = await DurableTaskTestHost.StartAsync(tasks => | ||
| { | ||
| tasks.AddOrchestratorFunc<DateTime, string>(orchestratorName, async (ctx, fireAt) => | ||
| { | ||
| await ctx.CreateTimer(fireAt, CancellationToken.None); | ||
| return $"done:{ctx.InstanceId}"; | ||
| }); | ||
| }); | ||
|
|
||
| DateTime sharedFireAt = DateTime.UtcNow.AddSeconds(5); | ||
|
|
||
| string[] instanceIds = new string[orchestrationCount]; | ||
| for (int i = 0; i < orchestrationCount; i++) | ||
| { | ||
| instanceIds[i] = await host.Client.ScheduleNewOrchestrationInstanceAsync( | ||
| orchestratorName, sharedFireAt); | ||
| } | ||
|
|
||
| using CancellationTokenSource cts = new(TimeSpan.FromSeconds(30)); | ||
|
|
||
| Task<OrchestrationMetadata>[] waitTasks = instanceIds | ||
| .Select(id => host.Client.WaitForInstanceCompletionAsync( | ||
| id, getInputsAndOutputs: true, cts.Token)) | ||
| .ToArray(); | ||
|
|
||
| OrchestrationMetadata[] results = await Task.WhenAll(waitTasks); | ||
|
|
||
| for (int i = 0; i < orchestrationCount; i++) | ||
| { | ||
| Assert.NotNull(results[i]); | ||
| Assert.Equal(OrchestrationRuntimeStatus.Completed, results[i].RuntimeStatus); | ||
| string output = results[i].ReadOutputAs<string>()!; | ||
| Assert.Equal($"done:{instanceIds[i]}", output); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| // Test that fan-out sub-orchestrations with timers that all fire at the same time | ||
| // can all complete correctly. | ||
| public async Task SubOrchestrations_WithIdenticalTimers_AllComplete() | ||
nytian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| const int subOrchestrationCount = 10; | ||
| const string parentName = "ParentOrchestrator"; | ||
| const string childName = "ChildTimerOrchestrator"; | ||
|
|
||
| await using DurableTaskTestHost host = await DurableTaskTestHost.StartAsync(tasks => | ||
| { | ||
| tasks.AddOrchestratorFunc<int>(parentName, async ctx => | ||
| { | ||
| DateTime sharedFireAt = ctx.CurrentUtcDateTime.AddSeconds(2); | ||
|
|
||
| // A parent orchestration will schedule 10 sub-orchestrations which has a timer | ||
| // fires at the same time. | ||
| Task<string>[] childTasks = Enumerable.Range(0, subOrchestrationCount) | ||
| .Select(i => ctx.CallSubOrchestratorAsync<string>(childName, sharedFireAt)) | ||
| .ToArray(); | ||
|
|
||
| string[] results = await Task.WhenAll(childTasks); | ||
| return results.Length; | ||
| }); | ||
|
|
||
| tasks.AddOrchestratorFunc<DateTime, string>(childName, async (ctx, fireAt) => | ||
| { | ||
| await ctx.CreateTimer(fireAt, CancellationToken.None); | ||
| return $"child-done:{ctx.InstanceId}"; | ||
| }); | ||
| }); | ||
|
|
||
| string instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync(parentName); | ||
|
|
||
| using CancellationTokenSource cts = new(TimeSpan.FromSeconds(60)); | ||
| OrchestrationMetadata metadata = await host.Client.WaitForInstanceCompletionAsync( | ||
| instanceId, getInputsAndOutputs: true, cts.Token); | ||
|
|
||
| Assert.NotNull(metadata); | ||
| Assert.Equal(OrchestrationRuntimeStatus.Completed, metadata.RuntimeStatus); | ||
| Assert.Equal(subOrchestrationCount, metadata.ReadOutputAs<int>()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.