|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Options; |
| 12 | +using NuGet.Common; |
| 13 | + |
| 14 | +namespace NuGet.Services.PackageHash |
| 15 | +{ |
| 16 | + public class BatchProcessor : IBatchProcessor |
| 17 | + { |
| 18 | + private readonly IPackageHashCalculator _calculator; |
| 19 | + private readonly IOptionsSnapshot<PackageHashConfiguration> _configuration; |
| 20 | + private readonly ILogger<BatchProcessor> _logger; |
| 21 | + |
| 22 | + public BatchProcessor( |
| 23 | + IPackageHashCalculator calculator, |
| 24 | + IOptionsSnapshot<PackageHashConfiguration> configuration, |
| 25 | + ILogger<BatchProcessor> logger) |
| 26 | + { |
| 27 | + _calculator = calculator ?? throw new ArgumentNullException(nameof(configuration)); |
| 28 | + _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); |
| 29 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 30 | + } |
| 31 | + |
| 32 | + public async Task<IReadOnlyList<InvalidPackageHash>> ProcessBatchAsync( |
| 33 | + IReadOnlyList<PackageHash> batch, |
| 34 | + string hashAlgorithmId, |
| 35 | + CancellationToken token) |
| 36 | + { |
| 37 | + // Build up a bag of work. |
| 38 | + var remainingWork = new ConcurrentBag<Work>(); |
| 39 | + var failures = new ConcurrentBag<InvalidPackageHash>(); |
| 40 | + foreach (var source in _configuration.Value.Sources) |
| 41 | + { |
| 42 | + foreach (var package in batch) |
| 43 | + { |
| 44 | + remainingWork.Add(new Work(source, package)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Perform the work in parallel. |
| 49 | + _logger.LogInformation( |
| 50 | + "Starting to check hashes for {PackageCount} packages from {SourceCount} sources ({WorkCount} total" + |
| 51 | + " checks, {DegreeOfParallelism} tasks).", |
| 52 | + batch.Count, |
| 53 | + _configuration.Value.Sources.Count, |
| 54 | + remainingWork.Count, |
| 55 | + _configuration.Value.DegreeOfParallelism); |
| 56 | + |
| 57 | + var tasks = Enumerable |
| 58 | + .Range(0, _configuration.Value.DegreeOfParallelism) |
| 59 | + .Select(x => ProcessWorkAsync(remainingWork, failures, hashAlgorithmId, token)) |
| 60 | + .ToList(); |
| 61 | + |
| 62 | + await Task.WhenAll(tasks); |
| 63 | + |
| 64 | + _logger.LogInformation( |
| 65 | + "Completed the batch. {FailureResultCount} failure results were found.", |
| 66 | + failures.Count); |
| 67 | + |
| 68 | + return failures.ToList(); |
| 69 | + } |
| 70 | + |
| 71 | + private async Task ProcessWorkAsync( |
| 72 | + ConcurrentBag<Work> remainingWork, |
| 73 | + ConcurrentBag<InvalidPackageHash> failures, |
| 74 | + string hashAlgorithmId, |
| 75 | + CancellationToken token) |
| 76 | + { |
| 77 | + while (remainingWork.TryTake(out var work) && !token.IsCancellationRequested) |
| 78 | + { |
| 79 | + var actualHash = await _calculator.GetPackageHashAsync( |
| 80 | + work.Source, |
| 81 | + work.Package.Identity, |
| 82 | + hashAlgorithmId, |
| 83 | + token); |
| 84 | + |
| 85 | + if (work.Package.ExpectedHash != actualHash) |
| 86 | + { |
| 87 | + failures.Add(new InvalidPackageHash(work.Source, work.Package, actualHash)); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private class Work |
| 93 | + { |
| 94 | + public Work(PackageSource source, PackageHash package) |
| 95 | + { |
| 96 | + Source = source ?? throw new ArgumentNullException(nameof(source)); |
| 97 | + Package = package ?? throw new ArgumentNullException(nameof(package)); |
| 98 | + } |
| 99 | + |
| 100 | + public PackageSource Source { get; } |
| 101 | + public PackageHash Package { get; } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments