-
Notifications
You must be signed in to change notification settings - Fork 7
/
WorkflowRunMessageProcessor.cs
244 lines (213 loc) · 10.8 KB
/
WorkflowRunMessageProcessor.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using NuGet.Insights.Worker.KustoIngestion;
namespace NuGet.Insights.Worker.Workflow
{
public class WorkflowRunMessageProcessor : IMessageProcessor<WorkflowRunMessage>
{
private static readonly IReadOnlyList<WorkflowStateTransition> Transitions = new[]
{
new WorkflowStateTransition(
CurrentState: WorkflowRunState.Created,
IsIncompleteAsync: self => Task.FromResult(false),
TransitionAsync: async (self, run) =>
{
await self._workflowService.StartCatalogScansAsync();
return WorkflowRunState.CatalogScanWorking;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.CatalogScanWorking,
IsIncompleteAsync: self => self._workflowService.AreCatalogScansRunningAsync(),
TransitionAsync: async (self, run) =>
{
await self._workflowService.StartCleanupOrphanRecordsAsync();
return WorkflowRunState.CleanupOrphanRecordsWorking;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.CleanupOrphanRecordsWorking,
IsIncompleteAsync: self => self._workflowService.AreCleanupOrphanRecordsRunningAsync(),
TransitionAsync: async (self, run) =>
{
await self._workflowService.StartTimedReprocessAsync();
return WorkflowRunState.TimedReprocessWorking;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.TimedReprocessWorking,
IsIncompleteAsync: self => self._workflowService.IsTimedReprocessRunningAsync(),
TransitionAsync: async (self, run) =>
{
await self._workflowService.StartAuxiliaryFilesAsync();
return WorkflowRunState.AuxiliaryFilesWorking;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.AuxiliaryFilesWorking,
IsIncompleteAsync: self => self._workflowService.AreAuxiliaryFilesRunningAsync(),
TransitionAsync: async (self, run) =>
{
await self._workflowService.StartKustoIngestionAsync();
return WorkflowRunState.KustoIngestionWorking;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.KustoIngestionWorking,
IsIncompleteAsync: self => self._workflowService.IsKustoIngestionRunningAsync(),
TransitionAsync: async (self, run) =>
{
var latestState = await self._kustoIngestionStorageService.GetLatestStateAsync();
if (latestState.HasValue)
{
if (latestState == KustoIngestionState.FailedValidation)
{
if (run.AttemptCount >= self._options.Value.WorkflowMaxAttempts)
{
throw new InvalidOperationException($"The workflow could not complete due to Kusto {latestState} state after {run.AttemptCount} attempts.");
}
self._logger.LogWarning("Retrying the entire workflow due to Kusto {latestState} state.", latestState);
run.AttemptCount++;
return WorkflowRunState.Created;
}
else if (latestState != KustoIngestionState.Complete)
{
throw new InvalidOperationException("Unexpected workflow state: " + latestState);
}
}
return WorkflowRunState.Finalizing;
}),
new WorkflowStateTransition(
CurrentState: WorkflowRunState.Finalizing,
IsIncompleteAsync: self => Task.FromResult(false),
TransitionAsync: async (self, run) =>
{
await self.FinalizeAsync(run);
return WorkflowRunState.Complete;
}),
};
private async Task FinalizeAsync(WorkflowRun run)
{
await _storageService.DeleteOldRunsAsync(run.RunId);
_logger.LogInformation("The workflow is complete.");
await EmitCsvBlobMetricsAsync();
run.Completed = DateTimeOffset.UtcNow;
}
private async Task EmitCsvBlobMetricsAsync()
{
var countMetric = _telemetryClient.GetMetric(MetricNames.CsvBlobCount, "ContainerName");
var recordCountMetric = _telemetryClient.GetMetric(MetricNames.CsvBlobRecordCount, "ContainerName");
var compressedSizeMetric = _telemetryClient.GetMetric(MetricNames.CsvBlobCompressedSize, "ContainerName");
var uncompressedSizeMetric = _telemetryClient.GetMetric(MetricNames.CsvBlobUncompressedSize, "ContainerName");
foreach (var containerName in _csvRecordContainers.ContainerNames)
{
var blobs = await _csvRecordContainers.GetBlobsAsync(containerName);
countMetric.TrackValue(blobs.Count, containerName);
foreach (var blob in blobs)
{
if (blob.RecordCount.HasValue)
{
recordCountMetric.TrackValue(blob.RecordCount.Value, containerName);
}
compressedSizeMetric.TrackValue(blob.CompressedSizeBytes, containerName);
if (blob.RawSizeBytes.HasValue)
{
uncompressedSizeMetric.TrackValue(blob.RawSizeBytes.Value, containerName);
}
}
}
}
private static readonly IReadOnlyDictionary<WorkflowRunState, WorkflowStateTransition> CurrentStateToTransition = Transitions
.ToDictionary(x => x.CurrentState);
private readonly WorkflowService _workflowService;
private readonly WorkflowStorageService _storageService;
private readonly CsvRecordContainers _csvRecordContainers;
private readonly KustoIngestionStorageService _kustoIngestionStorageService;
private readonly AutoRenewingStorageLeaseService _leaseService;
private readonly IMessageEnqueuer _messageEnqueuer;
private readonly ITelemetryClient _telemetryClient;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
private readonly ILogger<WorkflowRunMessageProcessor> _logger;
public WorkflowRunMessageProcessor(
WorkflowService workflowService,
WorkflowStorageService storageService,
CsvRecordContainers csvRecordContainers,
KustoIngestionStorageService kustoIngestionStorageService,
AutoRenewingStorageLeaseService leaseService,
IMessageEnqueuer messageEnqueuer,
ITelemetryClient telemetryClient,
IOptions<NuGetInsightsWorkerSettings> options,
ILogger<WorkflowRunMessageProcessor> logger)
{
_workflowService = workflowService;
_storageService = storageService;
_csvRecordContainers = csvRecordContainers;
_kustoIngestionStorageService = kustoIngestionStorageService;
_leaseService = leaseService;
_messageEnqueuer = messageEnqueuer;
_telemetryClient = telemetryClient;
_options = options;
_logger = logger;
}
public async Task ProcessAsync(WorkflowRunMessage message, long dequeueCount)
{
var run = await _storageService.GetRunAsync(message.RunId);
if (run == null)
{
if (message.AttemptCount < 10)
{
_logger.LogTransientWarning("After {AttemptCount} attempts, the workflow run {RunId} should have already been created. Trying again.",
message.AttemptCount,
message.RunId);
message.AttemptCount++;
await _messageEnqueuer.EnqueueAsync(new[] { message }, StorageUtility.GetMessageDelay(message.AttemptCount));
}
else
{
_logger.LogTransientWarning("After {AttemptCount} attempts, the workflow run {RunId} should have already been created. Giving up.",
message.AttemptCount,
message.RunId);
}
return;
}
await using var lease = await LeaseOrNullAsync(message);
if (lease == null)
{
return;
}
_logger.LogInformation("Processing workflow run {RunId}.", run.RunId);
while (!run.State.IsTerminal())
{
var transition = CurrentStateToTransition[run.State];
if (await transition.IsIncompleteAsync(this))
{
_logger.LogInformation("The {State} state is not yet complete.", run.State);
message.AttemptCount++;
await _messageEnqueuer.EnqueueAsync(new[] { message }, TimeSpan.FromSeconds(5));
return;
}
else
{
var nextState = await transition.TransitionAsync(this, run);
_telemetryClient.TrackMetric(
MetricNames.WorkflowStateTransition,
1,
new Dictionary<string, string> { { "NextState", nextState.ToString() } });
run.State = nextState;
await _storageService.ReplaceRunAsync(run);
}
}
}
private async Task<IAsyncDisposable> LeaseOrNullAsync(WorkflowRunMessage message)
{
var lease = await _leaseService.TryAcquireAsync(nameof(WorkflowRunMessageProcessor));
if (!lease.Acquired)
{
_logger.LogTransientWarning("The lease for workflow run processing is not available.");
message.AttemptCount++;
await _messageEnqueuer.EnqueueAsync(new[] { message }, StorageUtility.GetMessageDelay(message.AttemptCount));
return null;
}
return lease;
}
private record WorkflowStateTransition(
WorkflowRunState CurrentState,
Func<WorkflowRunMessageProcessor, Task<bool>> IsIncompleteAsync,
Func<WorkflowRunMessageProcessor, WorkflowRun, Task<WorkflowRunState>> TransitionAsync);
}
}