-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGovernanceExecutionManager.cs
More file actions
168 lines (149 loc) · 7.08 KB
/
Copy pathGovernanceExecutionManager.cs
File metadata and controls
168 lines (149 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Abstractions.Engine;
using ModularityKit.Mutator.Abstractions.Results;
using ModularityKit.Mutator.Governance.Abstractions.Execution.Contracts;
using ModularityKit.Mutator.Governance.Abstractions.Execution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Contracts;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies;
using ModularityKit.Mutator.Governance.Abstractions.Storage;
using ModularityKit.Mutator.Governance.Runtime.Execution.Mutation;
using ModularityKit.Mutator.Governance.Runtime.Execution.Outcome;
using ModularityKit.Mutator.Governance.Runtime.Execution.Persistence;
namespace ModularityKit.Mutator.Governance.Runtime.Execution.Orchestration;
/// <summary>
/// Closes the loop from approved governance request to core mutation execution and terminal request state.
/// </summary>
public sealed class GovernanceExecutionManager(
IMutationRequestStore requestStore,
IMutationRequestVersionResolutionManager resolutionManager,
IMutationEngine mutationEngine) : IGovernanceExecutionManager
{
private readonly IMutationRequestVersionResolutionManager _resolutionManager = resolutionManager ?? throw new ArgumentNullException(nameof(resolutionManager));
private readonly IMutationEngine _mutationEngine = mutationEngine ?? throw new ArgumentNullException(nameof(mutationEngine));
private readonly GovernedExecutionOutcomeHandler _outcomeHandler =
new(new GovernedExecutionRequestPersistence(requestStore ?? throw new ArgumentNullException(nameof(requestStore))));
public async Task<GovernedExecutionResult<TState>> ExecuteApproved<TState>(
string requestId,
IMutation<TState> mutation,
TState currentState,
string currentStateVersion,
Func<TState, string> resultingStateVersionProvider,
MutationContext governanceContext,
VersionedRequestResolutionStrategy strategy,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(requestId))
throw new ArgumentException("Request ID is required.", nameof(requestId));
if (string.IsNullOrWhiteSpace(currentStateVersion))
throw new ArgumentException("Current state version is required.", nameof(currentStateVersion));
ArgumentNullException.ThrowIfNull(mutation);
ArgumentNullException.ThrowIfNull(resultingStateVersionProvider);
ArgumentNullException.ThrowIfNull(governanceContext);
var execution = await ResolveExecutionContext(
requestId,
mutation,
currentState,
currentStateVersion,
resultingStateVersionProvider,
governanceContext,
strategy,
cancellationToken).ConfigureAwait(false);
if (execution.Resolution.Outcome is MutationRequestVersionResolutionOutcome.RejectedAsStale or
MutationRequestVersionResolutionOutcome.RequiresRenewedApproval)
{
return _outcomeHandler.BuildNonExecutedResult<TState>(execution.Resolution);
}
MutationResult<TState> mutationResult;
try
{
mutationResult = await ExecuteMutation(execution, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
await _outcomeHandler.PersistRejectedExecution(
execution.Resolution.Request,
execution.GovernanceContext,
$"Governed execution threw '{ex.GetType().Name}': {ex.Message}",
GovernedExecutionFailureMetadataFactory.CreateExceptionMetadata(execution.CurrentStateVersion, ex),
cancellationToken).ConfigureAwait(false);
throw;
}
if (!mutationResult.IsSuccess || mutationResult.NewState is null)
{
var rejectedRequest = await _outcomeHandler.PersistRejectedExecution(
execution.Resolution.Request,
execution.GovernanceContext,
GovernedExecutionDecisionFactory.BuildRejectedExecutionReason(mutationResult),
GovernedExecutionFailureMetadataFactory.CreateRejectedExecutionMetadata(
execution.CurrentStateVersion,
mutationResult),
cancellationToken).ConfigureAwait(false);
return _outcomeHandler.BuildNonExecutedResult(
execution.Resolution with { Request = rejectedRequest },
mutationResult);
}
var resultingStateVersion = execution.ResultingStateVersionProvider(mutationResult.NewState);
if (string.IsNullOrWhiteSpace(resultingStateVersion))
throw new InvalidOperationException("Governed execution requires a non-empty resulting state version.");
var executedRequest = await _outcomeHandler.PersistExecutedRequest(
execution.Resolution.Request,
resultingStateVersion,
execution.GovernanceContext,
mutationResult,
cancellationToken).ConfigureAwait(false);
return _outcomeHandler.BuildExecutedResult(
execution.Resolution,
mutationResult,
executedRequest,
resultingStateVersion);
}
public Task<GovernedExecutionResult<TState>> ExecuteApproved<TState>(
string requestId,
IMutation<TState> mutation,
TState currentState,
MutationContext governanceContext,
VersionedRequestResolutionStrategy strategy,
CancellationToken cancellationToken = default)
where TState : IVersionedState
=> ExecuteApproved(
requestId,
mutation,
currentState,
currentState.Version,
state => state.Version,
governanceContext,
strategy,
cancellationToken);
private async Task<GovernedExecutionContext<TState>> ResolveExecutionContext<TState>(
string requestId,
IMutation<TState> mutation,
TState currentState,
string currentStateVersion,
Func<TState, string> resultingStateVersionProvider,
MutationContext governanceContext,
VersionedRequestResolutionStrategy strategy,
CancellationToken cancellationToken)
{
var resolution = await _resolutionManager.ResolveAndStore(
requestId,
currentStateVersion,
governanceContext,
strategy,
cancellationToken).ConfigureAwait(false);
return new GovernedExecutionContext<TState>(
resolution,
new GovernedMutation<TState>(mutation, resolution.Request),
currentState,
currentStateVersion,
resultingStateVersionProvider,
governanceContext);
}
private Task<MutationResult<TState>> ExecuteMutation<TState>(
GovernedExecutionContext<TState> execution,
CancellationToken cancellationToken)
=> _mutationEngine.ExecuteAsync(
execution.Mutation,
execution.CurrentState,
cancellationToken);
}