|
| 1 | +using KeyLoad.Query; |
| 2 | +using KeyLoad.Query.Search; |
| 3 | +using KeyLoad.Server.Search; |
| 4 | +using KeyLoad.Storage; |
| 5 | + |
| 6 | +namespace KeyLoad.FunctionalTests.Search; |
| 7 | + |
| 8 | +internal sealed class TextGenerationRetirementTests |
| 9 | +{ |
| 10 | + private const string Collection = "text-retirement-publication"; |
| 11 | + private const string Field = "/text"; |
| 12 | + private const string Term = "needle"; |
| 13 | + private const string Revised = """{"text":"needle revised"}"""; |
| 14 | + |
| 15 | + [Test] |
| 16 | + public async Task CurrentSearchRemainsReadableAtTheRetiredDirectoryDeletionBoundary() |
| 17 | + { |
| 18 | + using var database = new TestDatabase(); |
| 19 | + database.Configure(Collection, ResourceKind.Collection); |
| 20 | + database.Commit(new PutDocument(Collection, "one", """{"text":"needle original"}""")); |
| 21 | + var cleanup = new TextGenerationFixtureLifetime(); |
| 22 | + await cleanup.RunAsync(async () => |
| 23 | + { |
| 24 | + var root = Path.Combine(database.Directory, "text-retirement-publication"); |
| 25 | + var token = TestContext.Current!.Execution.CancellationToken; |
| 26 | + var request = new SearchRequest(database.Partition, Collection, Field, Term); |
| 27 | + SearchEngine? engine = null; |
| 28 | + Task<RankedDocument[]>? overlapping = null; |
| 29 | + var observed = 0; |
| 30 | + var projection = cleanup.TrackProjection(new(root, database.Store.Identity.NodeId, UnitTextOptions.Execution(), |
| 31 | + faultObserver: stage => |
| 32 | + { |
| 33 | + if (stage == TextFaultStage.RetiredGenerationDeleted) |
| 34 | + { |
| 35 | + observed++; |
| 36 | + overlapping = engine!.SearchAsync("root", request, token); |
| 37 | + } |
| 38 | + })); |
| 39 | + engine = new(database.Database, UnitExecutionOptions.QueryExecution(), projection); |
| 40 | + var original = await engine.SearchAsync("root", request, token); |
| 41 | + var retired = GenerationPaths(root).Single(); |
| 42 | + var oldBudget = new ReadExecutionBudget(); |
| 43 | + var retained = cleanup.TrackLease(projection.Acquire(CaptureScope(database), oldBudget)); |
| 44 | + retained.BeginRecord(original[0].Document.Reference, original[0].Document.Revision); |
| 45 | + retained.ObserveToken(Term); |
| 46 | + database.Commit(new PutDocument(Collection, "one", Revised)); |
| 47 | + var replacement = await engine.SearchAsync("root", request, token); |
| 48 | + retained.VerifyCandidates([Term], [original[0].Document.Reference], oldBudget); |
| 49 | + cleanup.SettleLease(retained); |
| 50 | + await Assert.That(observed).IsEqualTo(1); |
| 51 | + var actual = await (overlapping ?? throw new InvalidOperationException("The retirement boundary did not start its current read.")); |
| 52 | + await Assert.That(actual).HasSingleItem(); |
| 53 | + await Assert.That(actual[0].Document.Revision).IsEqualTo(2L); |
| 54 | + await Assert.That(actual[0].Document.Json).IsEqualTo(Revised); |
| 55 | + await Assert.That(JsonDefaults.Serialize(actual).AsSpan().SequenceEqual(JsonDefaults.Serialize(replacement))).IsTrue(); |
| 56 | + await Assert.That(Directory.Exists(retired)).IsFalse(); |
| 57 | + await Assert.That(GenerationPaths(root).Length).IsEqualTo(1); |
| 58 | + var healthy = await engine.SearchAsync("root", request, token); |
| 59 | + await Assert.That(JsonDefaults.Serialize(healthy).AsSpan().SequenceEqual(JsonDefaults.Serialize(replacement))).IsTrue(); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private static string[] GenerationPaths(string root) |
| 64 | + => Directory.EnumerateDirectories(root) |
| 65 | + .Where(path => Path.GetFileName(path).StartsWith(TextProtocol.GenerationPrefix, StringComparison.Ordinal)) |
| 66 | + .Order(StringComparer.Ordinal).ToArray(); |
| 67 | + |
| 68 | + private static TextProjectionScope CaptureScope(TestDatabase database) |
| 69 | + { |
| 70 | + var identity = database.Store.Identity; |
| 71 | + var principal = database.Store.Read(view => view.GetRecord<PrincipalRecord>(KeySpace.Principal("root")))!; |
| 72 | + var resource = database.Store.Read(view => view.GetRecord<ResourceDefinition>(KeySpace.Resource( |
| 73 | + database.Partition.TenantId, database.Partition.DatabaseId, Collection)))!; |
| 74 | + return new(identity.NodeId, identity.Incarnation, identity.FormatVersion, identity.ReadGeneration, |
| 75 | + database.Store.Position, database.Partition, Collection, Field, principal.Id, principal.PolicyEpoch, |
| 76 | + resource.SchemaVersion); |
| 77 | + } |
| 78 | +} |
0 commit comments