fix(kpi): rollup backfill plans from its own persisted watermark, not the periodic fold's MAX (KPI-1, WP4 task 4.10)
This commit is contained in:
+34
@@ -487,6 +487,40 @@ public class KpiHistoryRepositoryTests
|
||||
Assert.Empty(ctx.ChangeTracker.Entries<KpiSample>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KPI-1: the backfill's own watermark is absent until a slice advances it, then moves only
|
||||
/// forward, and a contiguity-guarded advance neither creates it nor jumps a gap.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task RollupBackfillWatermark_IsMonotonic_AndContiguityGuarded()
|
||||
{
|
||||
await using var ctx = NewContext();
|
||||
var repo = new KpiHistoryRepository(ctx);
|
||||
|
||||
Assert.Null(await repo.GetRollupBackfillWatermarkAsync());
|
||||
|
||||
// A contiguity-guarded advance (the periodic fold's) never creates the marker.
|
||||
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(5), onlyIfReachedUtc: Base.AddHours(2));
|
||||
Assert.Null(await repo.GetRollupBackfillWatermarkAsync());
|
||||
|
||||
// A backfill slice creates and advances it.
|
||||
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(24));
|
||||
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
|
||||
|
||||
// Never backwards.
|
||||
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(10));
|
||||
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
|
||||
|
||||
// A periodic fold whose window starts past the marker (a gap) does not move it…
|
||||
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(30), onlyIfReachedUtc: Base.AddHours(27));
|
||||
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
|
||||
|
||||
// …one that the marker reaches does.
|
||||
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(26), onlyIfReachedUtc: Base.AddHours(23));
|
||||
Assert.Equal(Base.AddHours(26), await repo.GetRollupBackfillWatermarkAsync());
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the latest rollup hour is null when no rollups exist.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
|
||||
@@ -239,6 +239,57 @@ public class KpiHistoryRecorderActorTests : TestKit
|
||||
return Task.FromResult(_latestRollupHour);
|
||||
}
|
||||
}
|
||||
|
||||
private DateTime? _backfillWatermark;
|
||||
private int _backfillWatermarkQueryCount;
|
||||
private readonly List<(DateTime Through, DateTime? OnlyIfReached)> _advances = new();
|
||||
|
||||
/// <summary>The backfill's own persisted watermark the fake reports (settable; KPI-1).</summary>
|
||||
public DateTime? BackfillWatermark
|
||||
{
|
||||
get { lock (_gate) { return _backfillWatermark; } }
|
||||
set { lock (_gate) { _backfillWatermark = value; } }
|
||||
}
|
||||
|
||||
/// <summary>Number of times the backfill plan pass read the backfill watermark.</summary>
|
||||
public int BackfillWatermarkQueryCount
|
||||
{
|
||||
get { lock (_gate) { return _backfillWatermarkQueryCount; } }
|
||||
}
|
||||
|
||||
/// <summary>Every watermark advance requested, in call order.</summary>
|
||||
public IReadOnlyList<(DateTime Through, DateTime? OnlyIfReached)> Advances
|
||||
{
|
||||
get { lock (_gate) { return _advances.ToArray(); } }
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<DateTime?> GetRollupBackfillWatermarkAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_backfillWatermarkQueryCount++;
|
||||
return Task.FromResult(_backfillWatermark);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task AdvanceRollupBackfillWatermarkAsync(
|
||||
DateTime backfilledThroughUtc, DateTime? onlyIfReachedUtc = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_advances.Add((backfilledThroughUtc, onlyIfReachedUtc));
|
||||
var reached = onlyIfReachedUtc is null
|
||||
|| (_backfillWatermark is { } w && w >= onlyIfReachedUtc.Value);
|
||||
if (reached && (onlyIfReachedUtc is null || _backfillWatermark is not null)
|
||||
&& (_backfillWatermark is null || _backfillWatermark < backfilledThroughUtc))
|
||||
{
|
||||
_backfillWatermark = backfilledThroughUtc;
|
||||
}
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1168,7 +1219,7 @@ public class KpiHistoryRecorderActorTests : TestKit
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the backfill is skipped when rollups are already current.</summary>
|
||||
/// <summary>Verifies that the backfill is skipped when its own watermark is already current.</summary>
|
||||
[Fact]
|
||||
public void Backfill_Skips_WhenRollupsAlreadyCurrent()
|
||||
{
|
||||
@@ -1187,17 +1238,18 @@ public class KpiHistoryRecorderActorTests : TestKit
|
||||
RollupLookbackHours = 3,
|
||||
});
|
||||
|
||||
// Rollups current through one hour ago — well inside the 3 h lookback tail.
|
||||
// The BACKFILL's own watermark is current through one hour ago (KPI-1: the plan reads its
|
||||
// own persisted marker, never the periodic fold's MAX(HourStartUtc)).
|
||||
var nowHour = new DateTime(
|
||||
DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
|
||||
DateTime.UtcNow.Hour, 0, 0, DateTimeKind.Utc);
|
||||
repository.LatestRollupHour = nowHour - TimeSpan.FromHours(1);
|
||||
repository.BackfillWatermark = nowHour - TimeSpan.FromHours(1);
|
||||
|
||||
actor.Tell(KpiHistoryRecorderActor.BackfillTick.Instance);
|
||||
|
||||
// The plan pass must consult the watermark…
|
||||
AwaitAssert(
|
||||
() => Assert.True(repository.LatestRollupHourQueryCount >= 1),
|
||||
() => Assert.True(repository.BackfillWatermarkQueryCount >= 1),
|
||||
duration: TimeSpan.FromSeconds(3),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
|
||||
@@ -1234,7 +1286,7 @@ public class KpiHistoryRecorderActorTests : TestKit
|
||||
DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
|
||||
DateTime.UtcNow.Hour, 0, 0, DateTimeKind.Utc);
|
||||
var watermark = nowHour - TimeSpan.FromDays(2);
|
||||
repository.LatestRollupHour = watermark;
|
||||
repository.BackfillWatermark = watermark;
|
||||
|
||||
actor.Tell(KpiHistoryRecorderActor.BackfillTick.Instance);
|
||||
|
||||
@@ -1250,4 +1302,114 @@ public class KpiHistoryRecorderActorTests : TestKit
|
||||
duration: TimeSpan.FromSeconds(5),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KPI-1 regression: the periodic fold has just written the last few hours (so
|
||||
/// MAX(HourStartUtc) is an hour ago) but the backfill has never run — e.g. an upgrade onto 90
|
||||
/// days of raw samples. The backfill must fold the whole retention window, not latch itself
|
||||
/// "current" from the periodic fold's MAX.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Backfill_IgnoresThePeriodicFoldsMax_AndFoldsTheWholeWindow()
|
||||
{
|
||||
var repository = new RecordingRepository();
|
||||
var sp = BuildServiceProvider(repository, new HealthySource());
|
||||
var actor = CreateActor(sp, new KpiHistoryOptions
|
||||
{
|
||||
SampleInterval = TimeSpan.FromHours(1),
|
||||
PurgeInterval = TimeSpan.FromHours(1),
|
||||
RollupInterval = TimeSpan.FromHours(1),
|
||||
RetentionDays = 5,
|
||||
RollupLookbackHours = 3,
|
||||
});
|
||||
|
||||
var nowHour = new DateTime(
|
||||
DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
|
||||
DateTime.UtcNow.Hour, 0, 0, DateTimeKind.Utc);
|
||||
repository.LatestRollupHour = nowHour - TimeSpan.FromHours(1); // periodic fold's MAX
|
||||
repository.BackfillWatermark = null; // backfill never ran
|
||||
|
||||
actor.Tell(KpiHistoryRecorderActor.BackfillTick.Instance);
|
||||
|
||||
AwaitAssert(
|
||||
() =>
|
||||
{
|
||||
var windows = repository.FoldWindows;
|
||||
Assert.Equal(5, windows.Count);
|
||||
Assert.Equal(nowHour - TimeSpan.FromDays(5), windows[0].FromHourUtc);
|
||||
// Each folded slice advanced the backfill's own marker, ending at the plan's end.
|
||||
Assert.Equal(nowHour, repository.BackfillWatermark);
|
||||
},
|
||||
duration: TimeSpan.FromSeconds(5),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KPI-1: an interrupted backfill resumes from its own watermark (the next singleton plans from
|
||||
/// where the completed slices got to), not from the rollup table's MAX.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Backfill_ResumesFromItsOwnWatermark()
|
||||
{
|
||||
var repository = new RecordingRepository();
|
||||
var sp = BuildServiceProvider(repository, new HealthySource());
|
||||
var actor = CreateActor(sp, new KpiHistoryOptions
|
||||
{
|
||||
SampleInterval = TimeSpan.FromHours(1),
|
||||
PurgeInterval = TimeSpan.FromHours(1),
|
||||
RollupInterval = TimeSpan.FromHours(1),
|
||||
RetentionDays = 90,
|
||||
RollupLookbackHours = 3,
|
||||
});
|
||||
|
||||
var nowHour = new DateTime(
|
||||
DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day,
|
||||
DateTime.UtcNow.Hour, 0, 0, DateTimeKind.Utc);
|
||||
repository.LatestRollupHour = nowHour - TimeSpan.FromHours(1);
|
||||
repository.BackfillWatermark = nowHour - TimeSpan.FromDays(40);
|
||||
|
||||
actor.Tell(KpiHistoryRecorderActor.BackfillTick.Instance);
|
||||
|
||||
AwaitAssert(
|
||||
() =>
|
||||
{
|
||||
var windows = repository.FoldWindows;
|
||||
Assert.NotEmpty(windows);
|
||||
Assert.Equal(nowHour - TimeSpan.FromDays(40), windows[0].FromHourUtc);
|
||||
},
|
||||
duration: TimeSpan.FromSeconds(5),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// KPI-1: the periodic fold extends a completed backfill's marker only with the contiguity
|
||||
/// guard (only-if-reached = the fold's lower bound), so it can never jump a gap.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void PeriodicFold_AdvancesTheBackfillMarker_OnlyContiguously()
|
||||
{
|
||||
var repository = new RecordingRepository();
|
||||
var sp = BuildServiceProvider(repository, new HealthySource());
|
||||
var actor = CreateActor(sp, new KpiHistoryOptions
|
||||
{
|
||||
SampleInterval = TimeSpan.FromHours(1),
|
||||
PurgeInterval = TimeSpan.FromHours(1),
|
||||
RollupInterval = TimeSpan.FromHours(1),
|
||||
RetentionDays = 90,
|
||||
RollupLookbackHours = 3,
|
||||
});
|
||||
|
||||
actor.Tell(KpiHistoryRecorderActor.RollupTick.Instance);
|
||||
|
||||
AwaitAssert(
|
||||
() =>
|
||||
{
|
||||
var advance = Assert.Single(repository.Advances);
|
||||
Assert.Equal(repository.FoldFromHourUtc, advance.OnlyIfReached);
|
||||
Assert.Equal(repository.FoldToHourUtc, advance.Through);
|
||||
Assert.Null(repository.BackfillWatermark); // never created by the periodic fold
|
||||
},
|
||||
duration: TimeSpan.FromSeconds(5),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user