-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathOrchestrationRuntimeState.cs
More file actions
463 lines (415 loc) · 18.7 KB
/
OrchestrationRuntimeState.cs
File metadata and controls
463 lines (415 loc) · 18.7 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
#nullable enable
namespace DurableTask.Core
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using DurableTask.Core.Common;
using DurableTask.Core.History;
using DurableTask.Core.Logging;
using DurableTask.Core.Tracing;
/// <summary>
/// Represents the runtime state of an orchestration
/// </summary>
public class OrchestrationRuntimeState
{
private OrchestrationStatus orchestrationStatus;
/// <summary>
/// List of all history events for this runtime state.
/// Note that this list is frequently a combination of <see cref="PastEvents"/> and <see cref="NewEvents"/>, but not always.
/// </summary>
public IList<HistoryEvent> Events { get; }
/// <summary>
/// List of new events added during an execution to keep track of the new events that were added during a particular execution.
/// Should not be serialized.
/// This list is NOT guaranteed to be a subset of <see cref="Events"/>.
/// </summary>
public IList<HistoryEvent> NewEvents { get; }
/// <summary>
/// A subset of <see cref="Events"/> that contains only events that have been previously played and should not be serialized.
/// </summary>
public IList<HistoryEvent> PastEvents { get; }
readonly ISet<int> completedEventIds;
/// <summary>
/// Compressed size of the serialized state
/// </summary>
public long CompressedSize;
/// <summary>
/// Gets the execution completed event
/// </summary>
public ExecutionCompletedEvent? ExecutionCompletedEvent { get; set; }
/// <summary>
/// Size of the serialized state (uncompressed)
/// </summary>
public long Size;
/// <summary>
/// The string status of the runtime state
/// </summary>
public string? Status;
/// <summary>
/// Creates a new instance of the OrchestrationRuntimeState
/// </summary>
public OrchestrationRuntimeState()
: this(null)
{
}
/// <summary>
/// Creates a new instance of the OrchestrationRuntimeState with the supplied events
/// </summary>
/// <param name="events">List of events for this runtime state</param>
public OrchestrationRuntimeState(IList<HistoryEvent>? events)
{
Events = events != null ? new List<HistoryEvent>(events.Count) : new List<HistoryEvent>();
PastEvents = events != null ? new List<HistoryEvent>(events.Count) : new List<HistoryEvent>();
NewEvents = new List<HistoryEvent>();
completedEventIds = new HashSet<int>();
orchestrationStatus = OrchestrationStatus.Running;
if (events != null && events.Count > 0)
{
foreach (HistoryEvent ev in events)
{
AddEvent(ev, false);
}
}
}
/// <summary>
/// Returns a deep copy of the object.
/// </summary>
/// <returns>Cloned object</returns>
public OrchestrationRuntimeState Clone()
{
return new OrchestrationRuntimeState(this.Events)
{
Size = this.Size,
Status = this.Status,
};
}
/// <summary>
/// Gets the execution started event
/// </summary>
public ExecutionStartedEvent? ExecutionStartedEvent
{
get; private set;
}
/// <summary>
/// Gets the created time of the ExecutionStartedEvent
/// </summary>
public DateTime CreatedTime => GetExecutionStartedEventOrThrow().Timestamp;
/// <summary>
/// Gets the created time of the ExecutionCompletedEvent if completed else a safe (from timezone shift) max datetime
/// </summary>
public DateTime CompletedTime => ExecutionCompletedEvent?.Timestamp ?? Utils.DateTimeSafeMaxValue;
/// <summary>
/// Gets the serialized input of the ExecutionStartedEvent
/// </summary>
public string? Input => GetExecutionStartedEventOrThrow().Input;
/// <summary>
/// Gets the serialized output of the ExecutionCompletedEvent if completed else null
/// </summary>
public string? Output => ExecutionCompletedEvent?.FailureDetails?.ToString() ?? ExecutionCompletedEvent?.Result;
/// <summary>
/// Gets the structured failure details for this orchestration.
/// </summary>
/// <remarks>
/// This property is set only when the orchestration fails and <see cref="TaskHubWorker.ErrorPropagationMode"/>
/// is set to <see cref="ErrorPropagationMode.UseFailureDetails"/>.
/// </remarks>
public FailureDetails? FailureDetails => ExecutionCompletedEvent?.FailureDetails;
/// <summary>
/// Failure that caused an orchestrator to complete, if any.
/// </summary>
/// <remarks>
/// This property was introduced to sanitize exceptions during logging. See it's usage in <see cref="LogEvents.OrchestrationCompleted"/>.
/// </remarks>
internal Exception? Exception { get; set; }
/// <summary>
/// Gets the orchestration name from the ExecutionStartedEvent
/// </summary>
public string Name => GetExecutionStartedEventOrThrow().Name;
/// <summary>
/// Gets the orchestration version of the ExecutionStartedEvent
/// </summary>
public string Version => GetExecutionStartedEventOrThrow().Version;
/// <summary>
/// Gets the tags from the ExecutionStartedEvent
/// </summary>
// This gets called by json.net for deserialization, we can't throw if there is no ExecutionStartedEvent
public IDictionary<string, string>? Tags => ExecutionStartedEvent?.Tags;
/// <summary>
/// Gets the status of the orchestration
/// If complete then the status from the ExecutionCompletedEvent else Running.
/// </summary>
public OrchestrationStatus OrchestrationStatus
{
get
{
GetExecutionStartedEventOrThrow();
return orchestrationStatus;
}
}
/// <summary>
/// Gets the OrchestrationInstance of the ExecutionStartedEvent else null
/// </summary>
public OrchestrationInstance? OrchestrationInstance => ExecutionStartedEvent?.OrchestrationInstance;
/// <summary>
/// Gets the ParentInstance of the ExecutionStartedEvent else null
/// </summary>
public ParentInstance? ParentInstance => ExecutionStartedEvent?.ParentInstance;
/// <summary>
/// Gets a value indicating whether the orchestration state is valid.
/// </summary>
/// <remarks>
/// An invalid orchestration runtime state means that the history is somehow corrupted.
/// </remarks>
public bool IsValid =>
this.Events.Count == 0 ||
this.Events.Count == 1 && this.Events[0].EventType == EventType.OrchestratorStarted ||
this.ExecutionStartedEvent != null;
/// <summary>
/// Gets or sets a LogHelper instance that can be used to log messages.
/// </summary>
/// <remarks>
/// Ideally, this would be set in the constructor but that would require a larger refactoring.
/// </remarks>
internal LogHelper? LogHelper { get; set; } = null;
/// <summary>
/// Adds a new history event to the Events list and NewEvents list
/// </summary>
/// <param name="historyEvent">The new history event to add</param>
public void AddEvent(HistoryEvent historyEvent)
{
AddEvent(historyEvent, true);
}
ExecutionStartedEvent GetExecutionStartedEventOrThrow()
{
ExecutionStartedEvent? executionStartedEvent = this.ExecutionStartedEvent;
if (executionStartedEvent == null)
{
throw new InvalidOperationException("An ExecutionStarted event is required.");
}
return executionStartedEvent;
}
/// <summary>
/// Adds a new history event to the Events list and optionally NewEvents list
/// </summary>
/// <param name="historyEvent">The history event to add</param>
/// <param name="isNewEvent">Flag indicating whether this is a new event or not</param>
void AddEvent(HistoryEvent historyEvent, bool isNewEvent)
{
if (IsDuplicateEvent(historyEvent))
{
return;
}
Events.Add(historyEvent);
if (isNewEvent)
{
NewEvents.Add(historyEvent);
}
else
{
PastEvents.Add(historyEvent);
}
SetMarkerEvents(historyEvent);
}
bool IsDuplicateEvent(HistoryEvent historyEvent)
{
if (historyEvent.EventId >= 0 &&
historyEvent.EventType == EventType.TaskCompleted &&
!completedEventIds.Add(historyEvent.EventId))
{
TraceHelper.Trace(TraceEventType.Warning,
"OrchestrationRuntimeState-DuplicateEvent",
"The orchestration '{0}' has already seen a completed task with id {1}.",
this.OrchestrationInstance?.InstanceId ?? "",
historyEvent.EventId);
return true;
}
return false;
}
void SetMarkerEvents(HistoryEvent historyEvent)
{
if (historyEvent is ExecutionStartedEvent startedEvent)
{
if (ExecutionStartedEvent != null)
{
throw new InvalidOperationException(
"Multiple ExecutionStartedEvent found, potential corruption in state storage");
}
ExecutionStartedEvent = startedEvent;
}
else if (historyEvent is ExecutionCompletedEvent completedEvent)
{
if (ExecutionCompletedEvent == null)
{
ExecutionCompletedEvent = completedEvent;
orchestrationStatus = completedEvent.OrchestrationStatus;
}
else
{
// It's not generally expected to receive multiple execution completed events for a given orchestrator, but it's possible under certain race conditions.
// For example: when an orchestrator is signaled to terminate at the same time as it attempts to continue-as-new.
var log = $"Received new {completedEvent.GetType().Name} event despite the orchestration being already in the {orchestrationStatus} state.";
if (orchestrationStatus == OrchestrationStatus.ContinuedAsNew && completedEvent.OrchestrationStatus == OrchestrationStatus.Terminated)
{
// If the orchestration planned to continue-as-new but termination is requested, we transition to the terminated state.
// This is because termination should be considered to be forceful.
log += " Discarding previous 'ExecutionCompletedEvent' as termination is forceful.";
ExecutionCompletedEvent = completedEvent;
orchestrationStatus = completedEvent.OrchestrationStatus;
}
else
{
// otherwise, we ignore the new event.
log += " Discarding new 'ExecutionCompletedEvent'.";
}
LogHelper?.OrchestrationDebugTrace(this.OrchestrationInstance?.InstanceId ?? "", this.OrchestrationInstance?.ExecutionId ?? "", log);
}
}
else if (historyEvent is ExecutionSuspendedEvent)
{
orchestrationStatus = OrchestrationStatus.Suspended;
}
else if (historyEvent is ExecutionResumedEvent)
{
if (ExecutionCompletedEvent == null)
{
orchestrationStatus = OrchestrationStatus.Running;
}
}
}
/// <summary>
/// Gets a statedump of the current list of events.
/// </summary>
/// <returns></returns>
public OrchestrationRuntimeStateDump GetOrchestrationRuntimeStateDump()
{
#if DEBUG
var runtimeStateDump = new OrchestrationRuntimeStateDump
{
Events = new List<HistoryEvent>(),
NewEvents = new List<HistoryEvent>(),
};
foreach (HistoryEvent evt in Events)
{
HistoryEvent abridgeEvent = GenerateAbridgedEvent(evt);
runtimeStateDump.Events.Add(abridgeEvent);
}
foreach (HistoryEvent evt in NewEvents)
{
HistoryEvent abridgeEvent = GenerateAbridgedEvent(evt);
runtimeStateDump.NewEvents.Add(abridgeEvent);
}
return runtimeStateDump;
#else
return new OrchestrationRuntimeStateDump
{
EventCount = Events.Count,
NewEventsCount = NewEvents.Count,
Events = new List<HistoryEvent>(),
NewEvents = new List<HistoryEvent>(),
};
#endif
}
HistoryEvent GenerateAbridgedEvent(HistoryEvent evt)
{
HistoryEvent returnedEvent = evt;
if (evt is TaskScheduledEvent taskScheduledEvent)
{
returnedEvent = new TaskScheduledEvent(taskScheduledEvent.EventId)
{
Timestamp = taskScheduledEvent.Timestamp,
IsPlayed = taskScheduledEvent.IsPlayed,
Name = taskScheduledEvent.Name,
Version = taskScheduledEvent.Version,
Input = "[..snipped..]",
};
}
else if (evt is TaskCompletedEvent taskCompletedEvent)
{
returnedEvent = new TaskCompletedEvent(taskCompletedEvent.EventId, taskCompletedEvent.TaskScheduledId, "[..snipped..]")
{
Timestamp = taskCompletedEvent.Timestamp,
IsPlayed = taskCompletedEvent.IsPlayed,
};
}
else if (evt is SubOrchestrationInstanceCreatedEvent subOrchestrationInstanceCreatedEvent)
{
returnedEvent = new SubOrchestrationInstanceCreatedEvent(subOrchestrationInstanceCreatedEvent.EventId)
{
Timestamp = subOrchestrationInstanceCreatedEvent.Timestamp,
IsPlayed = subOrchestrationInstanceCreatedEvent.IsPlayed,
Name = subOrchestrationInstanceCreatedEvent.Name,
Version = subOrchestrationInstanceCreatedEvent.Version,
Input = "[..snipped..]",
ClientSpanId = subOrchestrationInstanceCreatedEvent.ClientSpanId,
};
}
else if (evt is SubOrchestrationInstanceCompletedEvent subOrchestrationInstanceCompletedEvent)
{
returnedEvent = new SubOrchestrationInstanceCompletedEvent(subOrchestrationInstanceCompletedEvent.EventId,
subOrchestrationInstanceCompletedEvent.TaskScheduledId, "[..snipped..]")
{
Timestamp = subOrchestrationInstanceCompletedEvent.Timestamp,
IsPlayed = subOrchestrationInstanceCompletedEvent.IsPlayed,
};
}
else if (evt is TaskFailedEvent taskFailedEvent)
{
returnedEvent = new TaskFailedEvent(taskFailedEvent.EventId,
taskFailedEvent.TaskScheduledId, taskFailedEvent.Reason, "[..snipped..]")
{
Timestamp = taskFailedEvent.Timestamp,
IsPlayed = taskFailedEvent.IsPlayed,
};
}
else if (evt is SubOrchestrationInstanceFailedEvent subOrchestrationInstanceFailedEvent)
{
returnedEvent = new SubOrchestrationInstanceFailedEvent(subOrchestrationInstanceFailedEvent.EventId,
subOrchestrationInstanceFailedEvent.TaskScheduledId, subOrchestrationInstanceFailedEvent.Reason, "[..snipped..]")
{
Timestamp = subOrchestrationInstanceFailedEvent.Timestamp,
IsPlayed = subOrchestrationInstanceFailedEvent.IsPlayed,
};
}
else if (evt is ExecutionStartedEvent executionStartedEvent)
{
returnedEvent = new ExecutionStartedEvent(executionStartedEvent.EventId, "[..snipped..]")
{
Timestamp = executionStartedEvent.Timestamp,
IsPlayed = executionStartedEvent.IsPlayed,
};
}
else if (evt is ExecutionCompletedEvent executionCompletedEvent)
{
returnedEvent = new ExecutionCompletedEvent(executionCompletedEvent.EventId, "[..snipped..]",
executionCompletedEvent.OrchestrationStatus)
{
Timestamp = executionCompletedEvent.Timestamp,
IsPlayed = executionCompletedEvent.IsPlayed,
};
}
else if (evt is ExecutionTerminatedEvent executionTerminatedEvent)
{
returnedEvent = new ExecutionTerminatedEvent(executionTerminatedEvent.EventId, "[..snipped..]")
{
Timestamp = executionTerminatedEvent.Timestamp,
IsPlayed = executionTerminatedEvent.IsPlayed,
};
}
// ContinueAsNewEvent is covered by the ExecutionCompletedEvent block
return returnedEvent;
}
}
}