-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
TransformManyBlock.IAsyncEnumerable.cs
270 lines (256 loc) · 13.5 KB
/
TransformManyBlock.IAsyncEnumerable.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks.Dataflow.Internal;
namespace System.Threading.Tasks.Dataflow
{
public partial class TransformManyBlock<TInput, TOutput>
{
/// <summary>Initializes the <see cref="TransformManyBlock{TInput,TOutput}"/> with the specified function.</summary>
/// <param name="transform">
/// The function to invoke with each data element received. All of the data from the returned <see cref="IAsyncEnumerable{TOutput}"/>
/// will be made available as output from this <see cref="TransformManyBlock{TInput,TOutput}"/>.
/// </param>
/// <exception cref="ArgumentNullException">The <paramref name="transform"/> is <see langword="null" />.</exception>
public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform) :
this(transform, ExecutionDataflowBlockOptions.Default)
{
}
/// <summary>Initializes the <see cref="TransformManyBlock{TInput,TOutput}"/> with the specified function and <see cref="ExecutionDataflowBlockOptions"/>.</summary>
/// <param name="transform">
/// The function to invoke with each data element received. All of the data from the returned <see cref="IAsyncEnumerable{TOutput}"/>
/// will be made available as output from this <see cref="TransformManyBlock{TInput,TOutput}"/>.
/// </param>
/// <param name="dataflowBlockOptions">The options with which to configure this <see cref="TransformManyBlock{TInput,TOutput}"/>.</param>
/// <exception cref="ArgumentNullException">The <paramref name="transform"/> or <paramref name="dataflowBlockOptions"/> is <see langword="null" />.</exception>
public TransformManyBlock(Func<TInput, IAsyncEnumerable<TOutput>> transform, ExecutionDataflowBlockOptions dataflowBlockOptions)
{
if (transform is null)
{
throw new ArgumentNullException(nameof(transform));
}
Initialize(messageWithId =>
{
Task t = ProcessMessageAsync(transform, messageWithId);
#if DEBUG
// Task returned from ProcessMessageAsync is explicitly ignored.
// That function handles all exceptions.
t.ContinueWith(static t => Debug.Assert(t.IsCompletedSuccessfully), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
#endif
}, dataflowBlockOptions, ref _source, ref _target, ref _reorderingBuffer, TargetCoreOptions.UsesAsyncCompletion);
}
// Note:
// Enumerating the IAsyncEnumerable is done with ConfigureAwait(true), using the default behavior of
// paying attention to the current context/scheduler. This makes it so that the enumerable code runs on the target scheduler.
// For this to work correctly, there can't be any ConfigureAwait(false) in the same method prior to
// these await foreach loops, nor in the call chain prior to the method invocation.
/// <summary>Processes the message with a user-provided transform function that returns an async enumerable.</summary>
/// <param name="transformFunction">The transform function to use to process the message.</param>
/// <param name="messageWithId">The message to be processed.</param>
private async Task ProcessMessageAsync(Func<TInput, IAsyncEnumerable<TOutput>> transformFunction, KeyValuePair<TInput, long> messageWithId)
{
try
{
// Run the user transform and store the results.
IAsyncEnumerable<TOutput> outputItems = transformFunction(messageWithId.Key);
await StoreOutputItemsAsync(messageWithId, outputItems).ConfigureAwait(false);
}
catch (Exception exc)
{
// Enumerating the user's collection failed. If this exception represents cancellation,
// swallow it rather than shutting down the block.
if (!Common.IsCooperativeCancellation(exc))
{
// The exception was not for cancellation. We must add the exception before declining
// and signaling completion, as the exception is part of the operation, and the completion
// conditions depend on this.
Common.StoreDataflowMessageValueIntoExceptionData(exc, messageWithId.Key);
_target.Complete(exc, dropPendingMessages: true, storeExceptionEvenIfAlreadyCompleting: true, unwrapInnerExceptions: false);
}
}
finally
{
// Let the target know that one of the asynchronous operations it launched has completed.
_target.SignalOneAsyncMessageCompleted();
}
}
/// <summary>
/// Stores the output items, either into the reordering buffer or into the source half.
/// Ensures that the bounding count is correctly updated.
/// </summary>
/// <param name="messageWithId">The message with id.</param>
/// <param name="outputItems">The output items to be persisted.</param>
private async Task StoreOutputItemsAsync(
KeyValuePair<TInput, long> messageWithId, IAsyncEnumerable<TOutput>? outputItems)
{
// If there's a reordering buffer, pass the data along to it.
// The reordering buffer will handle all details, including bounding.
if (_reorderingBuffer is not null)
{
await StoreOutputItemsReorderedAsync(messageWithId.Value, outputItems).ConfigureAwait(false);
}
// Otherwise, output the data directly.
else if (outputItems is not null)
{
await StoreOutputItemsNonReorderedWithIterationAsync(outputItems).ConfigureAwait(false);
}
else if (_target.IsBounded)
{
// outputItems is null and there's no reordering buffer
// and we're bounding, so decrement the bounding count to
// signify that the input element we already accounted for
// produced no output
_target.ChangeBoundingCount(count: -1);
}
// else there's no reordering buffer, there are no output items, and we're not bounded,
// so there's nothing more to be done.
}
/// <summary>Stores the next item using the reordering buffer.</summary>
/// <param name="id">The ID of the item.</param>
/// <param name="item">The async enumerable.</param>
private async Task StoreOutputItemsReorderedAsync(long id, IAsyncEnumerable<TOutput>? item)
{
Debug.Assert(_reorderingBuffer is not null, "Expected a reordering buffer");
Debug.Assert(id != Common.INVALID_REORDERING_ID, "This ID should never have been handed out.");
// Grab info about the transform
TargetCore<TInput> target = _target;
bool isBounded = target.IsBounded;
// Handle invalid items (null enumerables) by delegating to the base
if (item is null)
{
_reorderingBuffer.AddItem(id, null, false);
if (isBounded)
{
target.ChangeBoundingCount(count: -1);
}
return;
}
// By this point, either we're not the next item, in which case we need to make a copy of the
// data and store it, or we are the next item and can store it immediately but we need to enumerate
// the items and store them individually because we don't want to enumerate while holding a lock.
List<TOutput>? itemCopy = null;
try
{
// If this is the next item, we can output it now.
if (_reorderingBuffer.IsNext(id))
{
await StoreOutputItemsNonReorderedWithIterationAsync(item).ConfigureAwait(false);
// here itemCopy remains null, so that base.AddItem will finish our interactions with the reordering buffer
}
else
{
// We're not the next item, and we're not trusted, so copy the data into a list.
// We need to enumerate outside of the lock in the base class.
int itemCount = 0;
try
{
itemCopy = new List<TOutput>();
await foreach (TOutput element in item.ConfigureAwait(true))
{
itemCopy.Add(element);
}
itemCount = itemCopy.Count;
}
finally
{
// If we're here successfully, then itemCount is the number of output items
// we actually received, and we should update the bounding count with it.
// If we're here because ToList threw an exception, then itemCount will be 0,
// and we still need to update the bounding count with this in order to counteract
// the increased bounding count for the corresponding input.
if (isBounded)
{
UpdateBoundingCountWithOutputCount(count: itemCount);
}
}
}
// else if the item isn't valid, the finally block will see itemCopy as null and output invalid
}
finally
{
// Tell the base reordering buffer that we're done. If we already output
// all of the data, itemCopy will be null, and we just pass down the invalid item.
// If we haven't, pass down the real thing. We do this even in the case of an exception,
// in which case this will be a dummy element.
_reorderingBuffer.AddItem(id, itemCopy, itemIsValid: itemCopy is not null);
}
}
/// <summary>
/// Stores the untrusted async enumerable into the source core.
/// This method does not go through the reordering buffer.
/// </summary>
/// <param name="outputItems">The untrusted enumerable.</param>
private async Task StoreOutputItemsNonReorderedWithIterationAsync(IAsyncEnumerable<TOutput> outputItems)
{
// The _source we're adding to isn't thread-safe, so we need to determine
// whether we need to lock. If the block is configured with a max degree
// of parallelism of 1, then only one transform can run at a time, and so
// we don't need to lock. Similarly, if there's a reordering buffer, then
// it guarantees that we're invoked serially, and we don't need to lock.
bool isSerial =
_target.DataflowBlockOptions.MaxDegreeOfParallelism == 1 ||
_reorderingBuffer is not null;
// If we're bounding, we need to increment the bounded count
// for each individual item as we enumerate it.
if (_target.IsBounded)
{
// When the input item that generated this
// output was loaded, we incremented the bounding count. If it only
// output a single a item, then we don't need to touch the bounding count.
// Otherwise, we need to adjust the bounding count accordingly.
bool outputFirstItem = false;
try
{
await foreach (TOutput item in outputItems.ConfigureAwait(true))
{
if (outputFirstItem)
{
_target.ChangeBoundingCount(count: 1);
}
outputFirstItem = true;
if (isSerial)
{
_source.AddMessage(item);
}
else
{
lock (ParallelSourceLock) // don't hold lock while enumerating
{
_source.AddMessage(item);
}
}
}
}
finally
{
if (!outputFirstItem)
{
_target.ChangeBoundingCount(count: -1);
}
}
}
// If we're not bounding, just output each individual item.
else
{
if (isSerial)
{
await foreach (TOutput item in outputItems.ConfigureAwait(true))
{
_source.AddMessage(item);
}
}
else
{
await foreach (TOutput item in outputItems.ConfigureAwait(true))
{
lock (ParallelSourceLock) // don't hold lock while enumerating
{
_source.AddMessage(item);
}
}
}
}
}
}
}