diff --git a/src/VectorNNTPD.BackFillerService/Retention/ArticleRetentionAuthority.cs b/src/VectorNNTPD.BackFillerService/Retention/ArticleRetentionAuthority.cs index ce3e9e23..cf4cba99 100644 --- a/src/VectorNNTPD.BackFillerService/Retention/ArticleRetentionAuthority.cs +++ b/src/VectorNNTPD.BackFillerService/Retention/ArticleRetentionAuthority.cs @@ -60,10 +60,9 @@ internal sealed class ArticleRetentionAuthority : IArticleRetentionAuthority, IA private readonly Dictionary _byRequestId = []; /// - /// Admission order, oldest at . FIFO reclaim and TTL sweep - /// walk from that end. is the admission time plus - /// , so this order matches expiry order when does not - /// move backward. then stops at the first unexpired node. + /// Admission order, oldest at . FIFO reclaim walks from that end. + /// TTL sweep also walks from that end but examines every node: expiry timestamps need not match + /// insertion order when moves backward between admissions. /// private readonly LinkedList _insertionOrder = []; @@ -755,7 +754,7 @@ private static string CreateDeliveryKey(ReadOnlySpan messageIdOctets) }); } - /// Reclaims TTL-expired entries in insertion order. + /// Reclaims every TTL-expired entry, walking insertion order without assuming expiry order. /// /// Payload bytes the authority released on this pass. Zero when the authority is disposed or nothing expired. /// An active transfer does not keep those bytes in the retained total. @@ -763,9 +762,8 @@ private static string CreateDeliveryKey(ReadOnlySpan messageIdOctets) /// /// Does not throw . Does not close admission. /// Writes only when the returned total is positive. - /// The walk stops at the first entry with still in the future. - /// That is complete when admission timestamps are non-decreasing, because every entry uses the same - /// . A clock that moves backward can leave a later, already-expired node in the list. + /// Every entry with less than or equal to the current clock is removed, + /// including later insertions that expired earlier after a backward clock step. /// public long SweepExpired() { @@ -895,17 +893,17 @@ private long ReclaimForAdmissionLocked(int requiredBytes, DateTimeOffset now) } /// - /// Unindexes every expired entry from the oldest insertion-order node until the first - /// still in the future. + /// Unindexes every entry whose is less than or equal to + /// , walking the full insertion-order list. /// /// Clock reading compared with . /// /// Bytes the authority detached. An active transfer does not delay that subtraction. /// /// - /// The early stop is valid when does not move backward between admissions. - /// is the admission timestamp plus the same , - /// so a later node then expires later. A backward clock step can leave a later expired node unvisited. + /// Captures before removal so unindex is safe during the walk. + /// Unexpired nodes are skipped rather than ending the walk, so a backward clock step cannot leave a + /// later-expired entry resident behind an earlier unexpired neighbor. /// private long ExpireEligibleLocked(DateTimeOffset now) { @@ -915,12 +913,7 @@ private long ExpireEligibleLocked(DateTimeOffset now) { var entry = node.Value; var next = node.Next; - if (entry.ExpiresUtc > now) - { - break; - } - - if (RemoveExpiredLocked(entry)) + if (IsExpiredLocked(entry, now) && RemoveExpiredLocked(entry)) { released += entry.WasPhysicallyDisposedThisRemoval ? entry.PayloadBytes : 0; } diff --git a/tests/VectorNNTPD.BackFillerService.Tests/Retention/ArticleRetentionSweepClockTests.cs b/tests/VectorNNTPD.BackFillerService.Tests/Retention/ArticleRetentionSweepClockTests.cs new file mode 100644 index 00000000..f7826a1e --- /dev/null +++ b/tests/VectorNNTPD.BackFillerService.Tests/Retention/ArticleRetentionSweepClockTests.cs @@ -0,0 +1,158 @@ +using VectorNNTPD.BackFillerService.Retention; +using VectorNNTPD.BackFillerService.Tests.Fixtures; +using VectorNNTPD.BackFillerService.Tests.TestDoubles; + +namespace VectorNNTPD.BackFillerService.Tests.Retention +{ + /// + /// Retention TTL sweep behaviour when wall-clock admission timestamps are not monotonic (issue #189). + /// + public sealed class ArticleRetentionSweepClockTests + { + /// Fixed UTC anchor for explicit expiry arithmetic. + private static readonly DateTimeOffset Start = new(2026, 9, 26, 12, 0, 0, TimeSpan.Zero); + + /// + /// Issue #189: after a backward clock step, a later-admitted entry can expire before an earlier + /// insertion. The sweep must remove the expired later entry without waiting for the earlier one. + /// + [Fact] + public async Task Sweep_removes_later_expired_entry_after_backward_clock_step() + { + var ttl = TimeSpan.FromHours(1); + var time = new ManualTimeProvider(Start); + await using var authority = ArticleRetentionAuthorityTests.Create(time, maxBytes: 1024 * 1024, ttl: ttl); + + var articleA = RetentionTestArticles.RetainPrepared(authority, "", "aaaa\r\n"); + time.Advance(TimeSpan.FromMinutes(30)); + var articleB = RetentionTestArticles.RetainPrepared(authority, "", "bbbbbb\r\n"); + + // Backward step so C is admitted earlier on the clock than A, but later in insertion order. + time.SetUtcNow(Start.AddMinutes(-30)); + var articleC = RetentionTestArticles.RetainPrepared(authority, "", "cc\r\n"); + + // C expires at Start+30m; A expires at Start+1h; B expires at Start+90m. + time.SetUtcNow(Start.AddMinutes(30)); + var beforeBytes = authority.RetainedPayloadBytes; + Assert.Equal( + articleA.Record.ArtSize + articleB.Record.ArtSize + articleC.Record.ArtSize, + beforeBytes); + + var released = authority.SweepExpired(); + + Assert.Equal(articleC.Record.ArtSize, released); + Assert.Equal(beforeBytes - articleC.Record.ArtSize, authority.RetainedPayloadBytes); + Assert.Equal(2, authority.RetainedCount); + ArticleRetentionAuthorityTests.AssertNotFound(authority, ""); + using var claimA = ArticleRetentionAuthorityTests.Claim(authority, "", articleA.ArtData); + using var claimB = ArticleRetentionAuthorityTests.Claim(authority, "", articleB.ArtData); + } + + /// + /// One sweep removes every expired entry even when unexpired neighbors sit between them in insertion order. + /// Admission times are chosen so no intermediate retain expires the drop entries early via reclaim. + /// + [Fact] + public async Task Sweep_removes_multiple_expired_entries_separated_by_unexpired_neighbors() + { + var ttl = TimeSpan.FromMinutes(10); + var time = new ManualTimeProvider(Start); + await using var authority = ArticleRetentionAuthorityTests.Create(time, maxBytes: 1024 * 1024, ttl: ttl); + + var keepEarly = RetentionTestArticles.RetainPrepared(authority, "", "keep1\r\n"); + // dropFirst expires at Start+5m; still live when keepMiddle is admitted at Start. + time.SetUtcNow(Start.AddMinutes(-5)); + var dropFirst = RetentionTestArticles.RetainPrepared(authority, "", "d1\r\n"); + time.SetUtcNow(Start); + var keepMiddle = RetentionTestArticles.RetainPrepared(authority, "", "keep22\r\n"); + time.SetUtcNow(Start.AddMinutes(-3)); + var dropSecond = RetentionTestArticles.RetainPrepared(authority, "", "d22\r\n"); + + // dropFirst at Start+5m, dropSecond at Start+7m, keep* at Start+10m. + time.SetUtcNow(Start.AddMinutes(7)); + var beforeBytes = authority.RetainedPayloadBytes; + var expectedReleased = dropFirst.Record.ArtSize + dropSecond.Record.ArtSize; + Assert.Equal( + keepEarly.Record.ArtSize + keepMiddle.Record.ArtSize + expectedReleased, + beforeBytes); + + var released = authority.SweepExpired(); + + Assert.Equal(expectedReleased, released); + Assert.Equal(beforeBytes - expectedReleased, authority.RetainedPayloadBytes); + Assert.Equal(2, authority.RetainedCount); + ArticleRetentionAuthorityTests.AssertNotFound(authority, ""); + ArticleRetentionAuthorityTests.AssertNotFound(authority, ""); + using var claimKeepEarly = ArticleRetentionAuthorityTests.Claim( + authority, + "", + keepEarly.ArtData); + using var claimKeepMiddle = ArticleRetentionAuthorityTests.Claim( + authority, + "", + keepMiddle.ArtData); + Assert.Equal(0, authority.SweepExpired()); + } + + /// An entry whose ExpiresUtc equals the sweep clock is removed; a still-future entry is kept. + [Fact] + public async Task Sweep_expires_at_exact_deadline_and_retains_future_entries() + { + var ttl = TimeSpan.FromSeconds(10); + var time = new ManualTimeProvider(Start); + await using var authority = ArticleRetentionAuthorityTests.Create(time, maxBytes: 1024 * 1024, ttl: ttl); + + var due = RetentionTestArticles.RetainPrepared(authority, "", "due\r\n"); + time.Advance(TimeSpan.FromSeconds(5)); + var future = RetentionTestArticles.RetainPrepared(authority, "", "future\r\n"); + + time.SetUtcNow(Start.AddSeconds(10)); + var released = authority.SweepExpired(); + + Assert.Equal(due.Record.ArtSize, released); + Assert.Equal(future.Record.ArtSize, authority.RetainedPayloadBytes); + Assert.Equal(1, authority.RetainedCount); + ArticleRetentionAuthorityTests.AssertNotFound(authority, ""); + using var claim = ArticleRetentionAuthorityTests.Claim(authority, "", future.ArtData); + } + + /// + /// FIFO capacity reclaim still evicts the oldest unpinned insertion, not the earliest ExpiresUtc. + /// + [Fact] + public async Task Fifo_reclaim_still_follows_insertion_order_after_backward_clock_admissions() + { + var ttl = TimeSpan.FromHours(1); + var time = new ManualTimeProvider(Start); + var one = RetentionTestArticles.Create("", "11111\r\n"); + var two = RetentionTestArticles.Create("", "22222\r\n"); + var three = RetentionTestArticles.Create("", "333\r\n"); + var maxBytes = one.Record.ArtSize + two.Record.ArtSize; + await using var authority = ArticleRetentionAuthorityTests.Create(time, maxBytes: maxBytes, ttl: ttl); + + Assert.Equal( + ArticleRetentionKind.Retained, + authority.RetainCanonical(one.MessageId, one.RequestId, one.Record, one.SelectedDateHeaderName).Kind); + + time.SetUtcNow(Start.AddMinutes(-30)); + Assert.Equal( + ArticleRetentionKind.Retained, + authority.RetainCanonical(two.MessageId, two.RequestId, two.Record, two.SelectedDateHeaderName).Kind); + + // Unpin oldest insertion (one). Two stays pinned by its openable RequestId. + Assert.True(authority.TryCancelPendingRequest(one.RequestId)); + + time.SetUtcNow(Start); + var third = authority.RetainCanonical( + three.MessageId, + three.RequestId, + three.Record, + three.SelectedDateHeaderName); + + Assert.Equal(ArticleRetentionKind.Retained, third.Kind); + ArticleRetentionAuthorityTests.AssertNotFound(authority, ""); + using var stillHeld = ArticleRetentionAuthorityTests.Claim(authority, "", two.ArtData); + Assert.Equal(two.Record.ArtSize + three.Record.ArtSize, authority.RetainedPayloadBytes); + } + } +} diff --git a/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ManualTimeProvider.cs b/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ManualTimeProvider.cs index d8cae920..873838f7 100644 --- a/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ManualTimeProvider.cs +++ b/tests/VectorNNTPD.BackFillerService.Tests/TestDoubles/ManualTimeProvider.cs @@ -77,6 +77,19 @@ internal void Advance(TimeSpan delta) } } + /// + /// Sets the clock to an absolute UTC instant without firing timers. + /// Used to simulate NTP or operator steps, including a backward adjustment. + /// + /// New instant returned from . + internal void SetUtcNow(DateTimeOffset utcNow) + { + lock (_gate) + { + _utcNow = utcNow; + } + } + /// Adds a timer created by to the set evaluated during . /// Timer instance to track; must belong to this provider. private void Register(ManualTimer timer)