|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Linq; |
4 | 6 | using P = Microsoft.DurableTask.Protobuf; |
5 | 7 |
|
6 | 8 | namespace Microsoft.DurableTask.Client.Grpc; |
7 | 9 |
|
8 | 10 | /// <summary> |
9 | 11 | /// Protobuf helpers and utilities. |
10 | 12 | /// </summary> |
11 | | -static class ProtoUtils |
| 13 | +public static class ProtoUtils |
12 | 14 | { |
| 15 | + /// <summary> |
| 16 | + /// Gets the terminal orchestration statuses that are commonly used for deduplication. |
| 17 | + /// These are the statuses that can be used in OrchestrationIdReusePolicy. |
| 18 | + /// </summary> |
| 19 | + /// <returns>An immutable array of terminal orchestration statuses.</returns> |
| 20 | + public static ImmutableArray<P.OrchestrationStatus> GetTerminalStatuses() |
| 21 | + { |
| 22 | +#pragma warning disable CS0618 // Type or member is obsolete - Canceled is intentionally included for compatibility |
| 23 | + return ImmutableArray.Create( |
| 24 | + P.OrchestrationStatus.Completed, |
| 25 | + P.OrchestrationStatus.Failed, |
| 26 | + P.OrchestrationStatus.Terminated, |
| 27 | + P.OrchestrationStatus.Canceled); |
| 28 | +#pragma warning restore CS0618 |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Converts dedupe statuses (statuses that should NOT be replaced) to an OrchestrationIdReusePolicy |
| 33 | + /// with replaceable statuses (statuses that CAN be replaced). |
| 34 | + /// </summary> |
| 35 | + /// <param name="dedupeStatuses">The orchestration statuses that should NOT be replaced. These are statuses for which an exception should be thrown if an orchestration already exists.</param> |
| 36 | + /// <returns>An OrchestrationIdReusePolicy with replaceable statuses set, or null if all terminal statuses are dedupe statuses.</returns> |
| 37 | + /// <remarks> |
| 38 | + /// The policy uses "replaceableStatus" - these are statuses that CAN be replaced. |
| 39 | + /// dedupeStatuses are statuses that should NOT be replaced. |
| 40 | + /// So replaceableStatus = all terminal statuses MINUS dedupeStatuses. |
| 41 | + /// </remarks> |
| 42 | + public static P.OrchestrationIdReusePolicy? ConvertDedupeStatusesToReusePolicy( |
| 43 | + IEnumerable<P.OrchestrationStatus>? dedupeStatuses) |
| 44 | + { |
| 45 | + ImmutableArray<P.OrchestrationStatus> terminalStatuses = GetTerminalStatuses(); |
| 46 | + ImmutableHashSet<P.OrchestrationStatus> dedupeStatusSet = dedupeStatuses?.ToImmutableHashSet() ?? ImmutableHashSet<P.OrchestrationStatus>.Empty; |
| 47 | + |
| 48 | + P.OrchestrationIdReusePolicy policy = new(); |
| 49 | + |
| 50 | + // Add terminal statuses that are NOT in dedupeStatuses as replaceable |
| 51 | + foreach (P.OrchestrationStatus terminalStatus in terminalStatuses.Where(status => !dedupeStatusSet.Contains(status))) |
| 52 | + { |
| 53 | + policy.ReplaceableStatus.Add(terminalStatus); |
| 54 | + } |
| 55 | + |
| 56 | + // Only return policy if we have replaceable statuses |
| 57 | + return policy.ReplaceableStatus.Count > 0 ? policy : null; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Converts an OrchestrationIdReusePolicy with replaceable statuses to dedupe statuses |
| 62 | + /// (statuses that should NOT be replaced). |
| 63 | + /// </summary> |
| 64 | + /// <param name="policy">The OrchestrationIdReusePolicy containing replaceable statuses.</param> |
| 65 | + /// <returns>An array of orchestration statuses that should NOT be replaced, or null if all terminal statuses are replaceable.</returns> |
| 66 | + /// <remarks> |
| 67 | + /// The policy uses "replaceableStatus" - these are statuses that CAN be replaced. |
| 68 | + /// dedupeStatuses are statuses that should NOT be replaced (should throw exception). |
| 69 | + /// So dedupeStatuses = all terminal statuses MINUS replaceableStatus. |
| 70 | + /// </remarks> |
| 71 | + public static P.OrchestrationStatus[]? ConvertReusePolicyToDedupeStatuses( |
| 72 | + P.OrchestrationIdReusePolicy? policy) |
| 73 | + { |
| 74 | + if (policy == null || policy.ReplaceableStatus.Count == 0) |
| 75 | + { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + ImmutableArray<P.OrchestrationStatus> terminalStatuses = GetTerminalStatuses(); |
| 80 | + ImmutableHashSet<P.OrchestrationStatus> replaceableStatusSet = policy.ReplaceableStatus.ToImmutableHashSet(); |
| 81 | + |
| 82 | + // Calculate dedupe statuses = terminal statuses - replaceable statuses |
| 83 | + P.OrchestrationStatus[] dedupeStatuses = terminalStatuses |
| 84 | + .Where(terminalStatus => !replaceableStatusSet.Contains(terminalStatus)) |
| 85 | + .ToArray(); |
| 86 | + |
| 87 | + // Only return if there are dedupe statuses |
| 88 | + return dedupeStatuses.Length > 0 ? dedupeStatuses : null; |
| 89 | + } |
| 90 | + |
13 | 91 | #pragma warning disable 0618 // Referencing Obsolete member. This is intention as we are only converting it. |
14 | 92 | /// <summary> |
15 | 93 | /// Converts <see cref="OrchestrationRuntimeStatus" /> to <see cref="P.OrchestrationStatus" />. |
|
0 commit comments