| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using Anichron.Core.Work; |
| | | 6 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | using NodaTime; |
| | | 9 | | |
| | | 10 | | namespace Anichron.Worker.ProxySweeps; |
| | | 11 | | |
| | | 12 | | internal sealed record FoundOrphan(string ProxyPath, Guid? StorageConfigId, long SizeBytes, Instant LastWriteTime); |
| | | 13 | | |
| | | 14 | | internal interface IProxyOrphanStore |
| | | 15 | | { |
| | | 16 | | /// <summary>Every <see cref="ProxyFile.ProxyPath"/> under a top-level shard folder, soft-deleted assets' included.< |
| | | 17 | | /// <param name="shard">The shard folder's name.</param> |
| | | 18 | | /// <param name="ct">Cancels the read.</param> |
| | | 19 | | Task<IReadOnlySet<string>> ReferencedUnderAsync(string shard, CancellationToken ct); |
| | | 20 | | |
| | | 21 | | /// <summary>Whether any <see cref="ProxyFile"/> row, a soft-deleted asset's included, references the path.</summary |
| | | 22 | | /// <param name="proxyPath">A path relative to the proxy root.</param> |
| | | 23 | | /// <param name="ct">Cancels the read.</param> |
| | | 24 | | Task<bool> IsReferencedAsync(string proxyPath, CancellationToken ct); |
| | | 25 | | |
| | | 26 | | /// <summary>Adds the orphans to the sweep's report, skipping paths it already holds.</summary> |
| | | 27 | | /// <param name="sweepId">The sweep's work item.</param> |
| | | 28 | | /// <param name="orphans">The orphans found.</param> |
| | | 29 | | /// <param name="ct">Cancels the insert.</param> |
| | | 30 | | Task RecordAsync(Guid sweepId, IReadOnlyList<FoundOrphan> orphans, CancellationToken ct); |
| | | 31 | | |
| | | 32 | | /// <summary>What the sweep's report holds, rows an earlier attempt of it recorded included.</summary> |
| | | 33 | | /// <param name="sweepId">The sweep's work item.</param> |
| | | 34 | | /// <param name="ct">Cancels the read.</param> |
| | | 35 | | Task<ProxySweepResult> SummariseAsync(Guid sweepId, CancellationToken ct); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The first unprocessed orphans in the deletion's scope, by id: of its config, or, unscoped, of no |
| | | 39 | | /// existing config. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="deletion">The sweep, the scope, and the optional selection.</param> |
| | | 42 | | /// <param name="take">How many to read at most.</param> |
| | | 43 | | /// <param name="ct">Cancels the read.</param> |
| | | 44 | | Task<IReadOnlyList<ProxyOrphan>> GetPendingAsync(ProxyDeletionPayload deletion, int take, CancellationToken ct); |
| | | 45 | | |
| | | 46 | | /// <summary>Stops tracking the orphans read so far.</summary> |
| | | 47 | | void ForgetLoaded(); |
| | | 48 | | } |
| | | 49 | | |
| | 15 | 50 | | internal sealed class EfProxyOrphanStore(AnichronDbContext db, IGuidFactory guids) : IProxyOrphanStore |
| | | 51 | | { |
| | | 52 | | public async Task<IReadOnlySet<string>> ReferencedUnderAsync(string shard, CancellationToken ct) |
| | 2 | 53 | | => (await ReferencedUnderQuery(db, shard).ToListAsync(ct)).ToHashSet(StringComparer.Ordinal); |
| | | 54 | | |
| | | 55 | | public Task<bool> IsReferencedAsync(string proxyPath, CancellationToken ct) |
| | 3 | 56 | | => db.ProxyFiles.IgnoreQueryFilters().AnyAsync(p => p.ProxyPath == proxyPath, ct); |
| | | 57 | | |
| | | 58 | | // Stryker disable all |
| | | 59 | | public async Task RecordAsync(Guid sweepId, IReadOnlyList<FoundOrphan> orphans, CancellationToken ct) |
| | 0 | 60 | | { |
| | 0 | 61 | | if (orphans.Count == 0) |
| | 0 | 62 | | return; |
| | | 63 | | |
| | | 64 | | var ids = orphans.Select(_ => guids.NewGuid()).ToArray(); |
| | 0 | 65 | | var paths = orphans.Select(o => o.ProxyPath).ToArray(); |
| | 0 | 66 | | var configs = orphans.Select(o => o.StorageConfigId).ToArray(); |
| | 0 | 67 | | var sizes = orphans.Select(o => o.SizeBytes).ToArray(); |
| | 0 | 68 | | var times = orphans.Select(o => o.LastWriteTime).ToArray(); |
| | 0 | 69 | | await db.Database.ExecuteSqlInterpolatedAsync( |
| | 0 | 70 | | $""" |
| | 0 | 71 | | INSERT INTO "ProxyOrphans" ("Id", "SweepId", "ProxyPath", "StorageConfigId", "SizeBytes", "LastWriteTime") |
| | 0 | 72 | | SELECT o.id, {sweepId}, o.path, o.config, o.size, o.time |
| | 0 | 73 | | FROM unnest({ids}, {paths}, {configs}, {sizes}, {times}) AS o(id, path, config, size, time) |
| | 0 | 74 | | ON CONFLICT ("SweepId", "ProxyPath") DO NOTHING |
| | 0 | 75 | | """, |
| | 0 | 76 | | ct); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public async Task<ProxySweepResult> SummariseAsync(Guid sweepId, CancellationToken ct) |
| | 2 | 80 | | { |
| | 2 | 81 | | var report = db.ProxyOrphans.Where(o => o.SweepId == sweepId); |
| | 2 | 82 | | var live = db.StorageConfigs.LiveIds(); |
| | 2 | 83 | | return new ProxySweepResult( |
| | 2 | 84 | | await report.CountAsync(ct), |
| | 2 | 85 | | await report.SumAsync(o => o.SizeBytes, ct), |
| | 2 | 86 | | await report.CountAsync(o => o.ProxyPath.EndsWith(ProxyStagingWriter.TemporaryFileSuffix), ct), |
| | 2 | 87 | | await report |
| | 2 | 88 | | .Where(o => o.StorageConfigId != null && !live.Contains(o.StorageConfigId)) |
| | 2 | 89 | | .Select(o => o.StorageConfigId) |
| | 2 | 90 | | .Distinct() |
| | 2 | 91 | | .CountAsync(ct)); |
| | 2 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task<IReadOnlyList<ProxyOrphan>> GetPendingAsync(ProxyDeletionPayload deletion, int take, CancellationT |
| | 7 | 95 | | { |
| | 7 | 96 | | var pending = db.ProxyOrphans.Where(o => o.SweepId == deletion.SweepId && o.DeletionOutcome == null); |
| | 7 | 97 | | var live = db.StorageConfigs.LiveIds(); |
| | 7 | 98 | | pending = deletion.StorageConfigId is { } configId |
| | 7 | 99 | | ? pending.Where(o => o.StorageConfigId == configId) |
| | 7 | 100 | | : pending.Where(o => o.StorageConfigId == null || !live.Contains(o.StorageConfigId)); |
| | 7 | 101 | | if (deletion.OrphanIds is { } selected) |
| | 2 | 102 | | pending = pending.Where(o => selected.Contains(o.Id)); |
| | 7 | 103 | | return await pending.OrderBy(o => o.Id).Take(take).ToListAsync(ct); |
| | 7 | 104 | | } |
| | | 105 | | |
| | 1 | 106 | | public void ForgetLoaded() => db.ChangeTracker.Clear(); |
| | | 107 | | |
| | | 108 | | internal static IQueryable<string> ReferencedUnderQuery(AnichronDbContext db, string shard) |
| | 3 | 109 | | => db.ProxyFiles.IgnoreQueryFilters() |
| | 3 | 110 | | .Where(p => p.ProxyPath.StartsWith(shard + "/")) |
| | 3 | 111 | | .Select(p => p.ProxyPath); |
| | | 112 | | } |