Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Darling.Tests;
/// failure at Warning, but only when the caller hands it a logger. These pins hold the two FinOps tools that were left passing none
/// (get_finops index_analysis and get_finops_recommendations) to the same shape as get_wait_stats (<c>ILogger? logger = null</c> just
/// before the cancellation token), and hold that no non-test caller of a logger-taking <c>CurrentDatabaseScope</c> method leaves the
/// logger off, so a new caller cannot reopen the gap. The Viewer reaches the same readers with no host logger; it calls only the
/// readers, whose logger parameter is optional, never <c>CurrentDatabaseScope</c> directly.
/// logger off, so a new caller cannot reopen the gap. The Viewer has no host logger: it reaches most of these through the readers, whose
/// logger parameter is optional, and the one direct call (the regressions grid, #6654) passes its own ViewerStoreLogger.
/// </summary>
[Trait("Stage", "Guard")] // #5798: walks the repo tree, so the guard stage runs it
public sealed class CurrentDatabaseScopeLoggerThreadingTests
Expand Down
4 changes: 3 additions & 1 deletion Darling/Darling.Tests/DarlingSelfQueryMarkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace Darling.Tests;

/// <summary>
/// Release walk V8, Darling side: the service's own statements against a monitored SQL Server carry the
/// <c>PerformanceMonitorLite</c> marker the collectors' self-exclusion reads, so they never top a Top Queries or Query Store list.
/// <c>PerformanceMonitorLite</c> marker the collectors' self-exclusion reads. On Darling the Query Store collector's drop of the
/// statement does not fire (the text is fetched separately, so the row it tests is empty); the readers that rank Query Store
/// queries hide it instead (<c>QueryStoreSelfStatement</c>, #6665).
/// Darling runs the shared Query Store collector in its own shape (plan XML and query text fetched by id), which Lite's census
/// does not build, so the by-id fetches, the database probe and the connect-time detection query are checked here.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion Darling/Darling.Tests/InvestigationEvidenceFamiliesLiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@ await RunAsync(async (setup, ct) =>
await PlantQueryStoreAsync(setup.Connection, ct, 511, 22, 2, cpuUs: 10_000, lastExec: AlertAt.AddMinutes(-1));
await PlantQueryStoreAsync(setup.Connection, ct, 512, 23, 1, cpuUs: 1_000, lastExec: AlertAt.AddMinutes(-4));
await PlantQueryStoreAsync(setup.Connection, ct, 512, 24, 2, cpuUs: 100_000, lastExec: AlertAt.AddMinutes(-1));
foreach (var (queryId, text) in new[] { (511L, "SELECT * FROM dbo.Orders"), (512L, "(@csn bigint,@batch_size int)delete top(@batch_size) from sys.[change_tracking_1228027148] where sys_change_xdes_id < @csn") })
/* #6665: the monitor's own statement regressed 200 times over, stored only in the text table; it is no target either. */
await PlantQueryStoreAsync(setup.Connection, ct, 513, 25, 1, cpuUs: 1_000, lastExec: AlertAt.AddMinutes(-4));
await PlantQueryStoreAsync(setup.Connection, ct, 513, 26, 2, cpuUs: 200_000, lastExec: AlertAt.AddMinutes(-1));
foreach (var (queryId, text) in new[]
{
(511L, "SELECT * FROM dbo.Orders"),
(512L, "(@csn bigint,@batch_size int)delete top(@batch_size) from sys.[change_tracking_1228027148] where sys_change_xdes_id < @csn"),
(513L, "(@cutoff_time datetime2(7))SELECT /* " + PerformanceMonitor.Collectors.QueryStoreCollector.SelfQueryMarker + " */ qsrs.plan_id"),
})
{
await DarlingMcpTestData.ExecAsync(setup.Connection, ct,
"INSERT INTO collect.query_store_text (server_id, database_name, query_id, query_sql_text, last_seen) VALUES ($1, $2, $3, $4, $5)",
Expand Down
22 changes: 22 additions & 0 deletions Darling/Darling.Tests/PlanRegressionBuiltDaysFallbackLiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,28 @@ public async Task ARegressionOfSqlServersOwnCleanup_IsNotAnOffender_AndALookalik
}

Assert.Null(await ReadAsync());

/* #6665: the monitor's own statement, stored only in the text table (the shape a live Darling store has), is no offender
either; the same row with an ordinary text is. */
await using (var restore = new NpgsqlCommand(
"UPDATE collect.query_store_text SET query_sql_text = $1 WHERE server_id = $2 AND database_name = 'fallbackdb' AND query_id = 1", connection))
{
restore.Parameters.AddWithValue("SELECT ordinary");
restore.Parameters.AddWithValue(ServerId);
await restore.ExecuteNonQueryAsync(ct);
}

Assert.NotNull(await ReadAsync());

await using (var own = new NpgsqlCommand(
"UPDATE collect.query_store_text SET query_sql_text = $1 WHERE server_id = $2 AND database_name = 'fallbackdb' AND query_id = 1", connection))
{
own.Parameters.AddWithValue("(@cutoff_time datetime2(7))SELECT /* " + PerformanceMonitor.Collectors.QueryStoreCollector.SelfQueryMarker + " */ qsrs.plan_id");
own.Parameters.AddWithValue(ServerId);
await own.ExecuteNonQueryAsync(ct);
}

Assert.Null(await ReadAsync());
}

private static List<CollectionFailure> PlanRegressionFailures(AnalysisContext context) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ loses the plan-cache flag. */
Assert.Contains(unrestricted, r => r.GetProperty("query_id").GetInt64() == NotAnOffender);
Assert.Equal(unrestricted.Select(r => r.GetRawText()), (await DrillDownAsync(postgres, Array.Empty<PlanRegressionOffender>())).Select(r => r.GetRawText()));
Assert.Equal(await TableRowsAsync(connection, ct), await RowsOfAsync(unrestricted));

/* ---- #6665: the monitor's own statement, stored only in the text table, is left out of both routes ---- */
await ExecAsync(connection, $"UPDATE collect.query_store_text SET query_sql_text = '/* PerformanceMonitorLite */ SELECT own' WHERE server_id = {ServerId} AND database_name = '{Db}' AND query_id = 1", ct);
Assert.DoesNotContain(OnPairs(await DrillDownAsync(postgres, Pairs)), r => r.GetProperty("query_id").GetInt64() == 1);
await ExecAsync(connection, $"DELETE FROM collect.plan_regression_daily_built WHERE server_id = {ServerId}", ct);
var tableWithoutOwn = OnPairs(await DrillDownAsync(postgres, Pairs));
Assert.DoesNotContain(tableWithoutOwn, r => r.GetProperty("query_id").GetInt64() == 1);
Assert.Contains(tableWithoutOwn, r => r.GetProperty("query_id").GetInt64() == 2);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private static async Task<List<string>> RunAsync(
command.Parameters.Add(new NpgsqlParameter { NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Date, Value = plan.RunDays });
command.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = plan.LowEnd });
command.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = plan.HighStart });
command.Parameters.Add(DatabaseFilter.All.ExcludeParameter());
var rows = new List<string>();
await using var reader = await command.ExecuteReaderAsync(ct);
while (await reader.ReadAsync(ct))
Expand Down
135 changes: 127 additions & 8 deletions Darling/Darling.Tests/QueryStoreRegressionsFailureGroupsLiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ own database through ScratchPostgres and works entirely inside it. It shares the
[Collection("gap-cache-serial")]
public sealed class QueryStoreRegressionsFailureGroupsLiveTests
{
private const int ServerId = QueryStoreIntervalWideBelowFloorLiveTests.ServerId;
private const string Db = "qsFail";
internal const int ServerId = QueryStoreIntervalWideBelowFloorLiveTests.ServerId;
internal const string Db = "qsFail";

private static string? BaseConnectionString => Environment.GetEnvironmentVariable("DARLING_TEST_PG");

private sealed class Rig : IAsyncDisposable
internal sealed class Rig : IAsyncDisposable
{
public required ScratchPostgres Scratch { get; init; }
public required NpgsqlConnection Connection { get; init; }
Expand All @@ -62,21 +62,22 @@ public async ValueTask DisposeAsync()

/// <summary>One interval of one query and outcome, written through the real collector write path. Its last execution is 30
/// minutes after its first, so its day and its side of the window start are those of <c>first + 30 minutes</c>.</summary>
private static async Task WriteAsync(
internal static async Task WriteAsync(
DarlingCollectorRunner runner, ServerRuntime server, CollectorContext context, string type, long intervalId,
DateTime first, long queryId, long executions, long cpuUs, long durationUs, CancellationToken ct)
DateTime first, long queryId, long executions, long cpuUs, long durationUs, CancellationToken ct,
string? databaseName = null, string? queryText = null)
{
var row = new QueryStoreCollector.Row
{
DatabaseName = Db,
DatabaseName = databaseName ?? Db,
QueryId = queryId,
PlanId = queryId * 10,
ExecutionTypeDesc = type,
FirstExecutionTime = first,
LastExecutionTime = first.AddMinutes(30),
QueryHash = "0x" + queryId.ToString("X8", CultureInfo.InvariantCulture),
QueryPlanHash = "0x" + queryId.ToString("X8", CultureInfo.InvariantCulture),
QueryText = "SELECT " + queryId.ToString(CultureInfo.InvariantCulture),
QueryText = queryText ?? "SELECT " + queryId.ToString(CultureInfo.InvariantCulture),
ExecutionCount = executions,
AvgCpuTimeUs = cpuUs,
AvgDurationUs = durationUs,
Expand All @@ -88,7 +89,7 @@ private static async Task WriteAsync(
await runner.WriteBackfillBatchAsync(QueryStoreCollector.Instance, [row], server, first.AddMinutes(40), context, ct);
}

private static async Task<Rig> OpenAsync(Func<DarlingCollectorRunner, ServerRuntime, CollectorContext, DateTime, DateTime, Task> seed, CancellationToken ct)
internal static async Task<Rig> OpenAsync(Func<DarlingCollectorRunner, ServerRuntime, CollectorContext, DateTime, DateTime, Task> seed, CancellationToken ct)
{
var baseCs = BaseConnectionString;
var scratch = await ScratchPostgres.CreateAsync(baseCs!, ct);
Expand Down Expand Up @@ -208,6 +209,7 @@ private static Task<Rig> OpenFixtureAsync(CancellationToken ct) =>
command.Parameters.Add(new NpgsqlParameter { NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Date, Value = plan.RunDays });
command.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = plan.LowEnd });
command.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = plan.HighStart });
command.Parameters.Add(DatabaseFilter.All.ExcludeParameter());
await using var reader = await command.ExecuteReaderAsync(ct);
return await DarlingQueryStoreRegressionReader.ReadRowsAsync(reader, ct);
}
Expand All @@ -221,6 +223,123 @@ private static string Describe(DarlingQueryStoreRegressionReader.RegressionRow r
private static DarlingQueryStoreRegressionReader.RegressionRow Row(IEnumerable<DarlingQueryStoreRegressionReader.RegressionRow> rows, long queryId) =>
rows.Single(r => r.QueryId == queryId);

/// <summary>
/// #6654: the monitor's own statement and a dropped database's queries are not regressions. Four queries each triple their CPU:
/// 20 (a normal statement), 21 and 22 (their text carries the collector's self-query marker: 21 as an inline sample, 22 only in
/// query_store_text with a NULL inline sample, the shape a live Darling store has) and 30 in a
/// database an earlier database_states snapshot lists and the newest does not. With no database chosen only 20 is listed and the
/// dropped database is named in <c>databases_left_out</c>; naming the dropped database reads it as asked, and the marker stays out.
/// </summary>
[Fact]
public async Task TheMonitorsOwnStatementAndADroppedDatabase_AreNotListed_AndTheDroppedOneIsNamed()
{
Assert.SkipWhen(string.IsNullOrEmpty(BaseConnectionString), "Set DARLING_TEST_PG to a Postgres connection string to run the #6654 live tests.");
var ct = TestContext.Current.CancellationToken;
const string Gone = "qsGone";
var now = LiveClock.Now();
await using var rig = await OpenAsync(async (runner, server, context, today, start) =>
{
long id = 9500;
async Task W(DateTime first, long q, long cpu, string database, string text)
{
id++;
await WriteAsync(runner, server, context, "Regular", id, first, q, 100, cpu, cpu * 2, ct, database, text);
}

foreach (var (q, database, text) in new[]
{
(20L, Db, "SELECT normal"),
(21L, Db, "/* " + QueryStoreCollector.SelfQueryMarker + " */ SELECT own"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixture carries inline text, which is the one shape a live Darling store does not produce — so the arm that fires in production is the one this does not test.

WriteAsync sets QueryStoreCollector.Row.QueryText, so the marker lands in query_store_stats.query_text and the new predicate finds it in the f.query_text_sample fallback. On a real Darling store that column is null for every row collected since the separate-fetch cutover (QueryStoreCollector.cs:768-770), and the marker lives only in query_store_text — the SELECT x.query_sql_text … arm (QueryStoreRegressionBaseline.cs:580-583). Nothing here puts a row in query_store_text, so a mistake in that subquery's own predicates (wrong column, database_name vs query_id swapped, a missing server_id) passes this test green: the subquery just returns NULL and the fallback answers.

This is the trap in .github/REVIEW-TRAPS.md: "A test that constructs a 'four maintenance anomalies in one run' fixture is testing a shape no live run produces (#3709…). Fixtures should carry the population the engine actually emits, or say they do not."

A second row for query 21 — INSERT INTO query_store_text (server_id, database_name, query_id, query_sql_text, …) with the marker and the inline QueryText left null — would exercise the arm that matters, and would also pin that the keyed lookup matches the projection's.

(22L, Db, "SELECT own_in_text_table"),
(30L, Gone, "SELECT in_dropped"),
})
{
await W(now.AddDays(-5), q, 1_000, database, text);
await W(now.AddHours(-3), q, 3_000, database, text);
}

await W(start.AddDays(-12), 90, 1_000, Db, "SELECT floor");
}, ct);

/* Query 22 is the shape a live Darling store has (#6665): the text fetch is separate, so the stats rows carry NO inline text
and the marker sits only in collect.query_store_text, which is the arm the predicate's lookup reads. Query 21 keeps the
inline sample, the fallback arm for a query the text table has no row for. */
await using (var blank = new NpgsqlCommand("UPDATE query_store_stats SET query_text = NULL WHERE server_id = $1 AND query_id = 22", rig.Connection))
{
blank.Parameters.Add(new NpgsqlParameter<int> { TypedValue = ServerId });
await blank.ExecuteNonQueryAsync(ct);
}

await using (var production = new NpgsqlCommand(
"INSERT INTO query_store_text (server_id, database_name, query_id, query_sql_text, last_seen) VALUES ($1, $2, 22, $3, $4)",
rig.Connection))
{
production.Parameters.Add(new NpgsqlParameter<int> { TypedValue = ServerId });
production.Parameters.Add(new NpgsqlParameter<string> { TypedValue = Db });
production.Parameters.Add(new NpgsqlParameter<string> { TypedValue = "/* " + QueryStoreCollector.SelfQueryMarker + " */ SELECT own_in_text_table" });
production.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = DateTime.SpecifyKind(now, DateTimeKind.Unspecified) });
await production.ExecuteNonQueryAsync(ct);
}

foreach (var (age, names) in new[] { (TimeSpan.FromHours(3.5), new[] { Db, Gone }), (TimeSpan.FromHours(1), new[] { Db, Gone }), (TimeSpan.FromMinutes(5), new[] { Db }) })
{
var at = DateTime.SpecifyKind(DateTime.UtcNow - age, DateTimeKind.Unspecified);
var n = 1;
foreach (var name in names)
{
await using var insert = new NpgsqlCommand(
"INSERT INTO database_states (collection_id, collection_time, server_id, server_name, database_name, database_id, state_desc, is_in_standby) VALUES ($1,$2,$3,'qsfail-host',$4,$5,'ONLINE',FALSE)",
rig.Connection);
insert.Parameters.Add(new NpgsqlParameter<long> { TypedValue = CollectionIdGenerator.Next() });
insert.Parameters.Add(new NpgsqlParameter<DateTime> { TypedValue = at });
insert.Parameters.Add(new NpgsqlParameter<int> { TypedValue = ServerId });
insert.Parameters.Add(new NpgsqlParameter<string> { TypedValue = name });
insert.Parameters.Add(new NpgsqlParameter<int> { TypedValue = n++ });
await insert.ExecuteNonQueryAsync(ct);
}
}

static (List<long> Ids, List<string> LeftOut) Parse(string json)
{
using var doc = System.Text.Json.JsonDocument.Parse(json);
var ids = doc.RootElement.GetProperty("regressions").EnumerateArray().Select(r => r.GetProperty("query_id").GetInt64()).OrderBy(x => x).ToList();
var left = doc.RootElement.TryGetProperty("databases_left_out", out var l) && l.ValueKind == System.Text.Json.JsonValueKind.Array
? l.EnumerateArray().Select(x => x.GetString()!).ToList()
: [];
return (ids, left);
}

var all = Parse(await DarlingMcpQueryStoreRegressionTools.GetQueryStoreRegressions(rig.Postgres, "qsfail", 24, DatabaseFilter.All, 30, false, null, NullLogger.Instance, ct));
Assert.Equal([20L], all.Ids);
Assert.Equal([Gone], all.LeftOut);

var named = Parse(await DarlingMcpQueryStoreRegressionTools.GetQueryStoreRegressions(rig.Postgres, "qsfail", 24, DatabaseFilter.One(Gone), 30, false, null, NullLogger.Instance, ct));
Assert.Equal([30L], named.Ids);
Assert.Empty(named.LeftOut);

var db = Parse(await DarlingMcpQueryStoreRegressionTools.GetQueryStoreRegressions(rig.Postgres, "qsfail", 24, DatabaseFilter.One(Db), 30, false, null, NullLogger.Instance, ct));
Assert.Equal([20L], db.Ids);

/* The Viewer grid reads the same way and hands the left-out names back for its banner (#6654 review): with no database chosen
the dropped one is named and its query is not listed; naming it reads it as asked and names nothing. */
await using var viewer = new ViewerDataService(rig.Scratch.ConnectionString);
var viewerStart = now.AddHours(-24);
var viewerAll = await viewer.GetQueryStoreRegressionsWithScopeAsync(ServerId, viewerStart, now, cancellationToken: ct);
Assert.Equal([20L], viewerAll.Rows.Select(r => r.QueryId).OrderBy(x => x).ToList());
Assert.Equal([Gone], viewerAll.DatabasesLeftOut);
var viewerNamed = await viewer.GetQueryStoreRegressionsWithScopeAsync(ServerId, viewerStart, now, [Gone], cancellationToken: ct);
Assert.Equal([30L], viewerNamed.Rows.Select(r => r.QueryId).OrderBy(x => x).ToList());
Assert.Null(viewerNamed.DatabasesLeftOut);

/* #6665: a custom range that ended BEFORE the drop is judged by the databases the server had then (the dropped set is
anchored at the range's end, as get_query_store_regressions does with as_of), so the database dropped later still shows
its rows for that range. The newest snapshot at or before the range's end (3.5 hours ago) lists both databases. */
var rangeEnd = now.AddHours(-2);
var viewerHistorical = await viewer.GetQueryStoreRegressionsWithScopeAsync(ServerId, rangeEnd.AddHours(-24), rangeEnd, literalEndUtc: rangeEnd, cancellationToken: ct);
Assert.Equal([20L, 30L], viewerHistorical.Rows.Select(r => r.QueryId).OrderBy(x => x).ToList());
Assert.Null(viewerHistorical.DatabasesLeftOut);
}

[Fact]
public async Task EachGroupsCountAndPerExecutionFigures_AreTheHandWorkedOnes_OnTheWideArmAndTheRawArm()
{
Expand Down
Loading
Loading