| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Work; |
| | | 3 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 4 | | using NodaTime; |
| | | 5 | | using System.IO.Abstractions; |
| | | 6 | | |
| | | 7 | | namespace Anichron.Worker.ProxySweeps; |
| | | 8 | | |
| | | 9 | | /// <summary>What a sweep reported.</summary> |
| | | 10 | | /// <param name="Orphans">Files no proxy row references, last written more than the grace period before the scan.</param |
| | | 11 | | /// <param name="Bytes">Their size in all.</param> |
| | | 12 | | /// <param name="TemporaryFiles">Orphans left by an unfinished proxy write.</param> |
| | | 13 | | /// <param name="ConfigsGone">Storage configs named by orphans' directories that no longer exist.</param> |
| | | 14 | | public sealed record ProxySweepResult(int Orphans, long Bytes, int TemporaryFiles, int ConfigsGone); |
| | | 15 | | |
| | 15 | 16 | | internal sealed partial class ProxySweepHandler( |
| | 15 | 17 | | IProxyOrphanStore store, |
| | 15 | 18 | | IProxyDirectoryStrategy directories, |
| | 15 | 19 | | IFileSystem fileSystem, |
| | 15 | 20 | | IClock clock, |
| | 15 | 21 | | ILogger<ProxySweepHandler> logger) : IWorkHandler<ProxySweepPayload> |
| | | 22 | | { |
| | | 23 | | internal const string ScanningNote = "Scanning proxy shards"; |
| | | 24 | | |
| | | 25 | | internal static Duration GracePeriod { get; } = Duration.FromHours(1); |
| | | 26 | | |
| | | 27 | | public async Task<object?> RunAsync(ProxySweepPayload payload, WorkRun run, CancellationToken ct) |
| | 14 | 28 | | { |
| | 14 | 29 | | if (!fileSystem.Directory.Exists(StoragePath.ProxiesRoot)) |
| | 1 | 30 | | throw new DirectoryNotFoundException($"The proxy root {StoragePath.ProxiesRoot} is missing."); |
| | | 31 | | |
| | 13 | 32 | | var writtenBefore = clock.GetCurrentInstant() - GracePeriod; |
| | 13 | 33 | | var atRoot = await ScanRootAsync(writtenBefore, ct); |
| | 13 | 34 | | await store.RecordAsync(run.ItemId, atRoot, ct); |
| | | 35 | | |
| | 13 | 36 | | var shards = fileSystem.Directory.EnumerateDirectories(StoragePath.ProxiesRoot).Order(StringComparer.Ordinal).To |
| | 48 | 37 | | for (var i = 0; i < shards.Count; i++) |
| | 11 | 38 | | { |
| | 11 | 39 | | run.Progress.Report(i, shards.Count, ScanningNote); |
| | 11 | 40 | | await store.RecordAsync(run.ItemId, await ScanShardAsync(shards[i], writtenBefore, ct), ct); |
| | 11 | 41 | | } |
| | | 42 | | |
| | 13 | 43 | | run.Progress.Report(shards.Count, shards.Count, ScanningNote); |
| | 13 | 44 | | var result = await store.SummariseAsync(run.ItemId, ct); |
| | 13 | 45 | | Log.Swept(logger, result.Orphans, result.Bytes, result.TemporaryFiles, result.ConfigsGone); |
| | 13 | 46 | | return result; |
| | 13 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async Task<List<FoundOrphan>> ScanRootAsync(Instant writtenBefore, CancellationToken ct) |
| | 13 | 50 | | { |
| | 13 | 51 | | var found = new List<FoundOrphan>(); |
| | 45 | 52 | | foreach (var file in fileSystem.Directory.EnumerateFiles(StoragePath.ProxiesRoot)) |
| | 3 | 53 | | { |
| | 3 | 54 | | var path = RelativePath(file); |
| | 3 | 55 | | if (Unreferenced(file, path, writtenBefore) is { } orphan && !await store.IsReferencedAsync(path, ct)) |
| | 1 | 56 | | found.Add(orphan); |
| | 3 | 57 | | } |
| | | 58 | | |
| | 13 | 59 | | return found; |
| | 13 | 60 | | } |
| | | 61 | | |
| | | 62 | | private async Task<List<FoundOrphan>> ScanShardAsync(string shard, Instant writtenBefore, CancellationToken ct) |
| | 11 | 63 | | { |
| | 11 | 64 | | var referenced = await store.ReferencedUnderAsync(fileSystem.Path.GetFileName(shard), ct); |
| | 11 | 65 | | return [.. FilesUnder(shard) |
| | 11 | 66 | | .Select(file => (File: file, Path: RelativePath(file))) |
| | 11 | 67 | | .Where(file => !referenced.Contains(file.Path)) |
| | 11 | 68 | | .Select(file => Unreferenced(file.File, file.Path, writtenBefore)) |
| | 11 | 69 | | .OfType<FoundOrphan>()]; |
| | 11 | 70 | | } |
| | | 71 | | |
| | | 72 | | private IEnumerable<string> FilesUnder(string directory) |
| | 23 | 73 | | => fileSystem.Directory.EnumerateFiles(directory) |
| | 23 | 74 | | .Concat(fileSystem.Directory.EnumerateDirectories(directory).SelectMany(FilesUnder)); |
| | | 75 | | |
| | | 76 | | private FoundOrphan? Unreferenced(string file, string path, Instant writtenBefore) |
| | 16 | 77 | | { |
| | 16 | 78 | | var stamp = ProxyFileStamp.Of(fileSystem.FileInfo.New(file)); |
| | 16 | 79 | | return stamp.LastWriteTime < writtenBefore |
| | 16 | 80 | | ? new FoundOrphan(path, directories.StorageConfigOf(path), stamp.SizeBytes, stamp.LastWriteTime) |
| | 16 | 81 | | : null; |
| | 16 | 82 | | } |
| | | 83 | | |
| | 17 | 84 | | private string RelativePath(string file) => fileSystem.Path.GetRelativePath(StoragePath.ProxiesRoot, file); |
| | | 85 | | |
| | | 86 | | private static partial class Log |
| | | 87 | | { |
| | | 88 | | [LoggerMessage( |
| | | 89 | | Level = LogLevel.Information, |
| | | 90 | | Message = "Swept the proxy root: {Orphans} orphan(s), {Bytes} B, {TemporaryFiles} temporary file(s), {Config |
| | | 91 | | public static partial void Swept(ILogger logger, int orphans, long bytes, int temporaryFiles, int configsGone); |
| | | 92 | | } |
| | | 93 | | } |