-
Notifications
You must be signed in to change notification settings - Fork 56
Validate UseWorkItemFilters names against registered tasks at worker build time #719
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
YunchuWang
merged 6 commits into
microsoft:main
from
YunchuWang:validate-workitem-filters-against-registry
May 6, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e745af
Validate UseWorkItemFilters names against registered tasks at worker …
Copilot 0e71490
Address PR review: idempotent validator registration, explicit TaskNa…
Copilot 9714c64
Simplify validator: single global instance keyed on Validate's name p…
Copilot 5c401a2
Address PR review: gate validator registration and improve default-na…
Copilot 5447f98
Address remaining PR review comments
YunchuWang 0d58458
Validator: return Skip for empty filters in DurableTaskWorkerWorkItem…
Copilot 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
111 changes: 111 additions & 0 deletions
111
src/Worker/Core/DependencyInjection/DurableTaskWorkerWorkItemFiltersValidator.cs
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,111 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Microsoft.DurableTask.Worker; | ||
|
|
||
| /// <summary> | ||
| /// Validates that every name configured on <see cref="DurableTaskWorkerWorkItemFilters"/> matches a | ||
| /// task registered with the corresponding named worker's <see cref="DurableTaskRegistry"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Registered as a single global <see cref="IValidateOptions{TOptions}"/> via | ||
| /// <see cref="DurableTaskWorkerBuilderExtensions.UseWorkItemFilters(IDurableTaskWorkerBuilder, DurableTaskWorkerWorkItemFilters?)"/>. | ||
| /// The Options framework dispatches the named-options name to <see cref="Validate(string?, DurableTaskWorkerWorkItemFilters)"/>, | ||
| /// which is then used to resolve the matching <see cref="DurableTaskRegistry"/>. Validation runs | ||
| /// lazily when <see cref="IOptionsMonitor{TOptions}.Get(string?)"/> is first called for a worker | ||
| /// (effectively at worker construction), so callers do not need to invoke validation explicitly. | ||
| /// </remarks> | ||
| sealed class DurableTaskWorkerWorkItemFiltersValidator : IValidateOptions<DurableTaskWorkerWorkItemFilters> | ||
| { | ||
| readonly IOptionsMonitor<DurableTaskRegistry> registryMonitor; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DurableTaskWorkerWorkItemFiltersValidator"/> class. | ||
| /// </summary> | ||
| /// <param name="registryMonitor">The monitor used to resolve the worker's <see cref="DurableTaskRegistry"/> at validation time.</param> | ||
| public DurableTaskWorkerWorkItemFiltersValidator(IOptionsMonitor<DurableTaskRegistry> registryMonitor) | ||
| { | ||
| this.registryMonitor = Check.NotNull(registryMonitor); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public ValidateOptionsResult Validate(string? name, DurableTaskWorkerWorkItemFilters options) | ||
| { | ||
| Check.NotNull(options); | ||
|
|
||
| // The validator is registered globally, so the Options framework dispatches every named | ||
| // worker's filter options through it -- including workers that never opted into filtering | ||
| // and therefore have no filter entries to validate. Skip those cases so the validator only | ||
| // reports a verdict for workers that actually configured filters. | ||
| if (options.Orchestrations.Count == 0 | ||
| && options.Activities.Count == 0 | ||
| && options.Entities.Count == 0) | ||
| { | ||
| return ValidateOptionsResult.Skip; | ||
| } | ||
|
|
||
| DurableTaskRegistry registry = this.registryMonitor.Get(name); | ||
|
|
||
| List<string> unknownOrchestrations = FindUnknown( | ||
| options.Orchestrations.Select(o => o.Name), n => registry.Orchestrators.ContainsKey(n)); | ||
| List<string> unknownActivities = FindUnknown( | ||
| options.Activities.Select(a => a.Name), n => registry.Activities.ContainsKey(n)); | ||
| List<string> unknownEntities = FindUnknown( | ||
| options.Entities.Select(e => e.Name), n => registry.Entities.ContainsKey(n)); | ||
|
|
||
| if (unknownOrchestrations.Count == 0 | ||
| && unknownActivities.Count == 0 | ||
| && unknownEntities.Count == 0) | ||
| { | ||
| return ValidateOptionsResult.Success; | ||
| } | ||
|
|
||
| StringBuilder sb = new(); | ||
| string displayName = string.IsNullOrEmpty(name) ? "<default>" : name!; | ||
| sb.Append("Cannot configure work item filters for worker '").Append(displayName) | ||
| .Append("': the following filter names do not match any registered task. ") | ||
| .Append("Register them on the worker (via AddTasks/AddOrchestrator/AddActivity/AddEntity) ") | ||
| .Append("or remove them from the filters."); | ||
| AppendCategory(sb, "Orchestrations", unknownOrchestrations); | ||
| AppendCategory(sb, "Activities", unknownActivities); | ||
| AppendCategory(sb, "Entities", unknownEntities); | ||
|
|
||
| return ValidateOptionsResult.Fail(sb.ToString()); | ||
| } | ||
|
|
||
| static List<string> FindUnknown(IEnumerable<string> names, Func<TaskName, bool> isRegistered) | ||
| { | ||
| List<string> unknown = []; | ||
| foreach (string name in names) | ||
| { | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| unknown.Add("<empty>"); | ||
| continue; | ||
| } | ||
|
|
||
| // TaskName equality is OrdinalIgnoreCase, mirroring how registered keys are compared. | ||
| // Construct the TaskName explicitly so the conversion is not dependent on the implicit | ||
| // string -> TaskName operator (which could be removed/changed independently). | ||
| if (!isRegistered(new TaskName(name))) | ||
| { | ||
| unknown.Add(name); | ||
| } | ||
| } | ||
|
|
||
| return unknown; | ||
| } | ||
|
YunchuWang marked this conversation as resolved.
|
||
|
|
||
| static void AppendCategory(StringBuilder sb, string category, List<string> unknown) | ||
| { | ||
| if (unknown.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| sb.Append(' ').Append(category).Append(": [").Append(string.Join(", ", unknown)).Append(']'); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.