diff --git a/README.md b/README.md index b92585e1..a73b419a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Standalone VectorNNTPD hosts extracted from VectorNNTP.NNTPD. This repository cu ## Structure - `src/VectorNNTPD.BackFillerService/VectorNNTPD.BackFillerService.csproj` - BackFiller worker app (net10.0) -- `src/VectorNNTPD.StorageService/VectorNNTPD.StorageService.csproj` - StorageService / VATP cache host (net10.0) +- `src/VectorNNTPD.StorageService/VectorNNTPD.StorageService.csproj` - StorageService / NNTP TAKETHIS feed host (net10.0) - `src/VectorNNTPD.Common/VectorNNTPD.Common.csproj` - shared library - `tests/VectorNNTPD.BackFillerService.Tests/` / `tests/VectorNNTPD.StorageService.Tests/` / `tests/VectorNNTPD.Common.Tests/` - xUnit suites - `VectorNNTPD.slnx` - solution including apps + tests diff --git a/docs/common/vatp.md b/docs/common/vatp.md index 6a113910..c3f2c479 100644 --- a/docs/common/vatp.md +++ b/docs/common/vatp.md @@ -193,17 +193,15 @@ and requires removal after terminalization before reuse. NNTPD retrieval already opens many OPEN streams on one pooled TLS connection, up to `MaxStreamsPerConnection` (64). BackFiller schedules their DATA frames one at a time on -a ready ring. StorageServer accepts concurrent STORE streams on one connection the same -way, and writes RESULT after the article is enqueued. `ArticlePlacementClient` keeps one TLS connection per target and sends at most -`Nntpd:ArticleIngestion:MaxOutstandingStoresPerConnection` STOREs on it (default 2, allowed 1 or 2). -One writer emits complete frames. A lost RESULT after END is acknowledgement-not-observed and is not -retried. The spool writer still does not call placement. Retrieval OPEN multiplexing is unchanged. +a ready ring. StorageService article *ingestion* is NNTP RFC 4644 `TAKETHIS` (not VATP STORE); +the Common advertisement field `vatpPort` still names the TLS feed listen port for placement. +Retrieval OPEN multiplexing is unchanged. VATP STORE multiplexing language that formerly +described StorageServer ingestion no longer applies to this host. ## Not implemented yet (later phases) - Numeric ARTICLE/HEAD/BODY/STAT (article-number / current-article forms) - OverviewDB article-number resolution -- Storage VATP consumers - Outbound VATP server-to-server transfers ## NNTP message-id retrieval (Phase 3B) diff --git a/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs b/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs index 183e153b..60297944 100644 --- a/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs +++ b/src/VectorNNTPD.StorageService/Configuration/SpoolPersistenceOptions.cs @@ -6,7 +6,7 @@ namespace VectorNNTPD.StorageService.Configuration /// /// /// - /// VATP RESULT is still sent when an article enters AcceptedArticleQueue. + /// TAKETHIS 239 is still sent when an article enters AcceptedArticleQueue. /// These settings bound later disk work. They do not make that RESULT durable. /// /// diff --git a/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs b/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs index 8db90224..6b21d95b 100644 --- a/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs +++ b/src/VectorNNTPD.StorageService/Configuration/StorageServerOptions.cs @@ -112,7 +112,7 @@ ServerId is { } serverId public int BindPort { get; set; } /// - /// Gets or sets the TLS TCP port used by the VATP listener. + /// Gets or sets the TLS TCP port used by the NNTP feed listener. /// /// /// Required. Range 1–65535. StorageServer is TLS-only; there is no cleartext fallback. diff --git a/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs b/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs index ea542980..42086907 100644 --- a/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs +++ b/src/VectorNNTPD.StorageService/Hosting/StorageServerServiceCollectionExtensions.cs @@ -35,7 +35,7 @@ public static class StorageServerServiceCollectionExtensions /// /// /// Application service start order: - /// NntpDB → nntpsharedconfig → Cloudflare DNS → RabbitMQ (connectivity only) → ACME → segment-pool validation → spool shard initialization → article index → block-device monitor → accepted-article worker → VATP listener → AdvertisementPublisher. + /// NntpDB → nntpsharedconfig → Cloudflare DNS → RabbitMQ (connectivity only) → ACME → segment-pool validation → spool shard initialization → article index → block-device monitor → accepted-article worker → NNTP feed listener → AdvertisementPublisher. /// Shutdown is that order reversed, so advertisement stops before the listener, and the listener completes the queue before the worker stops. /// Topology, queues, and consumers are not registered here. NntpDB does not migrate schema. /// @@ -138,8 +138,6 @@ public static HostApplicationBuilder AddStorageServerHosting(this HostApplicatio builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); - builder.Services.TryAddSingleton(); - builder.Services.TryAddSingleton(static sp => new DelegateRabbitMqConnectionNameProvider(() => RabbitMqRuntimeOptions.GetDefaultConnectionName( @@ -204,19 +202,33 @@ public static HostApplicationBuilder AddStorageServerHosting(this HostApplicatio { var listener = provider.GetRequiredService>().Value.Listener; var dns = provider.GetRequiredService(); - return new StorageVatpListenerService( + var index = provider.GetService(); + Func? articlePresent = index is null + ? null + : id => + { + try + { + return index.IsReady && index.Contains(id); + } + catch (InvalidOperationException) + { + return false; + } + }; + return new StorageNntpListenerService( provider.GetRequiredService(), provider.GetRequiredService(), - provider.GetRequiredService(), provider.GetRequiredService(), - provider.GetRequiredService>(), + provider.GetRequiredService>(), provider.GetRequiredService(), new StoreAssemblyAdmission(listener.MaxActiveConnections), + articlePresent: articlePresent, publishAfterBind: dns.PublishAfterListenerBoundAsync); }); builder.Services.TryAddEnumerable( - ServiceDescriptor.Singleton(static provider => - provider.GetRequiredService())); + ServiceDescriptor.Singleton(static provider => + provider.GetRequiredService())); builder.Services.TryAddEnumerable( ServiceDescriptor.Singleton(static sp => sp.GetRequiredService())); diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs index dae55b45..d8b6f8b1 100644 --- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs +++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticle.cs @@ -4,7 +4,7 @@ namespace VectorNNTPD.StorageService.Ingress { /// - /// One accepted VATP article handed to in-memory consumers before Path rewrite. + /// One accepted feed article handed to in-memory consumers before Path rewrite. /// /// Article identity from the accepted inbound bytes. /// Hash of , matching the inbound META hash. diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs index 6ff87ac6..8646178a 100644 --- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs +++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleQueue.cs @@ -15,7 +15,7 @@ namespace VectorNNTPD.StorageService.Ingress /// /// /// The default budget is one gibibyte. At 768,000-byte articles that is about 1,398 articles. - /// VATP RESULT is sent only after a true enqueue. A false enqueue is the overload signal + /// TAKETHIS 239 is sent only after a true enqueue. A false enqueue is the overload signal /// and is not a durable-storage acknowledgement. /// /// diff --git a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs index 46225c58..98bd3246 100644 --- a/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs +++ b/src/VectorNNTPD.StorageService/Ingress/AcceptedArticleWorker.cs @@ -22,7 +22,7 @@ namespace VectorNNTPD.StorageService.Ingress /// Each worker holds at most one dequeued article. A waiting-for-capacity result keeps that /// article off the queue and retries after . /// Shutdown cancels in-flight publishes. An article already dequeued and not yet published is - /// logged and dropped. VATP RESULT was sent at enqueue and is not repeated here. + /// logged and dropped. TAKETHIS 239 was sent at enqueue and is not repeated here. /// /// internal sealed class AcceptedArticleWorker : IApplicationService diff --git a/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs b/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs index 143044ba..1b5601b2 100644 --- a/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs +++ b/src/VectorNNTPD.StorageService/Ingress/IAcceptedArticleQueue.cs @@ -9,7 +9,7 @@ namespace VectorNNTPD.StorageService.Ingress public readonly record struct AcceptedArticleDelivery(bool HasArticle, AcceptedArticle Article); /// - /// In-memory handoff of accepted VATP articles. Multiple sessions may enqueue, + /// In-memory handoff of accepted feed articles. Multiple sessions may enqueue, /// and multiple consumers may dequeue. The queue does not persist articles. /// public interface IAcceptedArticleQueue diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs b/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs deleted file mode 100644 index c54516cf..00000000 --- a/src/VectorNNTPD.StorageService/Listener/IStorageArticleOpenBoundary.cs +++ /dev/null @@ -1,78 +0,0 @@ -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; - -namespace VectorNNTPD.StorageService.Listener -{ - - /// - /// Boundary between the VATP OPEN path and article bytes. - /// - /// - /// Spool files are not an OPEN index. The host registration still rejects every OPEN. - /// RequestId is correlation metadata. Null and rejecting implementations remain for tests. - /// - public interface IStorageArticleOpenBoundary - { - /// - /// Attempts to open a transfer for the given RequestId and ArticleId. - /// - /// Client OPEN RequestId. Not consulted as a storage key. - /// Client OPEN ArticleId. - /// - /// Accepted result carrying the canonical record, or a rejected result with no record. - /// - StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId); - } - - /// Result of an article-open attempt. - /// Whether OPEN can be served. - /// Optional human-readable rejection reason (ASCII-safe, no secrets). - /// Canonical record when is true; otherwise default. - /// Date-family header used to build VATP META when accepted. - public readonly record struct StorageArticleOpenResult( - bool Accepted, - string? Reason = null, - ArticleRecord Record = default, - NntpArticleHeaderName SelectedDateHeaderName = NntpArticleHeaderName.Unknown) - { - /// Rejected open result. Does not carry an article record. - public static StorageArticleOpenResult Rejected(string? reason = null) => new(false, reason); - - /// Accepted open result. The session sends META and DATA from . - public static StorageArticleOpenResult Opened( - ArticleRecord record, - NntpArticleHeaderName selectedDateHeaderName) => - new(true, null, record, selectedDateHeaderName); - } - - /// - /// Default open boundary that always rejects OPEN requests. - /// - public sealed class RejectingStorageArticleOpenBoundary : IStorageArticleOpenBoundary - { - /// - public StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId) - { - _ = requestId; - _ = articleId; - return StorageArticleOpenResult.Rejected("storage-not-implemented"); - } - } - - /// - /// Null-object open boundary that always rejects OPEN requests. - /// - public sealed class NullStorageArticleOpenBoundary : IStorageArticleOpenBoundary - { - /// Shared singleton instance. - public static NullStorageArticleOpenBoundary Instance { get; } = new(); - - /// - public StorageArticleOpenResult TryOpen(Guid requestId, in ArticleId articleId) - { - _ = requestId; - _ = articleId; - return StorageArticleOpenResult.Rejected("storage-not-implemented"); - } - } -} diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs b/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs new file mode 100644 index 00000000..a44a6e11 --- /dev/null +++ b/src/VectorNNTPD.StorageService/Listener/IStorageListenerTransport.cs @@ -0,0 +1,19 @@ +namespace VectorNNTPD.StorageService.Listener +{ + /// One established TLS transport owned by a StorageServer NNTP feed session. + public interface IStorageListenerTransport : IAsyncDisposable + { + /// Reads bytes. Zero means the peer closed. + ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken); + + /// Writes bytes and returns how many were accepted. + ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken); + + /// + /// Suspends or resumes the read no-progress timeout. + /// A TAKETHIS wait for queue capacity suspends it until that wait ends. + /// + /// to disarm the outstanding read timer. + void SetReadProgressSuspended(bool suspended) { } + } +} diff --git a/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs new file mode 100644 index 00000000..fb46ef76 --- /dev/null +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpFeedSession.cs @@ -0,0 +1,844 @@ +using System.Buffers; +using VectorNNTPD.Common.Articles; +using VectorNNTPD.Common.Articles.Parsing; +using VectorNNTPD.Common.Articles.Validation; +using VectorNNTPD.Common.Core; +using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.StorageService.Ingress; + +namespace VectorNNTPD.StorageService.Listener +{ + /// + /// RFC 4644 STREAMING feed session over an established TLS transport. + /// Accepts CAPABILITIES, MODE STREAM, CHECK, TAKETHIS, and QUIT. + /// A successful TAKETHIS commits into before 239 is written. + /// + public sealed partial class StorageNntpFeedSession : IAsyncDisposable + { + private const int MaximumCommandOctets = 512; + + /// + /// Initial destuffed-assembly rental. Grows on demand up to + /// . Avoids a 1 MiB pool rent for typical articles. + /// + private const int InitialAssemblyRentBytes = 64 * 1024; + + private static readonly byte[] Greeting = "200 VectorNNTP StorageServer ready\r\n"u8.ToArray(); + private static readonly byte[] CapabilitiesHeader = "101 Capability list follows\r\n"u8.ToArray(); + private static readonly byte[] CapabilitiesBody = "VERSION 2\r\nSTREAMING\r\nIMPLEMENTATION VectorNNTP-StorageServer\r\n.\r\n"u8.ToArray(); + private static readonly byte[] ModeStreamOk = "203 Streaming allowed\r\n"u8.ToArray(); + private static readonly byte[] Closing = "205 Connection closing\r\n"u8.ToArray(); + private static readonly byte[] SyntaxError = "501 Syntax error\r\n"u8.ToArray(); + private static readonly byte[] UnknownCommand = "500 Unknown command\r\n"u8.ToArray(); + private static readonly byte[] TemporaryFailure = "400 Temporary failure\r\n"u8.ToArray(); + + private static readonly byte[] CapabilitiesKeyword = "CAPABILITIES"u8.ToArray(); + private static readonly byte[] ModeKeyword = "MODE"u8.ToArray(); + private static readonly byte[] StreamKeyword = "STREAM"u8.ToArray(); + private static readonly byte[] CheckKeyword = "CHECK"u8.ToArray(); + private static readonly byte[] TakeThisKeyword = "TAKETHIS"u8.ToArray(); + private static readonly byte[] QuitKeyword = "QUIT"u8.ToArray(); + + private readonly IStorageListenerTransport _transport; + private readonly StorageServerListenerRuntimeOptions _listener; + private readonly ILogger _logger; + private readonly IAcceptedArticleQueue? _acceptedArticles; + private readonly StoreAssemblyAdmission? _assemblyAdmission; + private readonly Func? _articlePresent; + private readonly NntpArticleParser _parser; + private byte[] _pending; + private readonly byte[] _readScratch; + private readonly byte[] _statusScratch; + private int _pendingCount; + private int _disposed; + + /// Initializes a STREAMING feed session over an established transport. + public StorageNntpFeedSession( + IStorageListenerTransport transport, + StorageServerListenerRuntimeOptions listener, + ILogger logger, + string pathIdentity, + IAcceptedArticleQueue? acceptedArticles = null, + StoreAssemblyAdmission? assemblyAdmission = null, + Func? articlePresent = null) + { + ArgumentNullException.ThrowIfNull(transport); + ArgumentNullException.ThrowIfNull(listener); + ArgumentNullException.ThrowIfNull(logger); + ArgumentException.ThrowIfNullOrWhiteSpace(pathIdentity); + _transport = transport; + _listener = listener; + _logger = logger; + _acceptedArticles = acceptedArticles; + _assemblyAdmission = assemblyAdmission; + _articlePresent = articlePresent; + _parser = new NntpArticleParser(pathIdentity.Trim()); + var capacity = Math.Max(listener.ParserAccumulationMaxBytes, MaximumCommandOctets); + _pending = new byte[MaximumCommandOctets]; + _readScratch = new byte[Math.Min(capacity, 64 * 1024)]; + _statusScratch = new byte[4 + 1 + NntpMessageIdValidation.MaxMessageIdLength + 2]; + } + + /// Sends the greeting, then reads commands until QUIT, peer close, or cancellation. + public async Task RunAsync(CancellationToken cancellationToken) + { + await WriteFullyAsync(Greeting, cancellationToken).ConfigureAwait(false); + while (!cancellationToken.IsCancellationRequested) + { + var command = await ReadCommandAsync(cancellationToken).ConfigureAwait(false); + if (command.Kind == CommandReadKind.Disconnected) + { + return; + } + + if (command.Kind == CommandReadKind.Overlong) + { + await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false); + continue; + } + + var line = command.Line.Span; + if (TryMatchKeyword(line, QuitKeyword, out var quitRest)) + { + if (!quitRest.IsEmpty) + { + await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false); + continue; + } + + await WriteFullyAsync(Closing, cancellationToken).ConfigureAwait(false); + return; + } + + if (TryMatchKeyword(line, CapabilitiesKeyword, out var capsRest)) + { + if (!capsRest.IsEmpty) + { + await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false); + continue; + } + + await WriteFullyAsync(CapabilitiesHeader, cancellationToken).ConfigureAwait(false); + await WriteFullyAsync(CapabilitiesBody, cancellationToken).ConfigureAwait(false); + continue; + } + + if (TryMatchModeStream(line, out var modeStreamSyntaxError)) + { + await WriteFullyAsync( + modeStreamSyntaxError ? SyntaxError : ModeStreamOk, + cancellationToken) + .ConfigureAwait(false); + continue; + } + + if (TryMatchKeyword(line, CheckKeyword, out var checkRest)) + { + await HandleCheckAsync(SkipSpaces(checkRest), cancellationToken).ConfigureAwait(false); + continue; + } + + if (TryMatchKeyword(line, TakeThisKeyword, out var takeRest)) + { + // One heap copy of the message-id argument; spans cannot cross the TAKETHIS awaits. + var takeArg = SkipSpaces(takeRest).ToArray(); + var keepOpen = await HandleTakeThisAsync(takeArg, cancellationToken).ConfigureAwait(false); + if (!keepOpen) + { + return; + } + + continue; + } + + await WriteFullyAsync(UnknownCommand, cancellationToken).ConfigureAwait(false); + } + } + + /// + public ValueTask DisposeAsync() + { + if (Interlocked.Exchange(ref _disposed, 1) == 1) + { + return ValueTask.CompletedTask; + } + + return ValueTask.CompletedTask; + } + + private Task HandleCheckAsync(ReadOnlySpan argument, CancellationToken cancellationToken) + { + if (!TryParseMessageIdArgument(argument, out var messageId)) + { + return WriteFullyAsync(SyntaxError, cancellationToken); + } + + var alreadyHave = _articlePresent?.Invoke(ArticleId.FromMessageId(messageId)) == true; + return WriteStatusAsync( + alreadyHave ? (byte)'4' : (byte)'2', + (byte)'3', + (byte)'8', + messageId, + cancellationToken); + } + + private async Task HandleTakeThisAsync(byte[] argument, CancellationToken cancellationToken) + { + if (!TryParseMessageIdArgument(argument, out var requestMessageId)) + { + // Peer already started the multiline; drain it so the next command is not contaminated. + await DrainArticleAsync(cancellationToken).ConfigureAwait(false); + await WriteFullyAsync(SyntaxError, cancellationToken).ConfigureAwait(false); + return true; + } + + // argument holds the message-id octets for the rest of this method (copied once in RunAsync). + var requestId = ArticleId.FromMessageId(requestMessageId); + + StoreAssemblyAdmission? admission = null; + var gate = _assemblyAdmission; + if (gate is not null) + { + if (!gate.TryEnter()) + { + await DrainArticleAsync(cancellationToken).ConfigureAwait(false); + await WriteStatusAsync( + (byte)'4', + (byte)'3', + (byte)'9', + argument, + cancellationToken) + .ConfigureAwait(false); + return true; + } + + admission = gate; + } + + byte[]? rented = null; + QueueReservation? reservation = null; + try + { + var destuffed = await ReadDestuffedArticleAsync(cancellationToken).ConfigureAwait(false); + rented = destuffed.Array; + var payload = destuffed.Memory; + + if (_articlePresent?.Invoke(requestId) == true) + { + await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + + var created = ArticleRecordFactory.TryCreate( + _parser, + payload, + ArticlePathMode.Normalize); + if (!created.IsAccepted) + { + await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + + var record = created.Record; + if (!record.ArtId.Equals(requestId)) + { + record.ReleaseUnusedCanonicalBuffer(); + await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + + var queue = _acceptedArticles; + if (queue is null || queue.IsCompleted) + { + record.ReleaseUnusedCanonicalBuffer(); + await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false); + return false; + } + + if (record.ArtSize > queue.MaxQueuedBytes) + { + record.ReleaseUnusedCanonicalBuffer(); + await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + + if (!queue.TryReserve(record.ArtSize, out var reserved)) + { + _transport.SetReadProgressSuspended(true); + try + { + reservation = await queue.ReserveAsync(record.ArtSize, cancellationToken).ConfigureAwait(false); + } + finally + { + _transport.SetReadProgressSuspended(false); + } + + if (reservation is null) + { + record.ReleaseUnusedCanonicalBuffer(); + await WriteStatusAsync((byte)'4', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + } + else + { + reservation = reserved; + } + + var queued = new AcceptedArticle(record.ArtId, record.ArtHash, record.ArtSize, record.ArtData); + bool committed; + try + { + committed = queue.TryCommit(reservation.Value, queued); + } + catch + { + queue.Release(reservation.Value); + reservation = null; + record.ReleaseUnusedCanonicalBuffer(); + throw; + } + + reservation = null; + if (!committed) + { + record.ReleaseUnusedCanonicalBuffer(); + await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false); + return false; + } + + // ArtData remains reachable via the queued Memory; Exact buffers are not pooled. + if (_logger.IsEnabled(LogLevel.Debug)) + { + StorageNntpFeedSessionLogMessages.ArticleAccepted( + _logger, + record.ArtId.ToLowerHexString()); + } + + await WriteStatusAsync((byte)'2', (byte)'3', (byte)'9', argument, cancellationToken) + .ConfigureAwait(false); + return true; + } + catch (EndOfStreamException) + { + if (reservation is { } held) + { + _acceptedArticles?.Release(held); + } + + throw; + } + catch (InvalidOperationException) + { + // Oversized article or unrecoverable framing residue: RFC 4644 temporary failure + close. + if (reservation is { } held) + { + _acceptedArticles?.Release(held); + } + + await WriteFullyAsync(TemporaryFailure, cancellationToken).ConfigureAwait(false); + return false; + } + finally + { + if (rented is not null) + { + ArrayPool.Shared.Return(rented); + } + + admission?.Exit(); + } + } + + private async Task DrainArticleAsync(CancellationToken cancellationToken) + { + try + { + var destuffed = await ReadDestuffedArticleAsync(cancellationToken).ConfigureAwait(false); + if (destuffed.Array is not null) + { + ArrayPool.Shared.Return(destuffed.Array); + } + } + catch (EndOfStreamException ex) + { + ExpectedException.Ignore(ex); + } + } + + private Task<(byte[]? Array, ReadOnlyMemory Memory)> ReadDestuffedArticleAsync( + CancellationToken cancellationToken) + => ReadDestuffedCoreAsync(ArticleResourceLimits.MaxArticleBytes, cancellationToken); + + private async Task<(byte[]? Array, ReadOnlyMemory Memory)> ReadDestuffedCoreAsync( + int maxBytes, + CancellationToken cancellationToken) + { + var rented = ArrayPool.Shared.Rent(Math.Min(maxBytes, InitialAssemblyRentBytes)); + var written = 0; + var atLineStart = true; + // Prefer the session scratch buffer so typical articles do not rent a second read array. + byte[]? rentedInput = null; + byte[] input; + if (_pendingCount > _readScratch.Length) + { + rentedInput = ArrayPool.Shared.Rent(_pendingCount); + input = rentedInput; + } + else + { + input = _readScratch; + } + + var inputCount = 0; + var inputOffset = 0; + try + { + // Seed from any leftover command-buffer bytes that follow the TAKETHIS line. + if (_pendingCount > 0) + { + _pending.AsSpan(0, _pendingCount).CopyTo(input); + inputCount = _pendingCount; + _pendingCount = 0; + } + + while (true) + { + if (inputCount - inputOffset == 0) + { + inputOffset = 0; + inputCount = await _transport.ReadAsync(input.AsMemory(), cancellationToken).ConfigureAwait(false); + if (inputCount == 0) + { + throw new EndOfStreamException("NNTP TAKETHIS ended before multiline terminator."); + } + } + + var span = input.AsSpan(inputOffset, inputCount - inputOffset); + var index = 0; + var needMore = false; + while (index < span.Length) + { + if (atLineStart && span[index] == (byte)'.') + { + if (index + 1 >= span.Length + || (span[index + 1] == (byte)'\r' && index + 2 >= span.Length)) + { + needMore = true; + break; + } + + var next = span[index + 1]; + if (next == (byte)'\n') + { + inputOffset += index + 2; + PreserveLeftover(input, inputOffset, inputCount - inputOffset); + ReturnRentedInput(rentedInput); + return (rented, rented.AsMemory(0, written)); + } + + if (next == (byte)'\r' && span[index + 2] == (byte)'\n') + { + inputOffset += index + 3; + PreserveLeftover(input, inputOffset, inputCount - inputOffset); + ReturnRentedInput(rentedInput); + return (rented, rented.AsMemory(0, written)); + } + + index += 1; + atLineStart = false; + continue; + } + + var start = index; + do + { + var current = span[index++]; + atLineStart = current is (byte)'\r' or (byte)'\n'; + } + while (index < span.Length && !(atLineStart && span[index] == (byte)'.')); + + var slice = span[start..index]; + if (written > maxBytes - slice.Length) + { + throw new InvalidOperationException("NNTP TAKETHIS article exceeds maximum size."); + } + + if (written + slice.Length > rented.Length) + { + var grown = ArrayPool.Shared.Rent( + Math.Min(maxBytes, Math.Max(rented.Length * 2, written + slice.Length))); + rented.AsSpan(0, written).CopyTo(grown); + ArrayPool.Shared.Return(rented); + rented = grown; + } + + slice.CopyTo(rented.AsSpan(written)); + written += slice.Length; + } + + inputOffset += index; + if (needMore) + { + var leftover = inputCount - inputOffset; + if (leftover > 0 && inputOffset > 0) + { + Buffer.BlockCopy(input, inputOffset, input, 0, leftover); + } + + inputOffset = 0; + inputCount = leftover; + var read = await _transport + .ReadAsync(input.AsMemory(inputCount, input.Length - inputCount), cancellationToken) + .ConfigureAwait(false); + if (read == 0) + { + throw new EndOfStreamException("NNTP TAKETHIS ended before multiline terminator."); + } + + inputCount += read; + } + } + } + catch + { + ArrayPool.Shared.Return(rented); + ReturnRentedInput(rentedInput); + throw; + } + } + + private static void ReturnRentedInput(byte[]? rentedInput) + { + if (rentedInput is not null) + { + ArrayPool.Shared.Return(rentedInput); + } + } + + private void PreserveLeftover(byte[] input, int offset, int count) + { + if (count <= 0) + { + return; + } + + // Pipelined TAKETHIS may leave the next command plus article bytes after the terminator. + // Grow beyond the 512-octet command-line limit so residue is not discarded. + if (count > _pending.Length) + { + var grown = GC.AllocateUninitializedArray(Math.Max(count, _pending.Length * 2)); + _pending.AsSpan(0, _pendingCount).CopyTo(grown); + _pending = grown; + } + + input.AsSpan(offset, count).CopyTo(_pending); + _pendingCount = count; + } + + private async Task ReadCommandAsync(CancellationToken cancellationToken) + { + while (true) + { + var span = _pending.AsSpan(0, _pendingCount); + var crlf = IndexOfCrlf(span); + if (crlf >= 0) + { + // RFC 3977: command line including CRLF must not exceed 512 octets. + if (crlf + 2 > MaximumCommandOctets) + { + var consumedOverlong = crlf + 2; + if (consumedOverlong < _pendingCount) + { + Buffer.BlockCopy(_pending, consumedOverlong, _pending, 0, _pendingCount - consumedOverlong); + } + + _pendingCount -= consumedOverlong; + return new CommandRead(CommandReadKind.Overlong, ReadOnlyMemory.Empty); + } + + var consumed = crlf + 2; + ReadOnlyMemory line; + if (consumed < _pendingCount) + { + // Residue follows the CRLF (pipelined article bytes). Copy the command line + // before BlockCopy shifts that residue over offset 0. + line = _pending.AsSpan(0, crlf).ToArray(); + Buffer.BlockCopy(_pending, consumed, _pending, 0, _pendingCount - consumed); + _pendingCount -= consumed; + } + else + { + // No residue: caller finishes using Line before the next read or PreserveLeftover. + line = _pending.AsMemory(0, crlf); + _pendingCount = 0; + } + + return new CommandRead(CommandReadKind.Complete, line); + } + + // No CRLF yet: overlong only when the first MaximumCommandOctets lack a terminator. + if (_pendingCount >= MaximumCommandOctets) + { + // Keep bytes after the first MaximumCommandOctets only once DrainUntilCrlf finishes. + _pendingCount = 0; + await DrainUntilCrlfAsync(cancellationToken).ConfigureAwait(false); + return new CommandRead(CommandReadKind.Overlong, ReadOnlyMemory.Empty); + } + + EnsurePendingCapacity(_pendingCount + 1); + var read = await _transport + .ReadAsync(_pending.AsMemory(_pendingCount, _pending.Length - _pendingCount), cancellationToken) + .ConfigureAwait(false); + if (read == 0) + { + return new CommandRead(CommandReadKind.Disconnected, ReadOnlyMemory.Empty); + } + + _pendingCount += read; + } + } + + private void EnsurePendingCapacity(int minimumLength) + { + if (minimumLength <= _pending.Length) + { + return; + } + + var grown = GC.AllocateUninitializedArray(Math.Max(minimumLength, _pending.Length * 2)); + _pending.AsSpan(0, _pendingCount).CopyTo(grown); + _pending = grown; + } + + private async Task DrainUntilCrlfAsync(CancellationToken cancellationToken) + { + var seenCr = false; + while (true) + { + var read = await _transport.ReadAsync(_readScratch, cancellationToken).ConfigureAwait(false); + if (read == 0) + { + return; + } + + for (var i = 0; i < read; i++) + { + var b = _readScratch[i]; + if (seenCr && b == (byte)'\n') + { + var remaining = read - i - 1; + if (remaining > 0) + { + EnsurePendingCapacity(remaining); + _readScratch.AsSpan(i + 1, remaining).CopyTo(_pending); + _pendingCount = remaining; + } + + return; + } + + seenCr = b == (byte)'\r'; + } + } + } + + private Task WriteStatusAsync( + byte hundreds, + byte tens, + byte ones, + byte[] messageIdArgument, + CancellationToken cancellationToken) + { + if (!TryParseMessageIdArgument(messageIdArgument, out var messageId)) + { + return WriteFullyAsync(SyntaxError, cancellationToken); + } + + return WriteStatusAsync(hundreds, tens, ones, messageId, cancellationToken); + } + + private Task WriteStatusAsync( + byte hundreds, + byte tens, + byte ones, + ReadOnlySpan messageId, + CancellationToken cancellationToken) + { + var length = 4 + messageId.Length + 2; + if ((uint)length > (uint)_statusScratch.Length) + { + return WriteFullyAsync(SyntaxError, cancellationToken); + } + + _statusScratch[0] = hundreds; + _statusScratch[1] = tens; + _statusScratch[2] = ones; + _statusScratch[3] = (byte)' '; + messageId.CopyTo(_statusScratch.AsSpan(4)); + _statusScratch[4 + messageId.Length] = (byte)'\r'; + _statusScratch[5 + messageId.Length] = (byte)'\n'; + return WriteFullyAsync(_statusScratch.AsMemory(0, length), cancellationToken); + } + + private async Task WriteFullyAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) + { + var remaining = buffer; + while (!remaining.IsEmpty) + { + var written = await _transport.WriteAsync(remaining, cancellationToken).ConfigureAwait(false); + if (written <= 0) + { + throw new IOException("Storage NNTP transport write returned no progress."); + } + + remaining = remaining[written..]; + } + } + + private static bool TryParseMessageIdArgument(ReadOnlySpan argument, out ReadOnlySpan messageId) + { + argument = SkipSpaces(argument); + if (argument.IsEmpty) + { + messageId = default; + return false; + } + + var end = 0; + while (end < argument.Length && argument[end] is not ((byte)' ' or (byte)'\t')) + { + end++; + } + + if (end < argument.Length) + { + var rest = SkipSpaces(argument[end..]); + if (!rest.IsEmpty) + { + messageId = default; + return false; + } + } + + messageId = argument[..end]; + return NntpMessageIdValidation.IsValidMessageId(messageId); + } + + private static bool TryMatchModeStream(ReadOnlySpan line, out bool syntaxError) + { + syntaxError = false; + if (!TryMatchKeyword(line, ModeKeyword, out var modeRest)) + { + return false; + } + + if (!TryMatchKeyword(SkipSpaces(modeRest), StreamKeyword, out var streamRest)) + { + return false; + } + + syntaxError = !streamRest.IsEmpty; + return true; + } + + private static bool TryMatchKeyword( + ReadOnlySpan line, + ReadOnlySpan keyword, + out ReadOnlySpan rest) + { + if (line.Length < keyword.Length) + { + rest = default; + return false; + } + + for (var i = 0; i < keyword.Length; i++) + { + if (ToAsciiUpper(line[i]) != keyword[i]) + { + rest = default; + return false; + } + } + + if (line.Length == keyword.Length) + { + rest = ReadOnlySpan.Empty; + return true; + } + + var next = line[keyword.Length]; + if (next is not ((byte)' ' or (byte)'\t')) + { + rest = default; + return false; + } + + rest = line[(keyword.Length + 1)..]; + return true; + } + + private static ReadOnlySpan SkipSpaces(ReadOnlySpan value) + { + var i = 0; + while (i < value.Length && value[i] is (byte)' ' or (byte)'\t') + { + i++; + } + + return value[i..]; + } + + private static int IndexOfCrlf(ReadOnlySpan span) + { + for (var i = 0; i < span.Length - 1; i++) + { + if (span[i] == (byte)'\r' && span[i + 1] == (byte)'\n') + { + return i; + } + } + + return -1; + } + + private static byte ToAsciiUpper(byte value) => + value is >= (byte)'a' and <= (byte)'z' ? (byte)(value - 32) : value; + + private enum CommandReadKind + { + Complete = 0, + Overlong = 1, + Disconnected = 2, + } + + private readonly struct CommandRead + { + public CommandRead(CommandReadKind kind, ReadOnlyMemory line) + { + Kind = kind; + Line = line; + } + + public CommandReadKind Kind { get; } + public ReadOnlyMemory Line { get; } + } + } + + /// Source-generated StorageServer NNTP feed session logs. + internal static partial class StorageNntpFeedSessionLogMessages + { + [LoggerMessage( + EventId = 5450, + Level = LogLevel.Debug, + Message = "Storage NNTP TAKETHIS accepted artId={ArtId}")] + public static partial void ArticleAccepted(ILogger logger, string ArtId); + } +} diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs similarity index 76% rename from src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs rename to src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs index 20d5a71d..b8dbce0c 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageVatpLogMessages.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerLogMessages.cs @@ -1,86 +1,86 @@ namespace VectorNNTPD.StorageService.Listener { - /// Source-generated StorageServer VATP listener logs. - internal static partial class StorageVatpListenerLogMessages + /// Source-generated StorageServer NNTP listener logs. + internal static partial class StorageNntpListenerLogMessages { [LoggerMessage( EventId = 5400, Level = LogLevel.Information, - Message = "Storage VATP Listener starting bindPortTls={BindPortTls} endpoints={EndpointCount}")] + Message = "Storage NNTP Listener starting bindPortTls={BindPortTls} endpoints={EndpointCount}")] public static partial void Starting(ILogger logger, int BindPortTls, int EndpointCount); [LoggerMessage( EventId = 5401, Level = LogLevel.Information, - Message = "Storage VATP Listener bound endpoint={Endpoint} family={AddressFamily}")] + Message = "Storage NNTP Listener bound endpoint={Endpoint} family={AddressFamily}")] public static partial void EndpointBound(ILogger logger, string Endpoint, string AddressFamily); [LoggerMessage( EventId = 5402, Level = LogLevel.Information, - Message = "Storage VATP Listener running sockets={SocketCount} bindPortTls={BindPortTls}")] + Message = "Storage NNTP Listener running sockets={SocketCount} bindPortTls={BindPortTls}")] public static partial void Running(ILogger logger, int SocketCount, int BindPortTls); [LoggerMessage( EventId = 5403, Level = LogLevel.Information, - Message = "Storage VATP Listener retiring")] + Message = "Storage NNTP Listener retiring")] public static partial void Retiring(ILogger logger); [LoggerMessage( EventId = 5404, Level = LogLevel.Information, - Message = "Storage VATP Listener stopped")] + Message = "Storage NNTP Listener stopped")] public static partial void Stopped(ILogger logger); [LoggerMessage( EventId = 5405, Level = LogLevel.Error, - Message = "Storage VATP Listener failed to start: {Reason}")] + Message = "Storage NNTP Listener failed to start: {Reason}")] public static partial void StartFailed(ILogger logger, string Reason); [LoggerMessage( EventId = 5406, Level = LogLevel.Warning, - Message = "Storage VATP Listener TLS handshake failed remote={Remote}")] + Message = "Storage NNTP Listener TLS handshake failed remote={Remote}")] public static partial void HandshakeFailed(ILogger logger, string Remote); [LoggerMessage( EventId = 5407, Level = LogLevel.Warning, - Message = "Storage VATP Listener connection timed out stage={Stage} remote={Remote}")] + Message = "Storage NNTP Listener connection timed out stage={Stage} remote={Remote}")] public static partial void ConnectionTimedOut(ILogger logger, string Stage, string Remote); [LoggerMessage( EventId = 5408, Level = LogLevel.Debug, - Message = "Storage VATP Listener accepted remote={Remote} active={Active}")] + Message = "Storage NNTP Listener accepted remote={Remote} active={Active}")] public static partial void Accepted(ILogger logger, string Remote, int Active); [LoggerMessage( EventId = 5409, Level = LogLevel.Warning, - Message = "Storage VATP Listener rejected connection at capacity remote={Remote} max={Max}")] + Message = "Storage NNTP Listener rejected connection at capacity remote={Remote} max={Max}")] public static partial void CapacityRejected(ILogger logger, string Remote, int Max); [LoggerMessage( EventId = 5410, Level = LogLevel.Warning, - Message = "Storage VATP Listener skipped unsupported wildcard family family={AddressFamily}: {Reason}")] + Message = "Storage NNTP Listener skipped unsupported wildcard family family={AddressFamily}: {Reason}")] public static partial void WildcardFamilySkipped(ILogger logger, string AddressFamily, string Reason); [LoggerMessage( EventId = 5411, Level = LogLevel.Warning, - Message = "Storage VATP Listener connection failed remote={Remote} error={ErrorType}")] + Message = "Storage NNTP Listener connection failed remote={Remote} error={ErrorType}")] public static partial void ConnectionFailed(ILogger logger, string Remote, string ErrorType); /// Routine peer reset or transport tear-down after accept; not an operational failure. [LoggerMessage( EventId = 5412, Level = LogLevel.Debug, - Message = "Storage VATP Listener connection peer disconnect remote={Remote}")] + Message = "Storage NNTP Listener connection peer disconnect remote={Remote}")] public static partial void ConnectionPeerDisconnected(ILogger logger, string Remote); /// @@ -90,59 +90,59 @@ internal static partial class StorageVatpListenerLogMessages [LoggerMessage( EventId = 5413, Level = LogLevel.Error, - Message = "Storage VATP Listener connection drain observed unexpected task failure")] + Message = "Storage NNTP Listener connection drain observed unexpected task failure")] public static partial void ConnectionDrainFailed(ILogger logger, Exception exception); } - /// Source-generated StorageServer VATP session logs. - internal static partial class StorageVatpSessionLogMessages + /// Source-generated StorageServer NNTP session logs. + internal static partial class StorageNntpFeedSessionLogMessages { [LoggerMessage( EventId = 5500, Level = LogLevel.Debug, - Message = "Storage VATP client HELLO accepted maxFramePayload={MaxFramePayload}")] + Message = "Storage NNTP client HELLO accepted maxFramePayload={MaxFramePayload}")] public static partial void ClientHelloAccepted(ILogger logger, uint MaxFramePayload); [LoggerMessage( EventId = 5501, Level = LogLevel.Debug, - Message = "Storage VATP OPEN rejected streamId={StreamId} requestId={RequestId}")] + Message = "Storage NNTP OPEN rejected streamId={StreamId} requestId={RequestId}")] public static partial void OpenRejected(ILogger logger, uint StreamId, Guid RequestId); [LoggerMessage( EventId = 5502, Level = LogLevel.Debug, - Message = "Storage VATP session closed")] + Message = "Storage NNTP session closed")] public static partial void ConnectionClosed(ILogger logger); [LoggerMessage( EventId = 5503, Level = LogLevel.Debug, - Message = "Storage VATP OPEN accepted streamId={StreamId} requestId={RequestId}")] + Message = "Storage NNTP OPEN accepted streamId={StreamId} requestId={RequestId}")] public static partial void OpenAccepted(ILogger logger, uint StreamId, Guid RequestId); [LoggerMessage( EventId = 5504, Level = LogLevel.Debug, - Message = "Storage VATP transfer completed streamId={StreamId}")] + Message = "Storage NNTP transfer completed streamId={StreamId}")] public static partial void TransferCompleted(ILogger logger, uint StreamId); [LoggerMessage( EventId = 5505, Level = LogLevel.Debug, - Message = "Storage VATP transfer cancelled streamId={StreamId}")] + Message = "Storage NNTP transfer cancelled streamId={StreamId}")] public static partial void TransferCancelled(ILogger logger, uint StreamId); [LoggerMessage( EventId = 5506, Level = LogLevel.Trace, - Message = "Storage VATP STORE completed artId={ArtId} outcome={Outcome} attempt=1")] + Message = "Storage NNTP STORE completed artId={ArtId} outcome={Outcome} attempt=1")] public static partial void StoreCompleted(ILogger logger, string ArtId, string Outcome); [LoggerMessage( EventId = 5507, Level = LogLevel.Warning, - Message = "Storage VATP STORE failed artId={ArtId} failure={Failure} attempt=1")] + Message = "Storage NNTP STORE failed artId={ArtId} failure={Failure} attempt=1")] public static partial void StoreFailed(ILogger logger, string ArtId, string Failure); /// Logs that STORE body credit is withheld until queue capacity is reserved. @@ -152,7 +152,7 @@ internal static partial class StorageVatpSessionLogMessages [LoggerMessage( EventId = 5508, Level = LogLevel.Debug, - Message = "Storage VATP STORE waiting for queue capacity streamId={StreamId} bytes={Bytes}")] + Message = "Storage NNTP STORE waiting for queue capacity streamId={StreamId} bytes={Bytes}")] public static partial void StoreAdmissionWaiting(ILogger logger, uint StreamId, int Bytes); } } diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs similarity index 81% rename from src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs rename to src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs index 65542733..1e9d98ba 100644 --- a/src/VectorNNTPD.StorageService/Listener/StorageVatpListenerService.cs +++ b/src/VectorNNTPD.StorageService/Listener/StorageNntpListenerService.cs @@ -7,29 +7,27 @@ using VectorNNTPD.Common.Acme; using VectorNNTPD.Common.Core; using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.Common.Transport.ArticleTransfer; using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.Common.Articles; using VectorNNTPD.StorageService.Ingress; using VectorNNTPD.Common.Networking.Listeners; namespace VectorNNTPD.StorageService.Listener { - /// - /// Process-wide VATP TLS listener: binds configured endpoints, authenticates TLS, and serves - /// minimal VATP sessions. Does not own article storage. + /// Process-wide NNTP TLS listener: binds configured endpoints, authenticates TLS, and serves + /// RFC 4644 STREAMING feed sessions (CHECK/TAKETHIS). Does not own article storage. /// - public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisposable + public sealed class StorageNntpListenerService : IApplicationService, IAsyncDisposable { private readonly StorageServerRuntimeOptionsSource? _runtimeSource; private StorageServerRuntimeOptions? _runtime; private readonly ITlsCertificateContextProvider _certificates; - private readonly IStorageArticleOpenBoundary _openBoundary; private readonly IAcmeCertificateReadiness _readiness; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly IAcceptedArticleQueue? _acceptedArticles; - private readonly StoreAssemblyAdmission? _storeAdmission; - private readonly ArticleTransferLimits? _transferLimits; + private readonly StoreAssemblyAdmission? _assemblyAdmission; + private readonly Func? _articlePresent; /// /// Publishes DNS after at least one socket is bound. Null skips publication. @@ -42,7 +40,7 @@ public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisp private readonly CancellationTokenSource _runCts = new(); private Task? _acceptTask; - private StorageVatpListenerState _state = StorageVatpListenerState.Created; + private StorageNntpListenerState _state = StorageNntpListenerState.Created; private int _started; private int _disposed; private int _activeConnections; @@ -54,41 +52,39 @@ public sealed class StorageVatpListenerService : IApplicationService, IAsyncDisp internal static int LastAcceptedReceiveBuffer { get; private set; } /// Initializes the Listener service for isolated tests with a pre-ready certificate gate. - public StorageVatpListenerService( + public StorageNntpListenerService( StorageServerRuntimeOptions runtime, ITlsCertificateContextProvider certificates, - IStorageArticleOpenBoundary openBoundary, - ILogger logger, - ArticleTransferLimits? transferLimits = null) - : this(runtime, certificates, openBoundary, CreateReadyGate(), logger, transferLimits: transferLimits) + ILogger logger, + IAcceptedArticleQueue? acceptedArticles = null, + StoreAssemblyAdmission? assemblyAdmission = null, + Func? articlePresent = null) + : this(runtime, certificates, CreateReadyGate(), logger, acceptedArticles, assemblyAdmission, articlePresent) { } /// Initializes the Listener service. - public StorageVatpListenerService( + public StorageNntpListenerService( StorageServerRuntimeOptions runtime, ITlsCertificateContextProvider certificates, - IStorageArticleOpenBoundary openBoundary, IAcmeCertificateReadiness readiness, - ILogger logger, + ILogger logger, IAcceptedArticleQueue? acceptedArticles = null, - StoreAssemblyAdmission? storeAdmission = null, - ArticleTransferLimits? transferLimits = null, + StoreAssemblyAdmission? assemblyAdmission = null, + Func? articlePresent = null, Func? publishAfterBind = null) { ArgumentNullException.ThrowIfNull(runtime); ArgumentNullException.ThrowIfNull(certificates); - ArgumentNullException.ThrowIfNull(openBoundary); ArgumentNullException.ThrowIfNull(readiness); ArgumentNullException.ThrowIfNull(logger); _runtime = runtime; _certificates = certificates; - _openBoundary = openBoundary; _readiness = readiness; _logger = logger; _acceptedArticles = acceptedArticles; - _storeAdmission = storeAdmission; - _transferLimits = transferLimits; + _assemblyAdmission = assemblyAdmission; + _articlePresent = articlePresent; _publishAfterBind = publishAfterBind; } @@ -96,39 +92,30 @@ public StorageVatpListenerService( /// Initializes the listener for the host. Runtime options are read when runs, /// after nntpsharedconfig has been published. /// - public StorageVatpListenerService( + public StorageNntpListenerService( StorageServerRuntimeOptionsSource runtimeSource, ITlsCertificateContextProvider certificates, - IStorageArticleOpenBoundary openBoundary, IAcmeCertificateReadiness readiness, - ILogger logger, + ILogger logger, IAcceptedArticleQueue? acceptedArticles, - StoreAssemblyAdmission? storeAdmission, - ArticleTransferLimits? transferLimits = null, + StoreAssemblyAdmission? assemblyAdmission, + Func? articlePresent = null, Func? publishAfterBind = null) { ArgumentNullException.ThrowIfNull(runtimeSource); ArgumentNullException.ThrowIfNull(certificates); - ArgumentNullException.ThrowIfNull(openBoundary); ArgumentNullException.ThrowIfNull(readiness); ArgumentNullException.ThrowIfNull(logger); _runtimeSource = runtimeSource; _certificates = certificates; - _openBoundary = openBoundary; _readiness = readiness; _logger = logger; _acceptedArticles = acceptedArticles; - _storeAdmission = storeAdmission; - _transferLimits = transferLimits; + _assemblyAdmission = assemblyAdmission; + _articlePresent = articlePresent; _publishAfterBind = publishAfterBind; } - /// - /// Gets the per-stream transfer limits for sessions this listener creates. - /// Null at construction uses , including the 1 MiB initial window. - /// - internal ArticleTransferLimits TransferLimits => _transferLimits ?? ArticleTransferLimits.Default; - private StorageServerRuntimeOptions Runtime => _runtime ?? throw new InvalidOperationException("StorageServer runtime options have not been published."); @@ -140,13 +127,13 @@ private static IAcmeCertificateReadiness CreateReadyGate() } /// - public string Name => "StorageVatpListener"; + public string Name => "StorageNntpListener"; /// public Task? Execution => _acceptTask; /// Gets the local lifecycle state. - public StorageVatpListenerState State + public StorageNntpListenerState State { get { @@ -182,7 +169,7 @@ public async Task StartAsync(CancellationToken cancellationToken) lock (_gate) { - _state = StorageVatpListenerState.Starting; + _state = StorageNntpListenerState.Starting; } try @@ -193,26 +180,26 @@ public async Task StartAsync(CancellationToken cancellationToken) if (!_readiness.IsReady) { throw new InvalidOperationException( - "Storage VATP Listener cannot start because the ACME certificate is not ready. StorageServer is TLS-only and the listener must wait for a usable certificate."); + "Storage NNTP Listener cannot start because the ACME certificate is not ready. StorageServer is TLS-only and the listener must wait for a usable certificate."); } if (!_certificates.IsAvailable) { throw new InvalidOperationException( - $"Storage VATP Listener cannot start because no TLS certificate is available from ACME state '{Runtime.CertificateDirectory}'."); + $"Storage NNTP Listener cannot start because no TLS certificate is available from ACME state '{Runtime.CertificateDirectory}'."); } var bindings = ListenEndpointPlanner.Plan(Runtime.BindAddressTokens, Runtime.BindPortTls); if (bindings.Count == 0) { - throw new InvalidOperationException("Storage VATP Listener has no bind endpoints."); + throw new InvalidOperationException("Storage NNTP Listener has no bind endpoints."); } - StorageVatpListenerLogMessages.Starting(_logger, Runtime.BindPortTls, bindings.Count); + StorageNntpListenerLogMessages.Starting(_logger, Runtime.BindPortTls, bindings.Count); BindEndpoints(bindings); if (_listenSockets.Count == 0) { - throw new InvalidOperationException("Storage VATP Listener failed to bind any endpoint."); + throw new InvalidOperationException("Storage NNTP Listener failed to bind any endpoint."); } if (_publishAfterBind is not null) @@ -222,19 +209,19 @@ public async Task StartAsync(CancellationToken cancellationToken) lock (_gate) { - _state = StorageVatpListenerState.Running; + _state = StorageNntpListenerState.Running; } - StorageVatpListenerLogMessages.Running(_logger, _listenSockets.Count, Runtime.BindPortTls); + StorageNntpListenerLogMessages.Running(_logger, _listenSockets.Count, Runtime.BindPortTls); _acceptTask = AcceptAllAsync(_runCts.Token); } catch (Exception ex) { - StorageVatpListenerLogMessages.StartFailed(_logger, ex.Message); + StorageNntpListenerLogMessages.StartFailed(_logger, ex.Message); await DisposeBoundResourcesAsync().ConfigureAwait(false); lock (_gate) { - _state = StorageVatpListenerState.Stopped; + _state = StorageNntpListenerState.Stopped; } Interlocked.Exchange(ref _started, 0); @@ -258,10 +245,10 @@ public async ValueTask DisposeAsync() lock (_gate) { - _state = StorageVatpListenerState.Retiring; + _state = StorageNntpListenerState.Retiring; } - StorageVatpListenerLogMessages.Retiring(_logger); + StorageNntpListenerLogMessages.Retiring(_logger); await _runCts.CancelAsync().ConfigureAwait(false); CloseListenSockets(); @@ -284,10 +271,10 @@ public async ValueTask DisposeAsync() _runCts.Dispose(); lock (_gate) { - _state = StorageVatpListenerState.Stopped; + _state = StorageNntpListenerState.Stopped; } - StorageVatpListenerLogMessages.Stopped(_logger); + StorageNntpListenerLogMessages.Stopped(_logger); } private void BindEndpoints(IReadOnlyList bindings) @@ -302,7 +289,7 @@ private void BindEndpoints(IReadOnlyList bindings) _listenSockets.Add(socket); } - StorageVatpListenerLogMessages.EndpointBound( + StorageNntpListenerLogMessages.EndpointBound( _logger, binding.EndPoint.ToString() ?? string.Empty, binding.Address.AddressFamily.ToString()); @@ -311,7 +298,7 @@ private void BindEndpoints(IReadOnlyList bindings) IsImplicitWildcard(binding.EndPoint) && ex.SocketErrorCode is SocketError.AddressFamilyNotSupported or SocketError.ProtocolNotSupported or SocketError.AddressNotAvailable) { - StorageVatpListenerLogMessages.WildcardFamilySkipped( + StorageNntpListenerLogMessages.WildcardFamilySkipped( _logger, binding.Address.AddressFamily.ToString(), ex.SocketErrorCode.ToString()); @@ -395,16 +382,16 @@ private async Task AcceptLoopAsync(Socket listenSocket, CancellationToken cancel var remote = accepted.RemoteEndPoint?.ToString() ?? ""; if (!TryAdmit()) { - StorageVatpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections); + StorageNntpListenerLogMessages.CapacityRejected(_logger, remote, Runtime.Listener.MaxActiveConnections); accepted.Dispose(); continue; } - StorageVatpListenerLogMessages.Accepted(_logger, remote, ActiveConnections); + StorageNntpListenerLogMessages.Accepted(_logger, remote, ActiveConnections); var connection = ProcessAsync(accepted, remote, cancellationToken); _connections[connection] = 0; _ = connection.ContinueWith( - static (task, state) => ((StorageVatpListenerService)state!)._connections.TryRemove(task, out _), + static (task, state) => ((StorageNntpListenerService)state!)._connections.TryRemove(task, out _), this, TaskScheduler.Default); } @@ -440,19 +427,19 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } handshakeCompleted = true; - await using var transport = new StreamStorageVatpTransport( + await using var transport = new StreamStorageListenerTransport( ssl, Runtime.Listener.IoProgressTimeout, leaveInnerStreamOpen: true); - await using var vatpSession = new StorageVatpSession( + await using var session = new StorageNntpFeedSession( transport, - _openBoundary, Runtime.Listener, _logger, - limits: _transferLimits, + Runtime.Fqdn, acceptedArticles: _acceptedArticles, - storeAdmission: _storeAdmission); - await vatpSession.RunAsync(cancellationToken).ConfigureAwait(false); + assemblyAdmission: _assemblyAdmission, + articlePresent: _articlePresent); + await session.RunAsync(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested) { @@ -460,11 +447,11 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke } catch (AuthenticationException) { - StorageVatpListenerLogMessages.HandshakeFailed(_logger, remote); + StorageNntpListenerLogMessages.HandshakeFailed(_logger, remote); } catch (TimeoutException ex) { - StorageVatpListenerLogMessages.ConnectionTimedOut( + StorageNntpListenerLogMessages.ConnectionTimedOut( _logger, handshakeCompleted ? "io-progress" : ex.Message, remote); @@ -472,11 +459,11 @@ private async Task ProcessAsync(Socket accepted, string remote, CancellationToke catch (IOException) { // Routine peer reset / transport tear-down after accept; not an operational failure. - StorageVatpListenerLogMessages.ConnectionPeerDisconnected(_logger, remote); + StorageNntpListenerLogMessages.ConnectionPeerDisconnected(_logger, remote); } catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex)) { - StorageVatpListenerLogMessages.ConnectionFailed(_logger, remote, ex.GetType().Name); + StorageNntpListenerLogMessages.ConnectionFailed(_logger, remote, ex.GetType().Name); } finally { @@ -568,13 +555,13 @@ private void LogUnexpectedDrainFailures(Task[] tasks, Exception awaitException) } logged = true; - StorageVatpListenerLogMessages.ConnectionDrainFailed(_logger, inner); + StorageNntpListenerLogMessages.ConnectionDrainFailed(_logger, inner); } } if (!logged) { - StorageVatpListenerLogMessages.ConnectionDrainFailed(_logger, awaitException); + StorageNntpListenerLogMessages.ConnectionDrainFailed(_logger, awaitException); } } @@ -585,8 +572,8 @@ private Task DisposeBoundResourcesAsync() } } - /// Local lifecycle states for the StorageServer VATP listener. - public enum StorageVatpListenerState + /// Local lifecycle states for the StorageServer NNTP listener. + public enum StorageNntpListenerState { /// Created but not started. Created = 0, diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs b/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs deleted file mode 100644 index 8eb7ddb4..00000000 --- a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.Store.cs +++ /dev/null @@ -1,642 +0,0 @@ -using System.Buffers; -using System.Diagnostics; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Ingress; - -namespace VectorNNTPD.StorageService.Listener -{ - - public sealed partial class StorageVatpSession - { - private async Task HandleStoreAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (!_clientHelloComplete) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken) - .ConfigureAwait(false); - return false; - } - - if (!VatpStorePayload.TryDecode(frame.Payload, out var articleId, out var decodeError)) - { - await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false); - return true; - } - - if (streamId == VatpProtocol.ConnectionStreamId - || _streams.ContainsKey(streamId) - || _storeStreams.ContainsKey(streamId) - || _streams.Count + _storeStreams.Count >= _limits.MaxStreamsPerConnection) - { - await WriteFailAsync( - streamId, - streamId == VatpProtocol.ConnectionStreamId - ? VatpErrorCode.InvalidStreamId - : VatpErrorCode.StreamTableError, - cancellationToken) - .ConfigureAwait(false); - return true; - } - - // Capture the gate that TryEnter acquired. Do not re-read _storeAdmission on release. - StoreAssemblyAdmission? acquiredAdmission = null; - var admission = _storeAdmission; - if (admission is not null) - { - if (!admission.TryEnter()) - { - await WriteFailAsync(streamId, VatpErrorCode.StreamTableError, cancellationToken) - .ConfigureAwait(false); - return true; - } - - acquiredAdmission = admission; - } - - var receive = new ArticleTransferReceiveStream( - streamId, - StoreReceiveCorrelation, - articleId, - _limits); - if (!_storeStreams.TryAdd(streamId, new StoreStream(receive, acquiredAdmission))) - { - acquiredAdmission?.Exit(); - - await WriteFailAsync(streamId, VatpErrorCode.StreamTableError, cancellationToken) - .ConfigureAwait(false); - return true; - } - - return true; - } - - private async Task HandleStoreMetaAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (!_storeStreams.TryGetValue(streamId, out var store)) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false); - return false; - } - - if (store.MetaSeen || store.BodyAuthorized || store.AdmissionTask is not null) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false); - return true; - } - - Span metaBytes = stackalloc byte[VatpProtocol.MetaPayloadLength]; - if (frame.Payload.Length != VatpProtocol.MetaPayloadLength) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.InvalidMeta, cancellationToken).ConfigureAwait(false); - return true; - } - - frame.Payload.CopyTo(metaBytes); - if (!VatpMetaCodec.TryDecode(metaBytes, out var meta, out var decodeError)) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false); - return true; - } - - if (meta.ArtSize < 1 || meta.ArtSize > ArticleResourceLimits.MaxArticleBytes) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.ArticleTooLarge, cancellationToken).ConfigureAwait(false); - return true; - } - - var queue = _acceptedArticles; - if (queue is null) - { - return await AcceptMetaWithoutReservationAsync(streamId, store, frame.Payload, meta, cancellationToken) - .ConfigureAwait(false); - } - - if (meta.ArtSize > queue.MaxQueuedBytes || queue.IsCompleted) - { - RemoveStore(streamId); - if (meta.ArtSize > queue.MaxQueuedBytes && !queue.IsCompleted) - { - await WriteEncodedAsync( - VatpFrameEncoder.EncodeResult(streamId, VatpStoreOutcome.RejectedCapacity), - cancellationToken) - .ConfigureAwait(false); - return true; - } - - await WriteFailAsync(streamId, VatpErrorCode.Cancelled, cancellationToken).ConfigureAwait(false); - return true; - } - - if (queue.TryReserve(meta.ArtSize, out var reserved)) - { - return await AuthorizeReservedBodyAsync(streamId, store, frame.Payload, meta, reserved, cancellationToken) - .ConfigureAwait(false); - } - - var metaCopy = new byte[VatpProtocol.MetaPayloadLength]; - frame.Payload.CopyTo(metaCopy); - lock (_storeGate) - { - if (store.MetaSeen || store.AdmissionTask is not null) - { - RemoveStore(streamId); - } - else - { - store.PendingMeta = metaCopy; - store.AdmissionCts = CancellationTokenSource.CreateLinkedTokenSource(_runCancellation); - // Log before starting the waiter. AdmissionWaiters can become visible as soon as - // ReserveAsync blocks; observers that wait on waiters must already see this log. - StorageVatpSessionLogMessages.StoreAdmissionWaiting(_logger, streamId, meta.ArtSize); - store.AdmissionTask = AdmitWhenCapacityAsync(streamId, store, meta.ArtSize, store.AdmissionCts.Token); - SyncAdmissionReadTimeout(); - } - } - - if (store.AdmissionTask is null) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false); - return true; - } - - return true; - } - - private async Task AdmitWhenCapacityAsync( - uint streamId, - StoreStream store, - int bytes, - CancellationToken cancellationToken) - { - var queue = _acceptedArticles; - if (queue is null) - { - return; - } - - QueueReservation? reservation = null; - try - { - reservation = await queue.ReserveAsync(bytes, cancellationToken).ConfigureAwait(false); - byte[]? metaCopy; - lock (_storeGate) - { - if (store.Closed || !_storeStreams.ContainsKey(streamId) || store.PendingMeta is null) - { - metaCopy = null; - } - else if (reservation is null) - { - metaCopy = null; - store.AdmissionFailed = true; - } - else - { - metaCopy = store.PendingMeta; - store.ReservationHold.Bind(queue, reservation.Value); - reservation = null; - } - } - - if (metaCopy is null) - { - if (reservation is { } abandoned) - { - queue.Release(abandoned); - } - - if (store.AdmissionFailed && !store.Closed) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.Cancelled, CancellationToken.None).ConfigureAwait(false); - } - - return; - } - - if (!VatpMetaCodec.TryDecode(metaCopy, out var meta, out var decodeError)) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, decodeError, CancellationToken.None).ConfigureAwait(false); - return; - } - - await AuthorizeReservedBodyAsync( - streamId, - store, - new ReadOnlySequence(metaCopy), - meta, - store.ReservationHold.Reservation, - CancellationToken.None) - .ConfigureAwait(false); - } - catch (OperationCanceledException) - { - if (reservation is { } abandoned) - { - queue.Release(abandoned); - } - } - catch (Exception ex) when (ex is IOException or ObjectDisposedException) - { - if (reservation is { } abandoned) - { - queue.Release(abandoned); - } - } - finally - { - lock (_storeGate) - { - SyncAdmissionReadTimeout(); - } - } - } - - /// - /// Suspends the read no-progress timeout while a STORE is waiting for queue capacity. - /// Resumes it when no such wait remains, including after WINDOW is granted. - /// Caller holds . - /// - private void SyncAdmissionReadTimeout() - { - var pending = false; - foreach (var store in _storeStreams.Values) - { - if (!store.Closed && !store.BodyAuthorized && store.AdmissionTask is { IsCompleted: false }) - { - pending = true; - break; - } - } - - _transport.SetReadProgressSuspended(pending); - } - - private async Task AcceptMetaWithoutReservationAsync( - uint streamId, - StoreStream store, - ReadOnlySequence payload, - ArticleCanonicalTransferMeta meta, - CancellationToken cancellationToken) - { - var apply = store.Receive.TryAcceptMeta(payload); - if (!apply.Success) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, store.Receive.Failure, cancellationToken).ConfigureAwait(false); - return true; - } - - store.MetaArtHash = meta.ArtHash; - store.MetaArtSize = meta.ArtSize; - store.MetaSeen = true; - store.BodyAuthorized = true; - return true; - } - - private async Task AuthorizeReservedBodyAsync( - uint streamId, - StoreStream store, - ReadOnlySequence payload, - ArticleCanonicalTransferMeta meta, - QueueReservation reservation, - CancellationToken cancellationToken) - { - // True when AdmitWhenCapacityAsync already bound this reservation before this call. - // Combined with StoreReservationHold.TryRelease (exactly-once), prevents double Release - // when RemoveStore races with this failure path. - var reservationAlreadyBound = store.ReservationHold.IsBound; - VatpErrorCode failure; - lock (_storeGate) - { - if (store.Closed || store.BodyAuthorized) - { - failure = VatpErrorCode.InvalidStateTransition; - } - else - { - var apply = store.Receive.TryAcceptMeta(payload); - if (!apply.Success) - { - failure = store.Receive.Failure; - } - else - { - store.ReservationHold.Bind(_acceptedArticles, reservation); - store.MetaArtHash = meta.ArtHash; - store.MetaArtSize = meta.ArtSize; - store.MetaSeen = true; - store.BodyAuthorized = true; - Interlocked.Increment(ref StoreBodiesAllocated); - failure = VatpErrorCode.None; - } - } - } - - if (failure != VatpErrorCode.None) - { - if (!store.ReservationHold.TryRelease() && !reservationAlreadyBound) - { - // Unbound TryReserve/ReserveAsync slot still owned by this call. - _acceptedArticles?.Release(reservation); - } - - RemoveStore(streamId); - await WriteFailAsync(streamId, failure, cancellationToken).ConfigureAwait(false); - return true; - } - - var grant = _limits.InitialStreamWindowBytes; - if (grant > 0) - { - await WriteEncodedAsync( - VatpFrameEncoder.EncodeWindow(streamId, (uint)grant), - cancellationToken) - .ConfigureAwait(false); - } - - return true; - } - - private async Task HandleStoreDataAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (!_storeStreams.TryGetValue(streamId, out var store)) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false); - return false; - } - - ArticleTransferApplyResult apply; - var unauthorized = false; - var dataStarted = Stopwatch.GetTimestamp(); - lock (_storeGate) - { - if (!store.BodyAuthorized || store.Closed) - { - unauthorized = true; - apply = ArticleTransferApplyResult.Fail(VatpErrorCode.FlowControlViolation); - } - else - { - apply = store.Receive.TryAcceptData(frame.Payload, frame.Header.HasFin); - } - } - - if (unauthorized) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.FlowControlViolation, cancellationToken).ConfigureAwait(false); - return true; - } - - NoteDataAccept( - Stopwatch.GetTimestamp() - dataStarted, - apply.Success ? frame.Payload.Length : 0); - if (!apply.Success) - { - var error = store.Receive.Failure; - RemoveStore(streamId); - await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false); - return true; - } - - var payloadLength = checked((uint)frame.Payload.Length); - if (payloadLength > 0 && store.Receive.TryAcceptWindow(payloadLength).Success) - { - var windowStarted = Stopwatch.GetTimestamp(); - await WriteEncodedAsync(VatpFrameEncoder.EncodeWindow(streamId, payloadLength), cancellationToken) - .ConfigureAwait(false); - NoteWindowWrite(Stopwatch.GetTimestamp() - windowStarted); - } - - if (store.Receive.IsTerminal) - { - var error = store.Receive.Failure == VatpErrorCode.None - ? VatpErrorCode.IncompleteTransfer - : store.Receive.Failure; - RemoveStore(streamId); - await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false); - } - - return true; - } - - private async Task HandleStoreEndAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (!_storeStreams.TryGetValue(streamId, out var store)) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameType, cancellationToken).ConfigureAwait(false); - return false; - } - - var endStarted = Stopwatch.GetTimestamp(); - ArticleTransferApplyResult apply; - var tookRecord = false; - ArticleRecord record = default; - lock (_storeGate) - { - apply = store.Receive.TryAcceptEnd(); - if (apply.Success) - { - tookRecord = store.Receive.TryTakeRecord(out record); - } - } - - NoteEndValidate(Stopwatch.GetTimestamp() - endStarted); - if (!tookRecord) - { - var error = store.Receive.Failure == VatpErrorCode.None - ? VatpErrorCode.IncompleteTransfer - : store.Receive.Failure; - RemoveStore(streamId); - await WriteFailAsync(streamId, error, cancellationToken).ConfigureAwait(false); - return true; - } - - var mismatch = Mismatch(store, record); - if (mismatch != VatpErrorCode.None) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, mismatch, cancellationToken).ConfigureAwait(false); - return true; - } - - if (_acceptedArticles is null) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.OpenRejected, cancellationToken).ConfigureAwait(false); - return true; - } - - var queued = new AcceptedArticle(record.ArtId, record.ArtHash, record.ArtSize, record.ArtData); - var committed = false; - // Take hold ownership before TryCommit so concurrent RemoveStore cannot Release - // a reservation the queue has already consumed (or is consuming). - if (store.ReservationHold.TryTakeForCommit(out var commitQueue, out var commitReservation)) - { - try - { - committed = commitQueue!.TryCommit(commitReservation, queued); - } - catch - { - // TryCommit throws before mutating the reservation table; return the slot. - commitQueue!.Release(commitReservation); - store.ReservationHold.MarkCommitOutcome(false); - RemoveStore(streamId); - throw; - } - - store.ReservationHold.MarkCommitOutcome(committed); - } - - if (!committed) - { - RemoveStore(streamId); - await WriteFailAsync(streamId, VatpErrorCode.IncompleteTransfer, cancellationToken).ConfigureAwait(false); - return true; - } - - var resultStarted = Stopwatch.GetTimestamp(); - store.AcceptStarted = true; - RemoveStore(streamId); - StorageVatpSessionLogMessages.StoreCompleted( - _logger, - record.ArtId.ToLowerHexString(), - "Accepted"); - await WriteEncodedAsync( - VatpFrameEncoder.EncodeResult(streamId, VatpStoreOutcome.Accepted), - cancellationToken) - .ConfigureAwait(false); - NoteEnqueueToResult(Stopwatch.GetTimestamp() - resultStarted); - return true; - } - - private static VatpErrorCode Mismatch(StoreStream store, in ArticleRecord record) - { - if (!store.MetaSeen) - { - return VatpErrorCode.IncompleteTransfer; - } - - if (!record.ArtId.Equals(store.Receive.ExpectedArtId)) - { - return VatpErrorCode.ArticleIdMismatch; - } - - if (record.ArtHash != store.MetaArtHash) - { - return VatpErrorCode.ArtHashMismatch; - } - - if (record.ArtSize != store.MetaArtSize) - { - return VatpErrorCode.ArtSizeMismatch; - } - - return VatpErrorCode.None; - } - - private void RemoveStore(uint streamId) - { - if (!_storeStreams.TryRemove(streamId, out var store)) - { - return; - } - - store.Closed = true; - // Exit the admission instance that TryEnter acquired — never re-read _storeAdmission. - store.ReleaseAcquiredAdmission(); - - try - { - store.AdmissionCts?.Cancel(); - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } - - // Serialize with Bind (always under _storeGate) so TryRelease cannot miss a - // mid-Bind publication. Monitor is reentrant for RemoveStore under the gate. - lock (_storeGate) - { - if (!store.ReservationHold.Committed) - { - store.ReservationHold.TryRelease(); - } - } - } - - private void ReleaseStoreSlots() - { - if (_storeStreams.Count == 0) - { - return; - } - - var ids = new uint[_storeStreams.Count]; - _storeStreams.Keys.CopyTo(ids, 0); - foreach (var streamId in ids) - { - RemoveStore(streamId); - } - } - - private sealed class StoreStream - { - private StoreAssemblyAdmission? _acquiredAdmission; - - public StoreStream(ArticleTransferReceiveStream receive, StoreAssemblyAdmission? acquiredAdmission) - { - Receive = receive; - _acquiredAdmission = acquiredAdmission; - } - - public ArticleTransferReceiveStream Receive { get; } - - /// Exactly-once hold for the queue reservation bound to this stream. - public StoreReservationHold ReservationHold { get; } = new(); - - /// Gets whether an assembly admission slot is still held for this stream. - public bool SlotHeld => Volatile.Read(ref _acquiredAdmission) is not null; - - public bool MetaSeen { get; set; } - - public ulong MetaArtHash { get; set; } - - public int MetaArtSize { get; set; } - - public bool AcceptStarted { get; set; } - - public bool BodyAuthorized { get; set; } - - public bool Closed { get; set; } - - public bool AdmissionFailed { get; set; } - - public byte[]? PendingMeta { get; set; } - - public CancellationTokenSource? AdmissionCts { get; set; } - - public Task? AdmissionTask { get; set; } - - /// Exits the assembly admission slot acquired for this stream, if any. - public void ReleaseAcquiredAdmission() - { - // Exactly-once: concurrent RemoveStore / failure paths must not double-Exit. - Interlocked.Exchange(ref _acquiredAdmission, null)?.Exit(); - } - } - } -} diff --git a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs b/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs deleted file mode 100644 index 58127425..00000000 --- a/src/VectorNNTPD.StorageService/Listener/StorageVatpSession.cs +++ /dev/null @@ -1,1245 +0,0 @@ -using System.Buffers; -using System.Collections.Concurrent; -using System.Diagnostics; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Configuration; -using VectorNNTPD.StorageService.Ingress; - -namespace VectorNNTPD.StorageService.Listener -{ - - /// - /// VATP server session: client HELLO, then OPEN served from - /// as META, credit-paced DATA/FIN, and END. - /// STORE is a separate receive stream. After META matches the inbound bytes, the original - /// article is enqueued. Path is not rewritten here. Retrieval does not rewrite Path. - /// - public sealed partial class StorageVatpSession : IAsyncDisposable - { - /// Local receive-stream correlation. Not a wire field and not a storage key. - private static readonly Guid StoreReceiveCorrelation = new("5f5f5f5f-5f5f-5f5f-5f5f-5f5f5f5f5f5f"); - - private static long _enqueueToResultTicks; - private static long _enqueueToResultCount; - private static long _windowWriteTicks; - private static long _windowWriteCount; - private static long _readWaitTicks; - private static long _readCount; - private static long _readBytes; - private static long _readLt4K; - private static long _readLt16K; - private static long _readLt32K; - private static long _readLt64K; - private static long _readGe64K; - private static long _readDurLt20Us; - private static long _readDurLt50Us; - private static long _readDurLt100Us; - private static long _readDurLt500Us; - private static long _readDurGe500Us; - private static long _incompleteParses; - private static long _framesParsed; - private static long _processTicks; - private static long _writeGateTicks; - private static long _dataAcceptTicks; - private static long _dataAcceptCount; - private static long _dataCopyBytes; - private static long _endValidateTicks; - private static long _endValidateCount; - private static long _readExact16K; - private static long _framesThisRead0; - private static long _framesThisRead1; - private static long _framesThisReadMany; - private static long _readsToFrame1; - private static long _readsToFrame2; - private static long _readsToFrame3; - private static long _readsToFrame4; - private static long _readsToFrame5Plus; - private static long _partialAtRead0; - private static long _partialAtReadHeader; - private static long _partialAtReadLt16K; - private static long _partialAtReadLt64K; - private static long _partialAtReadGe64K; - private static long _parseTicks; - private static long _compactCount; - private static long _compactBytes; - private static long _compactTicks; - private static int _clientWriteActive; - private static long _clientWriteEndedAt; - private static int _serverIoPhase; - private static int _clientWriteServerPhase; - private static long _readDuringWriteTicks; - private static long _readDuringWriteCount; - private static long _readAfterWriteTicks; - private static long _readAfterWriteCount; - private static long _readGapLt20Us; - private static long _readGapLt100Us; - private static long _readGapLt1Ms; - private static long _readGapGe1Ms; - private static long _clientWriteDuringReadTicks; - private static long _clientWriteDuringReadCount; - private static long _clientWriteDuringServerWriteTicks; - private static long _clientWriteDuringServerWriteCount; - private static long _clientWriteDuringOtherTicks; - private static long _clientWriteDuringOtherCount; - private static long _readyBehindPositive; - private static long _readyBehindSamples; - private static long _readyBehindSum; - private static long _sendGapLt20Us; - private static long _sendGapLt100Us; - private static long _sendGapLt1Ms; - private static long _sendGapGe1Ms; - private static long _sessionCpu100Ns; - - /// Clears session path timing counters. Does not change acceptance. - internal static void ResetEnqueueToResultTiming() - { - Interlocked.Exchange(ref _enqueueToResultTicks, 0); - Interlocked.Exchange(ref _enqueueToResultCount, 0); - Interlocked.Exchange(ref _windowWriteTicks, 0); - Interlocked.Exchange(ref _windowWriteCount, 0); - Interlocked.Exchange(ref _readWaitTicks, 0); - Interlocked.Exchange(ref _readCount, 0); - Interlocked.Exchange(ref _readBytes, 0); - Interlocked.Exchange(ref _readLt4K, 0); - Interlocked.Exchange(ref _readLt16K, 0); - Interlocked.Exchange(ref _readLt32K, 0); - Interlocked.Exchange(ref _readLt64K, 0); - Interlocked.Exchange(ref _readGe64K, 0); - Interlocked.Exchange(ref _readDurLt20Us, 0); - Interlocked.Exchange(ref _readDurLt50Us, 0); - Interlocked.Exchange(ref _readDurLt100Us, 0); - Interlocked.Exchange(ref _readDurLt500Us, 0); - Interlocked.Exchange(ref _readDurGe500Us, 0); - Interlocked.Exchange(ref _incompleteParses, 0); - Interlocked.Exchange(ref _framesParsed, 0); - Interlocked.Exchange(ref _processTicks, 0); - Interlocked.Exchange(ref _writeGateTicks, 0); - Interlocked.Exchange(ref _dataAcceptTicks, 0); - Interlocked.Exchange(ref _dataAcceptCount, 0); - Interlocked.Exchange(ref _dataCopyBytes, 0); - Interlocked.Exchange(ref _endValidateTicks, 0); - Interlocked.Exchange(ref _endValidateCount, 0); - Interlocked.Exchange(ref _readExact16K, 0); - Interlocked.Exchange(ref _framesThisRead0, 0); - Interlocked.Exchange(ref _framesThisRead1, 0); - Interlocked.Exchange(ref _framesThisReadMany, 0); - Interlocked.Exchange(ref _readsToFrame1, 0); - Interlocked.Exchange(ref _readsToFrame2, 0); - Interlocked.Exchange(ref _readsToFrame3, 0); - Interlocked.Exchange(ref _readsToFrame4, 0); - Interlocked.Exchange(ref _readsToFrame5Plus, 0); - Interlocked.Exchange(ref _partialAtRead0, 0); - Interlocked.Exchange(ref _partialAtReadHeader, 0); - Interlocked.Exchange(ref _partialAtReadLt16K, 0); - Interlocked.Exchange(ref _partialAtReadLt64K, 0); - Interlocked.Exchange(ref _partialAtReadGe64K, 0); - Interlocked.Exchange(ref _parseTicks, 0); - Interlocked.Exchange(ref _compactCount, 0); - Interlocked.Exchange(ref _compactBytes, 0); - Interlocked.Exchange(ref _compactTicks, 0); - Volatile.Write(ref _clientWriteActive, 0); - Volatile.Write(ref _clientWriteEndedAt, 0); - Volatile.Write(ref _serverIoPhase, 0); - Volatile.Write(ref _clientWriteServerPhase, 0); - Interlocked.Exchange(ref _readDuringWriteTicks, 0); - Interlocked.Exchange(ref _readDuringWriteCount, 0); - Interlocked.Exchange(ref _readAfterWriteTicks, 0); - Interlocked.Exchange(ref _readAfterWriteCount, 0); - Interlocked.Exchange(ref _readGapLt20Us, 0); - Interlocked.Exchange(ref _readGapLt100Us, 0); - Interlocked.Exchange(ref _readGapLt1Ms, 0); - Interlocked.Exchange(ref _readGapGe1Ms, 0); - Interlocked.Exchange(ref _clientWriteDuringReadTicks, 0); - Interlocked.Exchange(ref _clientWriteDuringReadCount, 0); - Interlocked.Exchange(ref _clientWriteDuringServerWriteTicks, 0); - Interlocked.Exchange(ref _clientWriteDuringServerWriteCount, 0); - Interlocked.Exchange(ref _clientWriteDuringOtherTicks, 0); - Interlocked.Exchange(ref _clientWriteDuringOtherCount, 0); - Interlocked.Exchange(ref _readyBehindPositive, 0); - Interlocked.Exchange(ref _readyBehindSamples, 0); - Interlocked.Exchange(ref _readyBehindSum, 0); - Interlocked.Exchange(ref _sendGapLt20Us, 0); - Interlocked.Exchange(ref _sendGapLt100Us, 0); - Interlocked.Exchange(ref _sendGapLt1Ms, 0); - Interlocked.Exchange(ref _sendGapGe1Ms, 0); - Interlocked.Exchange(ref _sessionCpu100Ns, 0); - StreamStorageVatpTransport.ResetIoSplit(); - } - - /// Gets how long successful RESULT writes took after TryEnqueue returned true. - internal static (long Count, double Seconds) ReadEnqueueToResultTiming() - { - var count = Volatile.Read(ref _enqueueToResultCount); - var ticks = Volatile.Read(ref _enqueueToResultTicks); - return (count, ticks / (double)Stopwatch.Frequency); - } - - /// Gets time spent copying STORE DATA, writing WINDOW credit, waiting to read, and validating END. - internal static ( - long DataCount, - double DataSeconds, - long WindowCount, - double WindowSeconds, - double ReadWaitSeconds, - long EndCount, - double EndSeconds) ReadReceivePathTiming() - { - return ( - Volatile.Read(ref _dataAcceptCount), - Volatile.Read(ref _dataAcceptTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _windowWriteCount), - Volatile.Read(ref _windowWriteTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _readWaitTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _endValidateCount), - Volatile.Read(ref _endValidateTicks) / (double)Stopwatch.Frequency); - } - - /// - /// Gets read sizes and the time between reads. Process time includes WINDOW and RESULT writes - /// and does not overlap read-wait time. - /// - internal static ReceiveObservation ReadReceiveObservation() - { - return new ReceiveObservation( - Volatile.Read(ref _readCount), - Volatile.Read(ref _readBytes), - Volatile.Read(ref _readLt4K), - Volatile.Read(ref _readLt16K), - Volatile.Read(ref _readLt32K), - Volatile.Read(ref _readLt64K), - Volatile.Read(ref _readGe64K), - Volatile.Read(ref _readDurLt20Us), - Volatile.Read(ref _readDurLt50Us), - Volatile.Read(ref _readDurLt100Us), - Volatile.Read(ref _readDurLt500Us), - Volatile.Read(ref _readDurGe500Us), - Volatile.Read(ref _incompleteParses), - Volatile.Read(ref _framesParsed), - Volatile.Read(ref _processTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _writeGateTicks) / (double)Stopwatch.Frequency); - } - - /// - /// Gets frame-assembly counters. Parse and compact times sit inside process time and do not overlap read-wait. - /// Timeout setup and stream-read times sit inside read-wait and do not overlap each other. - /// - internal static AssemblyObservation ReadAssemblyObservation() - { - var (timeoutSeconds, streamReadSeconds) = StreamStorageVatpTransport.ReadIoSplit(); - return new AssemblyObservation( - Volatile.Read(ref _readExact16K), - Volatile.Read(ref _framesThisRead0), - Volatile.Read(ref _framesThisRead1), - Volatile.Read(ref _framesThisReadMany), - Volatile.Read(ref _readsToFrame1), - Volatile.Read(ref _readsToFrame2), - Volatile.Read(ref _readsToFrame3), - Volatile.Read(ref _readsToFrame4), - Volatile.Read(ref _readsToFrame5Plus), - Volatile.Read(ref _partialAtRead0), - Volatile.Read(ref _partialAtReadHeader), - Volatile.Read(ref _partialAtReadLt16K), - Volatile.Read(ref _partialAtReadLt64K), - Volatile.Read(ref _partialAtReadGe64K), - Volatile.Read(ref _parseTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _compactCount), - Volatile.Read(ref _compactBytes), - Volatile.Read(ref _compactTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _dataCopyBytes), - timeoutSeconds, - streamReadSeconds); - } - - /// One level's frame-assembly counters. Times are elapsed and do not overlap. - internal readonly record struct AssemblyObservation( - long ReadExact16K, - long FramesThisRead0, - long FramesThisRead1, - long FramesThisReadMany, - long ReadsToFrame1, - long ReadsToFrame2, - long ReadsToFrame3, - long ReadsToFrame4, - long ReadsToFrame5Plus, - long PartialAtRead0, - long PartialAtReadHeader, - long PartialAtReadLt16K, - long PartialAtReadLt64K, - long PartialAtReadGe64K, - double ParseSeconds, - long CompactCount, - long CompactBytes, - double CompactSeconds, - long DataCopyBytes, - double TimeoutSetupSeconds, - double StreamReadSeconds); - - /// - /// Correlates client writes with server reads on . - /// Both sides run in this process, so the clock is the same. Intervals do not overlap within one side. - /// - internal static PaceObservation ReadPaceObservation() - { - return new PaceObservation( - Volatile.Read(ref _readDuringWriteTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _readDuringWriteCount), - Volatile.Read(ref _readAfterWriteTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _readAfterWriteCount), - Volatile.Read(ref _readGapLt20Us), - Volatile.Read(ref _readGapLt100Us), - Volatile.Read(ref _readGapLt1Ms), - Volatile.Read(ref _readGapGe1Ms), - Volatile.Read(ref _clientWriteDuringReadTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _clientWriteDuringReadCount), - Volatile.Read(ref _clientWriteDuringServerWriteTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _clientWriteDuringServerWriteCount), - Volatile.Read(ref _clientWriteDuringOtherTicks) / (double)Stopwatch.Frequency, - Volatile.Read(ref _clientWriteDuringOtherCount), - Volatile.Read(ref _readyBehindPositive), - Volatile.Read(ref _readyBehindSamples), - Volatile.Read(ref _readyBehindSum), - Volatile.Read(ref _sendGapLt20Us), - Volatile.Read(ref _sendGapLt100Us), - Volatile.Read(ref _sendGapLt1Ms), - Volatile.Read(ref _sendGapGe1Ms), - Volatile.Read(ref _sessionCpu100Ns) / 10_000_000d); - } - - /// - /// Records that the benchmark client is about to enter SslStream.WriteAsync. - /// is other STORE slots that already have credit and a frame. - /// is true when the send loop blocked since the previous write. - /// - internal static void NoteClientWriteStart(int readyBehind, bool afterLoopWait) - { - var now = Stopwatch.GetTimestamp(); - var previous = Volatile.Read(ref _clientWriteEndedAt); - if (previous != 0 && !afterLoopWait) - { - NoteSendGap(now - previous); - } - - if (readyBehind > 0) - { - Interlocked.Increment(ref _readyBehindPositive); - } - - Interlocked.Add(ref _readyBehindSum, readyBehind); - Interlocked.Increment(ref _readyBehindSamples); - Volatile.Write(ref _clientWriteServerPhase, Volatile.Read(ref _serverIoPhase)); - Volatile.Write(ref _clientWriteActive, 1); - } - - /// Records that the benchmark client's TLS write returned. is that write only. - internal static void NoteClientWriteEnd(long elapsedTicks) - { - var phase = Volatile.Read(ref _clientWriteServerPhase); - Volatile.Write(ref _clientWriteActive, 0); - Volatile.Write(ref _clientWriteEndedAt, Stopwatch.GetTimestamp()); - if (phase == 1) - { - Interlocked.Add(ref _clientWriteDuringReadTicks, elapsedTicks); - Interlocked.Increment(ref _clientWriteDuringReadCount); - } - else if (phase == 2) - { - Interlocked.Add(ref _clientWriteDuringServerWriteTicks, elapsedTicks); - Interlocked.Increment(ref _clientWriteDuringServerWriteCount); - } - else - { - Interlocked.Add(ref _clientWriteDuringOtherTicks, elapsedTicks); - Interlocked.Increment(ref _clientWriteDuringOtherCount); - } - } - - /// - /// One level of client/server overlap. Read-during-write and read-after-write partition server read-wait. - /// Client-write buckets partition client TLS-write time by the server phase at write start. - /// - internal readonly record struct PaceObservation( - double ReadDuringClientWriteSeconds, - long ReadDuringClientWriteCount, - double ReadAfterClientWriteSeconds, - long ReadAfterClientWriteCount, - long ReadGapLt20Us, - long ReadGapLt100Us, - long ReadGapLt1Ms, - long ReadGapGe1Ms, - double ClientWriteDuringServerReadSeconds, - long ClientWriteDuringServerReadCount, - double ClientWriteDuringServerWriteSeconds, - long ClientWriteDuringServerWriteCount, - double ClientWriteDuringOtherSeconds, - long ClientWriteDuringOtherCount, - long ReadyBehindPositive, - long ReadyBehindSamples, - long ReadyBehindSum, - long SendGapLt20Us, - long SendGapLt100Us, - long SendGapLt1Ms, - long SendGapGe1Ms, - double SessionCpuSeconds); - - /// One level's receive-size counters. Times are not CPU time. - internal readonly record struct ReceiveObservation( - long Reads, - long ReadBytes, - long ReadLt4K, - long ReadLt16K, - long ReadLt32K, - long ReadLt64K, - long ReadGe64K, - long ReadDurLt20Us, - long ReadDurLt50Us, - long ReadDurLt100Us, - long ReadDurLt500Us, - long ReadDurGe500Us, - long IncompleteParses, - long Frames, - double ProcessSeconds, - double WriteGateSeconds); - - /// Counts one completed transport read by size. Does not change the bytes. - private static void NoteRead(int bytes) - { - Interlocked.Increment(ref _readCount); - Interlocked.Add(ref _readBytes, bytes); - if (bytes < 4 * 1024) - { - Interlocked.Increment(ref _readLt4K); - } - else if (bytes < 16 * 1024) - { - Interlocked.Increment(ref _readLt16K); - } - else if (bytes < 32 * 1024) - { - Interlocked.Increment(ref _readLt32K); - if (bytes == 16 * 1024) - { - Interlocked.Increment(ref _readExact16K); - } - } - else if (bytes < 64 * 1024) - { - Interlocked.Increment(ref _readLt64K); - } - else - { - Interlocked.Increment(ref _readGe64K); - } - } - - /// Counts how long one read call took. This is elapsed time, not CPU time. - private static void NoteReadDuration(long ticks) - { - var microseconds = ticks * 1_000_000d / Stopwatch.Frequency; - if (microseconds < 20) - { - Interlocked.Increment(ref _readDurLt20Us); - } - else if (microseconds < 50) - { - Interlocked.Increment(ref _readDurLt50Us); - } - else if (microseconds < 100) - { - Interlocked.Increment(ref _readDurLt100Us); - } - else if (microseconds < 500) - { - Interlocked.Increment(ref _readDurLt500Us); - } - else - { - Interlocked.Increment(ref _readDurGe500Us); - } - } - - /// Counts bytes already retained when a transport read starts. Does not move those bytes. - private static void NotePartialAtRead(int buffered) - { - if (buffered == 0) - { - Interlocked.Increment(ref _partialAtRead0); - } - else if (buffered < VatpProtocol.HeaderLengthBytes) - { - Interlocked.Increment(ref _partialAtReadHeader); - } - else if (buffered < 16 * 1024) - { - Interlocked.Increment(ref _partialAtReadLt16K); - } - else if (buffered < 64 * 1024) - { - Interlocked.Increment(ref _partialAtReadLt64K); - } - else - { - Interlocked.Increment(ref _partialAtReadGe64K); - } - } - - /// Counts complete frames decoded from one transport read. - private static void NoteFramesThisRead(int frames) - { - if (frames == 0) - { - Interlocked.Increment(ref _framesThisRead0); - } - else if (frames == 1) - { - Interlocked.Increment(ref _framesThisRead1); - } - else - { - Interlocked.Increment(ref _framesThisReadMany); - } - } - - /// Counts transport reads from the previous complete frame through this one. - private static void NoteReadsToFrame(int reads) - { - if (reads <= 1) - { - Interlocked.Increment(ref _readsToFrame1); - } - else if (reads == 2) - { - Interlocked.Increment(ref _readsToFrame2); - } - else if (reads == 3) - { - Interlocked.Increment(ref _readsToFrame3); - } - else if (reads == 4) - { - Interlocked.Increment(ref _readsToFrame4); - } - else - { - Interlocked.Increment(ref _readsToFrame5Plus); - } - } - - /// Process-wide read-gap buckets. The buckets are mutually exclusive. - private static void NoteReadGap(long ticks) - { - var microseconds = ticks * 1_000_000d / Stopwatch.Frequency; - if (microseconds < 20) - { - Interlocked.Increment(ref _readGapLt20Us); - } - else if (microseconds < 100) - { - Interlocked.Increment(ref _readGapLt100Us); - } - else if (microseconds < 1_000) - { - Interlocked.Increment(ref _readGapLt1Ms); - } - else - { - Interlocked.Increment(ref _readGapGe1Ms); - } - } - - /// Process-wide send-gap buckets used by the bench client write markers. - private static void NoteSendGap(long ticks) - { - var microseconds = ticks * 1_000_000d / Stopwatch.Frequency; - if (microseconds < 20) - { - Interlocked.Increment(ref _sendGapLt20Us); - } - else if (microseconds < 100) - { - Interlocked.Increment(ref _sendGapLt100Us); - } - else if (microseconds < 1_000) - { - Interlocked.Increment(ref _sendGapLt1Ms); - } - else - { - Interlocked.Increment(ref _sendGapGe1Ms); - } - } - - private static void AddProcessTicks(long ticks) => Interlocked.Add(ref _processTicks, ticks); - - private static void AddReadWaitTicks(long ticks) => Interlocked.Add(ref _readWaitTicks, ticks); - - private static void AddParseTicks(long ticks) => Interlocked.Add(ref _parseTicks, ticks); - - private static void AddWriteGateTicks(long ticks) => Interlocked.Add(ref _writeGateTicks, ticks); - - private static void AddSessionCpu(long cpu100Ns) => Interlocked.Add(ref _sessionCpu100Ns, cpu100Ns); - - private static void NoteIncompleteParse() => Interlocked.Increment(ref _incompleteParses); - - private static void NoteFrameParsed() => Interlocked.Increment(ref _framesParsed); - - private static void NoteCompact(long ticks, int bytes) - { - Interlocked.Add(ref _compactTicks, ticks); - Interlocked.Increment(ref _compactCount); - Interlocked.Add(ref _compactBytes, bytes); - } - - private static void NoteReadOverlap(bool clientWriting, long readTicks) - { - if (clientWriting) - { - Interlocked.Add(ref _readDuringWriteTicks, readTicks); - Interlocked.Increment(ref _readDuringWriteCount); - } - else - { - Interlocked.Add(ref _readAfterWriteTicks, readTicks); - Interlocked.Increment(ref _readAfterWriteCount); - } - } - - private static void SetServerIoPhase(int phase) => Volatile.Write(ref _serverIoPhase, phase); - - private static bool IsClientWriteActive() => Volatile.Read(ref _clientWriteActive) != 0; - - private static long ReadClientWriteEndedAt() => Volatile.Read(ref _clientWriteEndedAt); - - private static void NoteDataAccept(long ticks, long copiedBytes) - { - Interlocked.Add(ref _dataAcceptTicks, ticks); - Interlocked.Increment(ref _dataAcceptCount); - if (copiedBytes > 0) - { - Interlocked.Add(ref _dataCopyBytes, copiedBytes); - } - } - - private static void NoteWindowWrite(long ticks) - { - Interlocked.Add(ref _windowWriteTicks, ticks); - Interlocked.Increment(ref _windowWriteCount); - } - - private static void NoteEndValidate(long ticks) - { - Interlocked.Add(ref _endValidateTicks, ticks); - Interlocked.Increment(ref _endValidateCount); - } - - private static void NoteEnqueueToResult(long ticks) - { - Interlocked.Add(ref _enqueueToResultTicks, ticks); - Interlocked.Increment(ref _enqueueToResultCount); - } - - private readonly IStorageVatpTransport _transport; - private readonly IStorageArticleOpenBoundary _openBoundary; - private readonly StorageServerListenerRuntimeOptions _listener; - private readonly ArticleTransferLimits _limits; - private readonly ILogger _logger; - private readonly IAcceptedArticleQueue? _acceptedArticles; - private readonly StoreAssemblyAdmission? _storeAdmission; - private readonly Dictionary _streams = []; - private readonly ConcurrentDictionary _storeStreams = []; - private readonly ArticleTransferReadyRing _readyRing = new(); - private readonly SemaphoreSlim _writeGate = new(1, 1); - private readonly object _storeGate = new(); - private readonly byte[] _smallWrite = new byte[512]; - private CancellationToken _runCancellation; - - /// Article buffers allocated by this session after a STORE reservation was granted. - internal long StoreBodiesAllocated; - private Exception? _fatalSessionException; - private uint _maxFramePayload = VatpProtocol.DefaultMaxFramePayload; - private bool _clientHelloComplete; - private bool _serverHelloSent; - private int _disposed; - - /// Initializes a VATP session over an established transport. - public StorageVatpSession( - IStorageVatpTransport transport, - IStorageArticleOpenBoundary openBoundary, - StorageServerListenerRuntimeOptions listener, - ILogger logger, - ArticleTransferLimits? limits = null, - IAcceptedArticleQueue? acceptedArticles = null, - StoreAssemblyAdmission? storeAdmission = null) - { - ArgumentNullException.ThrowIfNull(transport); - ArgumentNullException.ThrowIfNull(openBoundary); - ArgumentNullException.ThrowIfNull(listener); - ArgumentNullException.ThrowIfNull(logger); - _transport = transport; - _openBoundary = openBoundary; - _listener = listener; - _limits = limits ?? ArticleTransferLimits.Default; - _logger = logger; - _acceptedArticles = acceptedArticles; - _storeAdmission = storeAdmission; - } - - /// Runs until peer close, cancellation, or fatal protocol error. - public async Task RunAsync(CancellationToken cancellationToken) - { - _runCancellation = cancellationToken; - var capacity = _listener.ParserAccumulationMaxBytes; - var parseBuffer = ArrayPool.Shared.Rent(capacity); - var usable = Math.Min(parseBuffer.Length, capacity); - var buffered = 0; - var cpuStart = ThreadCpuTimes.Capture(); - try - { - // 0 means "no prior successful read yet"; after the first read this holds a timestamp. - var processMark = 0L; - var readsSinceFrame = 0; - while (!cancellationToken.IsCancellationRequested) - { - if (processMark != 0) - { - AddProcessTicks(Stopwatch.GetTimestamp() - processMark); - } - - if (buffered >= usable) - { - await WriteFailAsync( - VatpProtocol.ConnectionStreamId, - VatpErrorCode.FrameTooLarge, - cancellationToken) - .ConfigureAwait(false); - return; - } - - int bytesRead; - var clientWriting = IsClientWriteActive(); - if (!clientWriting) - { - var ended = ReadClientWriteEndedAt(); - if (ended != 0) - { - NoteReadGap(Stopwatch.GetTimestamp() - ended); - } - } - - try - { - NotePartialAtRead(buffered); - SetServerIoPhase(1); - var readStarted = Stopwatch.GetTimestamp(); - bytesRead = await _transport - .ReadAsync(parseBuffer.AsMemory(buffered, usable - buffered), cancellationToken) - .ConfigureAwait(false); - var readTicks = Stopwatch.GetTimestamp() - readStarted; - SetServerIoPhase(0); - AddReadWaitTicks(readTicks); - NoteReadDuration(readTicks); - NoteReadOverlap(clientWriting, readTicks); - - processMark = Stopwatch.GetTimestamp(); - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - return; - } - catch (TimeoutException) - { - if (_fatalSessionException is not null) - { - throw _fatalSessionException; - } - - return; - } - - if (bytesRead == 0) - { - if (_fatalSessionException is not null) - { - throw _fatalSessionException; - } - - return; - } - - buffered += bytesRead; - NoteRead(bytesRead); - readsSinceFrame++; - - var consumedTotal = 0; - var framesThisRead = 0; - while (true) - { - var remaining = new ReadOnlySequence(parseBuffer, consumedTotal, buffered - consumedTotal); - var parseStarted = Stopwatch.GetTimestamp(); - var parsed = VatpFrameParser.ParseOneFrame(remaining, _maxFramePayload); - AddParseTicks(Stopwatch.GetTimestamp() - parseStarted); - if (parsed.Status == VatpFrameParseStatus.Incomplete) - { - NoteIncompleteParse(); - break; - } - - if (parsed.Status == VatpFrameParseStatus.Invalid) - { - await WriteFailAsync( - VatpProtocol.ConnectionStreamId, - parsed.Error, - cancellationToken) - .ConfigureAwait(false); - return; - } - - consumedTotal += checked((int)parsed.ConsumedBytes); - framesThisRead++; - NoteFrameParsed(); - if (parsed.Frame is not { } frame) - { - await WriteFailAsync( - VatpProtocol.ConnectionStreamId, - VatpErrorCode.InvalidFrameType, - cancellationToken) - .ConfigureAwait(false); - return; - } - - var continueSession = await HandleFrameAsync(frame, cancellationToken).ConfigureAwait(false); - if (!continueSession) - { - return; - } - - if (_fatalSessionException is not null) - { - throw _fatalSessionException; - } - } - - if (consumedTotal > 0) - { - var remainingBytes = buffered - consumedTotal; - if (remainingBytes > 0) - { - var compactStarted = Stopwatch.GetTimestamp(); - Buffer.BlockCopy(parseBuffer, consumedTotal, parseBuffer, 0, remainingBytes); - NoteCompact(Stopwatch.GetTimestamp() - compactStarted, remainingBytes); - } - - buffered = remainingBytes; - } - - NoteFramesThisRead(framesThisRead); - if (framesThisRead > 0) - { - NoteReadsToFrame(readsSinceFrame); - readsSinceFrame = 0; - } - } - } - finally - { - ReleaseStoreSlots(); - _streams.Clear(); - _storeStreams.Clear(); - _readyRing.Clear(); - ArrayPool.Shared.Return(parseBuffer); - var cpuEnd = ThreadCpuTimes.Capture(); - AddSessionCpu((cpuEnd.Kernel - cpuStart.Kernel) + (cpuEnd.User - cpuStart.User)); - StorageVatpSessionLogMessages.ConnectionClosed(_logger); - } - } - - /// - public async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposed, 1) == 1) - { - return; - } - - await _transport.DisposeAsync().ConfigureAwait(false); - _writeGate.Dispose(); - } - - private async Task HandleFrameAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - switch (frame.Header.Type) - { - case VatpFrameType.Hello: - return await HandleHelloAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Open: - return await HandleOpenAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Store: - return await HandleStoreAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Meta: - return await HandleStoreMetaAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Data: - return await HandleStoreDataAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.End: - return await HandleStoreEndAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Window: - return await HandleWindowAsync(frame, cancellationToken).ConfigureAwait(false); - case VatpFrameType.Cancel: - return await HandleCancelAsync(frame, cancellationToken).ConfigureAwait(false); - default: - await WriteFailAsync( - frame.Header.StreamId == VatpProtocol.ConnectionStreamId - ? VatpProtocol.ConnectionStreamId - : frame.Header.StreamId, - VatpErrorCode.InvalidFrameType, - cancellationToken) - .ConfigureAwait(false); - return false; - } - } - - private async Task HandleHelloAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - if (frame.Header.StreamId != VatpProtocol.ConnectionStreamId) - { - await WriteFailAsync(VatpProtocol.ConnectionStreamId, VatpErrorCode.InvalidStreamId, cancellationToken) - .ConfigureAwait(false); - return false; - } - - if (_clientHelloComplete) - { - await WriteFailAsync(VatpProtocol.ConnectionStreamId, VatpErrorCode.InvalidHello, cancellationToken) - .ConfigureAwait(false); - return false; - } - - if (!VatpHello.TryDecode(frame.Payload, out var hello, out var error)) - { - await WriteFailAsync(VatpProtocol.ConnectionStreamId, error, cancellationToken).ConfigureAwait(false); - return false; - } - - _maxFramePayload = Math.Min(hello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload); - _clientHelloComplete = true; - StorageVatpSessionLogMessages.ClientHelloAccepted(_logger, _maxFramePayload); - - if (!_serverHelloSent) - { - _serverHelloSent = true; - var encoded = VatpFrameEncoder.EncodeHello(_maxFramePayload); - await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false); - } - - return true; - } - - private async Task HandleOpenAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (!_clientHelloComplete) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidStateTransition, cancellationToken).ConfigureAwait(false); - return false; - } - - if (!VatpOpenPayload.TryDecode(frame.Payload, out var open, out var decodeError)) - { - await WriteFailAsync(streamId, decodeError, cancellationToken).ConfigureAwait(false); - return true; - } - - if (streamId == VatpProtocol.ConnectionStreamId - || _streams.ContainsKey(streamId) - || _storeStreams.ContainsKey(streamId) - || _streams.Count + _storeStreams.Count >= _limits.MaxStreamsPerConnection) - { - await WriteFailAsync(streamId, streamId == VatpProtocol.ConnectionStreamId - ? VatpErrorCode.InvalidStreamId - : VatpErrorCode.StreamTableError, cancellationToken) - .ConfigureAwait(false); - return true; - } - - cancellationToken.ThrowIfCancellationRequested(); - var result = _openBoundary.TryOpen(open.RequestId, open.ArticleId); - cancellationToken.ThrowIfCancellationRequested(); - if (!result.Accepted || result.Record.ParseStatus != ArticleParseStatus.CanonicalV1) - { - StorageVatpSessionLogMessages.OpenRejected(_logger, streamId, open.RequestId); - await WriteFailAsync(streamId, VatpErrorCode.OpenRejected, cancellationToken).ConfigureAwait(false); - return true; - } - - var record = result.Record; - var meta = ArticleCanonicalTransferMeta.FromRecord(in record, result.SelectedDateHeaderName); - var metaBytes = VatpMetaCodec.Encode(in meta); - var stream = new SendStream( - streamId, - record, - new ArticleTransferWindow(_limits.InitialStreamWindowBytes, _limits.MaxStreamCreditBytes)); - _streams.Add(streamId, stream); - StorageVatpSessionLogMessages.OpenAccepted(_logger, streamId, open.RequestId); - await WriteEncodedAsync(VatpFrameEncoder.EncodeMeta(streamId, metaBytes), cancellationToken).ConfigureAwait(false); - stream.Phase = SendPhase.SendingData; - if (stream.SentBytes < record.ArtSize && stream.Window.HasCredit) - { - _readyRing.Enqueue(streamId); - } - - await PumpDataAsync(cancellationToken).ConfigureAwait(false); - return true; - } - - private async Task HandleWindowAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (frame.Payload.Length != VatpProtocol.WindowPayloadLength) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameLength, cancellationToken).ConfigureAwait(false); - return true; - } - - Span windowBytes = stackalloc byte[VatpProtocol.WindowPayloadLength]; - if (frame.Payload.IsSingleSegment) - { - frame.Payload.FirstSpan.CopyTo(windowBytes); - } - else - { - frame.Payload.CopyTo(windowBytes); - } - - if (!VatpControlPayload.TryDecodeWindow(windowBytes, out var addCredit)) - { - await WriteFailAsync(streamId, VatpErrorCode.InvalidFrameLength, cancellationToken).ConfigureAwait(false); - return true; - } - - if (addCredit == 0) - { - return true; - } - - if (!_streams.TryGetValue(streamId, out var stream) || stream.Phase != SendPhase.SendingData) - { - await WriteFailAsync(streamId, VatpErrorCode.UnknownStream, cancellationToken).ConfigureAwait(false); - return true; - } - - _ = stream.Window.Add(addCredit); - if (stream.SentBytes < stream.Record.ArtSize) - { - _readyRing.Enqueue(streamId); - } - - await PumpDataAsync(cancellationToken).ConfigureAwait(false); - return true; - } - - private async Task HandleCancelAsync(VatpParsedFrame frame, CancellationToken cancellationToken) - { - var streamId = frame.Header.StreamId; - if (_storeStreams.TryGetValue(streamId, out var store)) - { - if (store.AcceptStarted) - { - return true; - } - - _ = store.Receive.TryCancel(); - RemoveStore(streamId); - StorageVatpSessionLogMessages.TransferCancelled(_logger, streamId); - await WriteFailAsync(streamId, VatpErrorCode.Cancelled, cancellationToken).ConfigureAwait(false); - return true; - } - - if (!_streams.Remove(streamId, out var stream)) - { - await WriteFailAsync(streamId, VatpErrorCode.UnknownStream, cancellationToken).ConfigureAwait(false); - return true; - } - - _readyRing.Remove(streamId); - stream.Phase = SendPhase.Cancelled; - StorageVatpSessionLogMessages.TransferCancelled(_logger, streamId); - return true; - } - - private async Task PumpDataAsync(CancellationToken cancellationToken) - { - while (TryTakeDataFrame(out var encoded, out var completedStreamId)) - { - await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false); - if (completedStreamId is { } streamId) - { - await WriteEncodedAsync(VatpFrameEncoder.EncodeEnd(streamId), cancellationToken).ConfigureAwait(false); - } - } - } - - private bool TryTakeDataFrame(out VatpFrameEncoder.EncodedFrame encoded, out uint? completedStreamId) - { - encoded = default; - completedStreamId = null; - var attempts = _readyRing.Count; - while (attempts-- > 0 && _readyRing.TryTakeNext(out var streamId)) - { - if (!_streams.TryGetValue(streamId, out var stream) || stream.Phase != SendPhase.SendingData) - { - continue; - } - - var remaining = stream.Record.ArtSize - stream.SentBytes; - if (remaining <= 0) - { - CompleteStream(stream); - continue; - } - - if (!stream.Window.HasCredit) - { - continue; - } - - var length = ArticleTransferReadyRing.ComputeDataPayloadLength( - remaining, - stream.Window.Credit, - _maxFramePayload); - if (length <= 0 || !stream.Window.TryConsume(length)) - { - continue; - } - - var fin = length == remaining; - var payload = stream.Record.ArtData.Slice(stream.SentBytes, length); - stream.SentBytes += length; - encoded = VatpFrameEncoder.EncodeData(streamId, payload, fin); - if (stream.SentBytes < stream.Record.ArtSize) - { - _readyRing.Enqueue(streamId); - } - else - { - CompleteStream(stream); - completedStreamId = streamId; - } - - return true; - } - - return false; - } - - private void CompleteStream(SendStream stream) - { - _readyRing.Remove(stream.StreamId); - stream.Phase = SendPhase.Completed; - _ = _streams.Remove(stream.StreamId); - StorageVatpSessionLogMessages.TransferCompleted(_logger, stream.StreamId); - } - - private async Task WriteFailAsync(uint streamId, VatpErrorCode error, CancellationToken cancellationToken) - { - var encoded = VatpFrameEncoder.EncodeFail(streamId, error); - await WriteEncodedAsync(encoded, cancellationToken).ConfigureAwait(false); - } - - private async Task WriteEncodedAsync(VatpFrameEncoder.EncodedFrame encoded, CancellationToken cancellationToken) - { - SetServerIoPhase(2); - try - { - var gateStarted = Stopwatch.GetTimestamp(); - await _writeGate.WaitAsync(cancellationToken).ConfigureAwait(false); - AddWriteGateTicks(Stopwatch.GetTimestamp() - gateStarted); - try - { - var headerLength = encoded.Header.Length; - var payloadLength = encoded.Payload.Length; - if (headerLength + payloadLength <= _smallWrite.Length) - { - if (headerLength > 0) - { - encoded.Header.Span.CopyTo(_smallWrite); - } - - if (payloadLength > 0) - { - encoded.Payload.Span.CopyTo(_smallWrite.AsSpan(headerLength)); - } - - await WriteFullyAsync(_smallWrite.AsMemory(0, headerLength + payloadLength), cancellationToken) - .ConfigureAwait(false); - return; - } - - if (!encoded.Header.IsEmpty) - { - await WriteFullyAsync(encoded.Header, cancellationToken).ConfigureAwait(false); - } - - if (!encoded.Payload.IsEmpty) - { - await WriteFullyAsync(encoded.Payload, cancellationToken).ConfigureAwait(false); - } - } - finally - { - _writeGate.Release(); - } - } - finally - { - SetServerIoPhase(0); - } - } - - private async Task WriteFullyAsync(ReadOnlyMemory payload, CancellationToken cancellationToken) - { - var written = 0; - while (written < payload.Length) - { - var accepted = await _transport.WriteAsync(payload[written..], cancellationToken).ConfigureAwait(false); - if (accepted <= 0) - { - throw new IOException("Transport returned zero accepted bytes."); - } - - written += accepted; - } - } - - private void TrySetFatalSessionException(Exception exception) - { - _ = Interlocked.CompareExchange(ref _fatalSessionException, exception, null); - } - - private enum SendPhase - { - SendingData, - Completed, - Cancelled, - } - - private sealed class SendStream - { - public SendStream(uint streamId, ArticleRecord record, ArticleTransferWindow window) - { - StreamId = streamId; - Record = record; - Window = window; - } - - public uint StreamId { get; } - - public ArticleRecord Record { get; } - - /// Mutable credit. Must stay a field so updates this stream. - public ArticleTransferWindow Window; - - public int SentBytes { get; set; } - - public SendPhase Phase { get; set; } - } - } -} diff --git a/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs b/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs index dbf0265f..75fa81da 100644 --- a/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs +++ b/src/VectorNNTPD.StorageService/Listener/StoreAssemblyAdmission.cs @@ -2,8 +2,8 @@ namespace VectorNNTPD.StorageService.Listener { /// - /// Process-wide cap on in-flight STORE article assemblies, acquired before the receive - /// stream allocates ArtData. + /// Process-wide cap on in-flight TAKETHIS article assemblies, acquired before the article + /// payload is buffered and admitted to the accepted-article queue. /// public sealed class StoreAssemblyAdmission { diff --git a/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs b/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs deleted file mode 100644 index 68680823..00000000 --- a/src/VectorNNTPD.StorageService/Listener/StoreReservationHold.cs +++ /dev/null @@ -1,110 +0,0 @@ -using VectorNNTPD.StorageService.Ingress; - -namespace VectorNNTPD.StorageService.Listener -{ - - /// - /// Exactly-once ownership of a queue reservation bound to a STORE stream. - /// - /// - /// Bind, release, and commit-take share an atomic ownership flag so concurrent - /// RemoveStore, authorization failure, and TryCommit paths cannot both - /// release or both consume the same slot. The hold must be taken before - /// so teardown cannot observe a still-bound - /// reservation after the queue has already consumed it. - /// - internal sealed class StoreReservationHold - { - private IAcceptedArticleQueue? _queue; - private QueueReservation _reservation; - private int _bound; - - /// Gets whether a reservation is currently bound to this hold. - public bool IsBound => Volatile.Read(ref _bound) != 0; - - /// Gets the bound reservation. Meaningful only while is true. - public QueueReservation Reservation => _reservation; - - /// Gets the queue that owns the bound reservation. - public IAcceptedArticleQueue? Queue => _queue; - - /// - /// Records a reserved queue slot and the queue that must release it. - /// - /// - /// Callers must serialize this method with and - /// (the session _storeGate) so a concurrent - /// teardown cannot observe as false while - /// and are only partially published. - /// - public void Bind(IAcceptedArticleQueue? queue, QueueReservation reservation) - { - _queue = queue; - _reservation = reservation; - // Publish ownership last; paired take/release uses Interlocked.Exchange. - Volatile.Write(ref _bound, queue is not null ? 1 : 0); - } - - /// - /// Releases the bound reservation if this caller wins ownership. - /// - /// - /// when this call performed the release; - /// when there was no bound reservation or another caller already took it. - /// - public bool TryRelease() - { - if (!TryTake(out var queue, out var reservation)) - { - return false; - } - - queue?.Release(reservation); - return true; - } - - /// - /// Takes exclusive ownership for a commit attempt without releasing the reservation. - /// - /// - /// when this caller now owns and - /// and must either - /// or them; - /// when another path already released or took the reservation. - /// - public bool TryTakeForCommit(out IAcceptedArticleQueue? queue, out QueueReservation reservation) - { - return TryTake(out queue, out reservation); - } - - /// - /// Records whether a taken reservation was committed into the queue. - /// Does not call . - /// - /// - /// when returned true. - /// - public void MarkCommitOutcome(bool committed) - { - Committed = committed; - } - - /// Gets whether the reservation was committed into the queue. - public bool Committed { get; private set; } - - private bool TryTake(out IAcceptedArticleQueue? queue, out QueueReservation reservation) - { - if (Interlocked.Exchange(ref _bound, 0) == 0) - { - queue = null; - reservation = default; - return false; - } - - queue = _queue; - reservation = _reservation; - _queue = null; - return true; - } - } -} diff --git a/src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs b/src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs similarity index 86% rename from src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs rename to src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs index 7228c870..93b016f8 100644 --- a/src/VectorNNTPD.StorageService/Listener/IStorageVatpTransport.cs +++ b/src/VectorNNTPD.StorageService/Listener/StreamStorageListenerTransport.cs @@ -2,40 +2,13 @@ namespace VectorNNTPD.StorageService.Listener { - - /// One established connected transport owned by a StorageServer VATP session. - public interface IStorageVatpTransport : IAsyncDisposable - { - /// Reads bytes. Zero means the peer closed. - ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken); - - /// Writes bytes and returns how many were accepted. - ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken); - - /// - /// Suspends or resumes the read no-progress timeout. - /// STORE admission suspends it while the client must stay silent, then resumes it - /// when that wait ends so a later stall still times out. The default does nothing. - /// - /// to disarm the outstanding read timer. - void SetReadProgressSuspended(bool suspended) { } - } - /// /// Stream adapter that applies the configured I/O no-progress timeout to each read or write. /// Successive reads reuse one timeout source, and successive writes reuse another. - /// One read may overlap one write. STORE admission does that when it grants WINDOW while the - /// session is blocked waiting for the next client frame. Two overlapping reads, or two - /// overlapping writes, are not supported. A read and a write keep separate timeout sources, - /// so a WINDOW write that passes cannot cancel the read. + /// One read may overlap one write. Queue admission may suspend the read timer while waiting + /// for capacity. Two overlapping reads, or two overlapping writes, are not supported. /// - /// - /// Timeout CTS and dispose state are per transport instance. I/O-split and TLS thread-cost - /// counters are process-wide aggregates for bench observation (same model as - /// receive metrics): every live transport contributes through - /// updates, and clears the process totals. - /// - public sealed class StreamStorageVatpTransport : IStorageVatpTransport + public sealed class StreamStorageListenerTransport : IStorageListenerTransport { private readonly Stream _stream; private readonly TimeSpan _ioProgressTimeout; @@ -178,7 +151,7 @@ private static void NoteWriteTlsThreadCost(int threadId, long cpuKernel, long cp } /// Initializes a stream-backed transport. - public StreamStorageVatpTransport(Stream stream, TimeSpan ioProgressTimeout, bool leaveInnerStreamOpen = false) + public StreamStorageListenerTransport(Stream stream, TimeSpan ioProgressTimeout, bool leaveInnerStreamOpen = false) { ArgumentNullException.ThrowIfNull(stream); ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(ioProgressTimeout, TimeSpan.Zero); @@ -216,7 +189,7 @@ public async ValueTask ReadAsync(Memory buffer, CancellationToken can } catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) { - throw new TimeoutException($"Storage VATP transport read exceeded no-progress timeout of {_ioProgressTimeout}."); + throw new TimeoutException($"Storage NNTP transport read exceeded no-progress timeout of {_ioProgressTimeout}."); } } @@ -245,7 +218,7 @@ public async ValueTask WriteAsync(ReadOnlyMemory buffer, Cancellation } catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !cancellationToken.IsCancellationRequested) { - throw new TimeoutException($"Storage VATP transport write exceeded no-progress timeout of {_ioProgressTimeout}."); + throw new TimeoutException($"Storage NNTP transport write exceeded no-progress timeout of {_ioProgressTimeout}."); } } diff --git a/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs b/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs deleted file mode 100644 index 972fcab3..00000000 --- a/src/VectorNNTPD.StorageService/Listener/VatpStoreOutcome.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace VectorNNTPD.StorageService.Listener -{ - - /// VATP RESULT outcome byte written after an article is queued. - public static class VatpStoreOutcome - { - /// - /// The Path-rewritten article was accepted into the in-memory queue. - /// This is the historical ArticleAcceptOutcome.Accepted value (1). - /// - public const byte Accepted = 1; - - /// - /// The article cannot fit in the queue budget, so the body is not authorized. - /// This is the historical ArticleAcceptOutcome.RejectedCapacity value (6). - /// A full queue does not use this outcome. The server withholds WINDOW until a reservation fits. - /// - public const byte RejectedCapacity = 6; - } -} diff --git a/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs b/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs index 1be41785..f46d5cc0 100644 --- a/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs +++ b/src/VectorNNTPD.StorageService/Storage/ArticleIndex/ArticleIndexService.cs @@ -18,11 +18,11 @@ namespace VectorNNTPD.StorageService.Storage.ArticleIndex /// Startup creates DbDir when it is missing and fails if that directory cannot be used. /// It does not substitute a spool directory. Membership is loaded from the open databases, /// and only then does return. Later application services, including the - /// accepted-article worker and the VATP listener, start after that. + /// accepted-article worker and the NNTP feed listener, start after that. /// Startup does not walk article files or compare them with index rows. /// /// - /// VATP RESULT is still sent when an article is queued, before the spool write and before this index. + /// TAKETHIS 239 is still sent when an article is queued, before the spool write and before this index. /// A committed index row is durable across process and power loss because each writer uses WAL and /// synchronous=FULL. An access update that is only queued is not durable until its batch commits. /// Eviction queries skip a row whose queued access time is newer than the committed time. diff --git a/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs b/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs index deea82f5..41f752a9 100644 --- a/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs +++ b/src/VectorNNTPD.StorageService/Storage/SegmentPools/SegmentPoolValidationService.cs @@ -7,7 +7,7 @@ namespace VectorNNTPD.StorageService.Storage.SegmentPools { /// - /// Hard startup gate. Completes, or throws, before the accepted-article worker, VATP listener, + /// Hard startup gate. Completes, or throws, before the accepted-article worker, NNTP feed listener, /// and RabbitMQ advertisement publisher are started. Thresholds are not enforced here. /// internal sealed class SegmentPoolValidationService : IApplicationService diff --git a/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs b/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs index 0aab9f19..4ba97c4a 100644 --- a/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs +++ b/src/VectorNNTPD.StorageService/Storage/SpoolPersistence/SpoolArticlePersistenceProcessor.cs @@ -13,7 +13,7 @@ namespace VectorNNTPD.StorageService.Storage.SpoolPersistence /// /// Rewrites Path, publishes the rewritten bytes to a selected spool, and then indexes the article. - /// A successful VATP enqueue has already happened. This processor does not change that. + /// A successful feed enqueue has already happened. This processor does not change that. /// The index insert runs only after a stored or duplicate publish and waits for the SQLite commit. /// internal sealed class SpoolArticlePersistenceProcessor : IAcceptedArticleProcessor diff --git a/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs b/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs index 8620ad34..f7a113de 100644 --- a/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs +++ b/src/VectorNNTPD.StorageService/Storage/SpoolShards/SpoolArticlePath.cs @@ -22,7 +22,7 @@ internal static class SpoolArticlePath /// Relative path {n0}/{n1}/{n2}/{64 lowercase hex}. /// The three directory nibbles are the first three characters of the canonical lowercase hex digest. /// - /// Canonical VATP article identity. + /// Canonical article identity. /// A relative path with no parent segments. internal static string RelativePath(ArticleId articleId) { @@ -38,7 +38,7 @@ internal static string RelativePath(ArticleId articleId) /// The normalized root prefix is cached for the life of the process. /// /// Absolute spool directory. - /// Canonical VATP article identity. + /// Canonical article identity. /// The absolute article path, using the platform directory separator. internal static string CombineArticle(string spoolRoot, ArticleId articleId) { diff --git a/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs b/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs index 27d0fa74..036231af 100644 --- a/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs +++ b/src/VectorNNTPD.StorageService/Storage/StorageServerAdvertisementPublisherService.cs @@ -14,7 +14,7 @@ namespace VectorNNTPD.StorageService.Storage /// Uses the shared connection; does not own TCP lifecycle. /// Heartbeat publication must not block clean shutdown. /// The periodic advertisement is the readiness signal. It starts only after this service starts, - /// which is after the VATP listener is accepting. Stop publishes one transient Draining + /// which is after the NNTP feed listener is accepting. Stop publishes one transient Draining /// announcement after the loop has stopped and before the channel is disposed. A failed Draining /// publication is logged and does not block shutdown past . /// diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs index 92434ccb..ae039433 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageDnsPublicationRollbackTests.cs @@ -41,7 +41,7 @@ public async Task Bind_failure_before_publication_leaves_existing_records() await Assert.ThrowsAsync(() => listener.StartAsync(CancellationToken.None)); Assert.Equal(0, published); - Assert.Equal(StorageVatpListenerState.Stopped, listener.State); + Assert.Equal(StorageNntpListenerState.Stopped, listener.State); await harness.Service.StopAsync(CancellationToken.None); Assert.Equal(before, Ids(harness.Client)); @@ -73,7 +73,7 @@ public async Task Failure_after_registration_completes_removes_the_owning_instan { var harness = CreateHarness(HealthyAddresses); var publisher = new FakeApplicationService( - "StorageVatpListener", + "StorageNntpListener", onStart: cancellationToken => harness.Service.PublishAfterListenerBoundAsync(cancellationToken)); var later = new FakeApplicationService( "later", @@ -96,7 +96,7 @@ public async Task Graceful_shutdown_of_the_owning_instance_removes_its_records() { var harness = CreateHarness(HealthyAddresses); var listener = new FakeApplicationService( - "StorageVatpListener", + "StorageNntpListener", onStart: cancellationToken => harness.Service.PublishAfterListenerBoundAsync(cancellationToken)); var manager = new ApplicationServiceManager( [new CloudflareDnsReconciliationApplicationService(harness.Service), listener], @@ -139,7 +139,7 @@ public async Task Second_instance_bind_failure_leaves_the_healthy_instance_recor await intruder.Service.StopAsync(CancellationToken.None); Assert.Equal(0, published); - Assert.Equal(StorageVatpListenerState.Running, ownerListener.State); + Assert.Equal(StorageNntpListenerState.Running, ownerListener.State); Assert.Equal(owned, Ids(client)); await ownerListener.StopAsync(CancellationToken.None); @@ -293,7 +293,7 @@ private static CloudflareDnsRecord Record(string id, string type, string fqdn, s Proxied = false, }; - private static StorageVatpListenerService CreateListener(int port, Func publish) + private static StorageNntpListenerService CreateListener(int port, Func publish) { var identity = StorageServerTestOptions.CreateValid(); identity.BindAddress = ["127.0.0.1"]; @@ -302,12 +302,11 @@ private static StorageVatpListenerService CreateListener(int port, Func.Instance, + NullLogger.Instance, publishAfterBind: publish); } diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs index b2cfc19d..4d72aae9 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerFleetLifecycleOrderTests.cs @@ -35,7 +35,7 @@ public sealed class StorageServerFleetLifecycleOrderTests public async Task AdvertisementPublisher_DoesNotStartBeforeVatpListener() { await using var session = await FleetSession.StartHeldAtListenerAsync(); - Assert.NotEqual(StorageVatpListenerState.Running, session.Listener.State); + Assert.NotEqual(StorageNntpListenerState.Running, session.Listener.State); Assert.Equal(0, session.Publisher.PublishedCount); Assert.Null(session.Publisher.Execution); Assert.DoesNotContain( @@ -47,7 +47,7 @@ public async Task AdvertisementPublisher_DoesNotStartBeforeVatpListener() public async Task AdvertisementPublisher_StartsAfterVatpListener() { await using var session = await FleetSession.StartAsync(); - Assert.Equal(StorageVatpListenerState.Running, session.Listener.State); + Assert.Equal(StorageNntpListenerState.Running, session.Listener.State); await WaitForAsync(() => session.Publisher.PublishedCount >= 1, TimeSpan.FromSeconds(5)); var publications = session.Connection.PublishChannels.SelectMany(static channel => channel.Publications).ToArray(); Assert.Contains(publications, static publication => IsAdvertisement(publication)); @@ -61,7 +61,7 @@ public async Task AdvertisementPublisher_StopsBeforeVatpListener() await WaitForAsync(() => session.Publisher.PublishedCount >= 1, TimeSpan.FromSeconds(5)); await session.StopAsync(); var publisherEnd = session.StopEvents.IndexOf("end:StorageServerAdvertisementPublisher"); - var listenerBegin = session.StopEvents.IndexOf("begin:StorageVatpListener"); + var listenerBegin = session.StopEvents.IndexOf("begin:StorageNntpListener"); Assert.True(publisherEnd >= 0); Assert.True(listenerBegin > publisherEnd); Assert.Equal(session.PublishedCountWhenListenerStopBegan, session.Publisher.PublishedCount); @@ -80,7 +80,7 @@ public async Task StartupFailure_DoesNotAdvertise() Assert.NotNull(failure); Assert.Equal(0, session.Publisher.PublishedCount); Assert.Null(session.Publisher.Execution); - Assert.NotEqual(StorageVatpListenerState.Running, session.Listener.State); + Assert.NotEqual(StorageNntpListenerState.Running, session.Listener.State); Assert.DoesNotContain( session.Connection.PublishChannels.SelectMany(static channel => channel.Publications), static publication => IsDraining(publication)); @@ -140,7 +140,7 @@ private sealed class FleetSession : IAsyncDisposable private FleetSession( IHost host, - StorageVatpListenerService listener, + StorageNntpListenerService listener, StorageServerAdvertisementPublisherService publisher, RecordingStorageServerRabbitMqConnectionFactory factory, bool held) @@ -156,7 +156,7 @@ private FleetSession( public ApplicationServiceManager Manager { get; private set; } = null!; - public StorageVatpListenerService Listener { get; } + public StorageNntpListenerService Listener { get; } public StorageServerAdvertisementPublisherService Publisher { get; } @@ -236,16 +236,16 @@ private static Task CreateAsync(int port, bool holdListener, bool var host = CreateHost(port, factory); var ordered = host.Services.GetServices().ToArray(); AssertProductionOrder(ordered); - var listener = host.Services.GetRequiredService(); + var listener = host.Services.GetRequiredService(); var publisher = host.Services.GetRequiredService(); var session = new FleetSession(host, listener, publisher, factory, holdListener); var services = ordered.Select(service => service switch { - StorageVatpListenerService when holdListener => (IApplicationService)new HoldListener( + StorageNntpListenerService when holdListener => (IApplicationService)new HoldListener( listener, session.ListenerEntered, session._releaseListener), - StorageVatpListenerService when traceStops => new StopTrace( + StorageNntpListenerService when traceStops => new StopTrace( service, session.StopEvents, () => @@ -268,7 +268,7 @@ private static Task CreateAsync(int port, bool holdListener, bool private static void AssertProductionOrder(IApplicationService[] ordered) { - var listenerIndex = Array.FindIndex(ordered, static service => service is StorageVatpListenerService); + var listenerIndex = Array.FindIndex(ordered, static service => service is StorageNntpListenerService); var publisherIndex = Array.FindIndex( ordered, static service => service is StorageServerAdvertisementPublisherService); @@ -328,12 +328,12 @@ private static void ReplaceAcme(IServiceCollection services) private sealed class HoldListener : IApplicationService { - private readonly StorageVatpListenerService _inner; + private readonly StorageNntpListenerService _inner; private readonly TaskCompletionSource _entered; private readonly TaskCompletionSource _release; public HoldListener( - StorageVatpListenerService inner, + StorageNntpListenerService inner, TaskCompletionSource entered, TaskCompletionSource release) { diff --git a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs index 4fdb887d..624b003b 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Hosting/StorageServerHostCompositionTests.cs @@ -68,10 +68,10 @@ public void AddStorageServerHosting_registers_expected_service_graph() Assert.IsType(application[7]); Assert.IsType(application[8]); Assert.Same(host.Services.GetRequiredService(), application[9]); - Assert.Same(host.Services.GetRequiredService(), application[10]); + Assert.Same(host.Services.GetRequiredService(), application[10]); Assert.IsType(application[11]); Assert.Equal(1, application.Count(static service => service is AcceptedArticleWorker)); - Assert.IsType(host.Services.GetRequiredService()); + Assert.IsType(host.Services.GetRequiredService()); Assert.IsType(host.Services.GetRequiredService()); Assert.IsType(host.Services.GetRequiredService()); Assert.NotNull(host.Services.GetRequiredService()); @@ -145,8 +145,8 @@ public async Task Pool_validation_failure_stops_the_host_before_the_listener_and Assert.NotEqual(ApplicationState.Running, lifecycle.State); Assert.Equal(ApplicationState.Stopped, lifecycle.State); - var listener = host.Services.GetRequiredService(); - Assert.Equal(StorageVatpListenerState.Created, listener.State); + var listener = host.Services.GetRequiredService(); + Assert.Equal(StorageNntpListenerState.Created, listener.State); Assert.Equal(0, host.Services.GetRequiredService().PublishedCount); var application = host.Services.GetServices().ToArray(); @@ -165,7 +165,7 @@ public async Task Shard_initialization_failure_stops_the_host_before_the_listene var lifecycle = host.Services.GetRequiredService(); Assert.Equal(ApplicationState.Stopped, lifecycle.State); - Assert.Equal(StorageVatpListenerState.Created, host.Services.GetRequiredService().State); + Assert.Equal(StorageNntpListenerState.Created, host.Services.GetRequiredService().State); Assert.Equal(0, host.Services.GetRequiredService().PublishedCount); var application = host.Services.GetServices().ToArray(); Assert.Null(application.OfType().Single().Execution); @@ -194,10 +194,10 @@ public async Task Host_starts_application_services_in_order_when_acme_and_rabbit Assert.IsType(application[7]); Assert.IsType(application[8]); Assert.IsType(application[9]); - var listener = host.Services.GetRequiredService(); + var listener = host.Services.GetRequiredService(); Assert.Same(listener, application[10]); Assert.IsType(application[11]); - Assert.Equal(StorageVatpListenerState.Running, listener.State); + Assert.Equal(StorageNntpListenerState.Running, listener.State); Assert.True(host.Services.GetRequiredService().IsReady); Assert.True(host.Services.GetRequiredService().IsReady); Assert.NotNull(host.Services.GetRequiredService()); diff --git a/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs b/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs index 9bf53995..38deb48a 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Lifecycle/StorageServerLifecycleTests.cs @@ -41,11 +41,11 @@ public async Task ApplicationServiceManager_starts_in_registration_order_and_rol return Task.CompletedTask; }); var listener = new FakeApplicationService( - "StorageVatpListener", + "StorageNntpListener", onStart: _ => throw new InvalidOperationException("listener-fail"), onStop: _ => { - stopped.Add("StorageVatpListener"); + stopped.Add("StorageNntpListener"); return Task.CompletedTask; }); @@ -68,7 +68,7 @@ public async Task Graceful_shutdown_stops_in_reverse_order() { new FakeApplicationService("CloudflareDns", onStop: _ => { stopped.Add("CloudflareDns"); return Task.CompletedTask; }), new FakeApplicationService("AcmeCertificate", onStop: _ => { stopped.Add("AcmeCertificate"); return Task.CompletedTask; }), - new FakeApplicationService("StorageVatpListener", onStop: _ => { stopped.Add("StorageVatpListener"); return Task.CompletedTask; }), + new FakeApplicationService("StorageNntpListener", onStop: _ => { stopped.Add("StorageNntpListener"); return Task.CompletedTask; }), }; var options = StorageServerTestOptions.CreateValid(); var manager = new ApplicationServiceManager(services, options, NullLogger.Instance); @@ -78,7 +78,7 @@ public async Task Graceful_shutdown_stops_in_reverse_order() Assert.Equal(ApplicationState.Running, lifecycle.State); await lifecycle.StopAsync(CancellationToken.None); Assert.Equal(ApplicationState.Stopped, lifecycle.State); - Assert.Equal(["StorageVatpListener", "AcmeCertificate", "CloudflareDns"], stopped); + Assert.Equal(["StorageNntpListener", "AcmeCertificate", "CloudflareDns"], stopped); } [Fact] diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs index 0fb2e783..657c71f6 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/ArticlePlacementClientBench.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Net.Security; using System.Text; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Articles; @@ -9,9 +8,8 @@ namespace VectorNNTPD.StorageService.Tests.Listener { - /// - /// Measures against the StorageServer ingress listener. + /// Measures against the StorageServer NNTP listener. /// Runs only when VECTORNNTPD_STORAGE_PLACEMENT_BENCH=1. /// public sealed class ArticlePlacementClientBench @@ -28,6 +26,7 @@ public async Task One_connection_compares_one_and_two_outstanding_stores() _output.WriteLine("Set VECTORNNTPD_STORAGE_PLACEMENT_BENCH=1 to measure ArticlePlacementClient."); return; } + var seconds = 8; var configured = Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_PLACEMENT_SECONDS"); if (int.TryParse(configured, out var parsed) && parsed is >= 1 and <= 120) @@ -36,14 +35,14 @@ public async Task One_connection_compares_one_and_two_outstanding_stores() } var record = CreateRecord(); - _output.WriteLine($"artBytes={record.ArtSize} bodyBytes={StorageVatpThroughputBench.BodyBytes} window=1048576"); + _output.WriteLine($"artBytes={record.ArtSize} bodyBytes={StorageNntpThroughputBench.BodyBytes}"); _output.WriteLine(await MeasureAsync(record, outstanding: 1, TimeSpan.FromSeconds(seconds))); _output.WriteLine(await MeasureAsync(record, outstanding: 2, TimeSpan.FromSeconds(seconds))); } private async Task MeasureAsync(ArticleRecord record, int outstanding, TimeSpan duration) { - await using var host = await StorageVatpThroughputBench.StartPlacementIngressHostAsync(); + await using var host = await StorageNntpThroughputBench.StartPlacementIngressHostAsync(); await using var client = new ArticlePlacementClient( NullLogger.Instance, outstanding) @@ -51,36 +50,16 @@ private async Task MeasureAsync(ArticleRecord record, int outstanding, T TestTcpConnectHost = "127.0.0.1", ServerCertificateValidationCallback = static (_, _, _, _) => true, }; - var target = new StorageServerFleetEntry( - 1, - "cache01.usenet.ninja", - 10_000, - 0, - 10_000, - DateTimeOffset.UtcNow, - host.Port); - var warmup = await client.PlaceAsync(record, target, CancellationToken.None); + var warmup = await client.PlaceAsync(record, host.Target, CancellationToken.None); if (warmup.Kind != ArticlePlacementKind.Accepted || client.ConnectCount != 1) { throw new InvalidOperationException($"Warmup failed kind={warmup.Kind} connects={client.ConnectCount} failure={warmup.Failure}"); } + _ = await host.Queue.DequeueAsync(CancellationToken.None); + var accepted = 0; - var duplicate = 0; - var rejected = 0; var failed = 0; - var ambiguous = 0; - var cancelled = 0; - var payload = 0L; - var inflight = 0; - var peak = 0; - var queueMax = 0L; - var allocBefore = GC.GetTotalAllocatedBytes(precise: true); - var gc0 = GC.CollectionCount(0); - var gc1 = GC.CollectionCount(1); - var gc2 = GC.CollectionCount(2); - using var process = Process.GetCurrentProcess(); - var cpu = process.TotalProcessorTime; var clock = Stopwatch.StartNew(); var workers = new Task[outstanding]; for (var worker = 0; worker < outstanding; worker++) @@ -89,123 +68,42 @@ private async Task MeasureAsync(ArticleRecord record, int outstanding, T { while (clock.Elapsed < duration) { - var now = Interlocked.Increment(ref inflight); - int seen; - do + var result = await client.PlaceAsync(record, host.Target, CancellationToken.None); + if (result.Kind == ArticlePlacementKind.Accepted) { - seen = Volatile.Read(ref peak); - if (now <= seen) - { - break; - } - } - while (Interlocked.CompareExchange(ref peak, now, seen) != seen); - - try - { - var result = await client.PlaceAsync(record, target, CancellationToken.None).ConfigureAwait(false); - var queued = host.Queue.QueuedBytes; - long previousQueue; - do - { - previousQueue = Volatile.Read(ref queueMax); - if (queued <= previousQueue) - { - break; - } - } - while (Interlocked.CompareExchange(ref queueMax, queued, previousQueue) != previousQueue); - - if (result.Kind == ArticlePlacementKind.Accepted) - { - Interlocked.Increment(ref accepted); - Interlocked.Add(ref payload, record.ArtSize); - } - else if (result.Kind == ArticlePlacementKind.Duplicate) - { - Interlocked.Increment(ref duplicate); - Interlocked.Add(ref payload, record.ArtSize); - } - else if (result.Kind == ArticlePlacementKind.AcknowledgementNotObserved) - { - Interlocked.Increment(ref ambiguous); - } - else if (result.Kind == ArticlePlacementKind.Cancelled) - { - Interlocked.Increment(ref cancelled); - } - else if (result.Succeeded) - { - Interlocked.Increment(ref accepted); - } - else if (result.Kind is ArticlePlacementKind.RejectedInvalid or ArticlePlacementKind.RejectedPressure or ArticlePlacementKind.RejectedCapacity or ArticlePlacementKind.Conflict) - { - Interlocked.Increment(ref rejected); - } - else - { - Interlocked.Increment(ref failed); - } + Interlocked.Increment(ref accepted); + _ = await host.Queue.DequeueAsync(CancellationToken.None); } - finally + else { - Interlocked.Decrement(ref inflight); + Interlocked.Increment(ref failed); } } }); } await Task.WhenAll(workers); - clock.Stop(); - var counters = client.Snapshot(); - var seconds = clock.Elapsed.TotalSeconds; - var artGbps = payload * 8 / seconds / 1_000_000_000d; - var bodyGbps = (accepted + duplicate) * (long)StorageVatpThroughputBench.BodyBytes * 8 / seconds / 1_000_000_000d; - var articlesPerSecond = (accepted + duplicate) / seconds; - var alloc = GC.GetTotalAllocatedBytes(precise: true) - allocBefore; - var cpuMs = (process.TotalProcessorTime - cpu).TotalMilliseconds; - var resultUs = counters.ResultCount == 0 - ? 0 - : counters.ResultTicks * 1_000_000d / Stopwatch.Frequency / counters.ResultCount; - var resultMaxUs = counters.ResultMaxTicks * 1_000_000d / Stopwatch.Frequency; - var slotMs = counters.SlotWaitTicks * 1000d / Stopwatch.Frequency; - var creditMs = counters.CreditWaitTicks * 1000d / Stopwatch.Frequency; - return - $"outstanding={outstanding} seconds={seconds:F3} artGbps={artGbps:F3} bodyGbps={bodyGbps:F3} articlesPerSec={articlesPerSecond:F1} " + - $"connects={client.ConnectCount} callerPeak={Volatile.Read(ref peak)} connectionPeak={counters.PeakActive} " + - $"accepted={Volatile.Read(ref accepted)} duplicate={Volatile.Read(ref duplicate)} rejected={Volatile.Read(ref rejected)} " + - $"failed={Volatile.Read(ref failed)} ambiguous={Volatile.Read(ref ambiguous)} cancelled={Volatile.Read(ref cancelled)} " + - $"resultAvgUs={resultUs:F1} resultMaxUs={resultMaxUs:F1} slotWaitMs={slotMs:F1} creditWaitMs={creditMs:F1} " + - $"queueMaxBytes={Volatile.Read(ref queueMax)} allocBytes={alloc} gc={GC.CollectionCount(0) - gc0}/{GC.CollectionCount(1) - gc1}/{GC.CollectionCount(2) - gc2} cpuMs={cpuMs:F0}"; + var snapshot = client.Snapshot(); + return $"outstanding={outstanding} accepted={accepted} failed={failed} " + + $"peak={snapshot.PeakActive} responseWaitMs={snapshot.CreditWaitTicks * 1000.0 / Stopwatch.Frequency:F1}"; } private static ArticleRecord CreateRecord() { - var body = new byte[StorageVatpThroughputBench.BodyBytes]; - for (var offset = 0; offset < body.Length; offset += 80) - { - body.AsSpan(offset, 78).Fill((byte)'x'); - body[offset + 78] = (byte)'\r'; - body[offset + 79] = (byte)'\n'; - } - - var header = Encoding.ASCII.GetBytes( + var text = "Path: peer.example\r\n" + "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + - "Message-ID: \r\n" + + "Message-ID: \r\n" + "Newsgroups: alt.test\r\n" + "From: user@example.test\r\n" + "Subject: placement-bench\r\n" + - "\r\n"); - var article = new byte[header.Length + body.Length]; - header.CopyTo(article, 0); - body.CopyTo(article, header.Length); - var created = ArticleRecordFactory.TryCreate(new NntpArticleParser("nntpd01.usenet.ninja"), article); - if (!created.IsAccepted) - { - throw new InvalidOperationException("Placement benchmark article was not accepted."); - } - + "\r\n" + + new string('y', StorageNntpThroughputBench.BodyBytes) + "\r\n"; + var created = ArticleRecordFactory.TryCreate( + new NntpArticleParser("nntpd01.usenet.ninja"), + Encoding.ASCII.GetBytes(text), + ArticlePathMode.Normalize); + Assert.True(created.IsAccepted); return created.Record; } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs new file mode 100644 index 00000000..d1608e88 --- /dev/null +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpFeedSessionTests.cs @@ -0,0 +1,400 @@ +using System.Text; +using System.Threading.Channels; +using Microsoft.Extensions.Logging.Abstractions; +using VectorNNTPD.Common.Articles; +using VectorNNTPD.Common.Articles.Parsing; +using VectorNNTPD.Common.Core; +using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.StorageService.Ingress; +using VectorNNTPD.StorageService.Listener; + +namespace VectorNNTPD.StorageService.Tests.Listener +{ + public sealed class StorageNntpFeedSessionTests + { + private const string CacheFqdn = "cache01.usenet.ninja"; + private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); + + [Fact] + public async Task TakeThis_enqueues_article_and_returns_239() + { + var wire = BuildWireArticle("", "body-line\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + + var status = await harness.TakeThisAsync(wire.MessageId, wire.Stuffed).WaitAsync(Safety); + Assert.StartsWith("239 ", status, StringComparison.Ordinal); + Assert.Contains(wire.MessageId, status, StringComparison.Ordinal); + + var delivery = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety); + Assert.True(delivery.HasArticle); + Assert.Equal(wire.ArtId, delivery.Article.ArtId); + Assert.True(delivery.Article.ArtData.Span.SequenceEqual(wire.Canonical.Span)); + Assert.Equal(0, CountHop(Encoding.ASCII.GetString(delivery.Article.ArtData.Span), CacheFqdn)); + } + + [Fact] + public async Task TakeThis_239_is_sent_before_persistence_finishes() + { + var wire = BuildWireArticle("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var processor = new BlockingProcessor(release.Task); + var worker = new AcceptedArticleWorker( + queue, + processor, + NullLogger.Instance, + new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 }, + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(), + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(), + TimeProvider.System); + await worker.StartAsync(CancellationToken.None); + try + { + await using var harness = await Harness.StartAsync(queue); + var status = await harness.TakeThisAsync(wire.MessageId, wire.Stuffed).WaitAsync(TimeSpan.FromSeconds(2)); + Assert.StartsWith("239 ", status, StringComparison.Ordinal); + Assert.Equal(0, processor.Finished); + } + finally + { + release.TrySetResult(); + await worker.StopAsync(CancellationToken.None); + } + } + + [Fact] + public async Task TakeThis_message_id_mismatch_returns_439_and_does_not_enqueue() + { + var wire = BuildWireArticle("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + + var status = await harness.TakeThisAsync("", wire.Stuffed).WaitAsync(Safety); + Assert.StartsWith("439 ", status, StringComparison.Ordinal); + Assert.False(queue.TryDequeue(out _)); + } + + [Fact] + public async Task Disconnect_during_article_does_not_enqueue() + { + var wire = BuildWireArticle("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + + await harness.WriteClientAsync(Encoding.ASCII.GetBytes($"TAKETHIS {wire.MessageId}\r\n")); + await harness.WriteClientAsync(wire.Stuffed[..Math.Min(32, wire.Stuffed.Length)]); + await harness.DisposeTransportAsync(); + + await Task.Delay(50); + Assert.False(queue.TryDequeue(out _)); + } + + [Fact] + public async Task Concurrent_sessions_enqueue_without_cross_contamination() + { + var left = BuildWireArticle("", "left-body\r\n"); + var right = BuildWireArticle("", "right-body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var a = await Harness.StartAsync(queue); + await using var b = await Harness.StartAsync(queue); + + var leftTask = a.TakeThisAsync(left.MessageId, left.Stuffed); + var rightTask = b.TakeThisAsync(right.MessageId, right.Stuffed); + var leftStatus = await leftTask.WaitAsync(Safety); + var rightStatus = await rightTask.WaitAsync(Safety); + Assert.StartsWith("239 ", leftStatus, StringComparison.Ordinal); + Assert.StartsWith("239 ", rightStatus, StringComparison.Ordinal); + + var first = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety); + var second = await queue.DequeueAsync(CancellationToken.None).AsTask().WaitAsync(Safety); + Assert.True(first.HasArticle && second.HasArticle); + var ids = new HashSet { first.Article.ArtId, second.Article.ArtId }; + Assert.Contains(left.ArtId, ids); + Assert.Contains(right.ArtId, ids); + } + + [Fact] + public async Task Check_then_mode_stream_then_capabilities_are_accepted() + { + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + Assert.StartsWith("101 ", await harness.CommandAsync("CAPABILITIES\r\n").WaitAsync(Safety), StringComparison.Ordinal); + Assert.StartsWith("203 ", await harness.CommandAsync("MODE STREAM\r\n").WaitAsync(Safety), StringComparison.Ordinal); + Assert.StartsWith("238 ", await harness.CommandAsync("CHECK \r\n").WaitAsync(Safety), StringComparison.Ordinal); + } + + private static int CountHop(string article, string hop) + { + var marker = hop + "!"; + var count = 0; + var index = 0; + while ((index = article.IndexOf(marker, index, StringComparison.Ordinal)) >= 0) + { + count++; + index += marker.Length; + } + + return count; + } + + private static BuiltWire BuildWireArticle(string messageId, string body, string ingressFqdn = "nntpd01.usenet.ninja") + { + var text = + $"Path: {ingressFqdn}!not-for-mail\r\n" + + $"From: demo@example.com\r\n" + + $"Newsgroups: misc.test\r\n" + + $"Subject: test\r\n" + + $"Date: Fri, 23 Oct 2026 12:00:00 +0000\r\n" + + $"Message-ID: {messageId}\r\n" + + "\r\n" + + body; + var destuffed = Encoding.ASCII.GetBytes(text); + var parser = new NntpArticleParser(CacheFqdn); + var created = ArticleRecordFactory.TryCreate(parser, destuffed, ArticlePathMode.Normalize); + Assert.True(created.IsAccepted); + var record = created.Record; + // Wire form for TAKETHIS: destuffed article with body lines dot-stuffed for transfer. + var stuffed = StuffForWire(destuffed); + return new BuiltWire(messageId, record.ArtId, record.ArtData, stuffed); + } + + private static byte[] StuffForWire(ReadOnlySpan destuffed) + { + // Destuffed input already has CRLF lines; double leading dots in body for NNTP transfer. + var headerEnd = destuffed.IndexOf("\r\n\r\n"u8); + Assert.True(headerEnd >= 0); + var bodyStart = headerEnd + 4; + var output = new List(destuffed.Length + 16); + output.AddRange(destuffed[..bodyStart].ToArray()); + var body = destuffed[bodyStart..]; + var offset = 0; + while (offset < body.Length) + { + var lineEnd = body[offset..].IndexOf("\r\n"u8); + ReadOnlySpan line; + int advance; + if (lineEnd < 0) + { + line = body[offset..]; + advance = line.Length; + } + else + { + line = body.Slice(offset, lineEnd); + advance = lineEnd + 2; + } + + if (line.Length > 0 && line[0] == (byte)'.') + { + output.Add((byte)'.'); + } + + output.AddRange(line.ToArray()); + if (lineEnd >= 0) + { + output.Add((byte)'\r'); + output.Add((byte)'\n'); + } + + offset += advance; + } + + output.Add((byte)'.'); + output.Add((byte)'\r'); + output.Add((byte)'\n'); + return output.ToArray(); + } + + private readonly record struct BuiltWire( + string MessageId, + ArticleId ArtId, + ReadOnlyMemory Canonical, + byte[] Stuffed); + + private sealed class BlockingProcessor : IAcceptedArticleProcessor + { + private readonly Task _release; + public int Finished; + + public BlockingProcessor(Task release) => _release = release; + + public void EnsureReady() + { + } + + public async ValueTask ProcessAsync( + AcceptedArticle article, + CancellationToken cancellationToken) + { + await _release.WaitAsync(cancellationToken).ConfigureAwait(false); + Finished++; + return AcceptedArticleProcessResult.Completed; + } + } + + private sealed class Harness : IAsyncDisposable + { + private readonly CancellationTokenSource _cts = new(); + private readonly Task _run; + private readonly PipeTransport _transport; + + private Harness(PipeTransport transport, StorageNntpFeedSession session) + { + _transport = transport; + _run = session.RunAsync(_cts.Token); + } + + public static async Task StartAsync(IAcceptedArticleQueue queue) + { + var transport = new PipeTransport(); + var session = new StorageNntpFeedSession( + transport, + new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8), + NullLogger.Instance, + CacheFqdn, + acceptedArticles: queue, + assemblyAdmission: new StoreAssemblyAdmission(8)); + var harness = new Harness(transport, session); + var greeting = await harness.ReadLineAsync().WaitAsync(Safety); + Assert.StartsWith("200 ", greeting, StringComparison.Ordinal); + return harness; + } + + public Task WriteClientAsync(byte[] bytes) => _transport.ClientWriteAsync(bytes); + + public async Task DisposeTransportAsync() + { + await _transport.DisposeAsync(); + } + + public async Task CommandAsync(string command) + { + await _transport.ClientWriteAsync(Encoding.ASCII.GetBytes(command)); + return await ReadLineAsync(); + } + + public async Task TakeThisAsync(string messageId, byte[] stuffedArticleIncludingTerminator) + { + await _transport.ClientWriteAsync(Encoding.ASCII.GetBytes($"TAKETHIS {messageId}\r\n")); + await _transport.ClientWriteAsync(stuffedArticleIncludingTerminator); + return await ReadLineAsync(); + } + + public async Task ReadLineAsync() + { + using var timeout = new CancellationTokenSource(Safety); + var buffer = new List(); + while (true) + { + var chunk = await _transport.ReadServerAsync(timeout.Token); + buffer.AddRange(chunk); + for (var i = 0; i < buffer.Count - 1; i++) + { + if (buffer[i] == (byte)'\r' && buffer[i + 1] == (byte)'\n') + { + var line = Encoding.ASCII.GetString(buffer.GetRange(0, i).ToArray()); + buffer.RemoveRange(0, i + 2); + // Capabilities multiline: keep reading until terminator line if 101. + if (line.StartsWith("101 ", StringComparison.Ordinal)) + { + while (true) + { + while (buffer.Count < 2) + { + buffer.AddRange(await _transport.ReadServerAsync(timeout.Token)); + } + + var end = -1; + for (var j = 0; j < buffer.Count - 1; j++) + { + if (buffer[j] == (byte)'\r' && buffer[j + 1] == (byte)'\n') + { + end = j; + break; + } + } + + if (end < 0) + { + buffer.AddRange(await _transport.ReadServerAsync(timeout.Token)); + continue; + } + + var next = Encoding.ASCII.GetString(buffer.GetRange(0, end).ToArray()); + buffer.RemoveRange(0, end + 2); + if (next == ".") + { + return line; + } + } + } + + return line; + } + } + } + } + + public async ValueTask DisposeAsync() + { + await _cts.CancelAsync(); + try + { + await _run; + } + catch (OperationCanceledException ex) + { + ExpectedException.Ignore(ex); + } + catch (Exception ex) when (!ExceptionHelpers.IsFatal(ex)) + { + ExpectedException.Ignore(ex); + } + + _cts.Dispose(); + await _transport.DisposeAsync(); + } + } + + private sealed class PipeTransport : IStorageListenerTransport + { + private readonly Channel _toServer = Channel.CreateUnbounded(); + private readonly Channel _toClient = Channel.CreateUnbounded(); + private byte[] _pending = []; + private int _pendingOffset; + + public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) + { + if (_pendingOffset >= _pending.Length) + { + _pending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); + _pendingOffset = 0; + } + + var count = Math.Min(buffer.Length, _pending.Length - _pendingOffset); + _pending.AsSpan(_pendingOffset, count).CopyTo(buffer.Span); + _pendingOffset += count; + return count; + } + + public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) + { + await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false); + return buffer.Length; + } + + public Task ClientWriteAsync(byte[] frame) => _toServer.Writer.WriteAsync(frame).AsTask(); + + public Task ReadServerAsync(CancellationToken cancellationToken) => + _toClient.Reader.ReadAsync(cancellationToken).AsTask(); + + public ValueTask DisposeAsync() + { + _toServer.Writer.TryComplete(); + _toClient.Writer.TryComplete(); + return ValueTask.CompletedTask; + } + } + } +} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs similarity index 72% rename from tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs rename to tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs index 3b807825..1b4b5b65 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpListenerServiceTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpListenerServiceTests.cs @@ -8,8 +8,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using VectorNNTPD.Common.Acme; -using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.Common.Transport.ArticleTransfer; using VectorNNTPD.StorageService.Configuration; using VectorNNTPD.StorageService.Listener; using VectorNNTPD.StorageService.Tests.Fixtures; @@ -17,8 +15,7 @@ namespace VectorNNTPD.StorageService.Tests.Listener { - - public sealed class StorageVatpListenerServiceTests + public sealed class StorageNntpListenerServiceTests { private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); @@ -27,16 +24,15 @@ public async Task Listener_refuses_start_without_acme_readiness() { var runtime = CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()); var readiness = new AcmeCertificateReadiness(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, - NullLogger.Instance); + NullLogger.Instance); var ex = await Assert.ThrowsAsync(() => service.StartAsync(CancellationToken.None)); Assert.Contains("ACME certificate is not ready", ex.Message, StringComparison.Ordinal); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); Assert.Empty(service.LocalEndPoints); } @@ -46,16 +42,15 @@ public async Task Listener_refuses_start_without_certificate() var runtime = CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()); var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreateUnavailableProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, - NullLogger.Instance); + NullLogger.Instance); var ex = await Assert.ThrowsAsync(() => service.StartAsync(CancellationToken.None)); Assert.Contains("no TLS certificate", ex.Message, StringComparison.Ordinal); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); } [Fact] @@ -65,15 +60,14 @@ public async Task Listener_starts_when_ready_and_shuts_down_cleanly() var runtime = CreateRuntime(port); var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, - NullLogger.Instance); + NullLogger.Instance); await service.StartAsync(CancellationToken.None); - Assert.Equal(StorageVatpListenerState.Running, service.State); + Assert.Equal(StorageNntpListenerState.Running, service.State); Assert.All(service.LocalEndPoints, endpoint => { var ip = Assert.IsType(endpoint); @@ -81,9 +75,9 @@ public async Task Listener_starts_when_ready_and_shuts_down_cleanly() }); await service.StopAsync(CancellationToken.None); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); await service.DisposeAsync(); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); } [Fact] @@ -98,18 +92,17 @@ public async Task Bind_failure_disposes_the_partial_socket_and_leaves_the_listen var runtime = CreateRuntime(port); var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, - NullLogger.Instance); + NullLogger.Instance); var ex = await Assert.ThrowsAnyAsync(() => service.StartAsync(CancellationToken.None)); Assert.True( ex is SocketException or InvalidOperationException, "Expected bind failure or no-endpoints failure, got " + ex.GetType().Name); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); Assert.Empty(service.LocalEndPoints); } @@ -121,12 +114,11 @@ public async Task Existing_connection_keeps_certificate_lease_after_rotation() var certificates = TestListenerCertificates.CreatePublishedProvider("cache-original.test"); var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, certificates, - NullStorageArticleOpenBoundary.Instance, readiness, - NullLogger.Instance); + NullLogger.Instance); await service.StartAsync(CancellationToken.None); @@ -165,47 +157,6 @@ await ssl2.AuthenticateAsClientAsync( await service.StopAsync(CancellationToken.None); } - [Fact] - public async Task Listener_uses_the_production_initial_window_when_no_limits_are_supplied() - { - await using var service = new StorageVatpListenerService( - CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()), - TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, - NullLogger.Instance); - Assert.Equal(1024 * 1024, service.TransferLimits.InitialStreamWindowBytes); - Assert.Equal(64L * 1024 * 1024, service.TransferLimits.MaxStreamCreditBytes); - } - - [Fact] - public async Task Listener_keeps_an_explicit_initial_window_for_its_sessions() - { - var limits = new ArticleTransferLimits { InitialStreamWindowBytes = 2 * 1024 * 1024 }; - await using var service = new StorageVatpListenerService( - CreateRuntime(StorageServerTestOptions.GetFreeTcpPort()), - TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, - NullLogger.Instance, - transferLimits: limits); - Assert.Same(limits, service.TransferLimits); - Assert.Equal(2 * 1024 * 1024, service.TransferLimits.InitialStreamWindowBytes); - Assert.Equal(ArticleTransferLimits.Default.MaxStreamCreditBytes, service.TransferLimits.MaxStreamCreditBytes); - } - - [Fact] - public void Bench_window_accepts_only_the_five_sizes() - { - Assert.Equal(1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow(null)); - Assert.Equal(256 * 1024, StorageVatpThroughputBench.ParseBenchWindow("262144")); - Assert.Equal(512 * 1024, StorageVatpThroughputBench.ParseBenchWindow("524288")); - Assert.Equal(1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("1048576")); - Assert.Equal(2 * 1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("2097152")); - Assert.Equal(4 * 1024 * 1024, StorageVatpThroughputBench.ParseBenchWindow("4194304")); - var rejected = Assert.Throws(() => StorageVatpThroughputBench.ParseBenchWindow("1048575")); - Assert.Contains("262144", rejected.Message, StringComparison.Ordinal); - Assert.Throws(() => StorageVatpThroughputBench.ParseBenchWindow("67108864")); - } - [Fact] public async Task Peer_disconnect_releases_admission_and_keeps_the_listener_accepting() { @@ -214,10 +165,9 @@ public async Task Peer_disconnect_releases_admission_and_keeps_the_listener_acce var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); var logger = new CapturingListenerLogger(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, logger); @@ -259,7 +209,7 @@ await ssl2.AuthenticateAsClientAsync( await WaitUntilAsync(() => service.ActiveConnections == 1); await service.StopAsync(CancellationToken.None); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); Assert.Equal(0, service.ActiveConnections); } @@ -271,10 +221,9 @@ public async Task Drain_logs_unexpected_faulted_connection_tasks_and_still_compl var readiness = new AcmeCertificateReadiness(); readiness.MarkReady(); var logger = new CapturingListenerLogger(); - await using var service = new StorageVatpListenerService( + await using var service = new StorageNntpListenerService( runtime, TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, readiness, logger); @@ -288,14 +237,14 @@ public async Task Drain_logs_unexpected_faulted_connection_tasks_and_still_compl await service.StopAsync(CancellationToken.None); - Assert.Equal(StorageVatpListenerState.Stopped, service.State); + Assert.Equal(StorageNntpListenerState.Stopped, service.State); Assert.Contains("unexpected task failure", logger.Text, StringComparison.Ordinal); Assert.Contains("injected-drain-fault", logger.Text, StringComparison.Ordinal); } - private static ConcurrentDictionary GetConnectionMap(StorageVatpListenerService service) + private static ConcurrentDictionary GetConnectionMap(StorageNntpListenerService service) { - var field = typeof(StorageVatpListenerService).GetField( + var field = typeof(StorageNntpListenerService).GetField( "_connections", BindingFlags.Instance | BindingFlags.NonPublic); Assert.NotNull(field); @@ -329,7 +278,7 @@ private static StorageServerRuntimeOptions CreateRuntime(int tlsPort) return StorageServerRuntimeOptionsFactory.Create(identity, acme); } - private sealed class CapturingListenerLogger : ILogger + private sealed class CapturingListenerLogger : ILogger { private readonly List _messages = []; @@ -357,13 +306,13 @@ public void Log( { lock (_messages) { - var message = formatter(state, exception); + var line = formatter(state, exception); if (exception is not null) { - message = $"{message} {exception}"; + line = line + " " + exception; } - _messages.Add(message); + _messages.Add(line); } } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs new file mode 100644 index 00000000..5b7842ae --- /dev/null +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpLiveBackpressureTests.cs @@ -0,0 +1,285 @@ +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Text; +using Microsoft.Extensions.Logging.Abstractions; +using VectorNNTPD.Common.Acme; +using VectorNNTPD.Common.Articles; +using VectorNNTPD.Common.Articles.Parsing; +using VectorNNTPD.StorageService; +using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.StorageService.Ingress; +using VectorNNTPD.StorageService.Listener; +using VectorNNTPD.StorageService.Tests.Fixtures; +using VectorNNTPD.StorageService.Tests.TestDoubles; + +namespace VectorNNTPD.StorageService.Tests.Listener +{ + /// + /// Live TLS proof that TAKETHIS enqueue completes before persistence finishes. + /// Full-queue backpressure is covered in-process by + /// . + /// + [Collection("StorageNntpLive")] + public sealed class StorageNntpLiveBackpressureTests + { + private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); + + [Fact] + public async Task Available_capacity_accepts_before_worker_persistence() + { + var article = Article("avail-0001"); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var processor = new GatePublisher(release.Task); + await using var host = await LiveHost.StartAsync(article.Record.ArtSize * 4L, processor, startWorker: true); + + await using var client = await RawNntpClient.ConnectAsync(host.Port); + Assert.Equal(239, await client.TakeThisAsync(article)); + await WaitUntilAsync(() => processor.Entered >= 1); + Assert.Equal(0, processor.Finished); + release.TrySetResult(); + await WaitUntilAsync(() => processor.Finished >= 1 && host.Queue.AccountedBytes == 0); + } + + private static BuiltArticle Article(string token) + { + var messageId = $"<{token}@backpressure.test>"; + var text = + "Path: peer.example\r\n" + + "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + + $"Message-ID: {messageId}\r\n" + + "Newsgroups: alt.test\r\n" + + "From: user@example.test\r\n" + + "Subject: backpressure\r\n" + + "\r\n" + + "body-line-for-backpressure\r\n"; + var logical = Encoding.ASCII.GetBytes(text); + var created = ArticleRecordFactory.TryCreate( + new NntpArticleParser("nntpd01.usenet.ninja"), + logical, + ArticlePathMode.Normalize); + Assert.True(created.IsAccepted, created.MaterializeFailure.ToString()); + return new BuiltArticle(messageId, created.Record, logical); + } + + private static async Task WaitUntilAsync(Func condition) + { + using var timeout = new CancellationTokenSource(Safety); + while (!condition()) + { + await Task.Delay(5, timeout.Token); + } + } + + private readonly record struct BuiltArticle(string MessageId, ArticleRecord Record, byte[] Logical); + + private sealed class GatePublisher : IAcceptedArticleProcessor + { + private readonly Task _release; + + public GatePublisher(Task release) => _release = release; + + public int Entered { get; private set; } + + public int Finished { get; private set; } + + public void EnsureReady() + { + } + + public async ValueTask ProcessAsync( + AcceptedArticle article, + CancellationToken cancellationToken) + { + Entered++; + await _release.WaitAsync(cancellationToken).ConfigureAwait(false); + Finished++; + return AcceptedArticleProcessResult.Completed; + } + } + + private sealed class RawNntpClient : IAsyncDisposable + { + private readonly TcpClient _tcp; + private readonly SslStream _ssl; + private readonly List _pending = []; + private readonly byte[] _readBuffer = new byte[16 * 1024]; + + private RawNntpClient(TcpClient tcp, SslStream ssl) + { + _tcp = tcp; + _ssl = ssl; + } + + public static async Task ConnectAsync(int port) + { + var tcp = new TcpClient { NoDelay = true }; + await tcp.ConnectAsync(IPAddress.Loopback, port); + var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false); + await ssl.AuthenticateAsClientAsync( + new SslClientAuthenticationOptions + { + TargetHost = "localhost", + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, + RemoteCertificateValidationCallback = static (_, _, _, _) => true, + }, + CancellationToken.None); + var client = new RawNntpClient(tcp, ssl); + Assert.Equal(200, await client.ReadStatusAsync()); + await ssl.WriteAsync("MODE STREAM\r\n"u8.ToArray()); + Assert.Equal(203, await client.ReadStatusAsync()); + return client; + } + + public async Task TakeThisAsync(BuiltArticle article) + { + await _ssl.WriteAsync(Encoding.ASCII.GetBytes("TAKETHIS " + article.MessageId + "\r\n")); + await _ssl.WriteAsync(DotStuff(article.Logical)); + await _ssl.WriteAsync(".\r\n"u8.ToArray()); + return await ReadStatusAsync(); + } + + public async ValueTask DisposeAsync() + { + try + { + await _ssl.DisposeAsync(); + } + catch (Exception ex) when (ex is IOException or ObjectDisposedException) + { + ExpectedException.Ignore(ex); + } + + _tcp.Dispose(); + } + + private async Task ReadStatusAsync() + { + using var timeout = new CancellationTokenSource(Safety); + while (true) + { + for (var i = 0; i < _pending.Count - 1; i++) + { + if (_pending[i] == (byte)'\r' && _pending[i + 1] == (byte)'\n') + { + var line = Encoding.ASCII.GetString(_pending.GetRange(0, i).ToArray()); + _pending.RemoveRange(0, i + 2); + return ((line[0] - '0') * 100) + ((line[1] - '0') * 10) + (line[2] - '0'); + } + } + + var read = await _ssl.ReadAsync(_readBuffer, timeout.Token); + if (read == 0) + { + throw new EndOfStreamException(); + } + + for (var i = 0; i < read; i++) + { + _pending.Add(_readBuffer[i]); + } + } + } + + private static byte[] DotStuff(ReadOnlySpan article) + { + using var ms = new MemoryStream(article.Length + 8); + var atLineStart = true; + for (var i = 0; i < article.Length; i++) + { + var b = article[i]; + if (atLineStart && b == (byte)'.') + { + ms.WriteByte((byte)'.'); + } + + ms.WriteByte(b); + atLineStart = b == (byte)'\n'; + } + + return ms.ToArray(); + } + } + + private sealed class LiveHost : IAsyncDisposable + { + private readonly StorageNntpListenerService _listener; + private readonly AcceptedArticleWorker? _worker; + + private LiveHost( + AcceptedArticleQueue queue, + StorageNntpListenerService listener, + AcceptedArticleWorker? worker, + int port) + { + Queue = queue; + _listener = listener; + _worker = worker; + Port = port; + } + + public int Port { get; } + + public AcceptedArticleQueue Queue { get; } + + public static async Task StartAsync( + long maxQueuedBytes, + IAcceptedArticleProcessor? processor = null, + bool startWorker = false) + { + var port = StorageServerTestOptions.GetFreeTcpPort(); + var identity = StorageServerTestOptions.CreateValid(); + identity.BindAddress = ["127.0.0.1"]; + identity.BindPortTls = port; + var acme = StorageServerTestOptions.CreateValidAcme(identity); + acme.BindAddress = ["127.0.0.1"]; + acme.BindPortTls = port; + var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme); + var queue = new AcceptedArticleQueue(maxQueuedBytes, TimeProvider.System); + var readiness = new AcmeCertificateReadiness(); + readiness.MarkReady(); + var listener = new StorageNntpListenerService( + runtime, + TestListenerCertificates.CreatePublishedProvider(), + readiness, + NullLogger.Instance, + acceptedArticles: queue, + assemblyAdmission: new StoreAssemblyAdmission(8)); + await listener.StartAsync(CancellationToken.None); + + AcceptedArticleWorker? worker = null; + if (startWorker) + { + ArgumentNullException.ThrowIfNull(processor); + worker = new AcceptedArticleWorker( + queue, + processor, + NullLogger.Instance, + new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 }, + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(), + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(), + TimeProvider.System); + await worker.StartAsync(CancellationToken.None); + } + + return new LiveHost(queue, listener, worker, port); + } + + public async ValueTask DisposeAsync() + { + if (_worker is not null) + { + await _worker.StopAsync(CancellationToken.None); + } + + await _listener.DisposeAsync(); + } + } + } + + [CollectionDefinition("StorageNntpLive", DisableParallelization = true)] + public sealed class StorageNntpLiveCollection + { + } +} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs new file mode 100644 index 00000000..c3129338 --- /dev/null +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpQueueIngressTests.cs @@ -0,0 +1,530 @@ +using System.Text; +using System.Threading.Channels; +using Microsoft.Extensions.Logging.Abstractions; +using VectorNNTPD.Common.Articles; +using VectorNNTPD.Common.Articles.Parsing; +using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.StorageService.Ingress; +using VectorNNTPD.StorageService.Listener; +using VectorNNTPD.StorageService; + +namespace VectorNNTPD.StorageService.Tests.Listener +{ + /// + /// In-process NNTP TAKETHIS ingress against over a pipe transport. + /// + public sealed class StorageNntpQueueIngressTests + { + private const string CacheFqdn = "cache01.usenet.ninja"; + private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); + + [Fact] + public async Task TakeThis_enqueues_the_original_article_without_this_servers_path_hop() + { + var inbound = Build("", "body-line\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(239, await harness.TakeThisAsync(inbound)); + + var delivery = await queue.DequeueAsync(CancellationToken.None); + Assert.True(delivery.HasArticle); + Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId); + Assert.Equal(inbound.Record.ArtHash, delivery.Article.ArtHash); + Assert.Equal(inbound.Record.ArtSize, delivery.Article.ArtSize); + Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); + var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span); + Assert.Equal(0, CountHop(queued, CacheFqdn)); + Assert.Contains("Path: ", queued, StringComparison.Ordinal); + Assert.Contains("body-line\r\n", queued, StringComparison.Ordinal); + Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal); + } + + [Fact] + public async Task Second_takeThis_of_the_same_bytes_enqueues_another_unmodified_copy() + { + var inbound = Build("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(239, await harness.TakeThisAsync(inbound)); + Assert.Equal(239, await harness.TakeThisAsync(inbound)); + + var first = await queue.DequeueAsync(CancellationToken.None); + var second = await queue.DequeueAsync(CancellationToken.None); + Assert.True(first.HasArticle && second.HasArticle); + Assert.True(first.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); + Assert.True(second.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); + } + + [Fact] + public async Task Status_239_is_sent_before_persistence_finishes() + { + var inbound = Build("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var processor = new BlockingProcessor(release.Task); + var worker = new AcceptedArticleWorker( + queue, + processor, + NullLogger.Instance, + new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 }, + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(), + new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(), + TimeProvider.System); + await worker.StartAsync(CancellationToken.None); + try + { + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(239, await harness.TakeThisAsync(inbound).WaitAsync(TimeSpan.FromSeconds(2))); + Assert.Equal(0, processor.Finished); + } + finally + { + release.TrySetResult(); + await worker.StopAsync(CancellationToken.None); + } + } + + [Fact] + public async Task Message_id_mismatch_is_rejected_without_enqueue() + { + var real = Build("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(439, await harness.TakeThisRawAsync("", real.Record.ArtData)); + Assert.Equal(0, queue.EnqueuedCount); + Assert.Equal(0, queue.ReservedBytes); + Assert.Equal(0, queue.AccountedBytes); + } + + [Fact] + public async Task Malformed_article_is_rejected_without_enqueue() + { + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + var junk = Encoding.ASCII.GetBytes("not-an-article\r\n"); + Assert.Equal(439, await harness.TakeThisRawAsync("", junk)); + Assert.Equal(0, queue.EnqueuedCount); + } + + [Fact] + public async Task Completed_queue_returns_400_and_closes() + { + var built = Build("", "body\r\n"); + var queue = new AcceptedArticleQueue(); + queue.Complete(); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(400, await harness.TakeThisAsync(built)); + Assert.Equal(0, queue.ReservedBytes); + var empty = await queue.DequeueAsync(CancellationToken.None); + Assert.False(empty.HasArticle); + } + + [Fact] + public async Task Two_sessions_enqueue_distinct_articles() + { + var queue = new AcceptedArticleQueue(); + var first = Build("", "alpha\r\n"); + var second = Build("", "beta\r\n"); + await using var left = await Harness.StartAsync(queue); + await using var right = await Harness.StartAsync(queue); + var leftTask = left.TakeThisAsync(first); + var rightTask = right.TakeThisAsync(second); + Assert.Equal(239, await leftTask); + Assert.Equal(239, await rightTask); + + var seen = new HashSet(StringComparer.Ordinal); + for (var i = 0; i < 2; i++) + { + var delivery = await queue.DequeueAsync(CancellationToken.None); + Assert.True(delivery.HasArticle); + seen.Add(Encoding.ASCII.GetString(delivery.Article.ArtData.Span)); + } + + Assert.Contains(seen, static text => text.Contains("alpha\r\n", StringComparison.Ordinal)); + Assert.Contains(seen, static text => text.Contains("beta\r\n", StringComparison.Ordinal)); + } + + [Fact] + public async Task Full_queue_blocks_on_reserve_until_dequeue_then_accepts() + { + var first = Build("", "alpha-body\r\n"); + var queue = new AcceptedArticleQueue(first.Record.ArtSize, TimeProvider.System); + await using var held = await Harness.StartAsync(queue); + Assert.Equal(239, await held.TakeThisAsync(first)); + + var second = Build("", "beta-body\r\n"); + await using var waiting = await Harness.StartAsync(queue); + var pending = waiting.TakeThisAsync(second); + await WaitUntilAsync(() => queue.AdmissionWaiters == 1); + Assert.Equal(0, queue.ReservedBytes); + Assert.Equal(first.Record.ArtSize, queue.AccountedBytes); + Assert.False(pending.IsCompleted); + + Assert.True((await queue.DequeueAsync(CancellationToken.None)).HasArticle); + Assert.Equal(239, await pending.WaitAsync(TimeSpan.FromSeconds(2))); + Assert.Equal(0, queue.ReservedBytes); + Assert.Equal(second.Record.ArtSize, queue.QueuedBytes); + var delivery = await queue.DequeueAsync(CancellationToken.None); + Assert.True(delivery.Article.ArtData.Span.SequenceEqual(second.Record.ArtData.Span)); + } + + [Fact] + public async Task Article_larger_than_queue_budget_is_rejected() + { + var built = Build("", "too-big-for-budget\r\n"); + var queue = new AcceptedArticleQueue(8, TimeProvider.System); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(439, await harness.TakeThisAsync(built)); + Assert.Equal(0, queue.EnqueuedCount); + Assert.Equal(0, queue.ReservedBytes); + } + + [Fact] + public async Task Disconnect_mid_transfer_does_not_enqueue() + { + var built = Build("", new string('x', 128) + "\r\n"); + var queue = new AcceptedArticleQueue(built.Record.ArtSize, TimeProvider.System); + var session = await Harness.StartAsync(queue); + await session.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS \r\n")); + await session.SendRawAsync(Encoding.ASCII.GetBytes("Path: peer.example\r\n")); + await session.DisposeAsync(); + + Assert.Equal(0, queue.EnqueuedCount); + Assert.Equal(0, queue.ReservedBytes); + Assert.Equal(0, queue.AccountedBytes); + Assert.Equal(0, queue.AdmissionWaiters); + + await using var retry = await Harness.StartAsync(queue); + Assert.Equal(239, await retry.TakeThisAsync(built)); + Assert.Equal(built.Record.ArtSize, queue.QueuedBytes); + } + + [Fact] + public async Task Dot_stuffed_article_enqueues_with_matching_art_id() + { + var inbound = Build("", "..leading\r\n.middle\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + Assert.Equal(239, await harness.TakeThisAsync(inbound)); + var delivery = await queue.DequeueAsync(CancellationToken.None); + Assert.True(delivery.HasArticle); + Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId); + Assert.Contains("..leading\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal); + Assert.Contains(".middle\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal); + } + + [Fact] + public async Task Pipelined_second_takeThis_with_large_residue_still_enqueues() + { + // After the first terminator, leftover must hold the next command plus article bytes + // even when that residue exceeds the 512-octet command-line limit. + var first = Build("", "first-body\r\n"); + var second = Build("", new string('y', 700) + "\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + + var firstId = Encoding.ASCII.GetString(first.Record.MessageId.ToArray()); + var secondId = Encoding.ASCII.GetString(second.Record.MessageId.ToArray()); + var secondCommand = Encoding.ASCII.GetBytes("TAKETHIS " + secondId + "\r\n"); + var secondBody = DotStuff(second.Logical); + Assert.True( + secondCommand.Length + secondBody.Length + 3 > 512, + "Residue after the first terminator must exceed the 512-octet command buffer."); + + // Complete the first article's body, then deliver terminator + entire second TAKETHIS in one + // chunk so the payload reader must PreserveLeftover a residue larger than 512 octets. + await harness.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS " + firstId + "\r\n")); + await harness.SendRawAsync(DotStuff(first.Logical)); + var afterFirst = new byte[3 + secondCommand.Length + secondBody.Length + 3]; + ".\r\n"u8.CopyTo(afterFirst); + secondCommand.CopyTo(afterFirst.AsSpan(3)); + secondBody.CopyTo(afterFirst.AsSpan(3 + secondCommand.Length)); + ".\r\n"u8.CopyTo(afterFirst.AsSpan(3 + secondCommand.Length + secondBody.Length)); + await harness.SendRawAsync(afterFirst); + + var firstStatus = await harness.ReadStatusAsync(); + var secondStatus = await harness.ReadStatusAsync(); + Assert.Equal((239, 239), (firstStatus, secondStatus)); + + var a = await queue.DequeueAsync(CancellationToken.None); + var b = await queue.DequeueAsync(CancellationToken.None); + Assert.True(a.HasArticle && b.HasArticle); + var ids = new HashSet { a.Article.ArtId, b.Article.ArtId }; + Assert.Contains(first.Record.ArtId, ids); + Assert.Contains(second.Record.ArtId, ids); + } + + [Fact] + public async Task Split_article_payload_writes_still_enqueue() + { + var built = Build("", "chunked-body\r\n"); + var queue = new AcceptedArticleQueue(); + await using var harness = await Harness.StartAsync(queue); + var messageId = Encoding.ASCII.GetString(built.Record.MessageId.ToArray()); + await harness.SendRawAsync(Encoding.ASCII.GetBytes("TAKETHIS " + messageId + "\r\n")); + var stuffed = DotStuff(built.Logical); + await harness.Transport.ClientWriteChunkedAsync(stuffed, 5); + await harness.SendRawAsync(".\r\n"u8.ToArray()); + Assert.Equal(239, await harness.ReadStatusAsync()); + var delivery = await queue.DequeueAsync(CancellationToken.None); + Assert.True(delivery.HasArticle); + Assert.Equal(built.Record.ArtId, delivery.Article.ArtId); + Assert.Contains("chunked-body\r\n", Encoding.ASCII.GetString(delivery.Article.ArtData.Span), StringComparison.Ordinal); + } + + private static async Task WaitUntilAsync(Func condition) + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2)); + while (!condition()) + { + await Task.Delay(5, timeout.Token); + } + } + + private static int CountHop(string article, string hop) => + article.Split(hop, StringSplitOptions.None).Length - 1; + + private static string Body(string article) + { + var split = article.IndexOf("\r\n\r\n", StringComparison.Ordinal); + Assert.True(split >= 0); + return article[(split + 4)..]; + } + + private static byte[] EncodeTakeThis(Built built) + { + var messageId = built.Record.MessageId.ToArray(); + var command = Encoding.ASCII.GetBytes("TAKETHIS "); + var crlf = "\r\n"u8.ToArray(); + var stuffed = DotStuff(built.Logical); + var terminator = ".\r\n"u8.ToArray(); + var framed = new byte[command.Length + messageId.Length + crlf.Length + stuffed.Length + terminator.Length]; + var offset = 0; + command.CopyTo(framed.AsSpan(offset)); + offset += command.Length; + messageId.CopyTo(framed.AsSpan(offset)); + offset += messageId.Length; + crlf.CopyTo(framed.AsSpan(offset)); + offset += crlf.Length; + stuffed.CopyTo(framed.AsSpan(offset)); + offset += stuffed.Length; + terminator.CopyTo(framed.AsSpan(offset)); + return framed; + } + + private static byte[] DotStuff(ReadOnlySpan article) + { + using var ms = new MemoryStream(article.Length + 16); + var atLineStart = true; + for (var i = 0; i < article.Length; i++) + { + var b = article[i]; + if (atLineStart && b == (byte)'.') + { + ms.WriteByte((byte)'.'); + } + + ms.WriteByte(b); + atLineStart = b == (byte)'\n'; + } + + return ms.ToArray(); + } + + private static Built Build(string messageId, string body, string ingressFqdn = "nntpd01.usenet.ninja") + { + var parser = new NntpArticleParser(CacheFqdn); + var path = string.Equals(ingressFqdn, CacheFqdn, StringComparison.Ordinal) + ? CacheFqdn + "!peer.example" + : "peer.example"; + var text = new StringBuilder() + .Append("Path: ").Append(path).Append("\r\n") + .Append("Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n") + .Append("Message-ID: ").Append(messageId).Append("\r\n") + .Append("Newsgroups: alt.test\r\n") + .Append("From: user@example.test\r\n") + .Append("Subject: nntp\r\n") + .Append("\r\n") + .Append(body); + var logical = Encoding.ASCII.GetBytes(text.ToString()); + var created = ArticleRecordFactory.TryCreate( + parser, + logical, + ArticlePathMode.Normalize); + Assert.True(created.IsAccepted, created.MaterializeFailure.ToString()); + return new Built(created.Record, logical); + } + + private readonly record struct Built(ArticleRecord Record, byte[] Logical); + + private sealed class BlockingProcessor : IAcceptedArticleProcessor + { + private readonly Task _release; + + public BlockingProcessor(Task release) => _release = release; + + public int Finished { get; private set; } + + public void EnsureReady() + { + } + + public async ValueTask ProcessAsync( + AcceptedArticle article, + CancellationToken cancellationToken) + { + await _release.WaitAsync(cancellationToken).ConfigureAwait(false); + Finished++; + return AcceptedArticleProcessResult.Completed; + } + } + + private sealed class Harness : IAsyncDisposable + { + private readonly CancellationTokenSource _cts = new(); + private readonly StorageNntpFeedSession _session; + private readonly Task _run; + private readonly List _pendingResponse = []; + + private Harness(PipeTransport transport, StorageNntpFeedSession session) + { + Transport = transport; + _session = session; + _run = session.RunAsync(_cts.Token); + } + + public PipeTransport Transport { get; } + + public static async Task StartAsync(IAcceptedArticleQueue queue) + { + var transport = new PipeTransport(); + var session = new StorageNntpFeedSession( + transport, + new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8), + NullLogger.Instance, + CacheFqdn, + acceptedArticles: queue, + assemblyAdmission: new StoreAssemblyAdmission(8)); + var harness = new Harness(transport, session); + var greeting = await harness.ReadStatusAsync(); + Assert.Equal(200, greeting); + await harness.SendRawAsync("MODE STREAM\r\n"u8.ToArray()); + Assert.Equal(203, await harness.ReadStatusAsync()); + return harness; + } + + public async Task TakeThisAsync(Built built) + { + var messageId = built.Record.MessageId.ToArray(); + return await TakeThisRawAsync(Encoding.ASCII.GetString(messageId), built.Logical); + } + + public async Task TakeThisRawAsync(string messageId, ReadOnlyMemory logicalArticle) + { + var command = Encoding.ASCII.GetBytes("TAKETHIS " + messageId + "\r\n"); + await SendRawAsync(command); + await SendRawAsync(DotStuff(logicalArticle.Span)); + await SendRawAsync(".\r\n"u8.ToArray()); + return await ReadStatusAsync(); + } + + public Task SendRawAsync(byte[] frame) => Transport.ClientWriteAsync(frame); + + public async Task ReadStatusAsync() + { + using var timeout = new CancellationTokenSource(Safety); + while (true) + { + for (var i = 0; i < _pendingResponse.Count - 1; i++) + { + if (_pendingResponse[i] == (byte)'\r' && _pendingResponse[i + 1] == (byte)'\n') + { + var line = Encoding.ASCII.GetString(_pendingResponse.GetRange(0, i).ToArray()); + _pendingResponse.RemoveRange(0, i + 2); + Assert.True(line.Length >= 3); + return ((line[0] - '0') * 100) + ((line[1] - '0') * 10) + (line[2] - '0'); + } + } + + var chunk = await Transport.ReadServerAsync(timeout.Token); + _pendingResponse.AddRange(chunk); + } + } + + public async ValueTask DisposeAsync() + { + await _cts.CancelAsync(); + try + { + await _run; + } + catch (OperationCanceledException ex) + { + ExpectedException.Ignore(ex); + } + catch (EndOfStreamException ex) + { + ExpectedException.Ignore(ex); + } + + _cts.Dispose(); + await Transport.DisposeAsync(); + await _session.DisposeAsync(); + } + } + + private sealed class PipeTransport : IStorageListenerTransport + { + private readonly Channel _toServer = Channel.CreateUnbounded(); + private readonly Channel _toClient = Channel.CreateUnbounded(); + private byte[] _pending = []; + private int _pendingOffset; + + public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) + { + if (_pendingOffset >= _pending.Length) + { + _pending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); + _pendingOffset = 0; + } + + var count = Math.Min(buffer.Length, _pending.Length - _pendingOffset); + _pending.AsSpan(_pendingOffset, count).CopyTo(buffer.Span); + _pendingOffset += count; + return count; + } + + public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) + { + await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false); + return buffer.Length; + } + + public Task ClientWriteAsync(byte[] frame) => _toServer.Writer.WriteAsync(frame).AsTask(); + + public async Task ClientWriteChunkedAsync(byte[] frame, int chunkSize) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(chunkSize); + var offset = 0; + while (offset < frame.Length) + { + var count = Math.Min(chunkSize, frame.Length - offset); + var slice = new byte[count]; + frame.AsSpan(offset, count).CopyTo(slice); + await _toServer.Writer.WriteAsync(slice).ConfigureAwait(false); + offset += count; + } + } + + public Task ReadServerAsync(CancellationToken cancellationToken) => + _toClient.Reader.ReadAsync(cancellationToken).AsTask(); + + public ValueTask DisposeAsync() + { + _toServer.Writer.TryComplete(); + _toClient.Writer.TryComplete(); + return ValueTask.CompletedTask; + } + } + } +} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs new file mode 100644 index 00000000..8e4e3d24 --- /dev/null +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpThroughputBench.cs @@ -0,0 +1,364 @@ +using System.Diagnostics; +using System.Text; +using System.Threading.Channels; +using Microsoft.Extensions.Logging.Abstractions; +using VectorNNTPD.Common.Acme; +using VectorNNTPD.Common.Articles; +using VectorNNTPD.Common.Articles.Parsing; +using VectorNNTPD.StorageService; +using VectorNNTPD.StorageService.Configuration; +using VectorNNTPD.StorageService.Ingress; +using VectorNNTPD.StorageService.Listener; +using VectorNNTPD.StorageService.Tests.Fixtures; +using VectorNNTPD.StorageService.Tests.Support.Placement; +using VectorNNTPD.StorageService.Tests.TestDoubles; +using Xunit.Abstractions; + +namespace VectorNNTPD.StorageService.Tests.Listener +{ + /// + /// Minimal NNTP TAKETHIS throughput harness. Sustained measurement runs only when + /// VECTORNNTPD_STORAGE_NNTP_BENCH=1. + /// + public sealed class StorageNntpThroughputBench + { + private readonly ITestOutputHelper _output; + + public StorageNntpThroughputBench(ITestOutputHelper output) => _output = output; + + public static int BodyBytes => 256; + + [Fact] + public async Task Warm_path_places_one_article() + { + await using var host = await StartPlacementIngressHostAsync(); + await using var client = new ArticlePlacementClient(NullLogger.Instance, 1) + { + TestTcpConnectHost = "127.0.0.1", + ServerCertificateValidationCallback = static (_, _, _, _) => true, + }; + var record = CreateRecord(); + var result = await client.PlaceAsync(record, host.Target, CancellationToken.None); + Assert.Equal(ArticlePlacementKind.Accepted, result.Kind); + Assert.Equal(1, host.Queue.EnqueuedCount); + } + + [Fact] + public async Task Optional_sustained_sweep() + { + if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_NNTP_BENCH"), "1", StringComparison.Ordinal)) + { + _output.WriteLine("Set VECTORNNTPD_STORAGE_NNTP_BENCH=1 to measure TAKETHIS throughput."); + return; + } + + await using var host = await StartPlacementIngressHostAsync(); + await using var client = new ArticlePlacementClient(NullLogger.Instance, 2) + { + TestTcpConnectHost = "127.0.0.1", + ServerCertificateValidationCallback = static (_, _, _, _) => true, + }; + var record = CreateRecord(); + var accepted = 0; + var clock = Stopwatch.StartNew(); + while (clock.Elapsed < TimeSpan.FromSeconds(3)) + { + var result = await client.PlaceAsync(record, host.Target, CancellationToken.None); + if (result.Kind == ArticlePlacementKind.Accepted) + { + accepted++; + _ = await host.Queue.DequeueAsync(CancellationToken.None); + } + } + + _output.WriteLine($"accepted={accepted} elapsedMs={clock.ElapsedMilliseconds}"); + Assert.True(accepted > 0); + } + + /// + /// In-memory (pipe) TAKETHIS allocation sample. Set VECTORNNTPD_STORAGE_NNTP_BENCH=1. + /// Isolates session parse/assembly/materialize/queue from TLS. + /// + [Fact] + public async Task Optional_in_memory_allocation_sample() + { + if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_NNTP_BENCH"), "1", StringComparison.Ordinal)) + { + _output.WriteLine("Set VECTORNNTPD_STORAGE_NNTP_BENCH=1 to measure in-memory TAKETHIS allocations."); + return; + } + + const int warmup = 64; + const int measured = 512; + var queue = new AcceptedArticleQueue(); + await using var harness = await InMemoryHarness.StartAsync(queue); + var wire = BuildWireArticle(); + + for (var i = 0; i < warmup; i++) + { + Assert.Equal(239, await harness.TakeThisAsync(wire)); + _ = await queue.DequeueAsync(CancellationToken.None); + } + + // Snapshot counters after warm-up without forcing GC. Deltas may include ambient + // runtime activity; compare relative runs with the same harness only. + var gen0 = GC.CollectionCount(0); + var gen1 = GC.CollectionCount(1); + var gen2 = GC.CollectionCount(2); + var allocatedBefore = GC.GetTotalAllocatedBytes(precise: true); + var clock = Stopwatch.StartNew(); + for (var i = 0; i < measured; i++) + { + Assert.Equal(239, await harness.TakeThisAsync(wire)); + _ = await queue.DequeueAsync(CancellationToken.None); + } + + clock.Stop(); + var allocated = GC.GetTotalAllocatedBytes(precise: true) - allocatedBefore; + // Includes harness status-line reads (PipeDuplex copies) as well as the session path. + // Compare before/after only with an identical harness; do not treat the absolute + // per-article figure as server-only cost or as a pristine GC baseline. + _output.WriteLine( + $"in-memory articles={measured} bodyBytes={BodyBytes} elapsedMs={clock.ElapsedMilliseconds} " + + $"allocatedBytes={allocated} allocatedPerArticle={allocated / (double)measured:0} " + + $"gen0={GC.CollectionCount(0) - gen0} gen1={GC.CollectionCount(1) - gen1} gen2={GC.CollectionCount(2) - gen2} " + + $"articlesPerSec={measured / clock.Elapsed.TotalSeconds:0} " + + $"note=includes-harness-status-reads;no-forced-gc"); + } + + private static byte[] BuildWireArticle() + { + var messageId = ""; + var text = + "Path: peer.example\r\n" + + "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + + $"Message-ID: {messageId}\r\n" + + "Newsgroups: alt.test\r\n" + + "From: user@example.test\r\n" + + "Subject: bench\r\n" + + "\r\n" + + new string('x', BodyBytes) + "\r\n"; + // Transfer form: body lines that start with '.' are stuffed; terminator appended. + var bodyStart = text.IndexOf("\r\n\r\n", StringComparison.Ordinal) + 4; + var header = Encoding.ASCII.GetBytes(text[..bodyStart]); + var body = Encoding.ASCII.GetBytes(text[bodyStart..]); + var stuffed = new byte[header.Length + body.Length + 3]; + header.CopyTo(stuffed, 0); + body.CopyTo(stuffed, header.Length); + stuffed[^3] = (byte)'.'; + stuffed[^2] = (byte)'\r'; + stuffed[^1] = (byte)'\n'; + var command = Encoding.ASCII.GetBytes($"TAKETHIS {messageId}\r\n"); + var framed = new byte[command.Length + stuffed.Length]; + command.CopyTo(framed, 0); + stuffed.CopyTo(framed, command.Length); + return framed; + } + + private sealed class InMemoryHarness : IAsyncDisposable + { + private readonly CancellationTokenSource _cts = new(); + private readonly Task _run; + private readonly PipeDuplex _transport; + + private InMemoryHarness(PipeDuplex transport, StorageNntpFeedSession session) + { + _transport = transport; + _run = session.RunAsync(_cts.Token); + } + + public static async Task StartAsync(IAcceptedArticleQueue queue) + { + var transport = new PipeDuplex(); + var session = new StorageNntpFeedSession( + transport, + new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8), + NullLogger.Instance, + "cache01.usenet.ninja", + acceptedArticles: queue, + assemblyAdmission: new StoreAssemblyAdmission(8)); + var harness = new InMemoryHarness(transport, session); + _ = await harness.ReadStatusAsync(); + return harness; + } + + public async Task TakeThisAsync(byte[] framed) + { + await _transport.ClientWriteAsync(framed); + return await ReadStatusAsync(); + } + + private async Task ReadStatusAsync() + { + var line = new List(64); + while (true) + { + var b = new byte[1]; + var read = await _transport.ClientReadAsync(b); + if (read == 0) + { + throw new EndOfStreamException(); + } + + if (b[0] == (byte)'\n' && line.Count > 0 && line[^1] == (byte)'\r') + { + line.RemoveAt(line.Count - 1); + break; + } + + line.Add(b[0]); + } + + var text = Encoding.ASCII.GetString(line.ToArray()); + return int.Parse(text.AsSpan(0, 3)); + } + + public async ValueTask DisposeAsync() + { + await _cts.CancelAsync(); + await _transport.DisposeAsync(); + try + { + await _run; + } + catch (OperationCanceledException ex) + { + // Expected when DisposeAsync cancels the session RunAsync token. + ExpectedException.Ignore(ex); + } + + _cts.Dispose(); + } + } + + private sealed class PipeDuplex : IStorageListenerTransport + { + private readonly Channel _toServer = Channel.CreateUnbounded(); + private readonly Channel _toClient = Channel.CreateUnbounded(); + private byte[] _serverPending = []; + private int _serverPendingOffset; + private byte[] _clientPending = []; + private int _clientPendingOffset; + + public Task ClientWriteAsync(byte[] bytes) => + _toServer.Writer.WriteAsync(bytes).AsTask(); + + public async Task ClientReadAsync(byte[] buffer) + { + if (_clientPendingOffset >= _clientPending.Length) + { + _clientPending = await _toClient.Reader.ReadAsync().ConfigureAwait(false); + _clientPendingOffset = 0; + } + + var count = Math.Min(buffer.Length, _clientPending.Length - _clientPendingOffset); + _clientPending.AsSpan(_clientPendingOffset, count).CopyTo(buffer); + _clientPendingOffset += count; + return count; + } + + public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) + { + if (_serverPendingOffset >= _serverPending.Length) + { + _serverPending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); + _serverPendingOffset = 0; + } + + var count = Math.Min(buffer.Length, _serverPending.Length - _serverPendingOffset); + _serverPending.AsSpan(_serverPendingOffset, count).CopyTo(buffer.Span); + _serverPendingOffset += count; + return count; + } + + public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) + { + await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false); + return buffer.Length; + } + + public ValueTask DisposeAsync() + { + _toServer.Writer.TryComplete(); + _toClient.Writer.TryComplete(); + return ValueTask.CompletedTask; + } + } + + public static async Task StartPlacementIngressHostAsync() + { + var port = StorageServerTestOptions.GetFreeTcpPort(); + var identity = StorageServerTestOptions.CreateValid(); + identity.BindAddress = ["127.0.0.1"]; + identity.BindPortTls = port; + var acme = StorageServerTestOptions.CreateValidAcme(identity); + acme.BindAddress = ["127.0.0.1"]; + acme.BindPortTls = port; + var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme); + var queue = new AcceptedArticleQueue(); + var readiness = new AcmeCertificateReadiness(); + readiness.MarkReady(); + var listener = new StorageNntpListenerService( + runtime, + TestListenerCertificates.CreatePublishedProvider(), + readiness, + NullLogger.Instance, + acceptedArticles: queue, + assemblyAdmission: new StoreAssemblyAdmission(64)); + await listener.StartAsync(CancellationToken.None); + var target = new StorageServerFleetEntry( + 1, + "cache01.usenet.ninja", + 10_000, + 0, + 10_000, + DateTimeOffset.UtcNow, + port); + return new PlacementIngressHost(port, queue, listener, target); + } + + private static ArticleRecord CreateRecord() + { + var text = + "Path: peer.example\r\n" + + "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + + "Message-ID: \r\n" + + "Newsgroups: alt.test\r\n" + + "From: user@example.test\r\n" + + "Subject: bench\r\n" + + "\r\n" + + new string('x', BodyBytes) + "\r\n"; + var created = ArticleRecordFactory.TryCreate( + new NntpArticleParser("nntpd01.usenet.ninja"), + Encoding.ASCII.GetBytes(text), + ArticlePathMode.Normalize); + Assert.True(created.IsAccepted); + return created.Record; + } + + public sealed class PlacementIngressHost : IAsyncDisposable + { + private readonly StorageNntpListenerService _listener; + + internal PlacementIngressHost( + int port, + AcceptedArticleQueue queue, + StorageNntpListenerService listener, + StorageServerFleetEntry target) + { + Port = port; + Queue = queue; + _listener = listener; + Target = target; + } + + public int Port { get; } + + public AcceptedArticleQueue Queue { get; } + + public StorageServerFleetEntry Target { get; } + + public async ValueTask DisposeAsync() => await _listener.DisposeAsync(); + } + } +} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs similarity index 77% rename from tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs rename to tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs index fcc8d948..f06b516a 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpTransportTimeoutTests.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageNntpTransportTimeoutTests.cs @@ -2,13 +2,22 @@ namespace VectorNNTPD.StorageService.Tests.Listener { + /// + /// Process-wide I/O and TLS sample counters on must not + /// race with listener or placement tests that construct the same type. + /// + [CollectionDefinition("StorageNntpTransportCounters", DisableParallelization = true)] + public sealed class StorageNntpTransportCountersCollection + { + } - public sealed class StorageVatpTransportTimeoutTests + [Collection("StorageNntpTransportCounters")] + public sealed class StorageNntpTransportTimeoutTests { [Fact] public async Task Successive_reads_complete() { - await using var transport = new StreamStorageVatpTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2)); + await using var transport = new StreamStorageListenerTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2)); Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None)); Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None)); } @@ -16,7 +25,7 @@ public async Task Successive_reads_complete() [Fact] public async Task Second_stalled_read_still_times_out_after_a_successful_read() { - await using var transport = new StreamStorageVatpTransport(new ScriptedStream(1), TimeSpan.FromMilliseconds(80)); + await using var transport = new StreamStorageListenerTransport(new ScriptedStream(1), TimeSpan.FromMilliseconds(80)); Assert.Equal(1, await transport.ReadAsync(new byte[4], CancellationToken.None)); await Assert.ThrowsAsync(async () => await transport.ReadAsync(new byte[4], CancellationToken.None)); @@ -26,7 +35,7 @@ await Assert.ThrowsAsync(async () => public async Task Write_during_a_blocked_read_does_not_cancel_the_read() { var stream = new HoldThenReleaseStream(); - await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromSeconds(2)); + await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromSeconds(2)); var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask(); await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2)); await transport.WriteAsync(new byte[] { 9, 8, 7 }, CancellationToken.None); @@ -40,7 +49,7 @@ public async Task Write_during_a_blocked_read_does_not_cancel_the_read() public async Task Suspended_read_does_not_time_out_until_resumed() { var stream = new HoldThenReleaseStream(); - await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromMilliseconds(80)); + await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromMilliseconds(80)); var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask(); await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2)); transport.SetReadProgressSuspended(true); @@ -56,7 +65,7 @@ public async Task Suspended_read_does_not_time_out_until_resumed() public async Task Stalled_write_times_out_without_cancelling_a_suspended_read() { var stream = new HoldThenReleaseStream { BlockWrites = true }; - await using var transport = new StreamStorageVatpTransport(stream, TimeSpan.FromMilliseconds(80)); + await using var transport = new StreamStorageListenerTransport(stream, TimeSpan.FromMilliseconds(80)); var read = transport.ReadAsync(new byte[4], CancellationToken.None).AsTask(); await stream.ReadEntered.Task.WaitAsync(TimeSpan.FromSeconds(2)); transport.SetReadProgressSuspended(true); @@ -70,7 +79,7 @@ await Assert.ThrowsAsync(async () => [Fact] public async Task Caller_cancellation_is_not_reported_as_an_idle_timeout() { - await using var transport = new StreamStorageVatpTransport(new ScriptedStream(0), TimeSpan.FromSeconds(30)); + await using var transport = new StreamStorageListenerTransport(new ScriptedStream(0), TimeSpan.FromSeconds(30)); using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(80)); await Assert.ThrowsAnyAsync(async () => await transport.ReadAsync(new byte[4], cts.Token)); @@ -79,32 +88,31 @@ await Assert.ThrowsAnyAsync(async () => [Fact] public async Task Independent_transports_keep_separate_timeout_state_and_share_process_io_counters() { - // Io-split counters are intentionally process-wide; timeout CTS ownership stays per instance. - StreamStorageVatpTransport.ResetIoSplit(); - await using var left = new StreamStorageVatpTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2)); + StreamStorageListenerTransport.ResetIoSplit(); + await using var left = new StreamStorageListenerTransport(new ScriptedStream(2), TimeSpan.FromSeconds(2)); var rightStream = new MemoryStream(); rightStream.Write([1, 2, 3]); rightStream.Position = 0; - await using var right = new StreamStorageVatpTransport(rightStream, TimeSpan.FromSeconds(2)); + await using var right = new StreamStorageListenerTransport(rightStream, TimeSpan.FromSeconds(2)); Assert.Equal(1, await left.ReadAsync(new byte[4], CancellationToken.None)); Assert.Equal(3, await right.ReadAsync(new byte[4], CancellationToken.None)); await right.WriteAsync(new byte[] { 9 }, CancellationToken.None); - var (timeoutSeconds, streamReadSeconds) = StreamStorageVatpTransport.ReadIoSplit(); + var (timeoutSeconds, streamReadSeconds) = StreamStorageListenerTransport.ReadIoSplit(); Assert.True(timeoutSeconds >= 0); Assert.True(streamReadSeconds >= 0); Assert.True(timeoutSeconds + streamReadSeconds > 0); - var tls = StreamStorageVatpTransport.ReadTlsThreadCost(); + var tls = StreamStorageListenerTransport.ReadTlsThreadCost(); Assert.True(tls.ReadSameThread + tls.ReadHops >= 2); Assert.True(tls.WriteSameThread + tls.WriteHops >= 1); await left.DisposeAsync(); Assert.Equal(0, await right.ReadAsync(new byte[4], CancellationToken.None)); - StreamStorageVatpTransport.ResetIoSplit(); - var afterReset = StreamStorageVatpTransport.ReadIoSplit(); + StreamStorageListenerTransport.ResetIoSplit(); + var afterReset = StreamStorageListenerTransport.ReadIoSplit(); Assert.Equal(0, afterReset.TimeoutSetupSeconds); Assert.Equal(0, afterReset.StreamReadSeconds); } @@ -112,10 +120,9 @@ public async Task Independent_transports_keep_separate_timeout_state_and_share_p [Fact] public async Task Concurrent_reads_on_separate_transports_do_not_corrupt_instance_lifecycle() { - // One outstanding read per transport (contract); both transports may run in parallel. - StreamStorageVatpTransport.ResetIoSplit(); - await using var left = new StreamStorageVatpTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2)); - await using var right = new StreamStorageVatpTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2)); + StreamStorageListenerTransport.ResetIoSplit(); + await using var left = new StreamStorageListenerTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2)); + await using var right = new StreamStorageListenerTransport(new ScriptedStream(8), TimeSpan.FromSeconds(2)); for (var i = 0; i < 8; i++) { @@ -126,7 +133,9 @@ public async Task Concurrent_reads_on_separate_transports_do_not_corrupt_instanc Assert.Equal(1, pair[1]); } - var tls = StreamStorageVatpTransport.ReadTlsThreadCost(); + var tls = StreamStorageListenerTransport.ReadTlsThreadCost(); + // Exact sample count: this collection disables parallelization so other tests cannot + // increment the process-wide counters between ResetIoSplit and this assert. Assert.Equal(16, tls.ReadSameThread + tls.ReadHops); } @@ -217,16 +226,14 @@ public override void Flush() public override void SetLength(long value) => throw new NotSupportedException(); - public override void Write(byte[] buffer, int offset, int count) => Written += count; - - public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) + public override void Write(byte[] buffer, int offset, int count) { if (BlockWrites) { - await Task.Delay(Timeout.Infinite, cancellationToken); + throw new NotSupportedException(); } - Written += buffer.Length; + Written += count; } public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) @@ -236,6 +243,17 @@ public override async ValueTask ReadAsync(Memory buffer, Cancellation buffer.Span[0] = (byte)_releaseByte; return 1; } + + public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) + { + if (BlockWrites) + { + await Task.Delay(Timeout.Infinite, cancellationToken); + return; + } + + Written += buffer.Length; + } } } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs deleted file mode 100644 index 3c70bb5a..00000000 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpLiveBackpressureTests.cs +++ /dev/null @@ -1,1039 +0,0 @@ -using System.Buffers; -using System.Net; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Text; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.Common.Acme; -using VectorNNTPD.Common.Networking.Certificates; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Tests.Support.Placement; -using VectorNNTPD.StorageService.Configuration; -using VectorNNTPD.StorageService.Ingress; -using VectorNNTPD.StorageService.Listener; -using VectorNNTPD.StorageService.Tests.Fixtures; -using VectorNNTPD.StorageService.Tests.TestDoubles; -using VectorNNTPD.StorageService; - -namespace VectorNNTPD.StorageService.Tests.Listener -{ - - /// - /// Live TLS proof that STORE withholds body credit until the isolated queue can reserve ArtSize. - /// The host binds 127.0.0.1 on an ephemeral port and does not publish DNS or touch a spool. - /// - public sealed class StorageVatpLiveBackpressureTests - { - private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); - - [Fact] - public async Task Available_capacity_orders_reservation_window_data_result_before_publication() - { - var article = Article("avail-0001"); - var budget = article.ArtSize * 4L; - var publisher = new GatePublisher(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: true); - await using var trace = await TracingClient.ConnectAsync(host.Port); - - await trace.SendStoreAndMetaAsync(article); - var outcome = await trace.SendBodyAfterWindowAsync(article); - Assert.Equal(VatpStoreOutcome.Accepted, outcome); - Assert.Equal(0, trace.DataFramesBeforeWindow); - Assert.Equal(1, trace.WindowsBeforeData); - Assert.Equal((uint)ArticleTransferLimits.Default.InitialStreamWindowBytes, trace.FirstWindowCredit); - Assert.True(trace.DataFrames >= 1); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.True(host.Queue.AccountedBytes <= budget); - - await WaitUntilAsync(() => publisher.Entered >= 1); - Assert.Equal(0, publisher.Finished); - publisher.Release(); - await WaitUntilAsync(() => publisher.Finished >= 1); - await WaitUntilAsync(() => host.Queue.AccountedBytes == 0 && host.Queue.AdmissionWaiters == 0); - - await using var client = PlacementClient(); - var beforeData = client.Snapshot().DataFramesWritten; - var placed = await client.PlaceAsync(article, host.Target, CancellationToken.None); - Assert.Equal(ArticlePlacementKind.Accepted, placed.Kind); - Assert.True(client.Snapshot().DataFramesWritten > beforeData); - await WaitUntilAsync(() => publisher.Finished >= 2 && host.Queue.AccountedBytes == 0); - - WriteEvidence( - "available", - budget, - article.ArtSize, - host, - deferred: host.Logger.Count("waiting for queue capacity"), - granted: 2, - dataBeforeAuthorization: trace.DataFramesBeforeWindow, - results: "Accepted,Accepted", - published: publisher.Finished, - cleanup: "accounted=0", - trace.TraceText()); - } - - [Fact] - public async Task Full_queue_withholds_body_until_capacity_returns_and_then_publishes() - { - var filler = Article("full-filler"); - var waiter = Article("full-waiter"); - Assert.Equal(filler.ArtSize, waiter.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false); - - await using var fillerClient = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await fillerClient.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - Assert.Equal(budget, host.Queue.QueuedBytes); - Assert.Equal(0, host.Queue.ReservedBytes); - - await using var blocked = await TracingClient.ConnectAsync(host.Port); - await blocked.SendStoreAndMetaAsync(waiter); - await WaitUntilAsync( - () => host.Queue.AdmissionWaiters == 1 - && host.Logger.Text.Contains("waiting for queue capacity", StringComparison.Ordinal)); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.AccountedBytes); - Assert.Equal(0, blocked.DataFrames); - Assert.Equal(0, blocked.WindowsBeforeData); - Assert.Equal(1, host.Queue.EnqueuedCount); - Assert.Contains("waiting for queue capacity", host.Logger.Text, StringComparison.Ordinal); - - var delivery = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - await blocked.WindowGranted.Task.WaitAsync(Safety); - Assert.Equal(1, blocked.WindowsBeforeData); - Assert.Equal(0, blocked.DataFramesBeforeWindow); - Assert.Equal(0, blocked.DataFrames); - Assert.Equal(waiter.ArtSize, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.QueuedBytes); - Assert.True(host.Queue.AccountedBytes <= budget); - - Assert.Equal(0, publisher.Published); - var outcome = await blocked.SendBodyAfterWindowAsync(waiter); - Assert.Equal(VatpStoreOutcome.Accepted, outcome); - Assert.Equal(0, blocked.DataFramesBeforeWindow); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes); - Assert.Equal(0, publisher.Published); - await publisher.PublishAsync(delivery.Article); - Assert.Equal(1, publisher.Published); - - var queued = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(queued.HasArticle); - Assert.True(queued.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span)); - await publisher.PublishAsync(queued.Article); - await WaitUntilAsync(() => - host.Queue.AccountedBytes == 0 - && host.Queue.ReservedBytes == 0 - && host.Queue.AdmissionWaiters == 0); - - await using var production = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await production.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - var dataBefore = production.Snapshot().DataFramesWritten; - var creditBefore = production.Snapshot().CreditWaitTicks; - var pending = production.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted); - Assert.Equal(dataBefore, production.Snapshot().DataFramesWritten); - Assert.Equal(creditBefore, production.Snapshot().CreditWaitTicks); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.AccountedBytes); - - var held = await host.Queue.DequeueAsync(CancellationToken.None); - var accepted = await pending.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind); - Assert.True(production.Snapshot().CreditWaitTicks > creditBefore); - Assert.True(production.Snapshot().DataFramesWritten > dataBefore); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes); - await publisher.PublishAsync(held.Article); - var second = await host.Queue.DequeueAsync(CancellationToken.None); - await publisher.PublishAsync(second.Article); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - - WriteEvidence( - "full-and-recovery", - budget, - waiter.ArtSize, - host, - deferred: host.Logger.Count("waiting for queue capacity"), - granted: 4, - dataBeforeAuthorization: blocked.DataFramesBeforeWindow, - results: "Accepted", - published: publisher.Published, - cleanup: "reserved=0 waiters=0", - blocked.TraceText()); - } - - [Fact] - public async Task Cancel_and_disconnect_release_waiters_reservations_and_partial_bodies() - { - var filler = Article("drop-filler"); - var cancelled = Article("drop-cancel"); - var disconnected = Article("drop-closeX"); - var reserved = Article("drop-window"); - var partial = Article("drop-partia"); - var follow = Article("drop-follow"); - Assert.Equal(filler.ArtSize, cancelled.ArtSize); - Assert.Equal(filler.ArtSize, disconnected.ArtSize); - Assert.Equal(filler.ArtSize, reserved.ArtSize); - Assert.Equal(filler.ArtSize, partial.ArtSize); - Assert.Equal(filler.ArtSize, follow.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false); - await using var owner = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - - await using var cancelClient = PlacementClient(); - using var cancel = new CancellationTokenSource(); - var dataBefore = cancelClient.Snapshot().DataFramesWritten; - var cancelTask = cancelClient.PlaceAsync(cancelled, host.Target, cancel.Token).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1); - Assert.Equal(dataBefore, cancelClient.Snapshot().DataFramesWritten); - Assert.Equal(0, host.Queue.ReservedBytes); - await cancel.CancelAsync(); - var cancelResult = await cancelTask.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Cancelled, cancelResult.Kind); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.QueuedBytes); - Assert.Equal(dataBefore, cancelClient.Snapshot().DataFramesWritten); - - await using var dropClient = PlacementClient(); - var dropTask = dropClient.PlaceAsync(disconnected, host.Target, CancellationToken.None).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1); - Assert.Equal(0, dropClient.Snapshot().DataFramesWritten); - await dropClient.DisposeAsync(); - var dropResult = await dropTask.WaitAsync(Safety); - Assert.True( - dropResult.Kind is ArticlePlacementKind.Cancelled or ArticlePlacementKind.TransportFailure, - dropResult.Kind.ToString()); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0 && host.Queue.ReservedBytes == 0); - Assert.Equal(budget, host.Queue.QueuedBytes); - await WaitUntilAsync(() => host.Admission.InFlight == 0); - - await using var windowClient = await TracingClient.ConnectAsync(host.Port); - await windowClient.SendStoreAndMetaAsync(reserved); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1); - var freed = await host.Queue.DequeueAsync(CancellationToken.None); - await publisher.PublishAsync(freed.Article); - await windowClient.WindowGranted.Task.WaitAsync(Safety); - Assert.Equal(0, windowClient.DataFrames); - Assert.Equal(reserved.ArtSize, host.Queue.ReservedBytes); - await windowClient.DisposeAsync(); - await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0); - Assert.Equal(0, host.Queue.QueuedBytes); - await WaitUntilAsync(() => host.Admission.InFlight == 0); - - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - await using var partialClient = await TracingClient.ConnectAsync(host.Port); - await partialClient.SendStoreAndMetaAsync(partial); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1); - var again = await host.Queue.DequeueAsync(CancellationToken.None); - await publisher.PublishAsync(again.Article); - await partialClient.WindowGranted.Task.WaitAsync(Safety); - await partialClient.SendPartialDataAsync(partial); - Assert.Equal(1, partialClient.DataFrames); - Assert.Equal(0, partialClient.DataFramesBeforeWindow); - Assert.Equal(partial.ArtSize, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.QueuedBytes); - var enqueuedBeforeDrop = host.Queue.EnqueuedCount; - await partialClient.DisposeAsync(); - await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0); - Assert.Equal(enqueuedBeforeDrop, host.Queue.EnqueuedCount); - Assert.Equal(0, host.Queue.AccountedBytes); - - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(follow, host.Target, CancellationToken.None)).Kind); - var delivered = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(delivered.Article.ArtData.Span.SequenceEqual(follow.ArtData.Span)); - await publisher.PublishAsync(delivered.Article); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - - WriteEvidence( - "cancel-disconnect", - budget, - filler.ArtSize, - host, - deferred: host.Logger.Count("waiting for queue capacity"), - granted: 3, - dataBeforeAuthorization: windowClient.DataFramesBeforeWindow + partialClient.DataFramesBeforeWindow, - results: cancelResult.Kind + "," + dropResult.Kind + ",Accepted", - published: publisher.Published, - cleanup: "reserved=0 waiters=0 accounted=0", - windowClient.TraceText()); - } - - [Fact] - public async Task Repeated_admission_does_not_leak_reservations_or_send_early_data() - { - const int cycles = 8; - var sample = Article("loop-000-a"); - var budget = sample.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false); - await using var client = PlacementClient(); - var earlyData = 0; - var deferred = 0; - var granted = 0; - - for (var i = 0; i < cycles; i++) - { - var filler = Article(FormattableString.Invariant($"loop-{i:000}-a")); - var waiter = Article(FormattableString.Invariant($"loop-{i:000}-b")); - Assert.Equal(budget, filler.ArtSize); - Assert.Equal(budget, waiter.ArtSize); - - Assert.Equal(ArticlePlacementKind.Accepted, (await client.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - granted++; - var dataBefore = client.Snapshot().DataFramesWritten; - var creditBefore = client.Snapshot().CreditWaitTicks; - var pending = client.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted); - deferred++; - if (client.Snapshot().DataFramesWritten != dataBefore || pending.IsCompleted) - { - earlyData++; - } - - Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten); - Assert.Equal(creditBefore, client.Snapshot().CreditWaitTicks); - Assert.False(pending.IsCompleted); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.QueuedBytes); - Assert.Equal(budget, host.Queue.AccountedBytes); - - var held = await host.Queue.DequeueAsync(CancellationToken.None); - var accepted = await pending.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind); - granted++; - Assert.True(client.Snapshot().CreditWaitTicks > creditBefore); - Assert.True(client.Snapshot().DataFramesWritten > dataBefore); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes); - await publisher.PublishAsync(held.Article); - var queued = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(queued.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span)); - await publisher.PublishAsync(queued.Article); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.QueuedBytes); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - } - - Assert.Equal(0, earlyData); - Assert.Equal(cycles, deferred); - Assert.Equal(cycles * 2, granted); - Assert.Equal(cycles * 2, publisher.Published); - - WriteEvidence( - "repeated", - budget, - sample.ArtSize, - host, - deferred, - granted, - earlyData, - results: "Accepted", - published: publisher.Published, - cleanup: "reserved=0 queued=0 waiters=0", - trace: host.Logger.Text); - } - - [Fact] - public async Task Capacity_wait_outlives_io_progress_timeout_then_admits_and_publishes() - { - var timeout = TimeSpan.FromMilliseconds(200); - var filler = Article("wait-filler"); - var waiter = Article("wait-waiter"); - Assert.Equal(filler.ArtSize, waiter.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout); - await using var owner = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - - await using var client = PlacementClient(timeout); - var dataBefore = client.Snapshot().DataFramesWritten; - var creditBefore = client.Snapshot().CreditWaitTicks; - var pending = client.PlaceAsync(waiter, host.Target, CancellationToken.None).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted); - - // The delay is the subject: admission must outlive both no-progress timeouts. - await Task.Delay(TimeSpan.FromMilliseconds(600)); - Assert.False(pending.IsCompleted); - Assert.Equal(1, host.Queue.AdmissionWaiters); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.AccountedBytes); - Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten); - Assert.Equal(creditBefore, client.Snapshot().CreditWaitTicks); - - var held = await host.Queue.DequeueAsync(CancellationToken.None); - var accepted = await pending.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Accepted, accepted.Kind); - Assert.True(client.Snapshot().DataFramesWritten > dataBefore); - Assert.True(client.Snapshot().CreditWaitTicks > creditBefore); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - Assert.Equal(waiter.ArtSize, host.Queue.QueuedBytes); - await publisher.PublishAsync(held.Article); - var second = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(second.HasArticle); - Assert.True(second.Article.ArtData.Span.SequenceEqual(waiter.ArtData.Span)); - await publisher.PublishAsync(second.Article); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - Assert.Equal(2, publisher.Published); - } - - [Fact] - public async Task Cancel_during_a_long_capacity_wait_releases_the_waiter() - { - var timeout = TimeSpan.FromMilliseconds(200); - var filler = Article("cncl-filler"); - var waiting = Article("cncl-waiter"); - Assert.Equal(filler.ArtSize, waiting.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout); - await using var owner = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - - await using var client = PlacementClient(timeout); - using var cancel = new CancellationTokenSource(); - var dataBefore = client.Snapshot().DataFramesWritten; - var pending = client.PlaceAsync(waiting, host.Target, cancel.Token).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted); - - // The delay is the subject: cancellation must still be observed after the no-progress timeout. - await Task.Delay(TimeSpan.FromMilliseconds(600)); - Assert.False(pending.IsCompleted); - Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten); - await cancel.CancelAsync(); - var result = await pending.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Cancelled, result.Kind); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(budget, host.Queue.QueuedBytes); - Assert.Equal(budget, host.Queue.AccountedBytes); - Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten); - } - - [Fact] - public async Task Disconnect_during_a_long_capacity_wait_lets_a_later_store_proceed() - { - var timeout = TimeSpan.FromMilliseconds(200); - var filler = Article("disc-filler"); - var waiting = Article("disc-waiter"); - var follow = Article("disc-follow"); - Assert.Equal(filler.ArtSize, waiting.ArtSize); - Assert.Equal(filler.ArtSize, follow.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout); - await using var owner = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - - await using var client = PlacementClient(timeout); - var dataBefore = client.Snapshot().DataFramesWritten; - var pending = client.PlaceAsync(waiting, host.Target, CancellationToken.None).AsTask(); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1 && !pending.IsCompleted); - - // The delay is the subject: disconnect cleanup must still run after the no-progress timeout. - await Task.Delay(TimeSpan.FromMilliseconds(600)); - Assert.False(pending.IsCompleted); - Assert.Equal(dataBefore, client.Snapshot().DataFramesWritten); - await client.DisposeAsync(); - var result = await pending.WaitAsync(Safety); - Assert.Equal(ArticlePlacementKind.Cancelled, result.Kind); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 0 && host.Queue.ReservedBytes == 0); - Assert.Equal(budget, host.Queue.QueuedBytes); - - var held = await host.Queue.DequeueAsync(CancellationToken.None); - await publisher.PublishAsync(held.Article); - await using var next = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await next.PlaceAsync(follow, host.Target, CancellationToken.None)).Kind); - var queued = await host.Queue.DequeueAsync(CancellationToken.None); - Assert.True(queued.HasArticle); - Assert.True(queued.Article.ArtData.Span.SequenceEqual(follow.ArtData.Span)); - await publisher.PublishAsync(queued.Article); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - Assert.Equal(2, publisher.Published); - } - - [Fact] - public async Task Silent_connection_after_hello_still_times_out() - { - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(1024 * 1024, publisher, startWorker: false, ioProgressTimeout: TimeSpan.FromMilliseconds(200)); - await using var trace = await TracingClient.ConnectAsync(host.Port); - await trace.Closed.WaitAsync(TimeSpan.FromSeconds(2)); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, host.Queue.ReservedBytes); - Assert.Equal(0, host.Queue.AdmissionWaiters); - Assert.Equal(0, publisher.Published); - } - - [Fact] - public async Task Silence_after_window_still_times_out_and_releases_the_reservation() - { - var timeout = TimeSpan.FromMilliseconds(200); - var filler = Article("stall-fillr"); - var waiter = Article("stall-waite"); - Assert.Equal(filler.ArtSize, waiter.ArtSize); - var budget = filler.ArtSize; - var publisher = new PublishCounter(); - await using var host = await LiveHost.StartAsync(budget, publisher, startWorker: false, ioProgressTimeout: timeout); - await using var owner = PlacementClient(); - Assert.Equal(ArticlePlacementKind.Accepted, (await owner.PlaceAsync(filler, host.Target, CancellationToken.None)).Kind); - - await using var trace = await TracingClient.ConnectAsync(host.Port); - await trace.SendStoreAndMetaAsync(waiter); - await WaitUntilAsync(() => host.Queue.AdmissionWaiters == 1); - var held = await host.Queue.DequeueAsync(CancellationToken.None); - await trace.WindowGranted.Task.WaitAsync(Safety); - Assert.Equal(waiter.ArtSize, host.Queue.ReservedBytes); - Assert.Equal(0, trace.DataFrames); - await trace.Closed.WaitAsync(TimeSpan.FromSeconds(2)); - await WaitUntilAsync(() => host.Queue.ReservedBytes == 0 && host.Queue.AdmissionWaiters == 0); - Assert.Equal(0, host.Queue.QueuedBytes); - Assert.Equal(0, host.Queue.AccountedBytes); - Assert.Equal(0, trace.DataFrames); - await publisher.PublishAsync(held.Article); - Assert.Equal(1, publisher.Published); - } - - private static void WriteEvidence( - string scenario, - long budget, - int artSize, - LiveHost host, - int deferred, - int granted, - int dataBeforeAuthorization, - string results, - int published, - string cleanup, - string trace) - { - var line = - $"EVIDENCE scenario={scenario} budget={budget} artSize={artSize} queued={host.Queue.QueuedBytes} reserved={host.Queue.ReservedBytes} accounted={host.Queue.AccountedBytes} deferred={deferred} granted={granted} dataBeforeAuthorization={dataBeforeAuthorization} results={results} published={published} cleanup={cleanup}"; - Console.WriteLine(line); - Console.WriteLine(trace); - } - - private static ArticlePlacementClient PlacementClient(TimeSpan? ioTimeout = null) => - new( - NullLogger.Instance, - new VatpClientOptions { IoTimeout = ioTimeout ?? TimeSpan.FromSeconds(30) }, - ArticleTransferLimits.Default, - maxOutstandingStores: 1) - { - TestTcpConnectHost = "127.0.0.1", - ServerCertificateValidationCallback = static (_, _, _, _) => true, - }; - - private static async Task WaitUntilAsync(Func condition) - { - using var timeout = new CancellationTokenSource(Safety); - while (!condition()) - { - await Task.Delay(1, timeout.Token); - } - } - - private static ArticleRecord Article(string token) - { - var parser = new NntpArticleParser("nntpd01.usenet.ninja"); - var text = new StringBuilder() - .Append("Path: peer.example\r\n") - .Append("Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n") - .Append("Message-ID: <").Append(token).Append("@seg.test>\r\n") - .Append("Newsgroups: alt.test\r\n") - .Append("From: user@example.test\r\n") - .Append("Subject: vatp\r\n") - .Append("\r\n") - .Append("live-body\r\n"); - var created = ArticleRecordFactory.TryCreate(parser, Encoding.ASCII.GetBytes(text.ToString())); - Assert.True(created.IsAccepted, created.MaterializeFailure.ToString()); - return created.Record; - } - - private sealed class LiveHost : IAsyncDisposable - { - private readonly StorageVatpListenerService _listener; - private readonly AcceptedArticleWorker? _worker; - private readonly TlsCertificateContextProvider _certificates; - private readonly GatePublisher? _gate; - - private LiveHost( - StorageVatpListenerService listener, - AcceptedArticleWorker? worker, - TlsCertificateContextProvider certificates, - GatePublisher? gate, - AcceptedArticleQueue queue, - StoreAssemblyAdmission admission, - ListLogger logger, - StorageServerFleetEntry target, - int port) - { - _listener = listener; - _worker = worker; - _certificates = certificates; - _gate = gate; - Queue = queue; - Admission = admission; - Logger = logger; - Target = target; - Port = port; - } - - public AcceptedArticleQueue Queue { get; } - - public StoreAssemblyAdmission Admission { get; } - - public ListLogger Logger { get; } - - public StorageServerFleetEntry Target { get; } - - public int Port { get; } - - public static async Task StartAsync( - long budget, - IAcceptedArticleProcessor processor, - bool startWorker, - TimeSpan? ioProgressTimeout = null) - { - var port = StorageServerTestOptions.GetFreeTcpPort(); - var identity = StorageServerTestOptions.CreateValid(); - identity.BindAddress = ["127.0.0.1"]; - identity.BindPortTls = port; - identity.Listener.IoProgressTimeoutSeconds = 30; - var acme = StorageServerTestOptions.CreateValidAcme(identity); - acme.BindAddress = ["127.0.0.1"]; - acme.BindPortTls = port; - var runtime = StorageServerRuntimeOptionsFactory.Create(identity, acme); - if (ioProgressTimeout is { } progress) - { - runtime = runtime with { Listener = runtime.Listener with { IoProgressTimeout = progress } }; - } - var readiness = new AcmeCertificateReadiness(); - readiness.MarkReady(); - var certificates = TestListenerCertificates.CreatePublishedProvider(); - var logger = new ListLogger(); - var queue = new AcceptedArticleQueue(budget, TimeProvider.System); - GatePublisher? gate = processor as GatePublisher; - var admission = new StoreAssemblyAdmission(8); - var listener = new StorageVatpListenerService( - runtime, - certificates, - NullStorageArticleOpenBoundary.Instance, - readiness, - logger, - queue, - admission); - await listener.StartAsync(CancellationToken.None); - AcceptedArticleWorker? worker = null; - if (startWorker) - { - worker = new AcceptedArticleWorker( - queue, - processor, - NullLogger.Instance, - new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 }); - await worker.StartAsync(CancellationToken.None); - } - - return new LiveHost( - listener, - worker, - certificates, - gate, - queue, - admission, - logger, - new StorageServerFleetEntry(identity.ServerId!.Value, identity.Fqdn, 1, 0, 1, DateTimeOffset.UtcNow, port), - port); - } - - public async ValueTask DisposeAsync() - { - _gate?.Release(); - await _listener.DisposeAsync(); - if (_worker is not null) - { - using var stop = new CancellationTokenSource(Safety); - await _worker.StopAsync(stop.Token); - } - - await _certificates.DisposeAsync(); - } - } - - private sealed class ListLogger : ILogger - { - private readonly List _messages = []; - - public string Text - { - get - { - lock (_messages) - { - return string.Join('\n', _messages); - } - } - } - - public int Count(string fragment) - { - lock (_messages) - { - return _messages.Count(message => message.Contains(fragment, StringComparison.Ordinal)); - } - } - - public IDisposable? BeginScope(TState state) where TState : notnull => null; - - public bool IsEnabled(LogLevel logLevel) => true; - - public void Log( - LogLevel logLevel, - EventId eventId, - TState state, - Exception? exception, - Func formatter) - { - lock (_messages) - { - _messages.Add(formatter(state, exception)); - } - } - } - - private sealed class PublishCounter : IAcceptedArticleProcessor - { - private int _published; - - public int Published => Volatile.Read(ref _published); - - public void EnsureReady() - { - } - - public ValueTask PublishAsync(AcceptedArticle article) => - ProcessAsync(article, CancellationToken.None); - - public ValueTask ProcessAsync( - AcceptedArticle article, - CancellationToken cancellationToken) - { - Interlocked.Increment(ref _published); - return new ValueTask(AcceptedArticleProcessResult.Completed); - } - } - - private sealed class GatePublisher : IAcceptedArticleProcessor - { - private readonly TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously); - private int _entered; - private int _finished; - - public int Entered => Volatile.Read(ref _entered); - - public int Finished => Volatile.Read(ref _finished); - - public void Release() => _release.TrySetResult(); - - public void EnsureReady() - { - } - - public async ValueTask ProcessAsync( - AcceptedArticle article, - CancellationToken cancellationToken) - { - Interlocked.Increment(ref _entered); - await _release.Task.WaitAsync(cancellationToken); - Interlocked.Increment(ref _finished); - return AcceptedArticleProcessResult.Completed; - } - } - - private sealed class TracingClient : IAsyncDisposable - { - private readonly TcpClient _tcp; - private readonly SslStream _ssl; - private readonly CancellationTokenSource _dispose = new(); - private readonly object _gate = new(); - private readonly List _trace = []; - private readonly Task _reader; - private int _disposedClient; - private long _credit; - private uint _maxFrame = VatpProtocol.DefaultMaxFramePayload; - private int _dataFrames; - private int _windowsBeforeData; - private readonly TaskCompletionSource _peerClosed = new(TaskCreationOptions.RunContinuationsAsynchronously); - - private TracingClient(TcpClient tcp, SslStream ssl) - { - _tcp = tcp; - _ssl = ssl; - WindowGranted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - Result = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - _reader = ReadLoopAsync(); - } - - public TaskCompletionSource WindowGranted { get; } - - public TaskCompletionSource Result { get; } - - public Task Closed => _peerClosed.Task; - - public int DataFrames => Volatile.Read(ref _dataFrames); - - public int DataFramesBeforeWindow { get; private set; } - - public int WindowsBeforeData => Volatile.Read(ref _windowsBeforeData); - - public uint FirstWindowCredit { get; private set; } - - public static async Task ConnectAsync(int port) - { - var tcp = new TcpClient(); - await tcp.ConnectAsync(IPAddress.Loopback, port); - var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false); - await ssl.AuthenticateAsClientAsync( - new SslClientAuthenticationOptions - { - TargetHost = "localhost", - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - RemoteCertificateValidationCallback = static (_, _, _, _) => true, - }, - CancellationToken.None); - var client = new TracingClient(tcp, ssl); - await client.WriteAsync(VatpFrameEncoder.EncodeHello(VatpProtocol.DefaultMaxFramePayload), "C HELLO"); - await client.Hello.Task.WaitAsync(Safety); - return client; - } - - private TaskCompletionSource Hello { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); - - public string TraceText() - { - lock (_gate) - { - return string.Join('\n', _trace); - } - } - - public async Task SendStoreAndMetaAsync(ArticleRecord record) - { - var id = new byte[ArticleId.Length]; - record.ArtId.CopyTo(id); - await WriteAsync(VatpFrameEncoder.EncodeStore(1, id), "C STORE"); - Assert.True(ArticlePlacementMeta.TryFromRecord(in record, out var meta)); - await WriteAsync(VatpFrameEncoder.EncodeMeta(1, VatpMetaCodec.Encode(in meta)), "C META"); - } - - public async Task SendBodyAfterWindowAsync(ArticleRecord record) - { - await WindowGranted.Task.WaitAsync(Safety); - Assert.True(record.ArtSize <= ArticleTransferLimits.Default.InitialStreamWindowBytes); - await SendDataAsync(record.ArtData, fin: true); - await WriteAsync(VatpFrameEncoder.EncodeEnd(1), "C END"); - return await Result.Task.WaitAsync(Safety); - } - - public async Task SendPartialDataAsync(ArticleRecord record) - { - await WindowGranted.Task.WaitAsync(Safety); - await SendDataAsync(record.ArtData.Slice(0, 1), fin: false); - } - - public async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposedClient, 1) != 0) - { - return; - } - - await _dispose.CancelAsync(); - try - { - await _reader.WaitAsync(TimeSpan.FromSeconds(2)); - } - catch (TimeoutException ex) - { - ExpectedException.Ignore(ex); - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - - await _ssl.DisposeAsync(); - _tcp.Dispose(); - _dispose.Dispose(); - } - - private async Task SendDataAsync(ReadOnlyMemory payload, bool fin) - { - if (WindowsBeforeData == 0) - { - DataFramesBeforeWindow++; - } - - Interlocked.Increment(ref _dataFrames); - await WriteAsync( - VatpFrameEncoder.EncodeData(1, payload, fin), - fin ? $"C DATA {payload.Length} fin" : $"C DATA {payload.Length}"); - } - - private async Task WriteAsync(VatpFrameEncoder.EncodedFrame frame, string trace) - { - lock (_gate) - { - _trace.Add(trace); - } - - var bytes = VatpFrameEncoder.ToSingleBuffer(frame); - await _ssl.WriteAsync(bytes, _dispose.Token); - } - - private async Task ReadLoopAsync() - { - var buffer = new byte[64 * 1024]; - var count = 0; - try - { - while (!_dispose.IsCancellationRequested) - { - while (TryTakeFrame(buffer, ref count)) - { - } - - if (count == buffer.Length) - { - Array.Resize(ref buffer, buffer.Length * 2); - } - - var read = await _ssl.ReadAsync(buffer.AsMemory(count), _dispose.Token); - if (read == 0) - { - _peerClosed.TrySetResult(); - return; - } - - count += read; - } - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - catch (Exception ex) when (ex is IOException or ObjectDisposedException) - { - _peerClosed.TrySetResult(); - } - } - - private bool TryTakeFrame(byte[] buffer, ref int count) - { - if (count < VatpProtocol.HeaderLengthBytes) - { - return false; - } - - var parsed = VatpFrameParser.ParseOneFrame(new ReadOnlySequence(buffer, 0, count), _maxFrame); - if (parsed.Status == VatpFrameParseStatus.Incomplete) - { - return false; - } - - if (parsed.Status != VatpFrameParseStatus.Success || parsed.Frame is not { } frame) - { - throw new InvalidOperationException(parsed.Error.ToString()); - } - - Note(frame); - var consumed = checked((int)parsed.ConsumedBytes); - Buffer.BlockCopy(buffer, consumed, buffer, 0, count - consumed); - count -= consumed; - return true; - } - - private void Note(VatpParsedFrame frame) - { - var payload = new byte[frame.Payload.Length]; - frame.Payload.CopyTo(payload); - switch (frame.Header.Type) - { - case VatpFrameType.Hello: - if (VatpHello.TryDecode(payload, out var hello, out _)) - { - _maxFrame = Math.Min(hello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload); - } - - lock (_gate) - { - _trace.Add("S HELLO"); - } - - Hello.TrySetResult(); - break; - case VatpFrameType.Window: - Assert.True(VatpControlPayload.TryDecodeWindow(payload, out var add)); - lock (_gate) - { - _trace.Add($"S WINDOW {add}"); - _credit += add; - if (_dataFrames == 0) - { - if (FirstWindowCredit == 0) - { - FirstWindowCredit = add; - } - - _windowsBeforeData++; - } - } - - WindowGranted.TrySetResult(); - break; - case VatpFrameType.Result: - lock (_gate) - { - _trace.Add($"S RESULT {payload[0]}"); - } - - Result.TrySetResult(payload[0]); - break; - case VatpFrameType.Fail: - lock (_gate) - { - _trace.Add("S FAIL"); - } - - break; - default: - lock (_gate) - { - _trace.Add("S " + frame.Header.Type); - } - - break; - } - } - } - } -} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs deleted file mode 100644 index 36514989..00000000 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpQueueIngressTests.cs +++ /dev/null @@ -1,874 +0,0 @@ -using System.Buffers; -using System.Text; -using System.Threading.Channels; -using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Configuration; -using VectorNNTPD.StorageService.Ingress; -using VectorNNTPD.StorageService.Listener; -using VectorNNTPD.StorageService; - -namespace VectorNNTPD.StorageService.Tests.Listener -{ - - public sealed class StorageVatpQueueIngressTests - { - private const string CacheFqdn = "cache01.usenet.ninja"; - private static readonly TimeSpan Safety = TimeSpan.FromSeconds(10); - - [Fact] - public async Task Store_enqueues_the_original_article_without_this_servers_path_hop() - { - var inbound = Build("", "body-line\r\n"); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - var frames = await harness.StoreAndReadAsync(inbound); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames)); - - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - Assert.Equal(inbound.Record.ArtId, delivery.Article.ArtId); - Assert.Equal(inbound.Record.ArtHash, delivery.Article.ArtHash); - Assert.Equal(inbound.Record.ArtSize, delivery.Article.ArtSize); - Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); - var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span); - Assert.Equal(0, CountHop(queued, CacheFqdn)); - Assert.Contains("Path: ", queued, StringComparison.Ordinal); - Assert.Contains("body-line\r\n", queued, StringComparison.Ordinal); - Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal); - } - - [Fact] - public async Task Store_of_bytes_that_already_contain_this_hop_prepends_it_again() - { - var inbound = Build("", "kept-body\r\n", CacheFqdn); - var inboundText = Encoding.ASCII.GetString(inbound.Record.ArtData.Span); - Assert.Equal(1, CountHop(inboundText, CacheFqdn)); - - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound))); - - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - var queued = Encoding.ASCII.GetString(delivery.Article.ArtData.Span); - Assert.True(delivery.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); - Assert.Equal(1, CountHop(queued, CacheFqdn)); - Assert.Equal(Body(inboundText), Body(queued)); - Assert.Contains("news.usenet.ninja", queued, StringComparison.Ordinal); - } - - [Fact] - public async Task Second_store_of_the_same_bytes_enqueues_another_unmodified_copy() - { - var inbound = Build("", "body\r\n"); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound))); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await harness.StoreAndReadAsync(inbound, streamId: 2))); - - var first = await queue.DequeueAsync(CancellationToken.None); - var second = await queue.DequeueAsync(CancellationToken.None); - Assert.True(first.HasArticle && second.HasArticle); - Assert.True(first.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); - Assert.True(second.Article.ArtData.Span.SequenceEqual(inbound.Record.ArtData.Span)); - Assert.Equal(0, CountHop(Encoding.ASCII.GetString(first.Article.ArtData.Span), CacheFqdn)); - Assert.Equal(0, CountHop(Encoding.ASCII.GetString(second.Article.ArtData.Span), CacheFqdn)); - } - - [Fact] - public async Task Result_is_sent_before_persistence_finishes() - { - var inbound = Build("", "body\r\n"); - var queue = new AcceptedArticleQueue(); - var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var processor = new BlockingProcessor(release.Task); - var worker = new AcceptedArticleWorker( - queue, - processor, - NullLogger.Instance, - new SpoolPersistenceOptions { MinWorkers = 1, MaxWorkers = 1 }, - new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPressure(), - new VectorNNTPD.StorageService.Storage.SpoolPersistence.SpoolPersistenceCounters(), - TimeProvider.System); - await worker.StartAsync(CancellationToken.None); - try - { - await using var harness = await Harness.StartAsync(queue); - var frames = await harness.StoreAndReadAsync(inbound).WaitAsync(TimeSpan.FromSeconds(2)); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames)); - Assert.Equal(0, processor.Finished); - } - finally - { - release.TrySetResult(); - await worker.StopAsync(CancellationToken.None); - } - } - - [Fact] - public async Task Hash_id_and_size_mismatches_do_not_enqueue() - { - var real = Build("", "body\r\n"); - var other = Build("", "body\r\n"); - var queue = new AcceptedArticleQueue(); - - await using var idMismatch = await Harness.StartAsync(queue); - await idMismatch.SendRawAsync(StoreFrame(other.Record.ArtId, 1)); - await idMismatch.SendArticleBodyAsync(real, 1); - Assert.Equal(VatpErrorCode.ArticleIdMismatch, FailCode(await idMismatch.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - - await using var hashMismatch = await Harness.StartAsync(queue); - await hashMismatch.SendStoreAsync(real, corruptLastDataByte: true); - Assert.Equal(VatpErrorCode.ArtHashMismatch, FailCode(await hashMismatch.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - - await using var sizeMismatch = await Harness.StartAsync(queue); - await sizeMismatch.SendRawAsync(StoreFrame(real.Record.ArtId, 1)); - await sizeMismatch.SendRawAsync(MetaFrame(real, 1)); - var shortPayload = real.Record.ArtData.Slice(0, Math.Max(1, real.Record.ArtSize / 2)); - await sizeMismatch.SendRawAsync(VatpFrameEncoder.ToSingleBuffer( - VatpFrameEncoder.EncodeData(1, shortPayload, fin: true))); - await sizeMismatch.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(1))); - Assert.Equal(VatpErrorCode.ArtSizeMismatch, FailCode(await sizeMismatch.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, queue.AccountedBytes); - - using var timeout = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); - await Assert.ThrowsAnyAsync(async () => - await queue.DequeueAsync(timeout.Token)); - } - - [Fact] - public async Task Oversized_meta_is_rejected_before_enqueue() - { - var built = Build("", "body\r\n"); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - await harness.SendRawAsync(StoreFrame(built.Record.ArtId, 1)); - var meta = MetaFrame(built, 1); - System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian( - meta.AsSpan(VatpProtocol.HeaderLengthBytes + 12, 4), - ArticleResourceLimits.MaxArticleBytes + 1); - await harness.SendRawAsync(meta); - Assert.Equal(VatpErrorCode.ArticleTooLarge, FailCode(await harness.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - using var timeout = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); - await Assert.ThrowsAnyAsync(async () => - await queue.DequeueAsync(timeout.Token)); - } - - [Fact] - public async Task Completed_queue_rejects_the_store_without_dropping_a_queued_article() - { - var built = Build("", "body\r\n"); - var queue = new AcceptedArticleQueue(); - queue.Complete(); - await using var harness = await Harness.StartAsync(queue); - await harness.SendRawAsync(StoreFrame(built.Record.ArtId, 1)); - await harness.SendRawAsync(MetaFrame(built, 1)); - Assert.Equal(VatpErrorCode.Cancelled, FailCode(await harness.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, harness.BodiesAllocated); - var empty = await queue.DequeueAsync(CancellationToken.None); - Assert.False(empty.HasArticle); - } - - [Fact] - public async Task Two_sessions_enqueue_distinct_articles() - { - var queue = new AcceptedArticleQueue(); - var first = Build("", "alpha\r\n"); - var second = Build("", "beta\r\n"); - await using var left = await Harness.StartAsync(queue); - await using var right = await Harness.StartAsync(queue); - var leftTask = left.StoreAndReadAsync(first); - var rightTask = right.StoreAndReadAsync(second, streamId: 3); - var leftFrames = await leftTask; - var rightFrames = await rightTask; - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(leftFrames)); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(rightFrames)); - - var seen = new HashSet(StringComparer.Ordinal); - for (var i = 0; i < 2; i++) - { - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - seen.Add(Encoding.ASCII.GetString(delivery.Article.ArtData.Span)); - } - - Assert.Contains(seen, static text => text.Contains("alpha\r\n", StringComparison.Ordinal)); - Assert.Contains(seen, static text => text.Contains("beta\r\n", StringComparison.Ordinal)); - } - - [Fact] - public async Task Windows_cover_every_payload_byte_and_precede_result() - { - var body = new StringBuilder(300_000); - for (var line = 0; line < 3_000; line++) - { - body.Append('x', 98).Append("\r\n"); - } - - var built = Build("", body.ToString()); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - await harness.SendRawAsync(StoreFrame(built.Record.ArtId, 1)); - await harness.SendRawAsync(MetaFrame(built, 1)); - - var art = built.Record.ArtData; - var offset = 0; - while (offset < art.Length) - { - var length = Math.Min(64 * 1024, art.Length - offset); - var fin = offset + length == art.Length; - await harness.SendRawAsync(VatpFrameEncoder.ToSingleBuffer( - VatpFrameEncoder.EncodeData(1, art.Slice(offset, length), fin))); - offset += length; - } - - await harness.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(1))); - var frames = await harness.ReadUntilAsync(static received => - received.Any(frame => frame.Header.Type == VatpFrameType.Result)); - - long credit = 0; - var admissionGrant = 0L; - var resultSeen = false; - foreach (var frame in frames) - { - if (frame.Header.Type == VatpFrameType.Window) - { - Assert.False(resultSeen); - Assert.Equal(1u, frame.Header.StreamId); - Assert.True(VatpControlPayload.TryDecodeWindow(frame.Payload, out var add)); - if (admissionGrant == 0) - { - admissionGrant = add; - } - else - { - credit += add; - } - } - - if (frame.Header.Type == VatpFrameType.Result) - { - resultSeen = true; - Assert.Equal(VatpStoreOutcome.Accepted, frame.Payload[0]); - } - } - - Assert.Equal(ArticleTransferLimits.Default.InitialStreamWindowBytes, admissionGrant); - Assert.Equal(built.Record.ArtSize, credit); - Assert.Equal(1, queue.EnqueuedCount); - } - - [Fact] - public void Default_receive_stream_starts_at_the_production_window() - { - var built = Build("", "body\r\n"); - var stream = new ArticleTransferReceiveStream(1, Guid.NewGuid(), built.Record.ArtId); - Assert.Equal(1024L * 1024, stream.ReceiveCredit); - Assert.Equal(1024 * 1024, ArticleTransferLimits.Default.InitialStreamWindowBytes); - } - - [Fact] - public async Task Explicit_initial_window_reaches_the_receive_stream_and_refunds_each_chunk() - { - const int initial = 1024 * 1024; - var limits = new ArticleTransferLimits { InitialStreamWindowBytes = initial }; - var built = Build("", "body-line\r\n"); - var stream = new ArticleTransferReceiveStream(1, Guid.NewGuid(), built.Record.ArtId, limits); - Assert.Equal((long)initial, stream.ReceiveCredit); - - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue, limits); - var frames = await harness.StoreAndReadAsync(built); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames)); - long refunded = 0; - var sawGrant = false; - foreach (var frame in frames) - { - if (frame.Header.Type != VatpFrameType.Window) - { - continue; - } - - Assert.True(VatpControlPayload.TryDecodeWindow(frame.Payload, out var add)); - if (!sawGrant) - { - Assert.Equal((uint)initial, add); - sawGrant = true; - continue; - } - - Assert.True(add <= 64 * 1024); - refunded += add; - } - - Assert.True(sawGrant); - Assert.Equal(built.Record.ArtSize, refunded); - Assert.Equal(1, queue.EnqueuedCount); - } - - [Fact] - public async Task Full_queue_withholds_body_credit_until_a_dequeue_frees_capacity() - { - var first = Build("", "alpha-body\r\n"); - var queue = new AcceptedArticleQueue(first.Record.ArtSize, TimeProvider.System); - await using var held = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await held.StoreAndReadAsync(first))); - - var second = Build("", "beta-body\r\n"); - await using var waiting = await Harness.StartAsync(queue); - await waiting.SendRawAsync(StoreFrame(second.Record.ArtId, 1)); - await waiting.SendRawAsync(MetaFrame(second, 1)); - var credit = waiting.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type is VatpFrameType.Window or VatpFrameType.Fail or VatpFrameType.Result)); - await WaitUntilAsync(() => queue.AdmissionWaiters == 1); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(first.Record.ArtSize, queue.AccountedBytes); - Assert.Equal(0, waiting.BodiesAllocated); - Assert.False(credit.IsCompleted); - - Assert.True((await queue.DequeueAsync(CancellationToken.None)).HasArticle); - var granted = await credit.WaitAsync(TimeSpan.FromSeconds(2)); - Assert.Equal(VatpFrameType.Window, granted[0].Header.Type); - Assert.Equal(1, waiting.BodiesAllocated); - Assert.Equal(second.Record.ArtSize, queue.ReservedBytes); - Assert.Equal(0, queue.QueuedBytes); - - await waiting.SendRawAsync(VatpFrameEncoder.ToSingleBuffer( - VatpFrameEncoder.EncodeData(1, second.Record.ArtData, fin: true))); - await waiting.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(1))); - var result = await waiting.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type == VatpFrameType.Result)); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(result)); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(second.Record.ArtSize, queue.QueuedBytes); - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.Article.ArtData.Span.SequenceEqual(second.Record.ArtData.Span)); - } - - [Fact] - public async Task Body_sent_before_queue_admission_is_rejected_without_allocating_it() - { - var first = Build("", "alpha-body\r\n"); - var queue = new AcceptedArticleQueue(first.Record.ArtSize, TimeProvider.System); - await using var held = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await held.StoreAndReadAsync(first))); - - var second = Build("", "beta-body\r\n"); - await using var early = await Harness.StartAsync(queue); - await early.SendStoreAsync(second); - var frames = await early.ReadUntilAsync(static received => - received.Any(static frame => frame.Header.Type is VatpFrameType.Fail or VatpFrameType.Result)); - Assert.Equal(VatpErrorCode.FlowControlViolation, FailCode(frames)); - Assert.Equal(1, queue.EnqueuedCount); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, queue.AdmissionWaiters); - Assert.Equal(0, early.BodiesAllocated); - } - - [Fact] - public async Task Cancel_before_body_admission_releases_the_waiter() - { - var first = Build("", "alpha-body\r\n"); - var queue = new AcceptedArticleQueue(first.Record.ArtSize, TimeProvider.System); - await using var held = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await held.StoreAndReadAsync(first))); - var second = Build("", "beta-body\r\n"); - await using var waiting = await Harness.StartAsync(queue); - await waiting.SendRawAsync(StoreFrame(second.Record.ArtId, 1)); - await waiting.SendRawAsync(MetaFrame(second, 1)); - await WaitUntilAsync(() => queue.AdmissionWaiters == 1); - await waiting.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeCancel(1))); - var frames = await waiting.ReadUntilAsync(static received => - received.Any(static frame => frame.Header.Type == VatpFrameType.Fail)); - Assert.Equal(VatpErrorCode.Cancelled, FailCode(frames)); - await WaitUntilAsync(() => queue.AdmissionWaiters == 0); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(first.Record.ArtSize, queue.AccountedBytes); - } - - [Fact] - public async Task Article_larger_than_the_queue_budget_is_rejected_before_the_body() - { - var built = Build("", "too-big-for-budget\r\n"); - var queue = new AcceptedArticleQueue(8, TimeProvider.System); - await using var harness = await Harness.StartAsync(queue); - await harness.SendRawAsync(StoreFrame(built.Record.ArtId, 1)); - await harness.SendRawAsync(MetaFrame(built, 1)); - var frames = await harness.ReadUntilAsync(static received => - received.Any(static frame => frame.Header.Type == VatpFrameType.Result)); - Assert.Equal(VatpStoreOutcome.RejectedCapacity, Outcome(frames)); - Assert.Equal(0, queue.EnqueuedCount); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, harness.BodiesAllocated); - } - - [Fact] - public async Task Two_sessions_cannot_both_reserve_the_last_free_slot() - { - var held = Build("", "held-body\r\n"); - var queue = new AcceptedArticleQueue(held.Record.ArtSize, TimeProvider.System); - await using var owner = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await owner.StoreAndReadAsync(held))); - - var left = Build("", "peer-body\r\n"); - var right = Build("", "peer-body\r\n"); - Assert.Equal(held.Record.ArtSize, left.Record.ArtSize); - Assert.Equal(held.Record.ArtSize, right.Record.ArtSize); - await using var first = await Harness.StartAsync(queue); - await using var second = await Harness.StartAsync(queue); - await first.SendRawAsync(StoreFrame(left.Record.ArtId, 1)); - await first.SendRawAsync(MetaFrame(left, 1)); - await second.SendRawAsync(StoreFrame(right.Record.ArtId, 1)); - await second.SendRawAsync(MetaFrame(right, 1)); - await WaitUntilAsync(() => queue.AdmissionWaiters == 2); - Assert.Equal(0, queue.ReservedBytes); - - var firstCredit = first.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type is VatpFrameType.Window or VatpFrameType.Fail or VatpFrameType.Result)); - var secondCredit = second.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type is VatpFrameType.Window or VatpFrameType.Fail or VatpFrameType.Result)); - Assert.True((await queue.DequeueAsync(CancellationToken.None)).HasArticle); - var winner = await Task.WhenAny(firstCredit, secondCredit).WaitAsync(TimeSpan.FromSeconds(2)); - await WaitUntilAsync(() => queue.AdmissionWaiters == 1 && queue.ReservedBytes == held.Record.ArtSize); - Assert.True(winner.IsCompletedSuccessfully); - Assert.Equal(VatpFrameType.Window, (await winner)[0].Header.Type); - Assert.Equal(1, (firstCredit.IsCompleted ? 1 : 0) + (secondCredit.IsCompleted ? 1 : 0)); - Assert.Equal(0, queue.QueuedBytes); - } - - [Fact] - public async Task Disconnect_during_body_reception_releases_the_reservation() - { - var built = Build("", new string('x', 128) + "\r\n"); - var queue = new AcceptedArticleQueue(built.Record.ArtSize, TimeProvider.System); - var session = await Harness.StartAsync(queue); - await session.SendRawAsync(StoreFrame(built.Record.ArtId, 1)); - await session.SendRawAsync(MetaFrame(built, 1)); - var granted = await session.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type == VatpFrameType.Window)); - Assert.Equal(VatpFrameType.Window, granted[0].Header.Type); - Assert.Equal(1, session.BodiesAllocated); - Assert.Equal(built.Record.ArtSize, queue.ReservedBytes); - - var chunk = built.Record.ArtData.Slice(0, 1); - await session.SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeData(1, chunk, fin: false))); - await session.ReadUntilAsync(static frames => - frames.Any(static frame => frame.Header.Type == VatpFrameType.Window)); - Assert.Equal(built.Record.ArtSize, queue.ReservedBytes); - Assert.Equal(0, queue.QueuedBytes); - - await session.DisposeAsync(); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, queue.AccountedBytes); - Assert.Equal(0, queue.AdmissionWaiters); - - await using var retry = await Harness.StartAsync(queue); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(await retry.StoreAndReadAsync(built))); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(built.Record.ArtSize, queue.QueuedBytes); - } - - private static async Task WaitUntilAsync(Func condition) - { - using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2)); - while (!condition()) - { - await Task.Delay(5, timeout.Token); - } - } - - [Fact] - public async Task Initial_window_smaller_than_a_chunk_is_a_flow_control_failure() - { - var limits = new ArticleTransferLimits { InitialStreamWindowBytes = 16 }; - var built = Build("", "body-line\r\n"); - Assert.True(built.Record.ArtSize > limits.InitialStreamWindowBytes); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue, limits); - await harness.SendStoreAsync(built); - Assert.Equal(VatpErrorCode.FlowControlViolation, FailCode(await harness.ReadUntilAsync(static frames => - frames.Any(frame => frame.Header.Type == VatpFrameType.Fail)))); - Assert.Equal(0, queue.EnqueuedCount); - } - - [Fact] - public void Initial_window_above_the_max_credit_is_rejected() - { - var built = Build("", "body\r\n"); - var limits = new ArticleTransferLimits - { - InitialStreamWindowBytes = (int)ArticleTransferLimits.Default.MaxStreamCreditBytes + 1, - }; - Assert.Throws(() => - new ArticleTransferReceiveStream(1, Guid.NewGuid(), built.Record.ArtId, limits)); - } - - [Fact] - public async Task Split_reads_enqueue_a_large_article() - { - var body = new StringBuilder(768_000); - for (var line = 0; line < 7_680; line++) - { - body.Append('y', 98).Append("\r\n"); - } - - var built = Build("", body.ToString()); - var framed = EncodeStore(built, 1); - - await AssertChunkedEnqueueAsync(built, framed, chunkSize: 7); - await AssertChunkedEnqueueAsync(built, framed, chunkSize: 16 * 1024); - await AssertChunkedEnqueueAsync(built, framed, chunkSize: framed.Length); - await AssertExactFrameReadsAsync(built); - } - - [Fact] - public async Task Interleaved_streams_split_across_reads_both_enqueue() - { - var first = Build("", "left-body\r\n"); - var second = Build("", "right-body\r\n"); - var framed = EncodeStore(first, 1).Concat(EncodeStore(second, 2)).ToArray(); - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - await harness.Transport.ClientWriteChunkedAsync(framed, 5); - var frames = await harness.ReadUntilAsync(static received => - received.Count(static frame => frame.Header.Type == VatpFrameType.Result) == 2); - - Assert.Equal(2, frames.Count(static frame => frame.Header.Type == VatpFrameType.Result && frame.Payload[0] == VatpStoreOutcome.Accepted)); - Assert.Equal(2, queue.EnqueuedCount); - var seen = new HashSet(StringComparer.Ordinal); - for (var i = 0; i < 2; i++) - { - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - seen.Add(Encoding.ASCII.GetString(delivery.Article.ArtData.Span)); - } - - Assert.Contains(seen, static text => text.Contains("left-body\r\n", StringComparison.Ordinal)); - Assert.Contains(seen, static text => text.Contains("right-body\r\n", StringComparison.Ordinal)); - } - - private static async Task AssertChunkedEnqueueAsync(Built built, byte[] framed, int chunkSize) - { - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - await harness.Transport.ClientWriteChunkedAsync(framed, chunkSize); - var frames = await harness.ReadUntilAsync(static received => - received.Any(frame => frame.Header.Type == VatpFrameType.Result)); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames)); - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - Assert.True(delivery.Article.ArtData.Span.SequenceEqual(built.Record.ArtData.Span)); - Assert.Equal(1, queue.EnqueuedCount); - } - - private static async Task AssertExactFrameReadsAsync(Built built) - { - var queue = new AcceptedArticleQueue(); - await using var harness = await Harness.StartAsync(queue); - foreach (var frame in EnumerateStoreFrames(built, 1)) - { - await harness.SendRawAsync(frame); - } - - var frames = await harness.ReadUntilAsync(static received => - received.Any(frame => frame.Header.Type == VatpFrameType.Result)); - Assert.Equal(VatpStoreOutcome.Accepted, Outcome(frames)); - var delivery = await queue.DequeueAsync(CancellationToken.None); - Assert.True(delivery.HasArticle); - Assert.True(delivery.Article.ArtData.Span.SequenceEqual(built.Record.ArtData.Span)); - } - - private static byte[] EncodeStore(Built built, uint streamId) - { - var parts = EnumerateStoreFrames(built, streamId); - var length = 0; - foreach (var part in parts) - { - length += part.Length; - } - - var framed = new byte[length]; - var offset = 0; - foreach (var part in parts) - { - part.CopyTo(framed.AsSpan(offset)); - offset += part.Length; - } - - return framed; - } - - private static List EnumerateStoreFrames(Built built, uint streamId) - { - var parts = new List - { - StoreFrame(built.Record.ArtId, streamId), - MetaFrame(built, streamId), - }; - var art = built.Record.ArtData; - var offset = 0; - while (offset < art.Length) - { - var length = Math.Min(64 * 1024, art.Length - offset); - var fin = offset + length == art.Length; - parts.Add(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeData(streamId, art.Slice(offset, length), fin))); - offset += length; - } - - parts.Add(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(streamId))); - return parts; - } - - private static int CountHop(string article, string hop) => - article.Split(hop, StringSplitOptions.None).Length - 1; - - private static string Body(string article) - { - var split = article.IndexOf("\r\n\r\n", StringComparison.Ordinal); - Assert.True(split >= 0); - return article[(split + 4)..]; - } - - private static byte Outcome(IReadOnlyList frames) => - frames.Last(static frame => frame.Header.Type == VatpFrameType.Result).Payload[0]; - - private static VatpErrorCode FailCode(IReadOnlyList frames) - { - var payload = frames.Last(static frame => frame.Header.Type == VatpFrameType.Fail).Payload; - Assert.True(VatpControlPayload.TryDecodeFail(payload, out var error, out _)); - return error; - } - - private static Built Build(string messageId, string body, string ingressFqdn = "nntpd01.usenet.ninja") - { - var parser = new NntpArticleParser(ingressFqdn); - var text = new StringBuilder() - .Append("Path: peer.example\r\n") - .Append("Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n") - .Append("Message-ID: ").Append(messageId).Append("\r\n") - .Append("Newsgroups: alt.test\r\n") - .Append("From: user@example.test\r\n") - .Append("Subject: vatp\r\n") - .Append("\r\n") - .Append(body); - var created = ArticleRecordFactory.TryCreate(parser, Encoding.ASCII.GetBytes(text.ToString())); - Assert.True(created.IsAccepted, created.MaterializeFailure.ToString()); - return new Built(created.Record, created.SelectedDateHeaderName); - } - - private static byte[] StoreFrame(ArticleId articleId, uint streamId) - { - var id = new byte[ArticleId.Length]; - articleId.CopyTo(id); - return VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeStore(streamId, id)); - } - - private static byte[] MetaFrame(Built built, uint streamId) - { - var meta = ArticleCanonicalTransferMeta.FromRecord(built.Record, built.SelectedDateHeaderName); - return VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeMeta(streamId, VatpMetaCodec.Encode(in meta))); - } - - private readonly record struct Built(ArticleRecord Record, NntpArticleHeaderName SelectedDateHeaderName); - - private sealed class BlockingProcessor : IAcceptedArticleProcessor - { - private readonly Task _release; - - public BlockingProcessor(Task release) => _release = release; - - public int Finished { get; private set; } - - public void EnsureReady() - { - } - - public async ValueTask ProcessAsync( - AcceptedArticle article, - CancellationToken cancellationToken) - { - await _release.WaitAsync(cancellationToken).ConfigureAwait(false); - Finished++; - return AcceptedArticleProcessResult.Completed; - } - } - - private readonly record struct Decoded(VatpFrameHeader Header, byte[] Payload); - - private sealed class Harness : IAsyncDisposable - { - private readonly CancellationTokenSource _cts = new(); - private readonly StorageVatpSession _session; - private readonly Task _run; - - private Harness(PipeTransport transport, StorageVatpSession session) - { - Transport = transport; - _session = session; - _run = session.RunAsync(_cts.Token); - } - - public PipeTransport Transport { get; } - - /// Article buffers this session allocated after admission. - public long BodiesAllocated => Volatile.Read(ref _session.StoreBodiesAllocated); - - public static async Task StartAsync(IAcceptedArticleQueue queue, ArticleTransferLimits? limits = null) - { - var transport = new PipeTransport(); - var session = new StorageVatpSession( - transport, - NullStorageArticleOpenBoundary.Instance, - new StorageServerListenerRuntimeOptions(262144, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), 8), - NullLogger.Instance, - limits, - acceptedArticles: queue, - storeAdmission: new StoreAssemblyAdmission(8)); - var harness = new Harness(transport, session); - await harness.Transport.ClientWriteAsync(VatpFrameEncoder.ToSingleBuffer( - VatpFrameEncoder.EncodeHello(VatpProtocol.DefaultMaxFramePayload))); - _ = await harness.ReadUntilAsync(static frames => frames.Any(frame => frame.Header.Type == VatpFrameType.Hello)); - return harness; - } - - public async Task SendStoreAsync(Built built, uint streamId = 1, bool corruptLastDataByte = false) - { - await SendRawAsync(StoreFrame(built.Record.ArtId, streamId)); - await SendArticleBodyAsync(built, streamId, corruptLastDataByte); - } - - public async Task SendArticleBodyAsync(Built built, uint streamId, bool corruptLastDataByte = false) - { - await SendRawAsync(MetaFrame(built, streamId)); - var payload = built.Record.ArtData.ToArray(); - if (corruptLastDataByte) - { - payload[^1] ^= 0xFF; - } - - await SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeData(streamId, payload, fin: true))); - await SendRawAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(streamId))); - } - - public Task SendRawAsync(byte[] frame) => Transport.ClientWriteAsync(frame); - - public async Task> StoreAndReadAsync(Built built, uint streamId = 1) - { - await SendRawAsync(StoreFrame(built.Record.ArtId, streamId)); - await SendArticleBodyAsync(built, streamId); - return await ReadUntilAsync(static frames => frames.Any(frame => frame.Header.Type == VatpFrameType.Result)); - } - - public async Task> ReadUntilAsync(Func, bool> done) - { - using var timeout = new CancellationTokenSource(Safety); - var buffer = new List(); - var frames = new List(); - while (!done(frames)) - { - var chunk = await Transport.ReadServerAsync(timeout.Token); - buffer.AddRange(chunk); - var offset = 0; - while (offset < buffer.Count) - { - var parsed = VatpFrameParser.ParseOneFrame(buffer.ToArray(), VatpProtocol.DefaultMaxFramePayload); - if (parsed.Status == VatpFrameParseStatus.Incomplete) - { - break; - } - - Assert.Equal(VatpFrameParseStatus.Success, parsed.Status); - var frame = parsed.Frame!.Value; - var payload = new byte[frame.Payload.Length]; - frame.Payload.CopyTo(payload); - frames.Add(new Decoded(frame.Header, payload)); - offset += checked((int)parsed.ConsumedBytes); - buffer.RemoveRange(0, checked((int)parsed.ConsumedBytes)); - } - } - - return frames; - } - - public async ValueTask DisposeAsync() - { - await _cts.CancelAsync(); - try - { - await _run; - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - - _cts.Dispose(); - await Transport.DisposeAsync(); - } - } - - private sealed class PipeTransport : IStorageVatpTransport - { - private readonly Channel _toServer = Channel.CreateUnbounded(); - private readonly Channel _toClient = Channel.CreateUnbounded(); - private byte[] _pending = []; - private int _pendingOffset; - - public async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) - { - if (_pendingOffset >= _pending.Length) - { - _pending = await _toServer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); - _pendingOffset = 0; - } - - var count = Math.Min(buffer.Length, _pending.Length - _pendingOffset); - _pending.AsSpan(_pendingOffset, count).CopyTo(buffer.Span); - _pendingOffset += count; - return count; - } - - public async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) - { - await _toClient.Writer.WriteAsync(buffer.ToArray(), cancellationToken).ConfigureAwait(false); - return buffer.Length; - } - - public Task ClientWriteAsync(byte[] frame) => _toServer.Writer.WriteAsync(frame).AsTask(); - - public async Task ClientWriteChunkedAsync(byte[] frame, int chunkSize) - { - ArgumentOutOfRangeException.ThrowIfNegativeOrZero(chunkSize); - var offset = 0; - while (offset < frame.Length) - { - var count = Math.Min(chunkSize, frame.Length - offset); - var slice = new byte[count]; - frame.AsSpan(offset, count).CopyTo(slice); - await _toServer.Writer.WriteAsync(slice).ConfigureAwait(false); - offset += count; - } - } - - public Task ReadServerAsync(CancellationToken cancellationToken) => - _toClient.Reader.ReadAsync(cancellationToken).AsTask(); - - public ValueTask DisposeAsync() - { - _toServer.Writer.TryComplete(); - _toClient.Writer.TryComplete(); - return ValueTask.CompletedTask; - } - } - } -} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpSingleConnectionBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpSingleConnectionBench.cs deleted file mode 100644 index 670dafcb..00000000 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpSingleConnectionBench.cs +++ /dev/null @@ -1,931 +0,0 @@ -using System.Buffers; -using System.Diagnostics; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Security; -using System.Net.Sockets; -using System.Runtime; -using System.Security.Authentication; -using System.Text; -using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.Common.Acme; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Configuration; -using VectorNNTPD.StorageService.Ingress; -using VectorNNTPD.StorageService.Listener; -using VectorNNTPD.StorageService.Tests.Fixtures; -using VectorNNTPD.StorageService.Tests.TestDoubles; -using Xunit.Abstractions; -using VectorNNTPD.StorageService; - -namespace VectorNNTPD.StorageService.Tests.Listener -{ - - /// - /// One TCP/TLS connection, many concurrent VATP STORE streams. Opt-in via - /// VECTORNNTPD_STORAGE_SINGLE=1. - /// - public sealed class StorageVatpSingleConnectionBench - { - private const int BodyBytes = 768_000; - private const int InitialWindowBytes = 256 * 1024; - private const int MaxFramePayload = 64 * 1024; - private const long QueueByteCap = 256L * 1024 * 1024; - - private readonly ITestOutputHelper _output; - - public StorageVatpSingleConnectionBench(ITestOutputHelper output) => _output = output; - - [Fact] - public async Task Two_streams_on_one_connection_both_enqueue() - { - PipelinedVatpClient.ResetConnectionCount(); - var first = PrepareArticle("body-a\r\n", "", 1); - var second = PrepareArticle("body-b\r\n", "", 2); - await using var host = await IngressHost.StartAsync(); - await using var client = await PipelinedVatpClient.ConnectAsync(host.Port, [first, second]); - await client.RunAsync(articleLimit: 2, CancellationToken.None); - Assert.Equal(1, client.ConnectionsOpened); - Assert.Equal(2, client.Submitted); - Assert.Equal(2, client.Succeeded); - Assert.Equal(0, client.Failed); - Assert.Equal(2, host.Queue.EnqueuedCount); - Assert.Equal(2, client.PeakInFlight); - } - - [Fact] - public async Task Single_connection_ingress_when_enabled() - { - if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_SINGLE"), "1", StringComparison.Ordinal)) - { - return; - } - - ThreadPool.GetMinThreads(out var workers, out var io); - var minThreads = Math.Max(workers, Environment.ProcessorCount * 8); - ThreadPool.SetMinThreads(minThreads, Math.Max(io, minThreads)); - var depths = ParseList(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_SINGLE_DEPTHS"), [1, 2, 4, 8, 16, 32, 64]); - var measureSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_SINGLE_SECONDS"), 8); - var warmupSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_SINGLE_WARMUP"), 2); - var report = new StringBuilder(); - report.AppendLine("topology=1tcp+1tls acceptance=AcceptedArticleQueue.TryEnqueue pathWorker=false"); - report.AppendLine($"bodyBytes={BodyBytes} measureSeconds={measureSeconds} warmupSeconds={warmupSeconds} serverGc={GCSettings.IsServerGC} processors={Environment.ProcessorCount} aesIsa={System.Runtime.Intrinsics.X86.Aes.IsSupported}"); - - foreach (var depth in depths) - { - if (depth is < 1 or > 64) - { - throw new InvalidOperationException($"In-flight depth {depth} is outside the 64-stream connection cap."); - } - - var line = await RunLevelAsync(depth, warmupSeconds, measureSeconds); - report.AppendLine(line); - _output.WriteLine(line); - } - - var directory = Path.GetFullPath(Path.Join(AppContext.BaseDirectory, "..", "..", "..", "..", "..", ".artifacts", "experiments")); - Directory.CreateDirectory(directory); - var path = Path.Join(directory, "vatp-single-connection-raw.txt"); - await File.WriteAllTextAsync(path, report.ToString()); - _output.WriteLine(path); - } - - private async Task RunLevelAsync(int depth, int warmupSeconds, int measureSeconds) - { - PipelinedVatpClient.ResetConnectionCount(); - StorageVatpSession.ResetEnqueueToResultTiming(); - var articles = new PreparedArticle[depth]; - for (var i = 0; i < depth; i++) - { - articles[i] = PrepareArticle(BodyBytes, $"", (uint)(i + 1)); - } - - await using var host = await IngressHost.StartAsync(); - await using var client = await PipelinedVatpClient.ConnectAsync(host.Port, articles); - var tcpBefore = IPGlobalProperties.GetIPGlobalProperties().GetTcpIPv4Statistics(); - using var runCts = new CancellationTokenSource(); - var sending = client.RunAsync(articleLimit: null, runCts.Token); - var started = Stopwatch.StartNew(); - var origin = Take(host, client, TimeSpan.Zero); - Sample? warmup = null; - var depthMax = 0L; - var capped = false; - while (started.Elapsed < TimeSpan.FromSeconds(warmupSeconds + measureSeconds)) - { - if (host.Queue.QueuedBytes > QueueByteCap) - { - capped = true; - break; - } - - var depthNow = host.Queue.EnqueuedCount - host.Queue.DequeuedCount; - if (depthNow > depthMax) - { - depthMax = depthNow; - } - - if (warmup is null && started.Elapsed >= TimeSpan.FromSeconds(warmupSeconds)) - { - warmup = Take(host, client, started.Elapsed); - } - - var remaining = TimeSpan.FromSeconds(warmupSeconds + measureSeconds) - started.Elapsed; - if (remaining <= TimeSpan.Zero) - { - break; - } - - await Task.Delay(remaining < TimeSpan.FromMilliseconds(250) ? remaining : TimeSpan.FromMilliseconds(250)); - } - - var steadyEnd = Take(host, client, started.Elapsed); - if (steadyEnd.Depth > depthMax) - { - depthMax = steadyEnd.Depth; - } - - client.StopStarting(); - await sending.WaitAsync(TimeSpan.FromSeconds(60)); - var idle = await WaitForDrainAsync(host, TimeSpan.FromSeconds(30)); - var finalSample = Take(host, client, started.Elapsed); - var clientSendBuffer = client.SendBuffer; - var clientReceiveBuffer = client.ReceiveBuffer; - var negotiatedTls = client.NegotiatedTls; - var serverSendBuffer = StorageVatpListenerService.LastAcceptedSendBuffer; - var serverReceiveBuffer = StorageVatpListenerService.LastAcceptedReceiveBuffer; - await client.DisposeAsync(); - var timing = StorageVatpSession.ReadEnqueueToResultTiming(); - var path = StorageVatpSession.ReadReceivePathTiming(); - var reads = StorageVatpSession.ReadReceiveObservation(); - var assembly = StorageVatpSession.ReadAssemblyObservation(); - var pace = StorageVatpSession.ReadPaceObservation(); - var tls = StreamStorageVatpTransport.ReadTlsThreadCost(); - var tcpAfter = IPGlobalProperties.GetIPGlobalProperties().GetTcpIPv4Statistics(); - var steady = warmup is null ? "n/a" : FormatRate(warmup.Value, steadyEnd, articles[0].ArtSize); - var allocated = finalSample.Allocated - origin.Allocated; - return - $"depth={depth} connections={client.ConnectionsOpened} peakInFlight={client.PeakInFlight} capped={capped} drained={idle} steady=[{steady}] " + - $"depthMax={depthMax} depthAtEnd={steadyEnd.Depth} " + - $"submitted={client.Submitted} succeeded={client.Succeeded} failed={client.Failed} " + - $"enqueued={finalSample.Enqueued} dequeued={finalSample.Dequeued} " + - $"creditWaitSec={client.CreditWaitSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} resultWaitSec={client.ResultWaitSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} blockedSec={client.BlockedSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteSec={client.WriteSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"enqueueToResultCount={timing.Count} enqueueToResultSec={timing.Seconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"dataAcceptSec={path.DataSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} windowWriteSec={path.WindowSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} windowFrames={path.WindowCount} readWaitSec={path.ReadWaitSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} processSec={reads.ProcessSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} writeGateSec={reads.WriteGateSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} endValidateSec={path.EndSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"reads={reads.Reads} readBytes={reads.ReadBytes} readLt4K={reads.ReadLt4K} readLt16K={reads.ReadLt16K} readLt32K={reads.ReadLt32K} readExact16K={assembly.ReadExact16K} readLt64K={reads.ReadLt64K} readGe64K={reads.ReadGe64K} readUsLt20={reads.ReadDurLt20Us} readUsLt50={reads.ReadDurLt50Us} readUsLt100={reads.ReadDurLt100Us} readUsLt500={reads.ReadDurLt500Us} readUsGe500={reads.ReadDurGe500Us} incompleteParses={reads.IncompleteParses} frames={reads.Frames} " + - $"framesRead0={assembly.FramesThisRead0} framesRead1={assembly.FramesThisRead1} framesReadMany={assembly.FramesThisReadMany} readsToFrame1={assembly.ReadsToFrame1} readsToFrame2={assembly.ReadsToFrame2} readsToFrame3={assembly.ReadsToFrame3} readsToFrame4={assembly.ReadsToFrame4} readsToFrame5Plus={assembly.ReadsToFrame5Plus} " + - $"partial0={assembly.PartialAtRead0} partialHeader={assembly.PartialAtReadHeader} partialLt16K={assembly.PartialAtReadLt16K} partialLt64K={assembly.PartialAtReadLt64K} partialGe64K={assembly.PartialAtReadGe64K} " + - $"parseSec={assembly.ParseSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} compactCount={assembly.CompactCount} compactBytes={assembly.CompactBytes} compactSec={assembly.CompactSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} dataCopyBytes={assembly.DataCopyBytes} timeoutSetupSec={assembly.TimeoutSetupSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} streamReadSec={assembly.StreamReadSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"readDuringWriteSec={pace.ReadDuringClientWriteSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} readDuringWriteN={pace.ReadDuringClientWriteCount} readAfterWriteSec={pace.ReadAfterClientWriteSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} readAfterWriteN={pace.ReadAfterClientWriteCount} readGapLt20={pace.ReadGapLt20Us} readGapLt100={pace.ReadGapLt100Us} readGapLt1Ms={pace.ReadGapLt1Ms} readGapGe1Ms={pace.ReadGapGe1Ms} " + - $"clientWriteDuringReadSec={pace.ClientWriteDuringServerReadSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteDuringReadN={pace.ClientWriteDuringServerReadCount} clientWriteDuringServerWriteSec={pace.ClientWriteDuringServerWriteSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteDuringServerWriteN={pace.ClientWriteDuringServerWriteCount} clientWriteDuringOtherSec={pace.ClientWriteDuringOtherSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteDuringOtherN={pace.ClientWriteDuringOtherCount} " + - $"readyBehindPositive={pace.ReadyBehindPositive} readyBehindSamples={pace.ReadyBehindSamples} readyBehindSum={pace.ReadyBehindSum} sendGapLt20={pace.SendGapLt20Us} sendGapLt100={pace.SendGapLt100Us} sendGapLt1Ms={pace.SendGapLt1Ms} sendGapGe1Ms={pace.SendGapGe1Ms} " + - $"sessionCpuSec={pace.SessionCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} sendCpuSec={client.SendCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} recvCpuSec={client.ReceiveCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"clientSndBuf={clientSendBuffer} clientRcvBuf={clientReceiveBuffer} serverSndBuf={serverSendBuffer} serverRcvBuf={serverReceiveBuffer} " + - $"tcpSegSent={tcpAfter.SegmentsSent - tcpBefore.SegmentsSent} tcpSegRecv={tcpAfter.SegmentsReceived - tcpBefore.SegmentsReceived} tcpRetrans={tcpAfter.SegmentsResent - tcpBefore.SegmentsResent} tcpErrors={tcpAfter.ErrorsReceived - tcpBefore.ErrorsReceived} tcpResets={tcpAfter.ResetsSent - tcpBefore.ResetsSent} " + - $"tlsReadSame={tls.ReadSameThread} tlsReadHop={tls.ReadHops} tlsReadSameSec={tls.ReadSameThreadSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} tlsReadHopSec={tls.ReadHopSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} tlsReadCpuSec={tls.ReadSameThreadCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"tlsWriteSame={tls.WriteSameThread} tlsWriteHop={tls.WriteHops} tlsWriteSameSec={tls.WriteSameThreadSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} tlsWriteHopSec={tls.WriteHopSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} tlsWriteCpuSec={tls.WriteSameThreadCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"clientWriteSame={client.ClientWriteSame} clientWriteHop={client.ClientWriteHop} clientWriteSameSec={client.ClientWriteSameSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteHopSec={client.ClientWriteHopSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} clientWriteCpuSec={client.ClientWriteSameCpuSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"gen0={finalSample.Gen0 - origin.Gen0} gen1={finalSample.Gen1 - origin.Gen1} gen2={finalSample.Gen2 - origin.Gen2} " + - $"allocatedBytes={allocated} heapBytes={finalSample.HeapBytes} workingSet={finalSample.WorkingSet} cpuMs={(finalSample.Cpu - origin.Cpu).TotalMilliseconds:F0} " + - $"artSize={articles[0].ArtSize} dataFrames={articles[0].Data.Length} tls={negotiatedTls}"; - } - - private static string FormatRate(Sample start, Sample end, int artSize) - { - var seconds = (end.Elapsed - start.Elapsed).TotalSeconds; - if (seconds <= 0) - { - return "seconds=0"; - } - - var enqueued = end.Enqueued - start.Enqueued; - var body = enqueued * (double)BodyBytes; - var canonical = enqueued * (double)artSize; - var wire = end.WireBytes - start.WireBytes; - return - $"seconds={seconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} enqueued={enqueued} enqueuedPerSec={(enqueued / seconds).ToString("F1", System.Globalization.CultureInfo.InvariantCulture)} " + - $"bodyGbps={(body * 8d / seconds / 1_000_000_000d).ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} artGbps={(canonical * 8d / seconds / 1_000_000_000d).ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} " + - $"wireGbps={(wire * 8d / seconds / 1_000_000_000d).ToString("F3", System.Globalization.CultureInfo.InvariantCulture)} depthStart={start.Depth} depthEnd={end.Depth}"; - } - - private static Sample Take(IngressHost host, PipelinedVatpClient client, TimeSpan elapsed) - { - using var process = Process.GetCurrentProcess(); - return new Sample( - elapsed, - host.Queue.EnqueuedCount, - host.Queue.DequeuedCount, - host.Queue.QueuedBytes, - client.WireBytes, - GC.CollectionCount(0), - GC.CollectionCount(1), - GC.CollectionCount(2), - GC.GetTotalMemory(forceFullCollection: false), - GC.GetTotalAllocatedBytes(precise: false), - process.WorkingSet64, - process.TotalProcessorTime); - } - - private static async Task WaitForDrainAsync(IngressHost host, TimeSpan timeout) - { - var deadline = Stopwatch.StartNew(); - while (host.Queue.DequeuedCount < host.Queue.EnqueuedCount) - { - if (deadline.Elapsed > timeout) - { - return false; - } - - await Task.Delay(20); - } - - return true; - } - - private static int[] ParseList(string? text, int[] fallback) - { - if (string.IsNullOrWhiteSpace(text)) - { - return fallback; - } - - return text.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .Select(static value => int.Parse(value, System.Globalization.CultureInfo.InvariantCulture)) - .ToArray(); - } - - private static int ParsePositive(string? text, int fallback) => - int.TryParse(text, out var value) && value > 0 ? value : fallback; - - private static PreparedArticle PrepareArticle(int bodyBytes, string messageId, uint streamId) - { - var body = new byte[bodyBytes]; - Array.Fill(body, (byte)'x'); - for (var line = 0; line < bodyBytes / 100; line++) - { - var offset = line * 100; - body[offset] = (byte)('A' + ((int)streamId % 26)); - body[offset + 1] = (byte)('0' + (line % 10)); - body[offset + 98] = (byte)'\r'; - body[offset + 99] = (byte)'\n'; - } - - return PrepareArticle(body, messageId, streamId); - } - - private static PreparedArticle PrepareArticle(string body, string messageId, uint streamId) => - PrepareArticle(Encoding.ASCII.GetBytes(body), messageId, streamId); - - private static PreparedArticle PrepareArticle(ReadOnlySpan body, string messageId, uint streamId) - { - var header = Encoding.ASCII.GetBytes( - "Path: peer.example\r\n" + - "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + - "Message-ID: " + messageId + "\r\n" + - "Newsgroups: alt.test\r\n" + - "From: user@example.test\r\n" + - "Subject: vatp-bench\r\n" + - "\r\n"); - var text = new byte[header.Length + body.Length]; - header.CopyTo(text); - body.CopyTo(text.AsSpan(header.Length)); - var created = ArticleRecordFactory.TryCreate(new NntpArticleParser("nntpd01.usenet.ninja"), text); - if (!created.IsAccepted) - { - throw new InvalidOperationException($"Article build failed: {created.MaterializeFailure}"); - } - - var art = created.Record.ArtData; - var frames = new List(); - var lengths = new List(); - var offset = 0; - while (offset < art.Length) - { - var length = Math.Min(MaxFramePayload, art.Length - offset); - var fin = offset + length == art.Length; - frames.Add(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeData(streamId, art.Slice(offset, length), fin))); - lengths.Add(length); - offset += length; - } - - var id = new byte[ArticleId.Length]; - created.Record.ArtId.CopyTo(id); - var store = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeStore(streamId, id)); - var meta = ArticleCanonicalTransferMeta.FromRecord(created.Record, created.SelectedDateHeaderName); - var metaFrame = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeMeta(streamId, VatpMetaCodec.Encode(in meta))); - var end = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(streamId)); - return new PreparedArticle(store, metaFrame, frames.ToArray(), lengths.ToArray(), end, created.Record.ArtSize); - } - - private readonly record struct Sample( - TimeSpan Elapsed, - long Enqueued, - long Dequeued, - long QueuedBytes, - long WireBytes, - int Gen0, - int Gen1, - int Gen2, - long HeapBytes, - long Allocated, - long WorkingSet, - TimeSpan Cpu) - { - public long Depth => Enqueued - Dequeued; - } - - private sealed record PreparedArticle(byte[] Store, byte[] Meta, byte[][] Data, int[] DataLengths, byte[] End, int ArtSize); - - private sealed class IngressHost : IAsyncDisposable - { - private readonly StorageVatpListenerService _listener; - private readonly CancellationTokenSource _drainCts = new(); - private readonly Task _drain; - - private IngressHost(StorageVatpListenerService listener, AcceptedArticleQueue queue, int port) - { - _listener = listener; - Queue = queue; - Port = port; - _drain = Task.Run(() => DrainAsync(queue, _drainCts.Token)); - } - - public AcceptedArticleQueue Queue { get; } - - public int Port { get; } - - public static async Task StartAsync() - { - var port = StorageServerTestOptions.GetFreeTcpPort(); - var readiness = new AcmeCertificateReadiness(); - readiness.MarkReady(); - var queue = new AcceptedArticleQueue(); - var listener = new StorageVatpListenerService( - CreateRuntime(port), - TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, - readiness, - NullLogger.Instance, - queue, - new StoreAssemblyAdmission(1024)); - await listener.StartAsync(CancellationToken.None); - return new IngressHost(listener, queue, port); - } - - public async ValueTask DisposeAsync() - { - await _listener.DisposeAsync(); - await _drainCts.CancelAsync(); - try - { - await _drain.WaitAsync(TimeSpan.FromSeconds(5)); - } - catch (TimeoutException ex) - { - ExpectedException.Ignore(ex); - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - - _drainCts.Dispose(); - } - - private static async Task DrainAsync(AcceptedArticleQueue queue, CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - try - { - var delivery = await queue.DequeueAsync(cancellationToken).ConfigureAwait(false); - if (!delivery.HasArticle) - { - break; - } - } - catch (OperationCanceledException) - { - break; - } - } - } - - private static StorageServerRuntimeOptions CreateRuntime(int tlsPort) - { - var identity = StorageServerTestOptions.CreateValid(); - identity.BindAddress = ["127.0.0.1"]; - identity.BindPortTls = tlsPort; - var acme = StorageServerTestOptions.CreateValidAcme(identity); - acme.BindAddress = ["127.0.0.1"]; - acme.BindPortTls = tlsPort; - return StorageServerRuntimeOptionsFactory.Create(identity, acme); - } - } - - private sealed class PipelinedVatpClient : IAsyncDisposable - { - private static int _opened; - - private readonly TcpClient _tcp; - private readonly SslStream _ssl; - private readonly Slot[] _slots; - private readonly Task _reader; - private readonly SemaphoreSlim _signal = new(0); - private readonly TaskCompletionSource _hello = new(TaskCreationOptions.RunContinuationsAsynchronously); - private readonly CancellationTokenSource _dispose = new(); - private readonly byte[] _window = new byte[VatpProtocol.WindowPayloadLength]; - private readonly byte[] _outcome = new byte[1]; - private int _stopNew; - private long _submitted; - private long _succeeded; - private long _failed; - private long _wireBytes; - private long _creditWaitTicks; - private long _resultWaitTicks; - private long _blockedTicks; - private long _writeTicks; - private long _clientWriteSame; - private long _clientWriteHop; - private long _clientWriteHopTicks; - private long _clientWriteSameTicks; - private long _clientWriteSameCpu; - private long _sendCpu100Ns; - private long _recvCpu100Ns; - private int _afterLoopWait; - private int _peakInFlight; - private Exception? _fault; - - private PipelinedVatpClient(TcpClient tcp, SslStream ssl, PreparedArticle[] articles) - { - _tcp = tcp; - _ssl = ssl; - _slots = new Slot[articles.Length]; - for (var i = 0; i < articles.Length; i++) - { - _slots[i] = new Slot(articles[i]) - { - Credit = InitialWindowBytes, - }; - } - - _reader = ReadLoopAsync(); - } - - public int ConnectionsOpened { get; private set; } - - public int PeakInFlight => Volatile.Read(ref _peakInFlight); - - public long Submitted => Volatile.Read(ref _submitted); - - public long Succeeded => Volatile.Read(ref _succeeded); - - public long Failed => Volatile.Read(ref _failed); - - public long WireBytes => Volatile.Read(ref _wireBytes); - - public double CreditWaitSeconds => Volatile.Read(ref _creditWaitTicks) / (double)Stopwatch.Frequency; - - public double ResultWaitSeconds => Volatile.Read(ref _resultWaitTicks) / (double)Stopwatch.Frequency; - - public double BlockedSeconds => Volatile.Read(ref _blockedTicks) / (double)Stopwatch.Frequency; - - public double WriteSeconds => Volatile.Read(ref _writeTicks) / (double)Stopwatch.Frequency; - - public double SendCpuSeconds => Volatile.Read(ref _sendCpu100Ns) / 10_000_000d; - - public double ReceiveCpuSeconds => Volatile.Read(ref _recvCpu100Ns) / 10_000_000d; - - public long ClientWriteSame => Volatile.Read(ref _clientWriteSame); - - public long ClientWriteHop => Volatile.Read(ref _clientWriteHop); - - public double ClientWriteSameSeconds => Volatile.Read(ref _clientWriteSameTicks) / (double)Stopwatch.Frequency; - - public double ClientWriteHopSeconds => Volatile.Read(ref _clientWriteHopTicks) / (double)Stopwatch.Frequency; - - public double ClientWriteSameCpuSeconds => Volatile.Read(ref _clientWriteSameCpu) / 10_000_000d; - - public int SendBuffer => _tcp.Client.SendBufferSize; - - public int ReceiveBuffer => _tcp.Client.ReceiveBufferSize; - - public string NegotiatedTls => $"{_ssl.SslProtocol}/{_ssl.NegotiatedCipherSuite}"; - - public static void ResetConnectionCount() => Interlocked.Exchange(ref _opened, 0); - - public static async Task ConnectAsync(int port, PreparedArticle[] articles) - { - var opened = Interlocked.Increment(ref _opened); - if (opened != 1) - { - throw new InvalidOperationException($"Benchmark opened TCP connection {opened}; exactly one is required."); - } - - var tcp = new TcpClient { NoDelay = true }; - tcp.Client.SendBufferSize = 1 << 20; - tcp.Client.ReceiveBufferSize = 1 << 20; - await tcp.ConnectAsync(IPAddress.Loopback, port); - var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false); - await ssl.AuthenticateAsClientAsync( - new SslClientAuthenticationOptions - { - TargetHost = "localhost", - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - RemoteCertificateValidationCallback = static (_, _, _, _) => true, - }, - CancellationToken.None); - var client = new PipelinedVatpClient(tcp, ssl, articles) - { - ConnectionsOpened = opened, - }; - await client.WriteAsync(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeHello(VatpProtocol.DefaultMaxFramePayload))); - using var helloTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - await client._hello.Task.WaitAsync(helloTimeout.Token); - if (client._fault is not null) - { - throw client._fault; - } - - return client; - } - - public void StopStarting() => Volatile.Write(ref _stopNew, 1); - - public async Task RunAsync(int? articleLimit, CancellationToken cancellationToken) - { - var inFlight = 0; - var cpuStart = ThreadCpuTimes.Capture(); - try - { - while (true) - { - if (_fault is not null) - { - throw _fault; - } - - var progressed = false; - var waitingCredit = false; - for (var i = 0; i < _slots.Length; i++) - { - var slot = _slots[i]; - var results = Volatile.Read(ref slot.Results); - if (slot.Phase == SlotPhase.WaitingResult && results != slot.ResultsSeen) - { - Interlocked.Add(ref _resultWaitTicks, Stopwatch.GetTimestamp() - slot.ResultWaitStart); - slot.ResultsSeen = results; - slot.Phase = SlotPhase.Idle; - slot.DataIndex = 0; - Volatile.Write(ref slot.Credit, InitialWindowBytes); - inFlight--; - progressed = true; - } - - if (slot.Phase == SlotPhase.Idle) - { - if (Volatile.Read(ref _stopNew) == 1 || (articleLimit is int limit && Volatile.Read(ref _submitted) >= limit)) - { - continue; - } - - await WriteAsync(slot.Article.Store, i); - await WriteAsync(slot.Article.Meta, i); - slot.Phase = SlotPhase.Sending; - slot.DataIndex = 0; - inFlight++; - UpdatePeak(inFlight); - Interlocked.Increment(ref _submitted); - progressed = true; - continue; - } - - if (slot.Phase != SlotPhase.Sending) - { - continue; - } - - if (slot.DataIndex < slot.Article.Data.Length) - { - var need = slot.Article.DataLengths[slot.DataIndex]; - if (!TryTakeCredit(slot, need)) - { - waitingCredit = true; - continue; - } - - await WriteAsync(slot.Article.Data[slot.DataIndex], i); - slot.DataIndex++; - progressed = true; - continue; - } - - await WriteAsync(slot.Article.End, i); - slot.ResultWaitStart = Stopwatch.GetTimestamp(); - slot.Phase = SlotPhase.WaitingResult; - progressed = true; - } - - if (inFlight == 0 && (Volatile.Read(ref _stopNew) == 1 || (articleLimit is int done && Volatile.Read(ref _submitted) >= done))) - { - return; - } - - if (progressed) - { - continue; - } - - var blocked = Stopwatch.GetTimestamp(); - await _signal.WaitAsync(cancellationToken); - var waited = Stopwatch.GetTimestamp() - blocked; - Interlocked.Add(ref _blockedTicks, waited); - Volatile.Write(ref _afterLoopWait, 1); - if (waitingCredit) - { - Interlocked.Add(ref _creditWaitTicks, waited); - } - } - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - StopStarting(); - while (inFlight > 0 && _fault is null) - { - await _signal.WaitAsync(CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(10)); - for (var i = 0; i < _slots.Length; i++) - { - var slot = _slots[i]; - if (slot.Phase == SlotPhase.WaitingResult && Volatile.Read(ref slot.Results) != slot.ResultsSeen) - { - slot.ResultsSeen = slot.Results; - slot.Phase = SlotPhase.Idle; - inFlight--; - } - } - } - } - finally - { - var cpuEnd = ThreadCpuTimes.Capture(); - Interlocked.Add(ref _sendCpu100Ns, (cpuEnd.Kernel - cpuStart.Kernel) + (cpuEnd.User - cpuStart.User)); - } - } - - private int _disposedClient; - - public async ValueTask DisposeAsync() - { - if (Interlocked.Exchange(ref _disposedClient, 1) == 1) - { - return; - } - - await _dispose.CancelAsync(); - try - { - await _reader; - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - catch (IOException ex) - { - ExpectedException.Ignore(ex); - } - - await _ssl.DisposeAsync(); - _tcp.Dispose(); - _dispose.Dispose(); - _signal.Dispose(); - } - - private void UpdatePeak(int inFlight) - { - int observed; - while ((observed = Volatile.Read(ref _peakInFlight)) < inFlight) - { - if (Interlocked.CompareExchange(ref _peakInFlight, inFlight, observed) == observed) - { - return; - } - } - } - - private static bool TryTakeCredit(Slot slot, int need) - { - while (true) - { - var current = Volatile.Read(ref slot.Credit); - if (current < need) - { - return false; - } - - if (Interlocked.CompareExchange(ref slot.Credit, current - need, current) == current) - { - return true; - } - } - } - - private async Task WriteAsync(byte[] frame, int slotIndex = -1) - { - var readyBehind = slotIndex < 0 ? 0 : CountSendableOthers(slotIndex); - var afterLoopWait = Interlocked.Exchange(ref _afterLoopWait, 0) == 1; - StorageVatpSession.NoteClientWriteStart(readyBehind, afterLoopWait); - var threadId = ThreadCpuTimes.CurrentThreadId; - var cpu = ThreadCpuTimes.Capture(); - var started = Stopwatch.GetTimestamp(); - await _ssl.WriteAsync(frame); - var elapsed = Stopwatch.GetTimestamp() - started; - Interlocked.Add(ref _writeTicks, elapsed); - Interlocked.Add(ref _wireBytes, frame.Length); - StorageVatpSession.NoteClientWriteEnd(elapsed); - if (threadId != 0 && threadId == ThreadCpuTimes.CurrentThreadId) - { - var after = ThreadCpuTimes.Capture(); - Interlocked.Increment(ref _clientWriteSame); - Interlocked.Add(ref _clientWriteSameTicks, elapsed); - var used = (after.Kernel - cpu.Kernel) + (after.User - cpu.User); - if (used > 0) - { - Interlocked.Add(ref _clientWriteSameCpu, used); - } - } - else - { - Interlocked.Increment(ref _clientWriteHop); - Interlocked.Add(ref _clientWriteHopTicks, elapsed); - } - } - - private int CountSendableOthers(int except) - { - var ready = 0; - for (var i = 0; i < _slots.Length; i++) - { - if (i == except) - { - continue; - } - - var slot = _slots[i]; - if (slot.Phase != SlotPhase.Sending || slot.DataIndex >= slot.Article.Data.Length) - { - continue; - } - - if (Volatile.Read(ref slot.Credit) >= slot.Article.DataLengths[slot.DataIndex]) - { - ready++; - } - } - - return ready; - } - - private async Task ReadLoopAsync() - { - var buffer = new byte[256 * 1024]; - var filled = 0; - var cpuStart = ThreadCpuTimes.Capture(); - try - { - while (!_dispose.IsCancellationRequested) - { - var read = await _ssl.ReadAsync(buffer.AsMemory(filled), _dispose.Token); - if (read == 0) - { - Fault(new IOException("VATP peer closed the connection.")); - _signal.Release(); - return; - } - - filled += read; - var offset = 0; - while (true) - { - var parsed = VatpFrameParser.ParseOneFrame( - new ReadOnlySequence(buffer, offset, filled - offset), - VatpProtocol.DefaultMaxFramePayload); - if (parsed.Status == VatpFrameParseStatus.Incomplete) - { - break; - } - - if (parsed.Status != VatpFrameParseStatus.Success || parsed.Frame is null) - { - Fault(new InvalidOperationException($"VATP parse failed: {parsed.Error}")); - _signal.Release(); - return; - } - - var frame = parsed.Frame.Value; - var streamId = frame.Header.StreamId; - switch (frame.Header.Type) - { - case VatpFrameType.Hello: - _hello.TrySetResult(); - break; - case VatpFrameType.Window: - frame.Payload.CopyTo(_window); - if (streamId < 1 || streamId > (uint)_slots.Length || !VatpControlPayload.TryDecodeWindow(_window, out var add)) - { - Fault(new InvalidOperationException("Invalid WINDOW.")); - _signal.Release(); - return; - } - - Interlocked.Add(ref _slots[(int)streamId - 1].Credit, add); - _signal.Release(); - break; - case VatpFrameType.Result: - frame.Payload.CopyTo(_outcome); - if (streamId < 1 || streamId > (uint)_slots.Length) - { - Fault(new InvalidOperationException($"RESULT for unknown stream {streamId}.")); - _signal.Release(); - return; - } - - if (_outcome[0] == VatpStoreOutcome.Accepted) - { - Interlocked.Increment(ref _succeeded); - } - else - { - Interlocked.Increment(ref _failed); - } - - Interlocked.Increment(ref _slots[(int)streamId - 1].Results); - _signal.Release(); - break; - case VatpFrameType.Fail: - Interlocked.Increment(ref _failed); - Fault(new InvalidOperationException($"VATP FAIL on stream {streamId}.")); - if (streamId >= 1 && (int)streamId <= _slots.Length) - { - Interlocked.Increment(ref _slots[(int)streamId - 1].Results); - } - - _signal.Release(); - return; - } - - offset += checked((int)parsed.ConsumedBytes); - } - - if (offset > 0) - { - Buffer.BlockCopy(buffer, offset, buffer, 0, filled - offset); - filled -= offset; - } - } - } - catch (OperationCanceledException ex) when (_dispose.IsCancellationRequested) - { - ExpectedException.Ignore(ex); - } - catch (Exception ex) - { - Fault(ex); - _signal.Release(); - } - finally - { - var cpuEnd = ThreadCpuTimes.Capture(); - Interlocked.Add(ref _recvCpu100Ns, (cpuEnd.Kernel - cpuStart.Kernel) + (cpuEnd.User - cpuStart.User)); - } - } - - private void Fault(Exception exception) => Interlocked.CompareExchange(ref _fault, exception, null); - - private enum SlotPhase - { - Idle = 0, - Sending = 3, - WaitingResult = 4, - } - - private sealed class Slot(PreparedArticle article) - { - public PreparedArticle Article { get; } = article; - - public SlotPhase Phase { get; set; } - - public int DataIndex { get; set; } - - public long Credit; - - public long Results; - - public long ResultsSeen { get; set; } - - public long ResultWaitStart { get; set; } - } - } - } -} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpThroughputBench.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpThroughputBench.cs deleted file mode 100644 index bfe47e7c..00000000 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StorageVatpThroughputBench.cs +++ /dev/null @@ -1,1293 +0,0 @@ -using System.Buffers; -using System.Buffers.Binary; -using System.Collections.Concurrent; -using System.Diagnostics; -using System.Runtime; -using System.Net; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Text; -using Microsoft.Extensions.Logging.Abstractions; -using VectorNNTPD.Common.Acme; -using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Configuration; -using VectorNNTPD.StorageService.Ingress; -using VectorNNTPD.StorageService.Listener; -using VectorNNTPD.StorageService.Tests.Fixtures; -using VectorNNTPD.StorageService.Tests.TestDoubles; -using Xunit.Abstractions; -using VectorNNTPD.StorageService; - -namespace VectorNNTPD.StorageService.Tests.Listener -{ - - /// - /// End-to-end VATP STORE benchmark. The sustained sweep runs only when - /// VECTORNNTPD_STORAGE_BENCH=1. The single-article fact always runs. - /// - public sealed class StorageVatpThroughputBench - { - public const int BodyBytes = 768_000; - private const int InitialWindowBytes = 256 * 1024; - private const int MaxFramePayload = 64 * 1024; - private const long QueueByteCap = 256L * 1024 * 1024; - private const string PathIdentity = "cache01.usenet.ninja"; - - private readonly ITestOutputHelper _output; - - public StorageVatpThroughputBench(ITestOutputHelper output) => _output = output; - - [Fact] - public async Task One_tls_article_is_accepted_and_path_processed() - { - var prepared = Prepare(Encoding.ASCII.GetBytes("body-line\r\n"), "", 1); - byte[]? rewritten = null; - await using var host = await BenchHost.StartAsync(seen => rewritten = seen.ToArray()); - await using var session = await VatpLoadSession.ConnectAsync(host.Port); - await session.SendArticlesAsync([prepared], count: 1, CancellationToken.None); - - var drained = await WaitForProcessedAsync(host, expected: 1, TimeSpan.FromSeconds(10)); - Assert.True(drained); - var processor = host.Processor ?? throw new InvalidOperationException("Path worker was not started."); - Assert.Equal(1, session.Accepted); - Assert.Equal(0, session.Rejected); - Assert.Equal(1, processor.Processed); - Assert.Equal(0, processor.Rejected); - Assert.Equal(0, host.Queue.EnqueuedCount - host.Queue.DequeuedCount); - Assert.NotNull(rewritten); - var text = Encoding.ASCII.GetString(rewritten); - Assert.Contains(PathIdentity, text, StringComparison.Ordinal); - Assert.Contains("body-line\r\n", text, StringComparison.Ordinal); - Assert.Contains("nntpd01.usenet.ninja", text, StringComparison.Ordinal); - } - - [Fact] - public async Task Sequential_pipe_accepts_each_store_before_the_next() - { - var pool = BuildPool(2); - await using var host = await BenchHost.StartAsync(rewritten: null, ingressOnly: true); - await using var session = await VatpLoadSession.ConnectAsync(host.Port); - await session.SendArticlesAsync(pool, count: 2, CancellationToken.None, pipe: 1); - - var drained = await WaitForDequeuedAsync(host, expected: 2, TimeSpan.FromSeconds(30)); - Assert.True(drained); - Assert.Equal(1, session.PipeUsed); - Assert.Equal(1, session.PipeMaxOutstanding); - Assert.Equal(2, session.Accepted); - Assert.Equal(2, session.TransfersCompleted); - Assert.Equal(0, session.Rejected); - Assert.Equal(0, session.TransfersOutstanding); - Assert.Equal(2, host.Queue.EnqueuedCount); - Assert.Equal(2, host.Queue.DequeuedCount); - } - - [Fact] - public async Task Piped_stores_on_one_connection_complete_independently() - { - var pool = BuildPool(4); - await using var host = await BenchHost.StartAsync(rewritten: null, ingressOnly: true); - await using var session = await VatpLoadSession.ConnectAsync(host.Port); - await session.SendArticlesAsync(pool, count: 4, CancellationToken.None, pipe: 4); - - var drained = await WaitForDequeuedAsync(host, expected: 4, TimeSpan.FromSeconds(30)); - Assert.True(drained); - Assert.Equal(4, session.PipeUsed); - Assert.True(session.PipeMaxOutstanding >= 2); - Assert.Equal(4, session.Accepted); - Assert.Equal(4, session.TransfersCompleted); - Assert.Equal(0, session.Rejected); - Assert.Equal(0, session.TransfersOutstanding); - Assert.Equal(4, host.Queue.EnqueuedCount); - Assert.Equal(4, host.Queue.DequeuedCount); - } - - [Fact] - public async Task Sustained_vatp_path_when_bench_enabled() - { - if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH"), "1", StringComparison.Ordinal)) - { - return; - } - - var sessions = ParseSessions(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_SESSIONS")); - var measureSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_SECONDS"), 8); - var warmupSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_WARMUP"), 2); - var pool = BuildPool(16); - var artSize = pool[0].ArtSize; - var report = new StringBuilder(); - report.AppendLine($"bodyBytes={BodyBytes} artSize={artSize} pool={pool.Length} measureSeconds={measureSeconds} warmupSeconds={warmupSeconds}"); - report.AppendLine($"runtime={Environment.Version} os={Environment.OSVersion} processors={Environment.ProcessorCount} serverGc={GCSettings.IsServerGC}"); - - foreach (var sessionCount in sessions) - { - var line = await RunLevelAsync(pool, sessionCount, warmupSeconds, measureSeconds); - report.AppendLine(line); - _output.WriteLine(line); - } - - await WriteReportAsync(report, "vatp-bench-raw.txt"); - } - - [Fact] - public async Task Ingress_enqueue_when_bench_enabled() - { - if (!string.Equals(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH"), "1", StringComparison.Ordinal)) - { - return; - } - - ThreadPool.GetMinThreads(out var workerThreads, out var ioThreads); - var minThreads = Math.Max(workerThreads, Environment.ProcessorCount * 8); - ThreadPool.SetMinThreads(minThreads, Math.Max(ioThreads, minThreads)); - var sessions = ParseSessions(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_SESSIONS")); - var measureSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_SECONDS"), 8); - var warmupSeconds = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_WARMUP"), 2); - var pipeRequested = ParsePositive(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_PIPE"), 1); - var pipe = Math.Min(pipeRequested, ArticleTransferLimits.Default.MaxStreamsPerConnection); - var initialWindow = ParseBenchWindow(Environment.GetEnvironmentVariable("VECTORNNTPD_STORAGE_BENCH_WINDOW")); - var pool = BuildPool(16); - var report = new StringBuilder(); - report.AppendLine("mode=ingress-enqueue boundary=AcceptedArticleQueue.TryEnqueue pathWorker=false"); - report.AppendLine($"threadPoolMin={minThreads}"); - report.AppendLine($"bodyBytes={BodyBytes} artSize={pool[0].ArtSize} pool={pool.Length} measureSeconds={measureSeconds} warmupSeconds={warmupSeconds} pipeRequested={pipeRequested} pipe={pipe} serverMaxStreams={ArticleTransferLimits.Default.MaxStreamsPerConnection} initialWindow={initialWindow}"); - report.AppendLine($"runtime={Environment.Version} os={Environment.OSVersion} processors={Environment.ProcessorCount} serverGc={GCSettings.IsServerGC} gcLatency={GCSettings.LatencyMode} lohCompact={GCSettings.LargeObjectHeapCompactionMode}"); - - foreach (var sessionCount in sessions) - { - var line = await RunLevelAsync(pool, sessionCount, warmupSeconds, measureSeconds, ingressOnly: true, pipe: pipe, initialWindowBytes: initialWindow); - report.AppendLine(line); - _output.WriteLine(line); - } - - await WriteReportAsync(report, "vatp-ingress-raw.txt"); - } - - private async Task WriteReportAsync(StringBuilder report, string fileName) - { - var directory = Path.GetFullPath(Path.Join( - AppContext.BaseDirectory, - "..", "..", "..", "..", "..", - ".artifacts", "experiments")); - Directory.CreateDirectory(directory); - var path = Path.Join(directory, fileName); - await File.WriteAllTextAsync(path, report.ToString()); - _output.WriteLine(path); - } - - private async Task RunLevelAsync( - Prepared[] pool, - int sessionCount, - int warmupSeconds, - int measureSeconds, - bool ingressOnly = false, - int pipe = 1, - int initialWindowBytes = InitialWindowBytes) - { - await using var host = await BenchHost.StartAsync(rewritten: null, ingressOnly: ingressOnly, initialWindowBytes: initialWindowBytes); - var sessions = new VatpLoadSession[sessionCount]; - for (var i = 0; i < sessions.Length; i++) - { - sessions[i] = await VatpLoadSession.ConnectAsync(host.Port); - } - - using var sendCts = new CancellationTokenSource(); - var senders = sessions.Select(session => session.SendArticlesAsync(pool, count: null, sendCts.Token, pipe, initialWindowBytes)).ToArray(); - var started = Stopwatch.StartNew(); - var origin = Sample.Take(host, sessions, TimeSpan.Zero); - var samples = new List(); - var capped = false; - var warmup = (Sample?)null; - var measureEnd = (Sample?)null; - var warmupStamp = 0L; - var measureStamp = 0L; - - while (started.Elapsed < TimeSpan.FromSeconds(warmupSeconds + measureSeconds)) - { - if (host.Queue.QueuedBytes > QueueByteCap) - { - capped = true; - break; - } - - samples.Add(Sample.Take(host, sessions, started.Elapsed)); - var delay = TimeSpan.FromMilliseconds(250); - var remaining = TimeSpan.FromSeconds(warmupSeconds + measureSeconds) - started.Elapsed; - if (remaining < delay) - { - delay = remaining; - } - - if (delay > TimeSpan.Zero) - { - await Task.Delay(delay); - } - - if (warmup is null && started.Elapsed >= TimeSpan.FromSeconds(warmupSeconds)) - { - warmup = Sample.Take(host, sessions, started.Elapsed); - warmupStamp = Stopwatch.GetTimestamp(); - } - } - - measureEnd = Sample.Take(host, sessions, started.Elapsed); - measureStamp = Stopwatch.GetTimestamp(); - await sendCts.CancelAsync(); - try - { - await Task.WhenAll(senders); - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - - var drained = ingressOnly - ? await WaitForDequeuedAsync(host, host.Queue.EnqueuedCount, TimeSpan.FromSeconds(30)) - : await WaitForProcessedAsync(host, host.Queue.EnqueuedCount, TimeSpan.FromSeconds(60)); - var drainedAt = Sample.Take(host, sessions, started.Elapsed); - double creditWait = 0; - double slotWait = 0; - var pipeUsed = 0; - var pipeMax = 0; - var incomplete = 0L; - var initialCredit = 0; - var windowFrames = 0; - long windowBytes = 0; - var windowAddMin = 0; - var windowAddMax = 0; - var cipher = sessions.Length == 0 ? "none" : sessions[0].NegotiatedCipher; - var latencies = new List(); - foreach (var session in sessions) - { - creditWait += session.CreditWaitSeconds; - slotWait += session.SlotWaitSeconds; - pipeUsed = Math.Max(pipeUsed, session.PipeUsed); - pipeMax = Math.Max(pipeMax, session.PipeMaxOutstanding); - incomplete += session.TransfersOutstanding; - initialCredit = session.InitialCredit; - windowFrames += session.WindowFrames; - windowBytes += session.WindowBytes; - if (session.WindowFrames > 0) - { - windowAddMin = windowAddMin == 0 - ? session.WindowAddMin - : Math.Min(windowAddMin, session.WindowAddMin); - } - - windowAddMax = Math.Max(windowAddMax, session.WindowAddMax); - session.CopySteadyLatencies(warmupStamp, measureStamp, latencies); - await session.DisposeAsync(); - } - - var (resultMedianUs, resultP95Us, resultSamples) = PercentileMicroseconds(latencies); - - var cold = samples.Count >= 2 ? Rate(samples[0], samples[Math.Min(4, samples.Count - 1)], pool[0].ArtSize) : "n/a"; - var steady = warmup is null - ? "n/a" - : Rate(warmup.Value, measureEnd.Value, pool[0].ArtSize); - var steadyAllocated = warmup is null ? 0 : measureEnd.Value.AllocatedBytes - warmup.Value.AllocatedBytes; - var steadyPauseMs = warmup is null - ? 0 - : (measureEnd.Value.GcPause - warmup.Value.GcPause).TotalMilliseconds; - var depthMax = measureEnd.Value.Depth; - foreach (var sample in samples) - { - if (sample.Depth > depthMax) - { - depthMax = sample.Depth; - } - } - - return - $"sessions={sessionCount} capped={capped} drained={drained} cold=[{cold}] steady=[{steady}] " + - $"depthMax={depthMax} depthAtMeasureEnd={measureEnd.Value.Depth} depthAfterDrain={drainedAt.Depth} " + - $"enqueued={drainedAt.Enqueued} dequeued={drainedAt.Dequeued} processed={drainedAt.Processed} rejectedPath={drainedAt.PathRejected} " + - $"accepted={drainedAt.Accepted} rejected={drainedAt.Rejected} " + - $"gen0={drainedAt.Gen0 - origin.Gen0} gen1={drainedAt.Gen1 - origin.Gen1} gen2={drainedAt.Gen2 - origin.Gen2} " + - $"allocatedBytes={drainedAt.AllocatedBytes - origin.AllocatedBytes} steadyAllocatedBytes={steadyAllocated} " + - $"gcPauseMs={(drainedAt.GcPause - origin.GcPause).TotalMilliseconds:F1} steadyGcPauseMs={steadyPauseMs:F1} " + - $"cipher={cipher} " + - $"heapBytes={drainedAt.HeapBytes} workingSet={drainedAt.WorkingSet} " + - $"cpuMs={(drainedAt.Cpu - origin.Cpu).TotalMilliseconds:F0} workerSec={host.Processor?.WorkerSeconds ?? 0:F3} ingressOnly={ingressOnly} " + - $"pipe={pipeUsed} pipeMax={pipeMax} initialCredit={initialCredit} windowFrames={windowFrames} windowBytes={windowBytes} windowAddMin={windowAddMin} windowAddMax={windowAddMax} creditWaitSec={creditWait:F3} slotWaitSec={slotWait:F3} " + - $"resultN={resultSamples} resultMedianUs={resultMedianUs:F0} resultP95Us={resultP95Us:F0} incomplete={incomplete}"; - } - - private static (double MedianUs, double P95Us, int Count) PercentileMicroseconds(List ticks) - { - if (ticks.Count == 0) - { - return (0, 0, 0); - } - - ticks.Sort(); - var median = ticks[ticks.Count / 2]; - var p95Index = (int)((ticks.Count - 1) * 0.95d); - var p95 = ticks[p95Index]; - var scale = 1_000_000d / Stopwatch.Frequency; - return (median * scale, p95 * scale, ticks.Count); - } - - private static string Rate(Sample start, Sample end, int artSize) - { - var seconds = (end.Elapsed - start.Elapsed).TotalSeconds; - if (seconds <= 0) - { - return "seconds=0"; - } - - var accepted = end.Accepted - start.Accepted; - var enqueued = end.Enqueued - start.Enqueued; - var processed = end.Processed - start.Processed; - var wire = end.WireBytes - start.WireBytes; - var payload = enqueued * (double)BodyBytes; - var canonical = enqueued * (double)artSize; - var processedPayload = processed * (double)BodyBytes; - return - $"seconds={seconds:F3} enqueuedPerSec={enqueued / seconds:F1} acceptedPerSec={accepted / seconds:F1} processedPerSec={processed / seconds:F1} " + - $"enqueueBodyGbps={Gbps(payload, seconds):F3} enqueueArtGbps={Gbps(canonical, seconds):F3} " + - $"processedBodyGbps={Gbps(processedPayload, seconds):F3} " + - $"wireGbps={Gbps(wire, seconds):F3} " + - $"depthStart={start.Depth} depthEnd={end.Depth} queuedBytesEnd={end.QueuedBytes}"; - } - - private static double Gbps(double bytes, double seconds) => bytes * 8d / seconds / 1_000_000_000d; - - private static async Task WaitForProcessedAsync(BenchHost host, long expected, TimeSpan timeout) - { - var deadline = Stopwatch.StartNew(); - var processor = host.Processor ?? throw new InvalidOperationException("Path worker was not started."); - while (processor.Processed + processor.Rejected < expected) - { - if (deadline.Elapsed > timeout) - { - return false; - } - - await Task.Delay(20); - } - - return true; - } - - private static async Task WaitForDequeuedAsync(BenchHost host, long expected, TimeSpan timeout) - { - var deadline = Stopwatch.StartNew(); - while (host.Queue.DequeuedCount < expected) - { - if (deadline.Elapsed > timeout) - { - return false; - } - - await Task.Delay(20); - } - - return true; - } - - private static int[] ParseSessions(string? text) - { - if (string.IsNullOrWhiteSpace(text)) - { - return [1, 2, 4, 8, 16]; - } - - return text.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) - .Select(static value => int.Parse(value, System.Globalization.CultureInfo.InvariantCulture)) - .ToArray(); - } - - private static int ParsePositive(string? text, int fallback) => - int.TryParse(text, out var value) && value > 0 ? value : fallback; - - /// - /// Parses VECTORNNTPD_STORAGE_BENCH_WINDOW. Unset means . - /// Any other value throws. Values are not clamped. - /// - internal static int ParseBenchWindow(string? text) - { - const int Kib = 1024; - ReadOnlySpan allowed = [256 * Kib, 512 * Kib, 1024 * Kib, 2 * 1024 * Kib, 4 * 1024 * Kib]; - if (string.IsNullOrWhiteSpace(text)) - { - var production = ArticleTransferLimits.Default.InitialStreamWindowBytes; - if (!allowed.Contains(production)) - { - throw new InvalidOperationException( - $"Production initial window {production} is not one of the benchmark sizes."); - } - - return production; - } - - if (!int.TryParse(text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out var value) - || !allowed.Contains(value)) - { - throw new InvalidOperationException( - "VECTORNNTPD_STORAGE_BENCH_WINDOW must be 262144, 524288, 1048576, 2097152, or 4194304 bytes."); - } - - if (value > ArticleTransferLimits.Default.MaxStreamCreditBytes) - { - throw new InvalidOperationException( - $"Initial window {value} exceeds MaxStreamCreditBytes {ArticleTransferLimits.Default.MaxStreamCreditBytes}."); - } - - return value; - } - - private static Prepared[] BuildPool(int count) - { - var pool = new Prepared[count]; - var body = new byte[BodyBytes]; - for (var article = 0; article < count; article++) - { - Array.Fill(body, (byte)'x'); - for (var line = 0; line < BodyBytes / 100; line++) - { - var offset = line * 100; - body[offset] = (byte)('A' + (article % 26)); - body[offset + 1] = (byte)('0' + (line % 10)); - body[offset + 98] = (byte)'\r'; - body[offset + 99] = (byte)'\n'; - } - - var id = $""; - pool[article] = Prepare(body, id, streamId: 1); - } - - return pool; - } - - private static Prepared Prepare(ReadOnlySpan body, string messageId, uint streamId) - { - var header = Encoding.ASCII.GetBytes( - "Path: peer.example\r\n" + - "Date: Fri, 23 Aug 2024 07:30:10 +0000\r\n" + - "Message-ID: " + messageId + "\r\n" + - "Newsgroups: alt.test\r\n" + - "From: user@example.test\r\n" + - "Subject: vatp-bench\r\n" + - "\r\n"); - var text = new byte[header.Length + body.Length]; - header.CopyTo(text); - body.CopyTo(text.AsSpan(header.Length)); - var parser = new NntpArticleParser("nntpd01.usenet.ninja"); - var created = ArticleRecordFactory.TryCreate(parser, text); - if (!created.IsAccepted) - { - throw new InvalidOperationException($"Article build failed: {created.MaterializeFailure}"); - } - - var art = created.Record.ArtData; - var frames = new List(); - var lengths = new List(); - var offset = 0; - while (offset < art.Length) - { - var length = Math.Min(MaxFramePayload, art.Length - offset); - var fin = offset + length == art.Length; - frames.Add(VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeData(streamId, art.Slice(offset, length), fin))); - lengths.Add(length); - offset += length; - } - - var id = new byte[ArticleId.Length]; - created.Record.ArtId.CopyTo(id); - var store = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeStore(streamId, id)); - var meta = ArticleCanonicalTransferMeta.FromRecord(created.Record, created.SelectedDateHeaderName); - var metaFrame = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeMeta(streamId, VatpMetaCodec.Encode(in meta))); - var end = VatpFrameEncoder.ToSingleBuffer(VatpFrameEncoder.EncodeEnd(streamId)); - var wire = store.Length + metaFrame.Length + end.Length + frames.Sum(static frame => frame.Length); - return new Prepared(store, metaFrame, frames.ToArray(), lengths.ToArray(), end, created.Record.ArtSize, wire); - } - - private readonly record struct Sample( - TimeSpan Elapsed, - long Enqueued, - long Dequeued, - long QueuedBytes, - long Processed, - long PathRejected, - long Accepted, - long Rejected, - long WireBytes, - int Gen0, - int Gen1, - int Gen2, - long AllocatedBytes, - TimeSpan GcPause, - long HeapBytes, - long WorkingSet, - TimeSpan Cpu) - { - public long Depth => Enqueued - Dequeued; - - public static Sample Take(BenchHost host, IReadOnlyList sessions, TimeSpan elapsed) - { - long accepted = 0; - long rejected = 0; - long wire = 0; - foreach (var session in sessions) - { - accepted += session.Accepted; - rejected += session.Rejected; - wire += session.WireBytes; - } - - using var process = Process.GetCurrentProcess(); - return new Sample( - elapsed, - host.Queue.EnqueuedCount, - host.Queue.DequeuedCount, - host.Queue.QueuedBytes, - host.Processor?.Processed ?? 0, - host.Processor?.Rejected ?? 0, - accepted, - rejected, - wire, - GC.CollectionCount(0), - GC.CollectionCount(1), - GC.CollectionCount(2), - GC.GetTotalAllocatedBytes(precise: false), - GC.GetTotalPauseDuration(), - GC.GetTotalMemory(forceFullCollection: false), - process.WorkingSet64, - process.TotalProcessorTime); - } - } - - private sealed record Prepared( - byte[] Store, - byte[] Meta, - byte[][] Data, - int[] DataLengths, - byte[] End, - int ArtSize, - int WireBytes); - - private sealed class CountingProcessor : IAcceptedArticleProcessor - { - private readonly AcceptedArticlePathProcessor _inner; - private long _processed; - private long _rejected; - private long _ticks; - - public CountingProcessor(Action>? rewritten) - { - _inner = new AcceptedArticlePathProcessor(PathIdentity, NullLogger.Instance, rewritten); - } - - public long Processed => Volatile.Read(ref _processed); - - public long Rejected => Volatile.Read(ref _rejected); - - public double WorkerSeconds => Volatile.Read(ref _ticks) / (double)Stopwatch.Frequency; - - public void EnsureReady() => _inner.EnsureReady(); - - public ValueTask ProcessAsync( - AcceptedArticle article, - CancellationToken cancellationToken) - { - var started = Stopwatch.GetTimestamp(); - var result = _inner.ProcessAsync(article, cancellationToken).GetAwaiter().GetResult(); - Interlocked.Add(ref _ticks, Stopwatch.GetTimestamp() - started); - if (result != AcceptedArticleProcessResult.Completed) - { - Interlocked.Increment(ref _rejected); - return ValueTask.FromResult(result); - } - - Interlocked.Increment(ref _processed); - return ValueTask.FromResult(result); - } - } - - internal static Task StartPlacementIngressHostAsync() => - PlacementIngressHost.OpenAsync(); - - internal sealed class PlacementIngressHost : IAsyncDisposable - { - private readonly BenchHost _host; - - private PlacementIngressHost(BenchHost host) => _host = host; - - public int Port => _host.Port; - - public AcceptedArticleQueue Queue => _host.Queue; - - public static async Task OpenAsync() => - new(await BenchHost.StartAsync(rewritten: null, ingressOnly: true).ConfigureAwait(false)); - - public ValueTask DisposeAsync() => _host.DisposeAsync(); - } - - private sealed class BenchHost : IAsyncDisposable - { - private readonly StorageVatpListenerService _listener; - private readonly AcceptedArticleWorker? _worker; - private readonly CancellationTokenSource? _drainCts; - private readonly Task? _drain; - - private BenchHost( - StorageVatpListenerService listener, - AcceptedArticleWorker? worker, - AcceptedArticleQueue queue, - CountingProcessor? processor, - int port, - CancellationTokenSource? drainCts, - Task? drain) - { - _listener = listener; - _worker = worker; - _drainCts = drainCts; - _drain = drain; - Queue = queue; - Processor = processor; - Port = port; - } - - public AcceptedArticleQueue Queue { get; } - - public CountingProcessor? Processor { get; } - - public int Port { get; } - - public static async Task StartAsync( - Action>? rewritten, - bool ingressOnly = false, - int? initialWindowBytes = null) - { - var port = StorageServerTestOptions.GetFreeTcpPort(); - var readiness = new AcmeCertificateReadiness(); - readiness.MarkReady(); - var queue = new AcceptedArticleQueue(); - CountingProcessor? processor = ingressOnly ? null : new CountingProcessor(rewritten); - AcceptedArticleWorker? worker = processor is null - ? null - : new AcceptedArticleWorker(queue, processor, NullLogger.Instance); - ArticleTransferLimits? limits = initialWindowBytes is null - ? null - : new ArticleTransferLimits { InitialStreamWindowBytes = initialWindowBytes.Value }; - var listener = new StorageVatpListenerService( - CreateRuntime(port), - TestListenerCertificates.CreatePublishedProvider(), - NullStorageArticleOpenBoundary.Instance, - readiness, - NullLogger.Instance, - queue, - new StoreAssemblyAdmission(1024), - limits); - await listener.StartAsync(CancellationToken.None); - if (worker is not null) - { - await worker.StartAsync(CancellationToken.None); - return new BenchHost(listener, worker, queue, processor, port, drainCts: null, drain: null); - } - - var drainCts = new CancellationTokenSource(); - var drain = Task.Run(() => DrainAsync(queue, drainCts.Token)); - return new BenchHost(listener, worker: null, queue, processor: null, port, drainCts, drain); - } - - public async ValueTask DisposeAsync() - { - await _listener.DisposeAsync(); - if (_drainCts is not null) - { - await _drainCts.CancelAsync(); - if (_drain is not null) - { - try - { - await _drain.WaitAsync(TimeSpan.FromSeconds(5)); - } - catch (TimeoutException ex) - { - ExpectedException.Ignore(ex); - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - } - - _drainCts.Dispose(); - } - - if (_worker is not null) - { - using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - await _worker.StopAsync(stop.Token); - } - } - - private static async Task DrainAsync(AcceptedArticleQueue queue, CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - AcceptedArticleDelivery delivery; - try - { - delivery = await queue.DequeueAsync(cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - break; - } - - if (!delivery.HasArticle) - { - break; - } - } - } - - private static StorageServerRuntimeOptions CreateRuntime(int tlsPort) - { - var identity = StorageServerTestOptions.CreateValid(); - identity.BindAddress = ["127.0.0.1"]; - identity.BindPortTls = tlsPort; - var acme = StorageServerTestOptions.CreateValidAcme(identity); - acme.BindAddress = ["127.0.0.1"]; - acme.BindPortTls = tlsPort; - return StorageServerRuntimeOptionsFactory.Create(identity, acme); - } - } - - private sealed class VatpLoadSession : IAsyncDisposable - { - private readonly SslStream _ssl; - private readonly TcpClient _tcp; - private readonly Task _reader; - private readonly SemaphoreSlim _helloGate = new(0); - private readonly SemaphoreSlim _doneGate = new(0); - private readonly ConcurrentQueue _freeSlots = new(); - private readonly List _latencies = []; - private readonly object _latencyLock = new(); - private readonly CancellationTokenSource _dispose = new(); - private readonly byte[] _window = new byte[VatpProtocol.WindowPayloadLength]; - private readonly byte[] _outcome = new byte[1]; - private TransferSlot[]? _slots; - private SemaphoreSlim? _slotGate; - private long _accepted; - private long _rejected; - private long _wireBytes; - private long _creditWaitTicks; - private long _slotWaitTicks; - private long _outstanding; - private long _started; - private long _completed; - private int _pipeUsed; - private int _pipeMaxOutstanding; - private int _initialCredit; - private int _windowFrames; - private long _windowBytes; - private int _windowAddMin = int.MaxValue; - private int _windowAddMax; - private Exception? _fault; - - private VatpLoadSession(TcpClient tcp, SslStream ssl) - { - _tcp = tcp; - _ssl = ssl; - _reader = ReadLoopAsync(); - } - - public long Accepted => Volatile.Read(ref _accepted); - - public long Rejected => Volatile.Read(ref _rejected); - - public long WireBytes => Volatile.Read(ref _wireBytes); - - public double CreditWaitSeconds => Volatile.Read(ref _creditWaitTicks) / (double)Stopwatch.Frequency; - - /// Gets time the single writer blocked because every stream slot was awaiting RESULT. - public double SlotWaitSeconds => Volatile.Read(ref _slotWaitTicks) / (double)Stopwatch.Frequency; - - public int PipeUsed => _pipeUsed; - - public int PipeMaxOutstanding => Volatile.Read(ref _pipeMaxOutstanding); - - public long TransfersCompleted => Volatile.Read(ref _completed); - - public long TransfersOutstanding => Math.Max(0, Volatile.Read(ref _started) - Volatile.Read(ref _completed)); - - public int InitialCredit => _initialCredit; - - public int WindowFrames => _windowFrames; - - public long WindowBytes => _windowBytes; - - public int WindowAddMin => _windowFrames == 0 ? 0 : _windowAddMin; - - public int WindowAddMax => _windowAddMax; - - public string NegotiatedCipher => - $"{_ssl.SslProtocol}/{_ssl.NegotiatedCipherSuite}"; - - public static async Task ConnectAsync(int port) - { - var tcp = new TcpClient(); - tcp.NoDelay = true; - tcp.Client.SendBufferSize = 1 << 20; - tcp.Client.ReceiveBufferSize = 1 << 20; - await tcp.ConnectAsync(IPAddress.Loopback, port); - var ssl = new SslStream(tcp.GetStream(), leaveInnerStreamOpen: false); - await ssl.AuthenticateAsClientAsync( - new SslClientAuthenticationOptions - { - TargetHost = "localhost", - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13, - RemoteCertificateValidationCallback = static (_, _, _, _) => true, - }, - CancellationToken.None); - var session = new VatpLoadSession(tcp, ssl); - await session.WriteAsync(VatpFrameEncoder.ToSingleBuffer( - VatpFrameEncoder.EncodeHello(VatpProtocol.DefaultMaxFramePayload))); - if (!await session._helloGate.WaitAsync(TimeSpan.FromSeconds(5))) - { - throw new TimeoutException("StorageServer did not answer HELLO."); - } - - if (session._fault is not null) - { - throw session._fault; - } - - return session; - } - - public async Task SendArticlesAsync( - IReadOnlyList pool, - int? count, - CancellationToken cancellationToken, - int pipe = 1, - int initialCredit = InitialWindowBytes) - { - if (initialCredit < 1 || initialCredit > ArticleTransferLimits.Default.MaxStreamCreditBytes) - { - throw new ArgumentOutOfRangeException( - nameof(initialCredit), - initialCredit, - "Initial credit must be positive and no greater than MaxStreamCreditBytes."); - } - - _initialCredit = initialCredit; - if (_slots is not null) - { - throw new InvalidOperationException("A load session sends one article loop."); - } - - var limit = ArticleTransferLimits.Default.MaxStreamsPerConnection; - _pipeUsed = Math.Clamp(pipe, 1, limit); - var slots = new TransferSlot[_pipeUsed]; - for (var i = 0; i < slots.Length; i++) - { - slots[i] = new TransferSlot((uint)(i + 1)); - _freeSlots.Enqueue(slots[i]); - } - - _slots = slots; - _slotGate = new SemaphoreSlim(_pipeUsed, _pipeUsed); - var sent = 0; - var index = 0; - while (count is null || sent < count) - { - cancellationToken.ThrowIfCancellationRequested(); - if (_fault is not null) - { - throw _fault; - } - - var slot = await AcquireSlotAsync(cancellationToken); - var article = pool[index]; - index = (index + 1) % pool.Count; - var now = Interlocked.Increment(ref _outstanding); - UpdatePipeMax(now); - Interlocked.Increment(ref _started); - Volatile.Write(ref slot.EndTimestamp, 0); - Volatile.Write(ref slot.Credit, initialCredit); - Volatile.Write(ref slot.AcceptWindow, 1); - Volatile.Write(ref slot.Active, 1); - await WriteStampedAsync(article.Store, slot.StreamId); - await WriteStampedAsync(article.Meta, slot.StreamId); - for (var frame = 0; frame < article.Data.Length; frame++) - { - await WaitForCreditAsync(slot, article.DataLengths[frame], cancellationToken); - await WriteStampedAsync(article.Data[frame], slot.StreamId); - } - - Volatile.Write(ref slot.AcceptWindow, 0); - Volatile.Write(ref slot.EndTimestamp, Stopwatch.GetTimestamp()); - await WriteStampedAsync(article.End, slot.StreamId); - sent++; - } - - while (Volatile.Read(ref _outstanding) > 0) - { - cancellationToken.ThrowIfCancellationRequested(); - if (_fault is not null) - { - throw _fault; - } - - await _doneGate.WaitAsync(cancellationToken); - } - } - - public void CopySteadyLatencies(long warmupStamp, long measureStamp, List latencies) - { - lock (_latencyLock) - { - foreach (var sample in _latencies) - { - if (sample.CompletedTimestamp >= warmupStamp && sample.CompletedTimestamp <= measureStamp) - { - latencies.Add(sample.LatencyTicks); - } - } - } - } - - public async ValueTask DisposeAsync() - { - await _dispose.CancelAsync(); - try - { - await _reader; - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); - } - catch (IOException ex) - { - ExpectedException.Ignore(ex); - } - - await _ssl.DisposeAsync(); - _tcp.Dispose(); - _dispose.Dispose(); - _helloGate.Dispose(); - _doneGate.Dispose(); - _slotGate?.Dispose(); - if (_slots is not null) - { - foreach (var slot in _slots) - { - slot.CreditGate.Dispose(); - } - } - } - - private async Task AcquireSlotAsync(CancellationToken cancellationToken) - { - var gate = _slotGate ?? throw new InvalidOperationException("Stream slots are not open."); - if (!gate.Wait(0, cancellationToken)) - { - var started = Stopwatch.GetTimestamp(); - await gate.WaitAsync(cancellationToken); - Interlocked.Add(ref _slotWaitTicks, Stopwatch.GetTimestamp() - started); - } - - if (!_freeSlots.TryDequeue(out var slot)) - { - if (_fault is not null) - { - throw _fault; - } - - throw new InvalidOperationException("Stream slot semaphore and free list diverged."); - } - - return slot; - } - - private void UpdatePipeMax(long outstanding) - { - int previous; - do - { - previous = Volatile.Read(ref _pipeMaxOutstanding); - if (outstanding <= previous) - { - return; - } - } - while (Interlocked.CompareExchange(ref _pipeMaxOutstanding, (int)outstanding, previous) != previous); - } - - private async Task WaitForCreditAsync(TransferSlot slot, int bytes, CancellationToken cancellationToken) - { - var waiting = false; - var started = 0L; - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - if (_fault is not null) - { - throw _fault; - } - - var current = Volatile.Read(ref slot.Credit); - if (current >= bytes && Interlocked.CompareExchange(ref slot.Credit, current - bytes, current) == current) - { - if (waiting) - { - Interlocked.Add(ref _creditWaitTicks, Stopwatch.GetTimestamp() - started); - } - - return; - } - - if (!waiting) - { - waiting = true; - started = Stopwatch.GetTimestamp(); - } - - await slot.CreditGate.WaitAsync(cancellationToken); - } - } - - private async Task WriteStampedAsync(byte[] frame, uint streamId) - { - BinaryPrimitives.WriteUInt32BigEndian(frame.AsSpan(4, 4), streamId); - await _ssl.WriteAsync(frame); - Interlocked.Add(ref _wireBytes, frame.Length); - } - - private async Task WriteAsync(byte[] frame) - { - await _ssl.WriteAsync(frame); - Interlocked.Add(ref _wireBytes, frame.Length); - } - - private async Task ReadLoopAsync() - { - var buffer = new byte[256 * 1024]; - var filled = 0; - try - { - while (!_dispose.IsCancellationRequested) - { - var read = await _ssl.ReadAsync(buffer.AsMemory(filled), _dispose.Token); - if (read == 0) - { - Fault(new IOException("VATP peer closed the connection.")); - return; - } - - filled += read; - var offset = 0; - Span window = _window; - Span outcome = _outcome; - while (true) - { - var parsed = VatpFrameParser.ParseOneFrame( - new ReadOnlySequence(buffer, offset, filled - offset), - VatpProtocol.DefaultMaxFramePayload); - if (parsed.Status == VatpFrameParseStatus.Incomplete) - { - break; - } - - if (parsed.Status != VatpFrameParseStatus.Success || parsed.Frame is null) - { - Fault(new InvalidOperationException($"VATP parse failed: {parsed.Error}")); - return; - } - - var frame = parsed.Frame.Value; - switch (frame.Header.Type) - { - case VatpFrameType.Hello: - _helloGate.Release(); - break; - case VatpFrameType.Window: - frame.Payload.CopyTo(window); - if (!VatpControlPayload.TryDecodeWindow(window, out var add)) - { - Fault(new InvalidOperationException("Invalid WINDOW.")); - return; - } - - NoteWindow(add); - ApplyWindow(frame.Header.StreamId, add); - break; - case VatpFrameType.Result: - frame.Payload.CopyTo(outcome); - if (!TryCompleteSlot(frame.Header.StreamId, outcome[0] == VatpStoreOutcome.Accepted)) - { - Fault(new InvalidOperationException($"RESULT for inactive stream {frame.Header.StreamId}.")); - return; - } - - break; - case VatpFrameType.Fail: - if (frame.Header.StreamId == VatpProtocol.ConnectionStreamId - || !TryCompleteSlot(frame.Header.StreamId, accepted: false)) - { - Interlocked.Increment(ref _rejected); - } - - Fault(new InvalidOperationException("VATP FAIL during transfer.")); - WakeSenders(); - return; - } - - offset += checked((int)parsed.ConsumedBytes); - } - - if (offset > 0) - { - Buffer.BlockCopy(buffer, offset, buffer, 0, filled - offset); - filled -= offset; - } - } - } - catch (OperationCanceledException ex) when (_dispose.IsCancellationRequested) - { - ExpectedException.Ignore(ex); - } - catch (Exception ex) - { - Fault(ex); - WakeSenders(); - } - } - - private void NoteWindow(uint add) - { - _windowFrames++; - _windowBytes += add; - if (add < _windowAddMin) - { - _windowAddMin = (int)add; - } - - if (add > _windowAddMax) - { - _windowAddMax = (int)add; - } - } - - private void ApplyWindow(uint streamId, uint add) - { - var slot = FindSlot(streamId); - if (slot is null) - { - Fault(new InvalidOperationException($"WINDOW for unknown stream {streamId}.")); - return; - } - - if (Volatile.Read(ref slot.AcceptWindow) == 0) - { - return; - } - - Interlocked.Add(ref slot.Credit, add); - slot.CreditGate.Release(); - } - - private bool TryCompleteSlot(uint streamId, bool accepted) - { - var slot = FindSlot(streamId); - if (slot is null || Interlocked.Exchange(ref slot.Active, 0) != 1) - { - return false; - } - - Volatile.Write(ref slot.AcceptWindow, 0); - if (accepted) - { - Interlocked.Increment(ref _accepted); - } - else - { - Interlocked.Increment(ref _rejected); - } - - var end = Volatile.Read(ref slot.EndTimestamp); - if (end != 0) - { - var completed = Stopwatch.GetTimestamp(); - lock (_latencyLock) - { - _latencies.Add(new LatencySample(completed, completed - end)); - } - } - - Interlocked.Increment(ref _completed); - Interlocked.Decrement(ref _outstanding); - _freeSlots.Enqueue(slot); - _slotGate?.Release(); - slot.CreditGate.Release(); - _doneGate.Release(); - return true; - } - - private TransferSlot? FindSlot(uint streamId) - { - var slots = _slots; - if (slots is null || streamId == 0 || streamId > (uint)slots.Length) - { - return null; - } - - return slots[streamId - 1]; - } - - private void WakeSenders() - { - try - { - _helloGate.Release(); - _doneGate.Release(); - _slotGate?.Release(); - if (_slots is null) - { - return; - } - - foreach (var slot in _slots) - { - slot.CreditGate.Release(); - } - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } - } - - private void Fault(Exception exception) => - Interlocked.CompareExchange(ref _fault, exception, null); - - private sealed class TransferSlot - { - public TransferSlot(uint streamId) => StreamId = streamId; - - public uint StreamId { get; } - - public SemaphoreSlim CreditGate { get; } = new(0); - - public long Credit; - - public int AcceptWindow; - - public int Active; - - public long EndTimestamp; - } - - private readonly record struct LatencySample(long CompletedTimestamp, long LatencyTicks); - } - } -} diff --git a/tests/VectorNNTPD.StorageService.Tests/Listener/StoreReservationHoldTests.cs b/tests/VectorNNTPD.StorageService.Tests/Listener/StoreReservationHoldTests.cs deleted file mode 100644 index ff8beb1e..00000000 --- a/tests/VectorNNTPD.StorageService.Tests/Listener/StoreReservationHoldTests.cs +++ /dev/null @@ -1,401 +0,0 @@ -using VectorNNTPD.StorageService.Ingress; -using VectorNNTPD.StorageService.Listener; - -namespace VectorNNTPD.StorageService.Tests.Listener -{ - - public sealed class StoreReservationHoldTests - { - [Fact] - public async Task TryRelease_invokes_queue_release_exactly_once_under_concurrency() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(64, out var reservation)); - Assert.Equal(64, queue.ReservedBytes); - - var hold = new StoreReservationHold(); - hold.Bind(queue, reservation); - Assert.True(hold.IsBound); - - var starter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var releasesWon = 0; - var workers = new Task[8]; - for (var i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - if (hold.TryRelease()) - { - Interlocked.Increment(ref releasesWon); - } - }); - } - - starter.SetResult(); - await Task.WhenAll(workers).WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(1, releasesWon); - Assert.Equal(1, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - Assert.False(hold.IsBound); - } - - /// - /// Models AuthorizeReservedBodyAsync failure when the reservation was never bound - /// (direct TryReserve path) while concurrent RemoveStore runs. - /// - [Fact] - public async Task Authorize_failure_releases_unbound_parameter_exactly_once_when_hold_never_bound() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(32, out var reservation)); - - var hold = new StoreReservationHold(); - var gate = new object(); - var starter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - var removeStore = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - lock (gate) - { - if (!hold.Committed) - { - hold.TryRelease(); - } - } - }); - - var authorizeFailure = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - var reservationAlreadyBound = hold.IsBound; - if (!hold.TryRelease() && !reservationAlreadyBound) - { - queue.Release(reservation); - } - - lock (gate) - { - if (!hold.Committed) - { - hold.TryRelease(); - } - } - }); - - starter.SetResult(); - await Task.WhenAll(removeStore, authorizeFailure).WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(1, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - } - - /// - /// Models AdmitWhenCapacity already-bound reservation racing Authorize failure with RemoveStore. - /// Capturing reservationAlreadyBound at entry must not cause a second Release. - /// - [Fact] - public async Task Authorize_failure_skips_parameter_when_reservation_already_bound() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(32, out var reservation)); - - var hold = new StoreReservationHold(); - var gate = new object(); - lock (gate) - { - hold.Bind(queue, reservation); - } - - var reservationAlreadyBound = hold.IsBound; - Assert.True(reservationAlreadyBound); - - var starter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var removeStore = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - lock (gate) - { - if (!hold.Committed) - { - hold.TryRelease(); - } - } - }); - - var authorizeFailure = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - if (!hold.TryRelease() && !reservationAlreadyBound) - { - queue.Release(reservation); - } - - lock (gate) - { - if (!hold.Committed) - { - hold.TryRelease(); - } - } - }); - - starter.SetResult(); - await Task.WhenAll(removeStore, authorizeFailure).WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(1, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - Assert.False(hold.IsBound); - } - - /// - /// Commit path takes hold ownership before TryCommit; concurrent RemoveStore - /// must not invoke Release after the reservation is consumed into the queue. - /// - [Fact] - public async Task Commit_take_wins_over_teardown_and_never_releases_consumed_reservation() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(16, out var reservation)); - var hold = new StoreReservationHold(); - hold.Bind(queue, reservation); - - var commitTaken = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var teardownFinished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - var teardown = Task.Run(async () => - { - await commitTaken.Task.ConfigureAwait(false); - // RemoveStore policy: skip TryRelease only after MarkCommitOutcome(true). - // Between Take and Mark, Committed is still false — TryRelease must lose the take. - if (!hold.Committed) - { - hold.TryRelease(); - } - - teardownFinished.SetResult(); - }); - - Assert.True(hold.TryTakeForCommit(out var commitQueue, out var commitReservation)); - commitTaken.SetResult(); - - Assert.True(commitQueue!.TryCommit(commitReservation, CreateArticle(16))); - hold.MarkCommitOutcome(true); - - await teardown.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - await teardownFinished.Task.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(0, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(16, queue.QueuedBytes); - Assert.Equal(1, queue.QueuedCount); - Assert.True(hold.Committed); - Assert.False(hold.IsBound); - Assert.False(hold.TryRelease()); - } - - /// - /// Teardown takes ownership before commit; the commit path must observe the lost take and - /// leave the article unqueued with reservation bytes fully released. - /// - [Fact] - public async Task Teardown_release_before_commit_take_prevents_consumption() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(16, out var reservation)); - var hold = new StoreReservationHold(); - hold.Bind(queue, reservation); - - var teardownReady = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var commitFinished = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - - var teardown = Task.Run(async () => - { - Assert.True(hold.TryRelease()); - teardownReady.SetResult(); - await commitFinished.Task.ConfigureAwait(false); - }); - - await teardownReady.Task.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - var taken = hold.TryTakeForCommit(out var commitQueue, out var commitReservation); - var committed = false; - if (taken) - { - committed = commitQueue!.TryCommit(commitReservation, CreateArticle(16)); - hold.MarkCommitOutcome(committed); - } - - commitFinished.SetResult(); - await teardown.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.False(taken); - Assert.False(committed); - Assert.Equal(1, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, queue.QueuedBytes); - Assert.Equal(0, queue.QueuedCount); - Assert.False(hold.IsBound); - } - - /// - /// Concurrent commit-take and teardown-release: exactly one path owns the reservation; - /// queue accounting ends with either one queued article or a full release, never both. - /// - [Fact] - public async Task Concurrent_commit_take_and_teardown_release_are_mutually_exclusive() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(16, out var reservation)); - var hold = new StoreReservationHold(); - hold.Bind(queue, reservation); - - var starter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var commitWon = 0; - var releaseWon = 0; - - var commit = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - if (hold.TryTakeForCommit(out var commitQueue, out var commitReservation)) - { - Interlocked.Increment(ref commitWon); - var ok = commitQueue!.TryCommit(commitReservation, CreateArticle(16)); - hold.MarkCommitOutcome(ok); - Assert.True(ok); - } - }); - - var teardown = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - if (hold.TryRelease()) - { - Interlocked.Increment(ref releaseWon); - } - }); - - starter.SetResult(); - await Task.WhenAll(commit, teardown).WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(1, commitWon + releaseWon); - Assert.Equal(0, queue.ReservedBytes); - if (commitWon == 1) - { - Assert.Equal(0, queue.ReleaseCalls); - Assert.Equal(1, queue.QueuedCount); - Assert.Equal(16, queue.QueuedBytes); - Assert.True(hold.Committed); - } - else - { - Assert.Equal(1, queue.ReleaseCalls); - Assert.Equal(0, queue.QueuedCount); - Assert.Equal(0, queue.QueuedBytes); - Assert.False(hold.Committed); - } - } - - [Fact] - public void Failed_commit_after_take_does_not_require_hold_release() - { - var queue = new CountingQueue(); - Assert.True(queue.TryReserve(16, out var reservation)); - var hold = new StoreReservationHold(); - hold.Bind(queue, reservation); - - Assert.True(hold.TryTakeForCommit(out var commitQueue, out var commitReservation)); - // Size mismatch: TryCommit removes the reservation and frees reserved bytes. - Assert.False(commitQueue!.TryCommit(commitReservation, CreateArticle(8))); - hold.MarkCommitOutcome(false); - - Assert.False(hold.TryRelease()); - Assert.Equal(0, queue.ReleaseCalls); - Assert.Equal(0, queue.ReservedBytes); - Assert.Equal(0, queue.QueuedCount); - Assert.False(hold.Committed); - } - - [Fact] - public async Task Admission_exit_is_exactly_once_under_concurrency() - { - var admission = new StoreAssemblyAdmission(4); - Assert.True(admission.TryEnter()); - Assert.Equal(1, admission.InFlight); - - StoreAssemblyAdmission? held = admission; - var starter = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var workers = new Task[8]; - for (var i = 0; i < workers.Length; i++) - { - workers[i] = Task.Run(async () => - { - await starter.Task.ConfigureAwait(false); - Interlocked.Exchange(ref held, null)?.Exit(); - }); - } - - starter.SetResult(); - await Task.WhenAll(workers).WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(true); - - Assert.Equal(0, admission.InFlight); - Assert.Null(held); - } - - private static AcceptedArticle CreateArticle(int size) - { - return new AcceptedArticle(default, 1, size, new byte[size]); - } - - /// Production queue wrapper that counts calls. - private sealed class CountingQueue : IAcceptedArticleQueue - { - private readonly AcceptedArticleQueue _inner = new(maxQueuedBytes: 1024 * 1024, TimeProvider.System); - private int _releaseCalls; - - public int ReleaseCalls => Volatile.Read(ref _releaseCalls); - - public long MaxQueuedBytes => _inner.MaxQueuedBytes; - - public long QueuedBytes => _inner.QueuedBytes; - - public long ReservedBytes => _inner.ReservedBytes; - - public long AccountedBytes => _inner.AccountedBytes; - - public int AdmissionWaiters => _inner.AdmissionWaiters; - - public int QueuedCount => _inner.QueuedCount; - - public bool IsCompleted => _inner.IsCompleted; - - public bool TryEnqueue(AcceptedArticle article) => _inner.TryEnqueue(article); - - public bool TryReserve(int bytes, out QueueReservation reservation) => - _inner.TryReserve(bytes, out reservation); - - public ValueTask ReserveAsync(int bytes, CancellationToken cancellationToken) => - _inner.ReserveAsync(bytes, cancellationToken); - - public bool TryCommit(QueueReservation reservation, AcceptedArticle article) => - _inner.TryCommit(reservation, article); - - public void Release(QueueReservation reservation) - { - Interlocked.Increment(ref _releaseCalls); - _inner.Release(reservation); - } - - public TimeSpan OldestAge(TimeProvider time) => _inner.OldestAge(time); - - public bool TryDequeue(out AcceptedArticleDelivery delivery) => _inner.TryDequeue(out delivery); - - public ValueTask DequeueAsync(CancellationToken cancellationToken) => - _inner.DequeueAsync(cancellationToken); - - public void Complete() => _inner.Complete(); - } - } -} diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacement.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacement.cs index 1510e03e..d87a10a3 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacement.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacement.cs @@ -1,41 +1,39 @@ +using System.Text; using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Articles.Parsing; -using VectorNNTPD.Common.Transport.ArticleTransfer; namespace VectorNNTPD.StorageService.Tests.Support.Placement { - /// Observable result of one single-copy placement attempt. public enum ArticlePlacementKind : byte { - /// Journal accept completed. Matches ArticleAcceptOutcome.Accepted (1). + /// Server accepted the article (239). Accepted = 1, - /// Identical article already represented. Matches ArticleAcceptOutcome.Duplicate (2). + /// Identical article already represented (mapped from reject when duplicate is known). Duplicate = 2, - /// Conflicting ArtHash or ArtSize. Matches ArticleAcceptOutcome.Conflict (3). + /// Conflicting identity. Unused for NNTP TAKETHIS; retained for enum stability. Conflict = 3, - /// Journal pressure rejection. Matches ArticleAcceptOutcome.RejectedPressure (4). + /// Temporary pressure / capacity rejection. RejectedPressure = 4, - /// Canonical or integrity rejection. Matches ArticleAcceptOutcome.RejectedInvalid (5). + /// Canonical or integrity rejection (439). RejectedInvalid = 5, - /// Capacity rejection. Matches ArticleAcceptOutcome.RejectedCapacity (6). + /// Capacity rejection (439 when ArtSize exceeds queue budget). RejectedCapacity = 6, /// No eligible registry entry. No dial. NoActiveServer = 7, - /// Connect, TLS, protocol FAIL, or I/O failure before END was fully written. + /// Connect, TLS, protocol, or I/O failure before the article was fully written. TransportFailure = 8, /// Caller cancelled before the server could accept. Cancelled = 9, - /// END was written and RESULT was not observed. The journal may already hold the article. + /// Article was written and no status line was observed. The queue may already hold it. AcknowledgementNotObserved = 10, } @@ -47,16 +45,13 @@ public readonly record struct ArticlePlacementResult(ArticlePlacementKind Kind, /// Gets whether the StorageServer accepted responsibility or already held the same article. public bool Succeeded => Kind is ArticlePlacementKind.Accepted or ArticlePlacementKind.Duplicate; - /// Maps a RESULT outcome byte. Unknown bytes are transport failures. - public static ArticlePlacementResult FromOutcomeByte(byte outcome) => outcome switch + /// Maps an NNTP status code from a TAKETHIS response. + public static ArticlePlacementResult FromNntpStatus(int code) => code switch { - (byte)ArticlePlacementKind.Accepted => new(ArticlePlacementKind.Accepted), - (byte)ArticlePlacementKind.Duplicate => new(ArticlePlacementKind.Duplicate), - (byte)ArticlePlacementKind.Conflict => new(ArticlePlacementKind.Conflict), - (byte)ArticlePlacementKind.RejectedPressure => new(ArticlePlacementKind.RejectedPressure), - (byte)ArticlePlacementKind.RejectedInvalid => new(ArticlePlacementKind.RejectedInvalid), - (byte)ArticlePlacementKind.RejectedCapacity => new(ArticlePlacementKind.RejectedCapacity), - _ => new(ArticlePlacementKind.TransportFailure, "result-outcome"), + 239 => new(ArticlePlacementKind.Accepted), + 439 => new(ArticlePlacementKind.RejectedInvalid), + 400 => new(ArticlePlacementKind.RejectedPressure, "temporary-failure"), + _ => new(ArticlePlacementKind.TransportFailure, "status-" + code.ToString(System.Globalization.CultureInfo.InvariantCulture)), }; } @@ -105,42 +100,29 @@ private static bool Prefer(in StorageServerFleetEntry candidate, in StorageServe } } - /// Builds VATP META from a CanonicalV1 record. - public static class ArticlePlacementMeta + /// Extracts the Message-ID value octets from a CanonicalV1 record for TAKETHIS. + public static class ArticlePlacementMessageId { - private static readonly NntpArticleHeaderName[] DateHeaders = - [ - NntpArticleHeaderName.Date, - NntpArticleHeaderName.InjectionDate, - NntpArticleHeaderName.NntpPostingDate, - NntpArticleHeaderName.Posted, - NntpArticleHeaderName.XDate, - NntpArticleHeaderName.DeliveryDate, - ]; - - /// - /// Recovers the winning Date-family header by locating fields that match the record, - /// then builds META with . - /// - public static bool TryFromRecord(in ArticleRecord record, out ArticleCanonicalTransferMeta meta) + /// Copies the Message-ID header value (including angle brackets) from . + public static bool TryCopy(in ArticleRecord record, out byte[] messageId) { - meta = default; + messageId = []; if (record.ParseStatus != ArticleParseStatus.CanonicalV1 || record.ArtSize <= 0) { return false; } - var artData = record.ArtData.Span; - foreach (var name in DateHeaders) + var value = record.MessageId; + if (value.IsEmpty) { - if (ArticleFieldTable.Locate(artData, name).Equals(record.Fields)) - { - meta = ArticleCanonicalTransferMeta.FromRecord(in record, name); - return true; - } + return false; } - return false; + messageId = value.ToArray(); + return true; } + + /// ASCII Message-ID for logging only. + public static string ToAscii(ReadOnlySpan messageId) => Encoding.ASCII.GetString(messageId); } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementClient.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementClient.cs index de85fc53..623a6190 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementClient.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementClient.cs @@ -1,34 +1,22 @@ -using System.Buffers; using System.Globalization; using System.Net.Security; using System.Net.Sockets; using Microsoft.Extensions.Logging; using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Transport.ArticleTransfer; using VectorNNTPD.StorageService; -using VectorNNTPD.StorageService.Tests.Support.Placement; - namespace VectorNNTPD.StorageService.Tests.Support.Placement { - /// - /// Long-lived STORE client. One TLS connection per placement target. Does not use . + /// Long-lived TAKETHIS client. One TLS connection per placement target. /// - /// - /// Connections stay open while they are healthy. Each connection has one frame writer and at most - /// outstanding STORE streams. A lost RESULT after END is - /// and is not retried. Placement itself - /// stays disabled in the spool writer. - /// public sealed class ArticlePlacementClient : IArticlePlacementClient, IAsyncDisposable { private static readonly TimeSpan InitialBackoff = TimeSpan.FromMilliseconds(200); private static readonly TimeSpan MaximumBackoff = TimeSpan.FromSeconds(5); private readonly ILogger _logger; - private readonly VatpClientOptions _options; - private readonly ArticleTransferLimits _limits; + private readonly StorageFeedClientOptions _options; private readonly TimeProvider _time; private readonly object _gate = new(); private readonly Dictionary _connections = new(StringComparer.OrdinalIgnoreCase); @@ -51,10 +39,10 @@ public sealed class ArticlePlacementClient : IArticlePlacementClient, IAsyncDisp /// Gets how many dial attempts have started, including failures. internal int DialAttempts => Volatile.Read(ref _dials); - /// Gets the outstanding STORE limit applied to each connection. + /// Gets the outstanding TAKETHIS limit applied to each connection. internal int MaxOutstandingStores { get; } - /// Sums wait and RESULT counters across connections still owned by this client. + /// Sums wait and response counters across connections still owned by this client. internal PlacementTransportCounters Snapshot() { lock (_gate) @@ -69,44 +57,39 @@ internal PlacementTransportCounters Snapshot() } } - /// - /// Production default for outstanding STORE operations per VATP connection (NNTPD ArticleIngestionOptions). - /// + /// Default outstanding TAKETHIS operations per connection. internal const int DefaultMaxOutstandingStoresPerConnection = 2; - /// Initializes a placement client with two outstanding STOREs and the production VATP limits. + /// Initializes a placement client with two outstanding TAKETHIS operations. public ArticlePlacementClient(ILogger logger) : this(logger, DefaultMaxOutstandingStoresPerConnection) { } - /// Initializes a placement client with an explicit outstanding-STORE limit. + /// Initializes a placement client with an explicit outstanding-TAKETHIS limit. public ArticlePlacementClient(ILogger logger, int maxOutstandingStores) - : this(logger, new VatpClientOptions(), ArticleTransferLimits.Default, maxOutstandingStores, TimeProvider.System) + : this(logger, new StorageFeedClientOptions(), maxOutstandingStores, TimeProvider.System) { } internal ArticlePlacementClient( ILogger logger, - VatpClientOptions options, - ArticleTransferLimits limits, + StorageFeedClientOptions options, int maxOutstandingStores = DefaultMaxOutstandingStoresPerConnection, TimeProvider? time = null) { ArgumentNullException.ThrowIfNull(logger); ArgumentNullException.ThrowIfNull(options); - ArgumentNullException.ThrowIfNull(limits); if (maxOutstandingStores is not (1 or 2)) { throw new ArgumentOutOfRangeException( nameof(maxOutstandingStores), maxOutstandingStores, - "Outstanding STORE operations per connection must be 1 or 2."); + "Outstanding TAKETHIS operations per connection must be 1 or 2."); } _logger = logger; _options = options; - _limits = limits; _time = time ?? TimeProvider.System; MaxOutstandingStores = maxOutstandingStores; } @@ -127,9 +110,9 @@ public async ValueTask PlaceAsync( return new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "vatp-port"); } - if (!ArticlePlacementMeta.TryFromRecord(in record, out var meta)) + if (!ArticlePlacementMessageId.TryCopy(in record, out _)) { - return new ArticlePlacementResult(ArticlePlacementKind.RejectedInvalid, "meta"); + return new ArticlePlacementResult(ArticlePlacementKind.RejectedInvalid, "message-id"); } try @@ -143,7 +126,7 @@ public async ValueTask PlaceAsync( var connection = leased.Connection ?? throw new InvalidOperationException("Placement lease returned neither a connection nor a failure."); - var result = await connection.PlaceAsync(record, meta, cancellationToken).ConfigureAwait(false); + var result = await connection.PlaceAsync(record, cancellationToken).ConfigureAwait(false); if (connection.IsDead) { Forget(target.Fqdn, port, connection); @@ -319,7 +302,6 @@ private async Task DialAsync(string fqdn, int port, TestTcpConnectHost ?? fqdn, _logger, _options, - _limits, MaxOutstandingStores, ServerCertificateValidationCallback, CancellationToken.None) @@ -382,7 +364,7 @@ private static string DescribeTransportFailure(Exception ex) } } - /// Wait and RESULT totals for connections still held by one placement client. + /// Wait and response totals for connections still held by one placement client. internal readonly record struct PlacementTransportCounters( long SlotWaitTicks, long CreditWaitTicks, @@ -403,269 +385,4 @@ internal PlacementTransportCounters Add(ArticlePlacementConnection connection) = Math.Max(PeakActive, connection.PeakActive), DataFramesWritten + connection.DataFramesWritten); } - - /// STORE/META/DATA/END sender over an already connected stream. - internal static class VatpPlacementTransfer - { - public static async Task PlaceAsync( - Stream stream, - ArticleRecord record, - ArticleCanonicalTransferMeta meta, - ArticleTransferLimits limits, - TimeSpan ioTimeout, - CancellationToken cancellationToken) - { - ArgumentNullException.ThrowIfNull(stream); - var reader = new FrameReader(stream, ioTimeout); - var endWritten = false; - try - { - cancellationToken.ThrowIfCancellationRequested(); - var maxFrame = limits.DefaultMaxFramePayload; - await WriteAsync(stream, VatpFrameEncoder.EncodeHello(maxFrame), cancellationToken).ConfigureAwait(false); - var hello = await reader.ReadAsync(maxFrame, cancellationToken).ConfigureAwait(false); - if (hello is null) - { - return new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "hello-closed"); - } - - if (hello.Value.Header.Type != VatpFrameType.Hello - || !VatpHello.TryDecode(hello.Value.Payload, out var decodedHello, out _)) - { - return new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "hello"); - } - - maxFrame = Math.Min(decodedHello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload); - const uint streamId = 1; - var id = new byte[ArticleId.Length]; - record.ArtId.CopyTo(id); - await WriteAsync(stream, VatpFrameEncoder.EncodeStore(streamId, id), cancellationToken).ConfigureAwait(false); - var metaBytes = VatpMetaCodec.Encode(in meta); - await WriteAsync(stream, VatpFrameEncoder.EncodeMeta(streamId, metaBytes), cancellationToken).ConfigureAwait(false); - - var window = new ArticleTransferWindow(0, limits.MaxStreamCreditBytes); - var offset = 0; - var artData = record.ArtData; - while (offset < record.ArtSize) - { - cancellationToken.ThrowIfCancellationRequested(); - var length = ArticleTransferReadyRing.ComputeDataPayloadLength( - record.ArtSize - offset, - window.Credit, - maxFrame); - if (length <= 0) - { - var paced = await ReadControlAsync(reader, maxFrame, endWritten: false, cancellationToken) - .ConfigureAwait(false); - if (paced.Terminal is { } terminal) - { - return terminal; - } - - if (paced.AddedCredit > 0) - { - _ = window.Add(paced.AddedCredit); - } - - continue; - } - - if (!window.TryConsume(length)) - { - return new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "window"); - } - - var fin = offset + length == record.ArtSize; - await WriteAsync( - stream, - VatpFrameEncoder.EncodeData(streamId, artData.Slice(offset, length), fin), - cancellationToken) - .ConfigureAwait(false); - offset += length; - } - - await WriteAsync(stream, VatpFrameEncoder.EncodeEnd(streamId), cancellationToken).ConfigureAwait(false); - endWritten = true; - while (true) - { - var final = await ReadControlAsync(reader, maxFrame, endWritten: true, cancellationToken) - .ConfigureAwait(false); - if (final.Terminal is { } terminal) - { - return terminal; - } - - if (final.AddedCredit > 0) - { - _ = window.Add(final.AddedCredit); - } - } - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - if (!endWritten) - { - try - { - await WriteAsync(stream, VatpFrameEncoder.EncodeCancel(1), CancellationToken.None) - .ConfigureAwait(false); - } - catch (Exception ex) when (ex is IOException or ObjectDisposedException) - { - ExpectedException.Ignore(ex); - } - - return new ArticlePlacementResult(ArticlePlacementKind.Cancelled); - } - - return new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved); - } - catch (Exception ex) when (ex is IOException or TimeoutException or ObjectDisposedException) - { - return endWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, ex.GetType().Name); - } - } - - private readonly record struct ControlRead(ArticlePlacementResult? Terminal, uint AddedCredit); - - private static async Task ReadControlAsync( - FrameReader reader, - uint maxFrame, - bool endWritten, - CancellationToken cancellationToken) - { - var frame = await reader.ReadAsync(maxFrame, cancellationToken).ConfigureAwait(false); - if (frame is null) - { - return new ControlRead( - endWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed"), - 0); - } - - switch (frame.Value.Header.Type) - { - case VatpFrameType.Window: - return VatpControlPayload.TryDecodeWindow(Copy(frame.Value.Payload), out var add) - ? new ControlRead(null, add) - : new ControlRead(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "window"), 0); - case VatpFrameType.Result: - if (!VatpResultPayload.TryDecode(frame.Value.Payload, out var outcome, out _)) - { - return new ControlRead(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "result"), 0); - } - - return new ControlRead(ArticlePlacementResult.FromOutcomeByte(outcome), 0); - case VatpFrameType.Fail: - if (!VatpControlPayload.TryDecodeFail(Copy(frame.Value.Payload), out var error, out _)) - { - return new ControlRead( - new ArticlePlacementResult( - endWritten - ? ArticlePlacementKind.AcknowledgementNotObserved - : ArticlePlacementKind.TransportFailure, - "fail"), - 0); - } - - if (error == VatpErrorCode.Cancelled) - { - return new ControlRead(new ArticlePlacementResult(ArticlePlacementKind.Cancelled), 0); - } - - return new ControlRead(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, error.ToString()), 0); - default: - return new ControlRead( - new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, frame.Value.Header.Type.ToString()), - 0); - } - } - - private static byte[] Copy(ReadOnlySequence payload) - { - var bytes = new byte[payload.Length]; - payload.CopyTo(bytes); - return bytes; - } - - private static async Task WriteAsync( - Stream stream, - VatpFrameEncoder.EncodedFrame frame, - CancellationToken cancellationToken) - { - if (!frame.Header.IsEmpty) - { - await stream.WriteAsync(frame.Header, cancellationToken).ConfigureAwait(false); - } - - if (!frame.Payload.IsEmpty) - { - await stream.WriteAsync(frame.Payload, cancellationToken).ConfigureAwait(false); - } - } - - private sealed class FrameReader - { - private readonly Stream _stream; - private readonly TimeSpan _ioTimeout; - private readonly byte[] _buffer = new byte[64 * 1024]; - private readonly List _pending = []; - - public FrameReader(Stream stream, TimeSpan ioTimeout) - { - _stream = stream; - _ioTimeout = ioTimeout; - } - - public async Task ReadAsync(uint maxFramePayload, CancellationToken cancellationToken) - { - while (true) - { - var parsed = VatpFrameParser.ParseOneFrame(new ReadOnlySequence(_pending.ToArray()), maxFramePayload); - if (parsed.Status == VatpFrameParseStatus.Invalid) - { - throw new IOException(parsed.Error.ToString()); - } - - if (parsed.Status == VatpFrameParseStatus.Success && parsed.Frame is { } frame) - { - _pending.RemoveRange(0, checked((int)parsed.ConsumedBytes)); - return CopyFrame(frame); - } - - using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - timeout.CancelAfter(_ioTimeout); - int read; - try - { - read = await _stream.ReadAsync(_buffer, timeout.Token).ConfigureAwait(false); - } - catch (OperationCanceledException) when (timeout.IsCancellationRequested && !cancellationToken.IsCancellationRequested) - { - throw new TimeoutException("placement-io"); - } - - if (read == 0) - { - return null; - } - - for (var i = 0; i < read; i++) - { - _pending.Add(_buffer[i]); - } - } - } - - private static VatpParsedFrame CopyFrame(VatpParsedFrame frame) - { - var payload = new byte[frame.Payload.Length]; - frame.Payload.CopyTo(payload); - return new VatpParsedFrame(frame.Header, new ReadOnlySequence(payload)); - } - } - } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementConnection.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementConnection.cs index 27968b43..edbed9f3 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementConnection.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementConnection.cs @@ -1,50 +1,48 @@ -using System.Buffers; +using System.Diagnostics; using System.Net.Security; using System.Net.Sockets; +using System.Text; using Microsoft.Extensions.Logging; using VectorNNTPD.Common.Articles; -using VectorNNTPD.Common.Transport.ArticleTransfer; -using VectorNNTPD.StorageService.Tests.Support.Placement; using VectorNNTPD.StorageService; namespace VectorNNTPD.StorageService.Tests.Support.Placement { - /// - /// One TLS connection to one placement target. One writer. At most - /// STORE streams. + /// One TLS NNTP STREAMING connection to one placement target. + /// At most pipelined TAKETHIS operations. /// internal sealed class ArticlePlacementConnection : IAsyncDisposable { + private static readonly byte[] ModeStream = "MODE STREAM\r\n"u8.ToArray(); + private static readonly byte[] Quit = "QUIT\r\n"u8.ToArray(); + private static readonly byte[] TakeThisPrefix = "TAKETHIS "u8.ToArray(); + private static readonly byte[] Crlf = "\r\n"u8.ToArray(); + private static readonly byte[] Terminator = ".\r\n"u8.ToArray(); + private readonly string _fqdn; private readonly int _port; private readonly TcpClient _tcp; private readonly SslStream _ssl; private readonly ILogger _logger; - private readonly ArticleTransferLimits _limits; private readonly TimeSpan _ioTimeout; private readonly SemaphoreSlim _slots; private readonly SemaphoreSlim _write = new(1, 1); - private readonly object _tableGate = new(); - private readonly Dictionary _operations = []; + private readonly object _pendingGate = new(); + private readonly Queue _pending = new(); private readonly CancellationTokenSource _lifetime = new(); - private readonly FrameBuffer _frames = new(); + private readonly byte[] _readBuffer = new byte[64 * 1024]; + private readonly List _lineBuffer = []; private Task? _reader; private int _dead; private int _disposed; private int _active; private int _peakActive; - private uint _nextStreamId = 1; - private uint _maxFramePayload; private long _slotWaitTicks; - private long _creditWaitTicks; - private long _resultTicks; - private long _resultCount; - private long _resultMaxTicks; - private long _dataFramesWritten; - private int _storesAwaitingInitialCredit; - private readonly object _readTimeoutGate = new(); - private CancellationTokenSource? _activeReadTimeout; + private long _responseWaitTicks; + private long _responseCount; + private long _responseMaxTicks; + private long _articleBytesWritten; private ArticlePlacementConnection( string fqdn, @@ -52,45 +50,36 @@ private ArticlePlacementConnection( TcpClient tcp, SslStream ssl, ILogger logger, - ArticleTransferLimits limits, TimeSpan ioTimeout, - int maxOutstanding, - uint maxFramePayload) + int maxOutstanding) { _fqdn = fqdn; _port = port; _tcp = tcp; _ssl = ssl; _logger = logger; - _limits = limits; _ioTimeout = ioTimeout; MaxOutstanding = maxOutstanding; - _maxFramePayload = maxFramePayload; _slots = new SemaphoreSlim(maxOutstanding, maxOutstanding); } public int MaxOutstanding { get; } - /// Gets the highest number of STORE operations that were in flight together. internal int PeakActive => Volatile.Read(ref _peakActive); - /// Gets ticks spent waiting for a free STORE slot. internal long SlotWaitTicks => Volatile.Read(ref _slotWaitTicks); - /// Gets ticks spent waiting for WINDOW credit. - internal long CreditWaitTicks => Volatile.Read(ref _creditWaitTicks); + /// Ticks spent waiting for a TAKETHIS status after the article was written. + internal long CreditWaitTicks => Volatile.Read(ref _responseWaitTicks); - /// Gets how many RESULT waits were recorded. - internal long ResultCount => Volatile.Read(ref _resultCount); + internal long ResultCount => Volatile.Read(ref _responseCount); - /// Gets the total ticks spent waiting for RESULT after END. - internal long ResultTicks => Volatile.Read(ref _resultTicks); + internal long ResultTicks => Volatile.Read(ref _responseWaitTicks); - /// Gets the longest RESULT wait in ticks. - internal long ResultMaxTicks => Volatile.Read(ref _resultMaxTicks); + internal long ResultMaxTicks => Volatile.Read(ref _responseMaxTicks); - /// Gets DATA frames this connection has written. Credit is zero until the server sends WINDOW. - internal long DataFramesWritten => Volatile.Read(ref _dataFramesWritten); + /// Article payload octets written (destuffed size, not including stuffing or framing). + internal long DataFramesWritten => Volatile.Read(ref _articleBytesWritten); public bool IsDead => Volatile.Read(ref _dead) != 0; @@ -101,8 +90,7 @@ public static async Task EstablishAsync( int port, string connectHost, ILogger logger, - VatpClientOptions options, - ArticleTransferLimits limits, + StorageFeedClientOptions options, int maxOutstanding, RemoteCertificateValidationCallback? certificateValidation, CancellationToken cancellationToken) @@ -139,10 +127,11 @@ public static async Task EstablishAsync( tcp.Dispose(); throw; } + SslStream ssl; try { - ssl = await VatpTlsClient.AuthenticateAsClientAsync( + ssl = await StorageTlsClient.AuthenticateAsClientAsync( tcp.GetStream(), fqdn, options.TlsHandshakeTimeout, @@ -150,137 +139,186 @@ public static async Task EstablishAsync( cancellationToken) .ConfigureAwait(false); } - catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + catch { - throw new TimeoutException("timeout"); + tcp.Dispose(); + throw; } + var connection = new ArticlePlacementConnection( fqdn, port, tcp, ssl, logger, - limits, options.IoTimeout, - maxOutstanding, - limits.DefaultMaxFramePayload); + maxOutstanding); try { - await connection.ExchangeHelloAsync(cancellationToken).ConfigureAwait(false); - connection._reader = Task.Run(connection.ReadLoopAsync); - ArticlePlacementTransportLogMessages.Established(logger, fqdn, port, maxOutstanding); - return connection; + await connection.HandshakeAsync(cancellationToken).ConfigureAwait(false); } catch { await connection.DisposeAsync().ConfigureAwait(false); throw; } + + ArticlePlacementTransportLogMessages.Established(logger, fqdn, port, maxOutstanding); + return connection; } public async Task PlaceAsync( ArticleRecord record, - ArticleCanonicalTransferMeta meta, CancellationToken cancellationToken) { - if (IsDead || Volatile.Read(ref _disposed) != 0) + if (IsDead) { return new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed"); } - var slotWait = System.Diagnostics.Stopwatch.GetTimestamp(); + if (!ArticlePlacementMessageId.TryCopy(in record, out var messageId)) + { + return new ArticlePlacementResult(ArticlePlacementKind.RejectedInvalid, "message-id"); + } + + var slotStarted = Stopwatch.GetTimestamp(); try { await _slots.WaitAsync(cancellationToken).ConfigureAwait(false); - Interlocked.Add(ref _slotWaitTicks, System.Diagnostics.Stopwatch.GetTimestamp() - slotWait); } catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { return new ArticlePlacementResult(ArticlePlacementKind.Cancelled); } - catch (ObjectDisposedException) - { - return new ArticlePlacementResult(ArticlePlacementKind.Cancelled); - } - StoreOperation? operation = null; + Interlocked.Add(ref _slotWaitTicks, Stopwatch.GetTimestamp() - slotStarted); + var active = Interlocked.Increment(ref _active); + UpdatePeak(active); + + var pending = new PendingTakeThis(); + var responseStarted = Stopwatch.GetTimestamp(); + var articleWritten = false; try { - operation = Begin(record); - var now = Interlocked.Increment(ref _active); - int previousPeak; - do + await _write.WaitAsync(cancellationToken).ConfigureAwait(false); + try { - previousPeak = Volatile.Read(ref _peakActive); - if (now <= previousPeak) + lock (_pendingGate) { - break; + _pending.Enqueue(pending); } - } - while (Interlocked.CompareExchange(ref _peakActive, now, previousPeak) != previousPeak); - return await SendAsync(operation, record, meta, cancellationToken).ConfigureAwait(false); - } - finally - { - if (operation is not null) + // ArtData is storage-stuffed; TAKETHIS needs the logical article with transfer stuffing only. + var logical = UnstuffCanonicalArticle(record.ArtData); + await WriteTakeThisAsync(messageId, logical, cancellationToken).ConfigureAwait(false); + articleWritten = true; + Interlocked.Add(ref _articleBytesWritten, record.ArtSize); + } + finally { - Interlocked.Decrement(ref _active); - lock (_tableGate) - { - _operations.Remove(operation.StreamId); - } - - operation.Dispose(); + _write.Release(); } - try + using var linked = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _lifetime.Token); + linked.CancelAfter(_ioTimeout); + var result = await pending.Completion.Task.WaitAsync(linked.Token).ConfigureAwait(false); + NoteResponse(Stopwatch.GetTimestamp() - responseStarted); + if (result.Succeeded) { - _slots.Release(); + ArticlePlacementTransportLogMessages.TakeThisCompleted(_logger, _fqdn, _port, result.Kind.ToString()); } - catch (ObjectDisposedException ex) + else { - ExpectedException.Ignore(ex); + ArticlePlacementTransportLogMessages.TakeThisRejected( + _logger, _fqdn, _port, result.Kind.ToString(), result.Failure); + } + + return result; + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + MarkDead(); + FailPending(pending, articleWritten + ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) + : new ArticlePlacementResult(ArticlePlacementKind.Cancelled)); + return articleWritten + ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) + : new ArticlePlacementResult(ArticlePlacementKind.Cancelled); + } + catch (OperationCanceledException) + { + MarkDead(); + var ambiguous = articleWritten + ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) + : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "timeout"); + FailPending(pending, ambiguous); + if (articleWritten) + { + ArticlePlacementTransportLogMessages.TakeThisAmbiguous(_logger, _fqdn, _port); } + + return ambiguous; + } + catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException) + { + MarkDead(); + var failure = articleWritten + ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) + : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, ex.GetType().Name); + FailPending(pending, failure); + return failure; + } + finally + { + Interlocked.Decrement(ref _active); + _slots.Release(); } } public async ValueTask DisposeAsync() { - if (Interlocked.Exchange(ref _disposed, 1) != 0) + if (Interlocked.Exchange(ref _disposed, 1) == 1) { return; } - var outstanding = ActiveStores; MarkDead(); try { - await _lifetime.CancelAsync().ConfigureAwait(false); + await _write.WaitAsync(CancellationToken.None).ConfigureAwait(false); + try + { + await _ssl.WriteAsync(Quit, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException) + { + ExpectedException.Ignore(ex); + } + finally + { + _write.Release(); + } } - catch (ObjectDisposedException ex) + catch (Exception ex) when (ex is ObjectDisposedException or IOException) { ExpectedException.Ignore(ex); } - try + await _lifetime.CancelAsync().ConfigureAwait(false); + if (_reader is not null) { - if (_reader is not null) + try { - await _reader.WaitAsync(TimeSpan.FromSeconds(2)).ConfigureAwait(false); + await _reader.ConfigureAwait(false); + } + catch (Exception ex) when (ex is IOException or ObjectDisposedException or OperationCanceledException) + { + ExpectedException.Ignore(ex); } - } - catch (TimeoutException ex) - { - ExpectedException.Ignore(ex); - } - catch (OperationCanceledException ex) - { - ExpectedException.Ignore(ex); } - FaultOutstanding(shutdown: true); - ArticlePlacementTransportLogMessages.Closed(_logger, _fqdn, _port, outstanding); + CompleteAllPending(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed")); + ArticlePlacementTransportLogMessages.Closed(_logger, _fqdn, _port, ActiveStores); await _ssl.DisposeAsync().ConfigureAwait(false); _tcp.Dispose(); _slots.Dispose(); @@ -288,625 +326,240 @@ public async ValueTask DisposeAsync() _lifetime.Dispose(); } - private StoreOperation Begin(ArticleRecord record) + private async Task HandshakeAsync(CancellationToken cancellationToken) { - lock (_tableGate) + var greeting = await ReadStatusLineAsync(cancellationToken).ConfigureAwait(false); + if (greeting.Code / 100 != 2) { - if (_nextStreamId == 0) - { - MarkDead(); - throw new IOException("stream-id-space"); - } + throw new IOException("nntp-greeting"); + } - var streamId = _nextStreamId++; - var operation = new StoreOperation(streamId, record.ArtId, _limits); - _operations.Add(streamId, operation); - return operation; + await _ssl.WriteAsync(ModeStream, cancellationToken).ConfigureAwait(false); + var mode = await ReadStatusLineAsync(cancellationToken).ConfigureAwait(false); + if (mode.Code != 203) + { + throw new IOException("mode-stream"); } + + _reader = Task.Run(() => ReadLoopAsync(_lifetime.Token), CancellationToken.None); } - private async Task SendAsync( - StoreOperation operation, - ArticleRecord record, - ArticleCanonicalTransferMeta meta, - CancellationToken cancellationToken) + private async Task ReadLoopAsync(CancellationToken cancellationToken) { - using var linked = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _lifetime.Token); try { - var idBytes = new byte[ArticleId.Length]; - record.ArtId.CopyTo(idBytes); - await WriteFrameAsync(VatpFrameEncoder.EncodeStore(operation.StreamId, idBytes), linked.Token) - .ConfigureAwait(false); - operation.Started = true; - var metaBytes = VatpMetaCodec.Encode(in meta); - operation.AwaitingInitialCredit = 1; - Interlocked.Increment(ref _storesAwaitingInitialCredit); - await WriteFrameAsync(VatpFrameEncoder.EncodeMeta(operation.StreamId, metaBytes), linked.Token) - .ConfigureAwait(false); - DisarmReadTimeout(); - - var artData = record.ArtData; - var offset = 0; - while (offset < record.ArtSize) + while (!cancellationToken.IsCancellationRequested && !IsDead) { - linked.Token.ThrowIfCancellationRequested(); - int length; - lock (operation.Gate) + var status = await ReadStatusLineAsync(cancellationToken).ConfigureAwait(false); + PendingTakeThis? next; + lock (_pendingGate) { - length = ArticleTransferReadyRing.ComputeDataPayloadLength( - record.ArtSize - offset, - operation.Window.Credit, - _maxFramePayload); - if (length > 0 && !operation.Window.TryConsume(length)) - { - length = 0; - } + next = _pending.Count > 0 ? _pending.Dequeue() : null; } - if (length <= 0) + if (next is null) { - var creditWait = System.Diagnostics.Stopwatch.GetTimestamp(); - await operation.Credit.WaitAsync(linked.Token).ConfigureAwait(false); - Interlocked.Add(ref _creditWaitTicks, System.Diagnostics.Stopwatch.GetTimestamp() - creditWait); - if (operation.IsCompleted) - { - return operation.Result; - } - - continue; + MarkDead(); + return; } - var fin = offset + length == record.ArtSize; - await WriteFrameAsync( - VatpFrameEncoder.EncodeData(operation.StreamId, artData.Slice(offset, length), fin), - linked.Token) - .ConfigureAwait(false); - Interlocked.Increment(ref _dataFramesWritten); - offset += length; - } - - await WriteFrameAsync(VatpFrameEncoder.EncodeEnd(operation.StreamId), linked.Token).ConfigureAwait(false); - operation.EndWritten = true; - var resultWait = System.Diagnostics.Stopwatch.GetTimestamp(); - var result = await operation.Completion.Task.WaitAsync(_ioTimeout, linked.Token).ConfigureAwait(false); - NoteResultWait(System.Diagnostics.Stopwatch.GetTimestamp() - resultWait); - LogOutcome(operation, result); - return result; - } - catch (TimeoutException) - { - var result = operation.EndWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "timeout"); - Finish(operation, result); - LogOutcome(operation, result); - return result; - } - catch (OperationCanceledException) when (operation.IsCompleted) - { - LogOutcome(operation, operation.Result); - return operation.Result; - } - catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) - { - if (operation.EndWritten) - { - var ambiguous = new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved); - Finish(operation, ambiguous); - LogOutcome(operation, ambiguous); - return ambiguous; - } - - await TryWriteCancelAsync(operation).ConfigureAwait(false); - var cancelled = new ArticlePlacementResult(ArticlePlacementKind.Cancelled); - Finish(operation, cancelled); - LogOutcome(operation, cancelled); - return cancelled; - } - catch (OperationCanceledException) - { - var result = _disposed != 0 - ? operation.EndWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.Cancelled) - : operation.EndWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed"); - Finish(operation, result); - LogOutcome(operation, result); - return result; - } - catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException) - { - MarkDead(); - var result = operation.EndWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, ex.GetType().Name); - Finish(operation, result); - LogOutcome(operation, result); - return result; - } - } - - private async Task ExchangeHelloAsync(CancellationToken cancellationToken) - { - await WriteFrameAsync(VatpFrameEncoder.EncodeHello(_maxFramePayload), cancellationToken).ConfigureAwait(false); - var frame = await ReadFrameAsync(cancellationToken).ConfigureAwait(false); - if (frame is null - || frame.Value.Header.Type != VatpFrameType.Hello - || !VatpHello.TryDecode(frame.Value.Payload, out var hello, out _)) - { - throw new IOException("hello"); - } - - _maxFramePayload = Math.Min(hello.MaxFramePayload, VatpProtocol.DefaultMaxFramePayload); - } - - private async Task ReadLoopAsync() - { - try - { - while (!IsDead && Volatile.Read(ref _disposed) == 0) - { - var frame = await ReadFrameAsync(_lifetime.Token).ConfigureAwait(false); - if (frame is null) + next.Completion.TrySetResult(ArticlePlacementResult.FromNntpStatus(status.Code)); + if (status.Code == 400) { MarkDead(); - FaultOutstanding(shutdown: false); + CompleteAllPending(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "temporary-failure")); return; } - - Dispatch(frame.Value); } } - catch (OperationCanceledException) + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { - FaultOutstanding(shutdown: Volatile.Read(ref _disposed) != 0); + CompleteAllPending(new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed")); } catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException or TimeoutException) { MarkDead(); - ArticlePlacementTransportLogMessages.ConnectionFailed(_logger, _fqdn, _port, ex.GetType().Name); - FaultOutstanding(shutdown: false); + CompleteAllPending(new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved)); } } - private void Dispatch(VatpParsedFrame frame) + private async Task WriteTakeThisAsync( + byte[] messageId, + ReadOnlyMemory article, + CancellationToken cancellationToken) { - if (frame.Header.Type == VatpFrameType.Hello) - { - return; - } - - if (frame.Header.StreamId == VatpProtocol.ConnectionStreamId) - { - MarkDead(); - FaultOutstanding(shutdown: false); - return; - } - - StoreOperation? operation; - lock (_tableGate) - { - _operations.TryGetValue(frame.Header.StreamId, out operation); - } - - if (operation is null) - { - return; - } + var commandLength = TakeThisPrefix.Length + messageId.Length + Crlf.Length; + var command = new byte[commandLength]; + TakeThisPrefix.CopyTo(command); + messageId.CopyTo(command.AsSpan(TakeThisPrefix.Length)); + Crlf.CopyTo(command.AsSpan(TakeThisPrefix.Length + messageId.Length)); + await _ssl.WriteAsync(command, cancellationToken).ConfigureAwait(false); + await WriteStuffedArticleAsync(article, cancellationToken).ConfigureAwait(false); + await _ssl.WriteAsync(Terminator, cancellationToken).ConfigureAwait(false); + } - switch (frame.Header.Type) + private async Task WriteStuffedArticleAsync(ReadOnlyMemory articleMemory, CancellationToken cancellationToken) + { + var offset = 0; + var atLineStart = true; + while (offset < articleMemory.Length) { - case VatpFrameType.Window: - if (!VatpControlPayload.TryDecodeWindow(Copy(frame.Payload), out var add)) - { - Finish(operation, new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "window")); - break; - } - - var gainedInitialCredit = false; - lock (operation.Gate) - { - gainedInitialCredit = operation.AwaitingInitialCredit == 1 && add > 0; - _ = operation.Window.Add(add); - } - - if (gainedInitialCredit && Interlocked.Exchange(ref operation.AwaitingInitialCredit, 0) == 1) - { - Interlocked.Decrement(ref _storesAwaitingInitialCredit); - } - - try - { - operation.Credit.Release(); - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } + var article = articleMemory.Span; + if (atLineStart && article[offset] == (byte)'.') + { + await _ssl.WriteAsync(new byte[] { (byte)'.' }, cancellationToken).ConfigureAwait(false); + article = articleMemory.Span; + } - break; - case VatpFrameType.Result: - if (!VatpResultPayload.TryDecode(frame.Payload, out var outcome, out _)) + var start = offset; + var end = offset; + while (end < article.Length) + { + var b = article[end++]; + if (b == (byte)'\n') { - Finish(operation, new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "result")); + atLineStart = true; break; } - Finish(operation, ArticlePlacementResult.FromOutcomeByte(outcome)); - break; - case VatpFrameType.Fail: - if (!VatpControlPayload.TryDecodeFail(Copy(frame.Payload), out var error, out _)) - { - Finish(operation, new ArticlePlacementResult( - operation.EndWritten - ? ArticlePlacementKind.AcknowledgementNotObserved - : ArticlePlacementKind.TransportFailure, - "fail")); - break; - } + atLineStart = false; + } - Finish(operation, error == VatpErrorCode.Cancelled - ? new ArticlePlacementResult(ArticlePlacementKind.Cancelled) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, error.ToString())); - break; - default: - Finish(operation, new ArticlePlacementResult( - ArticlePlacementKind.TransportFailure, - frame.Header.Type.ToString())); - break; + var chunk = article[start..end].ToArray(); + await _ssl.WriteAsync(chunk, cancellationToken).ConfigureAwait(false); + offset = end; } } - private void FaultOutstanding(bool shutdown) + /// + /// Drops one leading body stuffing dot from canonical ArtData so TAKETHIS can apply transfer stuffing once. + /// + private static byte[] UnstuffCanonicalArticle(ReadOnlyMemory artData) { - StoreOperation[] operations; - lock (_tableGate) + var span = artData.Span; + var separator = span.IndexOf("\r\n\r\n"u8); + if (separator < 0) { - operations = _operations.Values.ToArray(); - } - - foreach (var operation in operations) - { - var result = operation.EndWritten - ? new ArticlePlacementResult(ArticlePlacementKind.AcknowledgementNotObserved) - : shutdown - ? new ArticlePlacementResult(ArticlePlacementKind.Cancelled) - : new ArticlePlacementResult(ArticlePlacementKind.TransportFailure, "closed"); - Finish(operation, result); + return artData.ToArray(); } - } - private async Task TryWriteCancelAsync(StoreOperation operation) - { - if (IsDead) + var bodyStart = separator + 4; + using var ms = new MemoryStream(artData.Length); + ms.Write(span[..bodyStart]); + var atLineStart = true; + for (var i = bodyStart; i < span.Length; i++) { - return; - } - - try - { - await WriteFrameAsync(VatpFrameEncoder.EncodeCancel(operation.StreamId), CancellationToken.None) - .ConfigureAwait(false); - } - catch (Exception ex) when (ex is IOException or ObjectDisposedException or SocketException or OperationCanceledException) - { - ExpectedException.Ignore(ex); - } - } - - private async Task WriteFrameAsync(VatpFrameEncoder.EncodedFrame frame, CancellationToken cancellationToken) - { - await _write.WaitAsync(cancellationToken).ConfigureAwait(false); - try - { - try + var b = span[i]; + if (atLineStart && b == (byte)'.' && i + 1 < span.Length) { - if (!frame.Header.IsEmpty) - { - await _ssl.WriteAsync(frame.Header, cancellationToken).ConfigureAwait(false); - } - - if (!frame.Payload.IsEmpty) - { - await _ssl.WriteAsync(frame.Payload, cancellationToken).ConfigureAwait(false); - } + // Drop one storage stuffing dot; transfer stuffing is applied by WriteStuffedArticleAsync. + atLineStart = false; + continue; } - catch - { - MarkDead(); - throw; - } - } - finally - { - try - { - _write.Release(); - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } - } - } - private void NoteResultWait(long ticks) - { - Interlocked.Increment(ref _resultCount); - Interlocked.Add(ref _resultTicks, ticks); - long previous; - do - { - previous = Volatile.Read(ref _resultMaxTicks); - if (ticks <= previous) - { - return; - } + ms.WriteByte(b); + atLineStart = b == (byte)'\n'; } - while (Interlocked.CompareExchange(ref _resultMaxTicks, ticks, previous) != previous); + + return ms.ToArray(); } - private async Task ReadFrameAsync(CancellationToken cancellationToken) + private async Task<(int Code, string Line)> ReadStatusLineAsync(CancellationToken cancellationToken) { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + timeout.CancelAfter(_ioTimeout); while (true) { - if (_frames.TryTake(_maxFramePayload, out var frame)) - { - return frame; - } - - var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - lock (_readTimeoutGate) + for (var i = 0; i < _lineBuffer.Count - 1; i++) { - _activeReadTimeout = timeout; - if (Volatile.Read(ref _storesAwaitingInitialCredit) == 0) + if (_lineBuffer[i] == (byte)'\r' && _lineBuffer[i + 1] == (byte)'\n') { - timeout.CancelAfter(_ioTimeout); + var lineBytes = _lineBuffer.GetRange(0, i).ToArray(); + _lineBuffer.RemoveRange(0, i + 2); + return ParseStatus(lineBytes); } } - int read; - try - { - read = await _frames.ReadFromAsync(_ssl, timeout.Token).ConfigureAwait(false); - } - catch (OperationCanceledException) when (timeout.IsCancellationRequested && !cancellationToken.IsCancellationRequested) - { - throw new TimeoutException("placement-io"); - } - finally + var read = await _ssl.ReadAsync(_readBuffer, timeout.Token).ConfigureAwait(false); + if (read == 0) { - lock (_readTimeoutGate) - { - if (ReferenceEquals(_activeReadTimeout, timeout)) - { - _activeReadTimeout = null; - } - } - - timeout.Dispose(); + throw new EndOfStreamException("nntp-closed"); } - if (read == 0) + for (var i = 0; i < read; i++) { - return null; + _lineBuffer.Add(_readBuffer[i]); } } } - /// Completes one STORE and drops its initial-credit wait from the read-timeout count. - private void Finish(StoreOperation operation, ArticlePlacementResult result) + private static (int Code, string Line) ParseStatus(byte[] lineBytes) { - if (operation.TryFinish(result)) + var line = Encoding.ASCII.GetString(lineBytes); + if (line.Length < 3 + || !char.IsDigit(line[0]) + || !char.IsDigit(line[1]) + || !char.IsDigit(line[2])) { - Interlocked.Decrement(ref _storesAwaitingInitialCredit); + throw new IOException("nntp-status"); } - } - - /// - /// Disarms the outstanding read while at least one STORE is waiting for its first WINDOW. - /// The next read arms the timeout again once that count returns to zero. - /// - private void DisarmReadTimeout() - { - lock (_readTimeoutGate) - { - if (Volatile.Read(ref _storesAwaitingInitialCredit) == 0) - { - return; - } - - var timeout = _activeReadTimeout; - if (timeout is null) - { - return; - } - try - { - timeout.CancelAfter(Timeout.InfiniteTimeSpan); - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } - } + var code = ((line[0] - '0') * 100) + ((line[1] - '0') * 10) + (line[2] - '0'); + return (code, line); } - private void LogOutcome(StoreOperation operation, ArticlePlacementResult result) + private void FailPending(PendingTakeThis pending, ArticlePlacementResult result) { - if (result.Kind == ArticlePlacementKind.AcknowledgementNotObserved) - { - ArticlePlacementTransportLogMessages.StoreAmbiguous(_logger, _fqdn, _port, operation.StreamId); - return; - } - - if (result.Succeeded) + pending.Completion.TrySetResult(result); + lock (_pendingGate) { - ArticlePlacementTransportLogMessages.StoreCompleted( - _logger, _fqdn, _port, operation.StreamId, result.Kind.ToString()); - return; + // Leave orphaned slots; reader or dispose clears the queue. } - - ArticlePlacementTransportLogMessages.StoreRejected( - _logger, _fqdn, _port, operation.StreamId, result.Kind.ToString(), result.Failure); } - private void MarkDead() => Volatile.Write(ref _dead, 1); - - private static byte[] Copy(ReadOnlySequence payload) + private void CompleteAllPending(ArticlePlacementResult result) { - var bytes = new byte[payload.Length]; - payload.CopyTo(bytes); - return bytes; - } - - private sealed class StoreOperation : IDisposable - { - private int _completed; - - public StoreOperation(uint streamId, ArticleId articleId, ArticleTransferLimits limits) - { - StreamId = streamId; - ArticleId = articleId; - Window = new ArticleTransferWindow(0, limits.MaxStreamCreditBytes); - Credit = new SemaphoreSlim(0); - Completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - } - - public uint StreamId { get; } - - public ArticleId ArticleId { get; } - - public object Gate { get; } = new(); - - public ArticleTransferWindow Window; - - public SemaphoreSlim Credit { get; } - - public TaskCompletionSource Completion { get; } - - public bool Started { get; set; } - - private volatile bool _endWritten; - - public bool EndWritten - { - get => _endWritten; - set => _endWritten = value; - } - - public bool IsCompleted => Volatile.Read(ref _completed) != 0; - - public ArticlePlacementResult Result { get; private set; } - - /// 1 after META until the first WINDOW or a terminal result, then 0. - public int AwaitingInitialCredit; - - /// - /// Completes the operation once. - /// Returns when this call clears a pending initial-credit wait. - /// - public bool TryFinish(ArticlePlacementResult result) + lock (_pendingGate) { - var releasedCreditWait = Interlocked.Exchange(ref AwaitingInitialCredit, 0) == 1; - if (Interlocked.Exchange(ref _completed, 1) != 0) + while (_pending.Count > 0) { - return releasedCreditWait; + _pending.Dequeue().Completion.TrySetResult(result); } - - Result = result; - Completion.TrySetResult(result); - try - { - Credit.Release(); - } - catch (ObjectDisposedException ex) - { - ExpectedException.Ignore(ex); - } - - return releasedCreditWait; } - - public void Dispose() => Credit.Dispose(); } - /// Accumulates socket bytes and yields one parsed frame at a time. - private sealed class FrameBuffer - { - private byte[] _buffer = new byte[64 * 1024]; - private int _start; - private int _end; - - public bool TryTake(uint maxFramePayload, out VatpParsedFrame frame) - { - frame = default; - if (_end <= _start) - { - return false; - } - - var parsed = VatpFrameParser.ParseOneFrame( - new ReadOnlySequence(_buffer, _start, _end - _start), - maxFramePayload); - if (parsed.Status == VatpFrameParseStatus.Invalid) - { - throw new IOException(parsed.Error.ToString()); - } - - if (parsed.Status != VatpFrameParseStatus.Success || parsed.Frame is not { } parsedFrame) - { - return false; - } - - var payload = new byte[parsedFrame.Payload.Length]; - parsedFrame.Payload.CopyTo(payload); - frame = new VatpParsedFrame(parsedFrame.Header, new ReadOnlySequence(payload)); - _start += checked((int)parsed.ConsumedBytes); - if (_start == _end) - { - _start = 0; - _end = 0; - } - - return true; - } + private void MarkDead() => Interlocked.Exchange(ref _dead, 1); - public async ValueTask ReadFromAsync(Stream stream, CancellationToken cancellationToken) + private void UpdatePeak(int active) + { + while (true) { - Compact(); - if (_end == _buffer.Length) + var peak = Volatile.Read(ref _peakActive); + if (active <= peak || Interlocked.CompareExchange(ref _peakActive, active, peak) == peak) { - Array.Resize(ref _buffer, _buffer.Length * 2); + return; } - - var read = await stream.ReadAsync(_buffer.AsMemory(_end), cancellationToken).ConfigureAwait(false); - _end += read; - return read; } + } - private void Compact() + private void NoteResponse(long ticks) + { + Interlocked.Increment(ref _responseCount); + Interlocked.Add(ref _responseWaitTicks, ticks); + while (true) { - if (_start == 0) - { - return; - } - - if (_start == _end) + var max = Volatile.Read(ref _responseMaxTicks); + if (ticks <= max || Interlocked.CompareExchange(ref _responseMaxTicks, ticks, max) == max) { - _start = 0; - _end = 0; return; } - - Buffer.BlockCopy(_buffer, _start, _buffer, 0, _end - _start); - _end -= _start; - _start = 0; } } + + private sealed class PendingTakeThis + { + public TaskCompletionSource Completion { get; } = + new(TaskCreationOptions.RunContinuationsAsynchronously); + } } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementTransportLogMessages.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementTransportLogMessages.cs index 8983c832..31f62ed2 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementTransportLogMessages.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/ArticlePlacementTransportLogMessages.cs @@ -2,8 +2,7 @@ namespace VectorNNTPD.StorageService.Tests.Support.Placement { - - /// Source-generated logs for the long-lived placement connection. Not per DATA frame. + /// Source-generated logs for the long-lived placement connection. internal static partial class ArticlePlacementTransportLogMessages { [LoggerMessage( @@ -39,20 +38,20 @@ internal static partial class ArticlePlacementTransportLogMessages [LoggerMessage( EventId = 2645, Level = LogLevel.Debug, - Message = "Placement STORE completed fqdn={Fqdn} vatpPort={VatpPort} streamId={StreamId} outcome={Outcome}")] - public static partial void StoreCompleted(ILogger logger, string Fqdn, int VatpPort, uint StreamId, string Outcome); + Message = "Placement TAKETHIS completed fqdn={Fqdn} vatpPort={VatpPort} outcome={Outcome}")] + public static partial void TakeThisCompleted(ILogger logger, string Fqdn, int VatpPort, string Outcome); [LoggerMessage( EventId = 2646, Level = LogLevel.Warning, - Message = "Placement STORE rejected fqdn={Fqdn} vatpPort={VatpPort} streamId={StreamId} outcome={Outcome} failure={Failure}")] - public static partial void StoreRejected(ILogger logger, string Fqdn, int VatpPort, uint StreamId, string Outcome, string? Failure); + Message = "Placement TAKETHIS rejected fqdn={Fqdn} vatpPort={VatpPort} outcome={Outcome} failure={Failure}")] + public static partial void TakeThisRejected(ILogger logger, string Fqdn, int VatpPort, string Outcome, string? Failure); [LoggerMessage( EventId = 2647, Level = LogLevel.Warning, - Message = "Placement STORE acknowledgement not observed fqdn={Fqdn} vatpPort={VatpPort} streamId={StreamId}")] - public static partial void StoreAmbiguous(ILogger logger, string Fqdn, int VatpPort, uint StreamId); + Message = "Placement TAKETHIS acknowledgement not observed fqdn={Fqdn} vatpPort={VatpPort}")] + public static partial void TakeThisAmbiguous(ILogger logger, string Fqdn, int VatpPort); [LoggerMessage( EventId = 2648, diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpClientOptions.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageFeedClientOptions.cs similarity index 63% rename from tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpClientOptions.cs rename to tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageFeedClientOptions.cs index 5dd19e70..c75fe29b 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpClientOptions.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageFeedClientOptions.cs @@ -1,8 +1,7 @@ namespace VectorNNTPD.StorageService.Tests.Support.Placement { - - /// Internal VATP client resource and timeout defaults. - internal sealed class VatpClientOptions + /// Internal NNTP feed client resource and timeout defaults. + internal sealed class StorageFeedClientOptions { /// Maximum TLS connections per cache endpoint. public int MaxConnectionsPerEndpoint { get; init; } = 4; @@ -13,10 +12,7 @@ internal sealed class VatpClientOptions /// TLS handshake timeout. public TimeSpan TlsHandshakeTimeout { get; init; } = TimeSpan.FromSeconds(15); - /// - /// Per-read I/O timeout while a connection is active. - /// It is not armed while a STORE on the connection is still waiting for its first WINDOW. - /// + /// Per-read I/O timeout while a connection is active. public TimeSpan IoTimeout { get; init; } = TimeSpan.FromMinutes(5); } } diff --git a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpTlsClient.cs b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageTlsClient.cs similarity index 93% rename from tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpTlsClient.cs rename to tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageTlsClient.cs index feaf4109..39bec99d 100644 --- a/tests/VectorNNTPD.StorageService.Tests/Support/Placement/VatpTlsClient.cs +++ b/tests/VectorNNTPD.StorageService.Tests/Support/Placement/StorageTlsClient.cs @@ -3,11 +3,10 @@ namespace VectorNNTPD.StorageService.Tests.Support.Placement { - /// - /// Shared outbound TLS client options and handshake for VATP cache connections. + /// Shared outbound TLS client options and handshake for StorageServer feed connections. /// - internal static class VatpTlsClient + internal static class StorageTlsClient { internal static SslClientAuthenticationOptions CreateClientOptions( string host,