| | | 1 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 2 | | using CounterpointCollective.Dataflow.Internal; |
| | | 3 | | using CounterpointCollective.Threading; |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using System.Threading.Tasks.Dataflow; |
| | | 10 | | |
| | | 11 | | namespace CounterpointCollective.Dataflow |
| | | 12 | | { |
| | | 13 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable |
| | | 14 | | public sealed class ResizableBatchTransformBlock<I, O> |
| | | 15 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | | 16 | | : AbstractEncapsulatedPropagatorBlock<I, O>, IResizablePropagatorBlock<I, O> |
| | | 17 | | { |
| | 10160 | 18 | | protected override ITargetBlock<I> TargetSide => _boundedPropagatorBlock; |
| | | 19 | | |
| | 20998 | 20 | | protected override ISourceBlock<O> SourceSide => _boundedPropagatorBlock; |
| | | 21 | | |
| | | 22 | | private readonly SemaphoreSlim _semRunNextBatch; |
| | | 23 | | |
| | | 24 | | private readonly BoundedPropagatorBlock<I,O> _boundedPropagatorBlock; |
| | | 25 | | private readonly ResizableBufferBlock<I> _batchGatherBlock; |
| | | 26 | | private readonly TransformManyBlock<I[], O> _transformManyBlock; |
| | | 27 | | |
| | | 28 | | private DateTime earliestEntrance; |
| | | 29 | | |
| | 0 | 30 | | public bool IsBottleneckCurrently => OutputCount == 0 && IsFull; |
| | | 31 | | |
| | 0 | 32 | | public bool IsFull => _batchGatherBlock.Count >= BatchSize; |
| | | 33 | | |
| | | 34 | | public int BoundedCapacity |
| | | 35 | | { |
| | 0 | 36 | | get => _boundedPropagatorBlock.BoundedCapacity; |
| | 0 | 37 | | set => _boundedPropagatorBlock.BoundedCapacity = value; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | |
| | | 41 | | public int BatchSize |
| | | 42 | | { |
| | 5072 | 43 | | get => _batchGatherBlock.BoundedCapacity; |
| | | 44 | | set |
| | | 45 | | { |
| | 126 | 46 | | _batchGatherBlock.BoundedCapacity = value; |
| | 126 | 47 | | OnBatchSizeChanged?.Invoke(value); |
| | 126 | 48 | | RunBatchIfNeeded(); |
| | 126 | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 129 | 52 | | public Func<ResizableBatchTransformBlock<I, O>, IList<I>, IDisposable>? OnBatch { get; set; } |
| | | 53 | | |
| | 4 | 54 | | public int Count => _boundedPropagatorBlock.Count; |
| | | 55 | | |
| | | 56 | | private int inputCount; |
| | 7 | 57 | | public int InputCount => Volatile.Read(ref inputCount); |
| | | 58 | | |
| | 1 | 59 | | public int InProgressCount => Count - InputCount - OutputCount; |
| | | 60 | | |
| | 14 | 61 | | public int OutputCount => _transformManyBlock.OutputCount; |
| | | 62 | | |
| | 0 | 63 | | public TimeSpan LongestWait => InputCount == 0 ? TimeSpan.Zero : DateTime.UtcNow - earliestEntrance; |
| | | 64 | | |
| | 10 | 65 | | private readonly AsyncAutoResetEventSlim _sem = new(false); |
| | | 66 | | |
| | | 67 | | public Action<int>? OnBatchSizeChanged |
| | | 68 | | { |
| | 126 | 69 | | get; |
| | | 70 | | set |
| | | 71 | | { |
| | 0 | 72 | | field = value; |
| | 0 | 73 | | field?.Invoke(BatchSize); |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 10 | 77 | | public ResizableBatchTransformBlock( |
| | 10 | 78 | | Func<I[], Task<IEnumerable<O>>> transform, |
| | 10 | 79 | | int initialBatchSize, |
| | 10 | 80 | | ExecutionDataflowBlockOptions? options = null |
| | 10 | 81 | | ) |
| | | 82 | | { |
| | 10 | 83 | | options ??= new(); |
| | 10 | 84 | | _batchGatherBlock = new(new() { CancellationToken = options.CancellationToken }, RunBatchIfNeeded); |
| | | 85 | | |
| | 10 | 86 | | _semRunNextBatch = new SemaphoreSlim(options.MaxDegreeOfParallelism - 1); |
| | 10 | 87 | | _transformManyBlock = new TransformManyBlock<I[], O>( |
| | 10 | 88 | | async batch => |
| | 10 | 89 | | { |
| | 128 | 90 | | var d = OnBatch?.Invoke(this, batch); |
| | 10 | 91 | | IEnumerable<O> ret; |
| | 10 | 92 | | try |
| | 10 | 93 | | { |
| | 128 | 94 | | ret = await transform(batch); |
| | 127 | 95 | | } |
| | 10 | 96 | | finally |
| | 10 | 97 | | { |
| | 128 | 98 | | d?.Dispose(); |
| | 10 | 99 | | } |
| | 127 | 100 | | _semRunNextBatch.Release(); |
| | 127 | 101 | | return ret; |
| | 127 | 102 | | }, |
| | 10 | 103 | | new() { CancellationToken = options.CancellationToken, MaxDegreeOfParallelism = options.MaxDegreeOfParal |
| | 10 | 104 | | ); |
| | | 105 | | |
| | 10 | 106 | | BatchSize = initialBatchSize; |
| | | 107 | | |
| | 10 | 108 | | _boundedPropagatorBlock = new BoundedPropagatorBlock<I, O>( |
| | 10 | 109 | | _batchGatherBlock, |
| | 10 | 110 | | _transformManyBlock, |
| | 10 | 111 | | options.BoundedCapacity, |
| | 10 | 112 | | onEntered: () => |
| | 10 | 113 | | { |
| | 10155 | 114 | | if (Interlocked.Increment(ref inputCount) == 1) |
| | 10 | 115 | | { |
| | 12 | 116 | | earliestEntrance = DateTime.UtcNow; |
| | 10 | 117 | | } |
| | 10155 | 118 | | } |
| | 10 | 119 | | ); |
| | | 120 | | |
| | 10 | 121 | | Task.Run(async () => |
| | 10 | 122 | | { |
| | 20 | 123 | | var runBatchesTask = Task.Run(() => RunBatchesAsync()); |
| | 10 | 124 | | var t = await Task.WhenAny(_boundedPropagatorBlock.InputCompletion); |
| | 5 | 125 | | RunBatchIfNeeded(); |
| | 5 | 126 | | await _batchGatherBlock.Completion; |
| | 4 | 127 | | _sem.Terminate(); |
| | 4 | 128 | | await runBatchesTask; |
| | 10 | 129 | | |
| | 4 | 130 | | _ = t.PropagateCompletion(_transformManyBlock); |
| | 14 | 131 | | }); |
| | 10 | 132 | | } |
| | | 133 | | |
| | | 134 | | |
| | 10198 | 135 | | private void RunBatchIfNeeded() => _sem.Set(); |
| | | 136 | | |
| | | 137 | | private async Task RunBatchesAsync() |
| | | 138 | | { |
| | | 139 | | while (true) |
| | | 140 | | { |
| | 174 | 141 | | var t = await Task.WhenAny(_sem.WaitOneAsync()); |
| | 170 | 142 | | if (!t.IsCompletedSuccessfully) |
| | | 143 | | { |
| | | 144 | | break; |
| | | 145 | | } |
| | | 146 | | |
| | 293 | 147 | | while (TryGetBatch(out var batch)) |
| | | 148 | | { |
| | 129 | 149 | | Interlocked.Add(ref inputCount, -batch.Count); |
| | 129 | 150 | | _transformManyBlock.PostAsserted([.. batch]); |
| | 128 | 151 | | await _semRunNextBatch.WaitAsync(); |
| | 127 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | bool TryGetBatch([MaybeNullWhen(false)] out IList<I> batch) |
| | | 156 | | { |
| | 293 | 157 | | if (CanRunBatch()) |
| | | 158 | | { |
| | | 159 | | //First try to get maximally BatchSize items synchronously. |
| | | 160 | | //The buffer may be overfull though, in case we resized to a smaller BatchSize, so we need to check |
| | 129 | 161 | | if (!(_batchGatherBlock.Count <= BatchSize && _batchGatherBlock.TryReceiveAll(out batch))) |
| | | 162 | | { |
| | | 163 | | //Fall back to slow mode. |
| | 51 | 164 | | batch = []; |
| | 4647 | 165 | | while (batch.Count < BatchSize && _batchGatherBlock.TryReceive(out var item)) |
| | | 166 | | { |
| | 4596 | 167 | | batch.Add(item); |
| | 4596 | 168 | | } |
| | | 169 | | } |
| | 129 | 170 | | return true; |
| | | 171 | | } |
| | 164 | 172 | | batch = null; |
| | 164 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | // Normal -> buffer.Count >= BatchSize |
| | | 177 | | // Faulting/canceling -> false |
| | | 178 | | // Complete -> buffer.Count > 0 |
| | | 179 | | bool CanRunBatch() |
| | | 180 | | { |
| | 293 | 181 | | var finishedUnsuccessful = Completion.IsFaulted || Completion.IsCanceled; |
| | 293 | 182 | | return |
| | 293 | 183 | | !finishedUnsuccessful |
| | 293 | 184 | | && |
| | 293 | 185 | | ( |
| | 293 | 186 | | _batchGatherBlock.Count >= BatchSize |
| | 293 | 187 | | || (_boundedPropagatorBlock.InputCompletion.IsCompletedSuccessfully && _batchGatherBlock.Count > |
| | 293 | 188 | | ); |
| | | 189 | | } |
| | 4 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |