|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.microsoft.durabletask; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents work item filters for a Durable Task worker. These filters are passed to the backend |
| 11 | + * and only work items matching the filters will be processed by the worker. If no filters are provided, |
| 12 | + * the worker will process all work items. |
| 13 | + * <p> |
| 14 | + * Work item filtering can improve efficiency in multi-worker deployments by ensuring each worker |
| 15 | + * only receives work items it can handle. However, if an orchestration calls a task type |
| 16 | + * (e.g., an activity or sub-orchestrator) that is not registered with any connected worker, |
| 17 | + * the call may hang indefinitely instead of failing with an error. |
| 18 | + * <p> |
| 19 | + * Use {@link DurableTaskGrpcWorkerBuilder#useWorkItemFilters(WorkItemFilter)} to provide explicit filters, |
| 20 | + * or {@link DurableTaskGrpcWorkerBuilder#useWorkItemFilters()} to auto-generate filters from the |
| 21 | + * registered orchestrations and activities. |
| 22 | + */ |
| 23 | +public final class WorkItemFilter { |
| 24 | + |
| 25 | + private final List<OrchestrationFilter> orchestrations; |
| 26 | + private final List<ActivityFilter> activities; |
| 27 | + |
| 28 | + private WorkItemFilter(List<OrchestrationFilter> orchestrations, List<ActivityFilter> activities) { |
| 29 | + this.orchestrations = Collections.unmodifiableList(new ArrayList<OrchestrationFilter>(orchestrations)); |
| 30 | + this.activities = Collections.unmodifiableList(new ArrayList<ActivityFilter>(activities)); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the orchestration filters. |
| 35 | + * |
| 36 | + * @return an unmodifiable list of orchestration filters |
| 37 | + */ |
| 38 | + public List<OrchestrationFilter> getOrchestrations() { |
| 39 | + return this.orchestrations; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the activity filters. |
| 44 | + * |
| 45 | + * @return an unmodifiable list of activity filters |
| 46 | + */ |
| 47 | + public List<ActivityFilter> getActivities() { |
| 48 | + return this.activities; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new {@link Builder} for constructing {@link WorkItemFilter} instances. |
| 53 | + * |
| 54 | + * @return a new builder |
| 55 | + */ |
| 56 | + public static Builder newBuilder() { |
| 57 | + return new Builder(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Builder for constructing {@link WorkItemFilter} instances. |
| 62 | + */ |
| 63 | + public static final class Builder { |
| 64 | + private final List<OrchestrationFilter> orchestrations = new ArrayList<OrchestrationFilter>(); |
| 65 | + private final List<ActivityFilter> activities = new ArrayList<ActivityFilter>(); |
| 66 | + |
| 67 | + Builder() { |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Adds an orchestration filter with the specified name and no version constraint. |
| 72 | + * |
| 73 | + * @param name the orchestration name to filter on |
| 74 | + * @return this builder |
| 75 | + */ |
| 76 | + public Builder addOrchestration(String name) { |
| 77 | + if (name == null || name.isEmpty()) { |
| 78 | + throw new IllegalArgumentException("Orchestration filter name must not be null or empty."); |
| 79 | + } |
| 80 | + this.orchestrations.add(new OrchestrationFilter(name, Collections.<String>emptyList())); |
| 81 | + return this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Adds an orchestration filter with the specified name and versions. |
| 86 | + * |
| 87 | + * @param name the orchestration name to filter on |
| 88 | + * @param versions the versions to filter on |
| 89 | + * @return this builder |
| 90 | + */ |
| 91 | + public Builder addOrchestration(String name, List<String> versions) { |
| 92 | + if (name == null || name.isEmpty()) { |
| 93 | + throw new IllegalArgumentException("Orchestration filter name must not be null or empty."); |
| 94 | + } |
| 95 | + List<String> versionsCopy = versions != null |
| 96 | + ? Collections.unmodifiableList(new ArrayList<String>(versions)) |
| 97 | + : Collections.<String>emptyList(); |
| 98 | + this.orchestrations.add(new OrchestrationFilter(name, versionsCopy)); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Adds an activity filter with the specified name and no version constraint. |
| 104 | + * |
| 105 | + * @param name the activity name to filter on |
| 106 | + * @return this builder |
| 107 | + */ |
| 108 | + public Builder addActivity(String name) { |
| 109 | + if (name == null || name.isEmpty()) { |
| 110 | + throw new IllegalArgumentException("Activity filter name must not be null or empty."); |
| 111 | + } |
| 112 | + this.activities.add(new ActivityFilter(name, Collections.<String>emptyList())); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Adds an activity filter with the specified name and versions. |
| 118 | + * |
| 119 | + * @param name the activity name to filter on |
| 120 | + * @param versions the versions to filter on |
| 121 | + * @return this builder |
| 122 | + */ |
| 123 | + public Builder addActivity(String name, List<String> versions) { |
| 124 | + if (name == null || name.isEmpty()) { |
| 125 | + throw new IllegalArgumentException("Activity filter name must not be null or empty."); |
| 126 | + } |
| 127 | + List<String> versionsCopy = versions != null |
| 128 | + ? Collections.unmodifiableList(new ArrayList<String>(versions)) |
| 129 | + : Collections.<String>emptyList(); |
| 130 | + this.activities.add(new ActivityFilter(name, versionsCopy)); |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Builds a new {@link WorkItemFilter} from the configured filters. |
| 136 | + * |
| 137 | + * @return a new {@link WorkItemFilter} instance |
| 138 | + */ |
| 139 | + public WorkItemFilter build() { |
| 140 | + return new WorkItemFilter(this.orchestrations, this.activities); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Specifies an orchestration filter with a name and optional versions. |
| 146 | + */ |
| 147 | + public static final class OrchestrationFilter { |
| 148 | + private final String name; |
| 149 | + private final List<String> versions; |
| 150 | + |
| 151 | + OrchestrationFilter(String name, List<String> versions) { |
| 152 | + this.name = name; |
| 153 | + this.versions = versions; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Gets the name of the orchestration to filter. |
| 158 | + * |
| 159 | + * @return the orchestration name |
| 160 | + */ |
| 161 | + public String getName() { |
| 162 | + return this.name; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Gets the versions of the orchestration to filter. |
| 167 | + * |
| 168 | + * @return an unmodifiable list of versions, or an empty list if no version constraint |
| 169 | + */ |
| 170 | + public List<String> getVersions() { |
| 171 | + return this.versions; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Specifies an activity filter with a name and optional versions. |
| 177 | + */ |
| 178 | + public static final class ActivityFilter { |
| 179 | + private final String name; |
| 180 | + private final List<String> versions; |
| 181 | + |
| 182 | + ActivityFilter(String name, List<String> versions) { |
| 183 | + this.name = name; |
| 184 | + this.versions = versions; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Gets the name of the activity to filter. |
| 189 | + * |
| 190 | + * @return the activity name |
| 191 | + */ |
| 192 | + public String getName() { |
| 193 | + return this.name; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Gets the versions of the activity to filter. |
| 198 | + * |
| 199 | + * @return an unmodifiable list of versions, or an empty list if no version constraint |
| 200 | + */ |
| 201 | + public List<String> getVersions() { |
| 202 | + return this.versions; |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments