-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsyncProcessingPipelineStage.cs
378 lines (331 loc) · 11.7 KB
/
AsyncProcessingPipelineStage.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
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of the Griffin+ common library suite (https://github.com/griffinplus/dotnet-libs-logging)
// The source code is licensed under the MIT license.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GriffinPlus.Lib.Threading;
// ReSharper disable ForCanBeConvertedToForeach
namespace GriffinPlus.Lib.Logging;
/// <summary>
/// Base class for stages in the log message processing pipeline that feature asynchronous processing.
/// Messages can be processed synchronously or asynchronously. Heavyweight operations and all operations
/// that might block should be done asynchronously only to ensure that the thread writing a message is
/// not blocked.
/// </summary>
public abstract class AsyncProcessingPipelineStage : ProcessingPipelineStage
{
private AsyncContextThread mAsyncContextThread;
private Task mAsyncProcessingTask;
private AsyncAutoResetEvent mTriggerAsyncProcessingEvent;
private LocklessStack<LocalLogMessage> mAsyncProcessingMessageStack;
private bool mDiscardMessagesIfQueueFull;
private int mMessageQueueSize = 500;
private TimeSpan mShutdownTimeout = TimeSpan.FromMilliseconds(5000);
private CancellationTokenSource mAsyncProcessingCancellationTokenSource;
private volatile bool mTerminateProcessingTask;
/// <summary>
/// Initializes a new instance of the <see cref="AsyncProcessingPipelineStage"/> class.
/// </summary>
protected AsyncProcessingPipelineStage() { }
#region Initialization / Shutdown
/// <summary>
/// Initializes the processing pipeline stage (base class specific part).
/// </summary>
internal override void OnInitializeBase()
{
try
{
// set up asynchronous processing
mAsyncProcessingMessageStack = new LocklessStack<LocalLogMessage>(mMessageQueueSize, false);
mTriggerAsyncProcessingEvent = new AsyncAutoResetEvent(false);
mAsyncProcessingCancellationTokenSource = new CancellationTokenSource();
mTerminateProcessingTask = false;
mAsyncContextThread = new AsyncContextThread();
mAsyncProcessingTask = mAsyncContextThread.Factory.Run(ProcessingTask);
// Perform pipeline stage specific initialization
OnInitialize();
}
catch (Exception)
{
Shutdown();
throw;
}
}
/// <summary>
/// Shuts the processing pipeline down (base class specific part).
/// This method must not throw exceptions.
/// </summary>
internal override void OnShutdownBase()
{
// tell the processing thread to terminate
// (it will try to process the last messages and exit)
mTerminateProcessingTask = true;
mAsyncProcessingCancellationTokenSource?.CancelAfter(mShutdownTimeout);
mTriggerAsyncProcessingEvent?.Set();
mAsyncProcessingTask?.Wait();
// the processing task should have completed its work
Debug.Assert(mAsyncProcessingTask == null || mAsyncProcessingTask.IsCompleted);
// the stack should be empty now...
Debug.Assert(mAsyncProcessingMessageStack == null || mAsyncProcessingMessageStack.UsedItemCount == 0);
// clean up processing thread related stuff
mAsyncContextThread?.Join();
mAsyncProcessingCancellationTokenSource?.Dispose();
mAsyncContextThread = null;
mAsyncProcessingTask = null;
mAsyncProcessingCancellationTokenSource = null;
mAsyncProcessingMessageStack = null;
// perform pipeline stage specific cleanup
try
{
OnShutdown();
}
catch (Exception ex)
{
// swallow exception to avoid crashing the application, if the exception is not handled properly
Debug.Fail("The pipeline stage failed shutting down.", ex.ToString());
}
}
#endregion
#region Settings
/// <summary>
/// Gets or sets a value indicating whether messages are discarded when the queue is full.
/// The default is <c>false</c>.
/// </summary>
public bool DiscardMessagesIfQueueFull
{
get
{
lock (Sync)
{
return mDiscardMessagesIfQueueFull;
}
}
set
{
lock (Sync)
{
EnsureNotAttachedToLoggingSubsystem();
mDiscardMessagesIfQueueFull = value;
}
}
}
/// <summary>
/// Gets or sets the capacity of the queue buffering messages to be processed asynchronously.
/// The default is 500.
/// </summary>
public int MessageQueueSize
{
get
{
lock (Sync)
{
return mMessageQueueSize;
}
}
set
{
if (value < 1) throw new ArgumentOutOfRangeException(nameof(MessageQueueSize), "The shutdown timeout must be >= 1.");
lock (Sync)
{
EnsureNotAttachedToLoggingSubsystem();
mMessageQueueSize = value;
}
}
}
/// <summary>
/// Gets or sets the shutdown timeout (in ms).
/// The default is 5000 ms.
/// </summary>
public TimeSpan ShutdownTimeout
{
get
{
lock (Sync)
{
return mShutdownTimeout;
}
}
set
{
if (value < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(value), "The shutdown timeout must be positive.");
lock (Sync)
{
mShutdownTimeout = value;
}
}
}
#endregion
#region Processing Setting Changes
private readonly HashSet<IUntypedSettingProxy> mChangedSettings = [];
/// <summary>
/// Notifies that the specified setting has changed (for internal use only).
/// </summary>
/// <param name="setting">The setting that has changed.</param>
internal override void ProcessSettingChanged(IUntypedSettingProxy setting)
{
lock (mChangedSettings)
{
mChangedSettings.Add(setting);
}
}
/// <summary>
/// When overridden in a derived class, processes pending changes to registered setting proxies
/// (the method is executed by the stage's processing thread, do not use <c>ConfigureAwait(false)</c> to resume
/// execution in the processing thread when awaiting a task).
/// </summary>
/// <param name="settings">Settings that have changed.</param>
/// <param name="cancellationToken">Cancellation token that is signaled when the pipeline stage is shutting down.</param>
protected virtual Task OnSettingsChangedAsync(IUntypedProcessingPipelineStageSetting[] settings, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
#endregion
#region Processing Messages and Notifications
/// <summary>
/// Is called on behalf of <see cref="ProcessingPipelineStage.ProcessMessage"/> (for internal use only).
/// This method must not throw exceptions.
/// </summary>
/// <param name="message">Message to process.</param>
/// <returns>
/// <c>true</c> to pass the message to the following stages;<br/>
/// <c>false</c> to stop processing the message.
/// </returns>
internal override bool OnProcessMessageBase(LocalLogMessage message)
{
try
{
// synchronous processing
bool proceed = ProcessSync(message, out bool queueMessageForAsynchronousProcessing);
// asynchronous processing
if (queueMessageForAsynchronousProcessing)
{
if (mDiscardMessagesIfQueueFull)
{
message.AddRef();
bool pushed = mAsyncProcessingMessageStack.TryPush(message);
if (pushed) mTriggerAsyncProcessingEvent.Set();
else message.Release();
}
else
{
message.AddRef();
while (!mAsyncProcessingMessageStack.TryPush(message)) Thread.Sleep(10);
mTriggerAsyncProcessingEvent.Set();
}
}
return proceed;
}
catch (Exception ex)
{
// swallow exception to avoid crashing the application, if the exception is not handled properly
Debug.Fail("The pipeline stage threw an exception processing the message.", ex.ToString());
// let the following stages process the message
// (hopefully this is the right decision in this case)
return true;
}
}
/// <summary>
/// When overridden in a derived class, processes the specified log message synchronously.
/// This method is called by the thread writing the message and from within the pipeline stage lock.
/// This method must not throw exceptions.
/// </summary>
/// <param name="message">Message to process.</param>
/// <param name="queueForAsyncProcessing">
/// Receives a value indicating whether the message should be enqueued for asynchronous processing.
/// </param>
/// <returns>
/// <c>true</c> to pass the message to the following pipeline stages;<br/>
/// otherwise <c>false</c>.
/// </returns>
/// <remarks>
/// Call <see cref="LocalLogMessage.AddRef"/> on a message that should be stored any longer to prevent it from
/// returning to the log message pool too early. Call <see cref="LocalLogMessage.Release"/> as soon as you don't
/// need the message anymore.
/// </remarks>
protected virtual bool ProcessSync(LocalLogMessage message, out bool queueForAsyncProcessing)
{
queueForAsyncProcessing = true;
return true;
}
/// <summary>
/// When overridden in a derived class, processes the specified log messages asynchronously
/// (the method is executed by the stage's processing thread, do not use <c>ConfigureAwait(false)</c> to resume
/// execution in the processing thread when awaiting a task).
/// </summary>
/// <param name="messages">Messages to process.</param>
/// <param name="cancellationToken">Cancellation token that is signaled when the pipeline stage is shutting down.</param>
/// <remarks>
/// Call <see cref="LocalLogMessage.AddRef"/> on a message that should be stored any longer to prevent it from
/// returning to the log message pool too early. Call <see cref="LocalLogMessage.Release"/> as soon as you don't
/// need the message anymore.
/// </remarks>
protected virtual Task ProcessAsync(LocalLogMessage[] messages, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
/// <summary>
/// The processing task that runs until the pipeline stage is shut down.
/// </summary>
private async Task ProcessingTask()
{
CancellationToken cancellationToken = mAsyncProcessingCancellationTokenSource.Token;
while (true)
{
// wait for messages to process
await mTriggerAsyncProcessingEvent.WaitAsync(cancellationToken);
// pull setting changes, if available
IUntypedProcessingPipelineStageSetting[] settings = null;
lock (mChangedSettings)
{
if (mChangedSettings.Count > 0)
{
settings = mChangedSettings.Cast<IUntypedProcessingPipelineStageSetting>().ToArray();
mChangedSettings.Clear();
}
}
// process setting changes
if (settings != null)
await OnSettingsChangedAsync(settings, cancellationToken);
// process the messages
await ProcessQueuedMessages(cancellationToken);
// abort, if requested
if (mTerminateProcessingTask)
{
// process the last messages, if there is time left...
if (!cancellationToken.IsCancellationRequested)
{
await ProcessQueuedMessages(cancellationToken);
}
break;
}
}
}
/// <summary>
/// Processes log messages that have been buffered in the <see cref="mAsyncProcessingMessageStack"/> (for asynchronous processing only).
/// </summary>
/// <param name="cancellationToken">Cancellation token that is signaled when the pipeline stage is shutting down.</param>
private async Task ProcessQueuedMessages(CancellationToken cancellationToken)
{
LocalLogMessage[] messages = mAsyncProcessingMessageStack.FlushAndReverse();
if (messages == null) return;
try
{
await ProcessAsync(messages, cancellationToken);
}
catch (Exception ex)
{
Debug.Fail("ProcessAsync() threw an unhandled exception.", ex.ToString());
}
finally
{
// release message to let them return to the pool
foreach (LocalLogMessage message in messages) message.Release();
}
}
#endregion
}