On Unraid the archive is a user share (FUSE shfs), which reports the share's primary storage, often the cache, while the mover sends the photos to the array: the admin panel showed the cache's free space (owner review 2, R2). - New option SpacePath (server.json) / CHRONOS_SPACE_PATH: the admin panel shows that folder's volume; empty, or a folder that cannot be measured, falls back to the archive's volume (one warning, at start, not per request). - VolumeSpace measures folders (DriveInfo; the path root on Windows) and reads the filesystem type and mount from /proc/self/mountinfo on Linux (longest mount point on segment boundaries, octal escapes, optional fields, the top of stacked mounts), since DriveFormat calls an shfs share only "fuse". It is an injectable service, so tests simulate a full volume; the /data checks use it. - ArchiveStatsDto keeps FreeBytes/CapacityBytes for the shown volume and gains optional trailing members: the write volume's figures (when a space path is in use and they differ), the filesystem type and UsesSpacePath. - Admin panel (dashboard and archive): a tooltip on the storage figures names the volume and its filesystem and, on an shfs share without a space path, says they are the primary storage and to map the array view to /space; a "Write volume" line shows the archive's own volume when it differs. - Completing an upload, a render upload or a render node's result checks the archive volume for its size plus the 64 MiB reserve before the staging copy and answers 507 insufficient_storage, the upload still receiving. - Diagnostics list the archive and space volumes with filesystem and mount. - Unraid template: optional read-only "Array view" /space (default /mnt/user0/Timelapse) and "Space path" CHRONOS_SPACE_PATH=/space; compose and the releases README describe the same as an option. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
7.5 KiB
C#
167 lines
7.5 KiB
C#
using System.Collections.Concurrent;
|
|
using Microsoft.Extensions.Logging;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Server.Configuration;
|
|
using Chronos.Server.Storage;
|
|
|
|
namespace Chronos.Server.Tests;
|
|
|
|
/// <summary>
|
|
/// Stand-in for the archive module's ingest: files staged uploads as <c><folder>/originals/<name></c>, one
|
|
/// sequence per session key, and records every request it received.
|
|
/// </summary>
|
|
public sealed class FakeArchiveIngest(ChronosDatabase db) : IArchiveIngest
|
|
{
|
|
private readonly ConcurrentDictionary<string, Guid> _sessions = new(StringComparer.Ordinal);
|
|
private readonly SemaphoreSlim _gate = new(1, 1);
|
|
|
|
public ConcurrentQueue<IngestRequest> Requests { get; } = new();
|
|
|
|
public async Task<IngestResult> CommitAsync(IngestRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
Requests.Enqueue(request);
|
|
await _gate.WaitAsync(cancellationToken);
|
|
try
|
|
{
|
|
var archive = (await db.Archives.GetAsync(request.ArchiveId, cancellationToken))!;
|
|
var sequence = await SequenceForAsync(request, archive, cancellationToken);
|
|
var existing = (await db.Frames.FindBySha256Async(request.Sha256, cancellationToken)).FirstOrDefault(f => f.SequenceId == sequence.Id);
|
|
if (existing is not null)
|
|
{
|
|
File.Delete(request.StagedPath);
|
|
return new IngestResult(sequence.Id, existing.Id, existing.Index, existing.RelativePath, Duplicate: true);
|
|
}
|
|
var index = sequence.FrameCount;
|
|
var relative = $"{sequence.Folder}/originals/{index:D4}_{request.OriginalFileName}";
|
|
var target = Path.Combine(archive.RootPath, relative);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
|
File.Move(request.StagedPath, target);
|
|
var frame = new Frame
|
|
{
|
|
Id = Guid.CreateVersion7(),
|
|
SequenceId = sequence.Id,
|
|
Index = index,
|
|
RelativePath = relative,
|
|
FileName = Path.GetFileName(relative),
|
|
OriginalFileName = request.OriginalFileName,
|
|
Size = request.Size,
|
|
Sha256 = request.Sha256,
|
|
CapturedAt = request.ClientCapturedAt,
|
|
};
|
|
await db.Frames.InsertAsync(frame, cancellationToken);
|
|
await db.Sequences.RefreshStatsAsync(sequence.Id, cancellationToken);
|
|
return new IngestResult(sequence.Id, frame.Id, index, relative, Duplicate: false);
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
private async Task<Sequence> SequenceForAsync(IngestRequest request, Archive archive, CancellationToken cancellationToken)
|
|
{
|
|
Sequence? sequence = null;
|
|
if (request.SequenceId is { } id) sequence = await db.Sequences.GetAsync(id, cancellationToken);
|
|
else if (request.SessionKey is { } key && _sessions.TryGetValue(key, out var known)) sequence = await db.Sequences.GetAsync(known, cancellationToken);
|
|
if (sequence is null)
|
|
{
|
|
sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = archive.Id, Folder = string.Empty, Title = request.NewSequenceTitle, OwnerUserId = request.UserId };
|
|
if (request.SessionKey is { } key) _sessions[key] = sequence.Id;
|
|
}
|
|
if (string.IsNullOrEmpty(sequence.Folder))
|
|
{
|
|
sequence = sequence with { Folder = $"2026/{sequence.Id:N}", UpdatedAt = DateTimeOffset.UtcNow };
|
|
await db.Sequences.UpsertAsync(sequence, cancellationToken);
|
|
}
|
|
return sequence;
|
|
}
|
|
}
|
|
|
|
/// <summary>Proxies at <c><folder>/proxies/NNNN.jpg</c>; thumbnails as 64-byte slots of <c><folder>/.chronos/thumbs.pack</c>.</summary>
|
|
public sealed class FakeAssetLocator : IFrameAssetLocator
|
|
{
|
|
public const int ThumbnailSize = 64;
|
|
|
|
/// <summary>When set, every lookup answers this location (path traversal tests).</summary>
|
|
public AssetLocation? Override { get; set; }
|
|
|
|
public ValueTask<AssetLocation?> LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken)
|
|
{
|
|
if (Override is not null) return ValueTask.FromResult<AssetLocation?>(Override);
|
|
var folder = Path.Combine(archive.RootPath, sequence.Folder);
|
|
AssetLocation? location = asset == FrameAsset.Proxy
|
|
? new AssetLocation(Path.Combine(folder, "proxies", $"{frame.Index:D4}.jpg"), 0, -1, "image/jpeg")
|
|
: new AssetLocation(Path.Combine(folder, ".chronos", "thumbs.pack"), (long)frame.Index * ThumbnailSize, ThumbnailSize, "image/jpeg");
|
|
if (!File.Exists(location.Path)) return ValueTask.FromResult<AssetLocation?>(null);
|
|
if (location.Length < 0) location = location with { Length = new FileInfo(location.Path).Length };
|
|
return ValueTask.FromResult<AssetLocation?>(location);
|
|
}
|
|
}
|
|
|
|
/// <summary>Measures volumes for real, except the folders given figures of their own (a full archive, a separate array).</summary>
|
|
public sealed class FakeVolumeSpace(ServerOptions options, ILogger<VolumeSpace> logger) : VolumeSpace(options, logger)
|
|
{
|
|
private readonly ConcurrentDictionary<string, VolumeFigures> _figures = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Gives <paramref name="path"/> these figures; null measures it for real again.</summary>
|
|
public void Set(string path, VolumeFigures? figures)
|
|
{
|
|
if (figures is null) _figures.TryRemove(Key(path), out _);
|
|
else _figures[Key(path)] = figures;
|
|
}
|
|
|
|
public override VolumeFigures? Measure(string path) => _figures.TryGetValue(Key(path), out var figures) ? figures : base.Measure(path);
|
|
|
|
private static string Key(string path) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
|
|
}
|
|
|
|
/// <summary>A clock tests move by hand (token expiry, heartbeat timeouts).</summary>
|
|
public sealed class ManualTimeProvider : TimeProvider
|
|
{
|
|
private long _ticks = DateTimeOffset.UtcNow.UtcTicks;
|
|
|
|
public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero);
|
|
|
|
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A job handler that reports progress and then waits until <see cref="Release"/> (or cancellation), so tests can
|
|
/// observe running, paused and cancelled states.
|
|
/// </summary>
|
|
public sealed class BlockingJobHandler(JobKind kind) : IJobHandler
|
|
{
|
|
private TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
public JobKind Kind => kind;
|
|
|
|
public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
/// <summary>When set, the next run fails with this message.</summary>
|
|
public string? FailWith { get; set; }
|
|
|
|
public void Release() => _release.TrySetResult();
|
|
|
|
public async Task RunAsync(JobContext context)
|
|
{
|
|
await context.ReportAsync(1, 4, "started");
|
|
Started.TrySetResult();
|
|
if (FailWith is { } message)
|
|
{
|
|
FailWith = null;
|
|
throw new InvalidOperationException(message);
|
|
}
|
|
for (var i = 2; i <= 3; i++)
|
|
{
|
|
await Task.Delay(50, context.Cancellation);
|
|
await context.ReportAsync(i, 4);
|
|
}
|
|
await _release.Task.WaitAsync(context.Cancellation);
|
|
_release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
await context.ReportAsync(4, 4);
|
|
}
|
|
}
|