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
171 changes: 171 additions & 0 deletions Darling/Darling.Tests/XeShredGateLiveSqlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright (c) 2026 Erik Darling, Darling Data LLC
*
* This file is part of the SQL Server Performance Monitor.
*
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using PerformanceMonitor.Collectors;
using PerformanceMonitor.Darling.Service.Targets;
using Xunit;

namespace Darling.Tests;

/// <summary>
/// #6868: on an Azure SQL Managed Instance <c>sys.dm_xe_session_targets.execution_count</c> reads 0 on every target even
/// with events in the ring buffer, so the #4200 gate (skip the cast and shred when the count equals the stored prior)
/// compared 0 with 0 on every run after the first and never collected a deadlock or a blocked process report. A fresh
/// session on a boxed server reads 0 as well, so it stands in for the Managed Instance: the real batch, with a stored
/// prior of 0 and a current count of 0, must shred (<c>shred_gated</c> = 0).
///
/// <para>Gated on <c>DARLING_TEST_SQL</c> (the SQL Server host; optional <c>DARLING_TEST_SQL_USER</c> and
/// <c>DARLING_TEST_SQL_PASSWORD</c> for SQL auth), so it skips where the runner has no SQL Server. Each test CREATEs and DROPs
/// its own server-scoped session in the own-name form and never touches the shared sessions a live install collects from.
/// <c>DARLING_TEST_SQL_READONLY=1</c> skips the class: it creates a session, and a server that holds real monitoring data
/// must not get one.</para>
/// </summary>
public sealed class XeShredGateLiveSqlTests
{
private static string? ConnectionString(string applicationName)
{
var host = Environment.GetEnvironmentVariable("DARLING_TEST_SQL");
if (string.IsNullOrEmpty(host) || Environment.GetEnvironmentVariable("DARLING_TEST_SQL_READONLY") == "1")
{
return null;
}

var user = Environment.GetEnvironmentVariable("DARLING_TEST_SQL_USER");
var builder = new SqlConnectionStringBuilder
{
DataSource = host,
InitialCatalog = "master",
TrustServerCertificate = true,
Encrypt = SqlConnectionEncryptOption.Optional,
ApplicationName = applicationName,
Pooling = false,
IntegratedSecurity = string.IsNullOrEmpty(user),
};

if (!string.IsNullOrEmpty(user))
{
builder.UserID = user;
builder.Password = Environment.GetEnvironmentVariable("DARLING_TEST_SQL_PASSWORD") ?? string.Empty;
}

return builder.ConnectionString;
}

private const string SkipMessage =
"Set DARLING_TEST_SQL (and, for SQL auth, DARLING_TEST_SQL_USER / DARLING_TEST_SQL_PASSWORD) to run the live XE shred gate tests; DARLING_TEST_SQL_READONLY=1 skips them.";

private static async Task ExecuteAsync(string connectionString, string sql, CancellationToken ct)
{
await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection) { CommandTimeout = 60 };
await command.ExecuteNonQueryAsync(ct);
}

private static CollectorContext MakeContext(string sessionName, IReadOnlyDictionary<string, string> state)
=> new()
{
ServerId = 1,
ServerName = "live-sql",
CollectionTime = DateTime.UtcNow,
Deltas = null!,
State = state,
AlwaysOnSessionName = sessionName,
Watermark = DateTime.UtcNow.AddMinutes(-10),
};

/// <summary>The shred_gated measurement one real run of the collector's batch and its own <c>ReadAsync</c> produced.</summary>
private static async Task<long> RunAndReadShredGatedAsync<TRow>(
string connectionString, CollectorDefinitionBase<TRow> collector, string measurementLabel, string sessionName, string? priorCount, CancellationToken ct)
{
var state = priorCount is null
? CollectorContext.NoState
: new Dictionary<string, string> { [XeShredGate.KeyFor(null)] = priorCount };
var context = MakeContext(sessionName, state);
var query = collector.BuildQuery(context);

await using var connection = new SqlConnection(connectionString);
await connection.OpenAsync(ct);
await using var command = SqlServerTargetProvider.Instance.CreateCommand(query, connection, 60);
await using DbDataReader reader = await command.ExecuteReaderAsync(ct);
await collector.ReadAsync(reader, context, ct);

return context.Measurements.Single(m => m.Label == measurementLabel).Value;
}

private static string CreateSql(string sessionName, string eventName)
=> "CREATE EVENT SESSION [" + sessionName + "] ON SERVER ADD EVENT " + eventName
+ " ADD TARGET package0.ring_buffer (SET max_memory = 4096)"
+ " WITH (MAX_DISPATCH_LATENCY = 5 SECONDS, STARTUP_STATE = OFF);"
+ " ALTER EVENT SESSION [" + sessionName + "] ON SERVER STATE = START;";

[Fact]
public async Task BlockedProcessBatch_CountZeroAgainstPriorZero_Shreds()
{
var connectionString = ConnectionString("pm-6868-bpr-live-test");
Assert.SkipWhen(connectionString is null, SkipMessage);
var ct = TestContext.Current.CancellationToken;
var sessionName = AlwaysOnXeSessions.OwnNameFor(
LongQueryCompletionsCollector.DarlingProduct, Guid.NewGuid().ToString("N")[..8], AlwaysOnXeSessionKind.BlockedProcess);

var created = false;
try
{
await ExecuteAsync(connectionString!, CreateSql(sessionName, "sqlserver.blocked_process_report"), ct);
created = true;

Assert.Equal(0, await RunAndReadShredGatedAsync(connectionString!, BlockedProcessReportCollector.Instance,
BlockedProcessReportCollector.ShredGatedMeasurement, sessionName, "0", ct));
Assert.Equal(0, await RunAndReadShredGatedAsync(connectionString!, BlockedProcessReportCollector.Instance,
BlockedProcessReportCollector.ShredGatedMeasurement, sessionName, null, ct));
}
finally
{
if (created)
{
await ExecuteAsync(connectionString!, "DROP EVENT SESSION [" + sessionName + "] ON SERVER;", CancellationToken.None);
}
}
}

[Fact]
public async Task DeadlocksBatch_CountZeroAgainstPriorZero_Shreds()
{
var connectionString = ConnectionString("pm-6868-deadlocks-live-test");
Assert.SkipWhen(connectionString is null, SkipMessage);
var ct = TestContext.Current.CancellationToken;
var sessionName = AlwaysOnXeSessions.OwnNameFor(
LongQueryCompletionsCollector.DarlingProduct, Guid.NewGuid().ToString("N")[..8], AlwaysOnXeSessionKind.Deadlock);

var created = false;
try
{
await ExecuteAsync(connectionString!, CreateSql(sessionName, "sqlserver.xml_deadlock_report"), ct);
created = true;

Assert.Equal(0, await RunAndReadShredGatedAsync(connectionString!, DeadlocksCollector.Instance,
DeadlocksCollector.ShredGatedMeasurement, sessionName, "0", ct));
Assert.Equal(0, await RunAndReadShredGatedAsync(connectionString!, DeadlocksCollector.Instance,
DeadlocksCollector.ShredGatedMeasurement, sessionName, null, ct));
}
finally
{
if (created)
{
await ExecuteAsync(connectionString!, "DROP EVENT SESSION [" + sessionName + "] ON SERVER;", CancellationToken.None);
}
}
}
}
5 changes: 4 additions & 1 deletion Lite.Tests/XeShredGateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public sealed class XeShredGateTests
[InlineData(100L, 100L, false, "unchanged: skip the cast+shred")]
[InlineData(105L, 100L, true, "grew: shred")]
[InlineData(95L, 100L, true, "went down: session restarted/recreated, shred")]
[InlineData(0L, 0L, false, "both zero (a session that has delivered nothing yet, twice) still counts as unchanged")]
[InlineData(0L, 0L, true, "#6868: both zero shreds, because a Managed Instance never moves the count off 0 even with events buffered")]
[InlineData(0L, null, true, "#6868: a current count of 0 with no prior shreds")]
[InlineData(0L, 100L, true, "#6868: a current count of 0 against a non-zero prior shreds (session recreated)")]
[InlineData(5L, 5L, false, "a non-zero count that has not moved still skips the cast+shred")]
public void ShouldShred_MatchesTheFiveGateShapes(long? current, long? last, bool expected, string because)
{
Assert.True(expected == XeShredGate.ShouldShred(current, last), because);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,10 @@ anything shreds below. Compared against @last_execution_count (the prior cycle's
AND xet.target_name = N'ring_buffer'
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down
6 changes: 4 additions & 2 deletions PerformanceMonitor.Collectors/DeadlocksCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ JOIN sys.dm_xe_database_sessions AS xes
AND xet.target_name = N'ring_buffer'
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down Expand Up @@ -217,9 +218,10 @@ JOIN sys.dm_xe_sessions AS xes
AND xet.target_name = N'ring_buffer'
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down
19 changes: 12 additions & 7 deletions PerformanceMonitor.Collectors/XeShredGate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ namespace PerformanceMonitor.Collectors;
/// Both collectors read it in <c>BuildQuery</c>, BEFORE the cast, in the same batch, and skip the cast+shred
/// entirely when it has not moved.</para>
///
/// <para><b>The three shapes that always shred</b> -- the conservative default, the same rule
/// <para><b>The four shapes that always shred</b> -- the conservative default, the same rule
/// <see cref="QueryStoreOpenIntervalState"/> uses for its own stale-or-absent stamp: the counter is
/// unreadable this cycle (NULL -- the session or target is missing, mid-provisioning, or the DMV join found
/// no row); there is no stored prior (first run, a restarted host, or a store that lost the row); or the
/// counter no longer AGREES with the stored prior, whether it grew (the ordinary case) or fell (the session
/// restarted or was recreated, so the ring buffer's contents no longer correspond to what the prior was
/// measured against). Only an exact match skips -- every other case runs the same full read the collector
/// always ran before this gate existed, so the rows stored are identical either way.</para>
/// no row); the counter reads 0 (#6868: an Azure SQL Managed Instance never moves
/// <c>execution_count</c> off 0 even with events in the ring buffer, so 0 = 0 would skip every run after the
/// first, and on a boxed server a session that has delivered nothing reads 0 too, where shredding an empty
/// buffer is cheap); there is no stored prior (first run, a restarted host, or a store that lost the row); or
/// the counter no longer AGREES with the stored prior, whether it grew (the ordinary case) or fell (the
/// session restarted or was recreated, so the ring buffer's contents no longer correspond to what the prior
/// was measured against). Only a non-zero exact match skips -- every other case runs the same full read the
/// collector always ran before this gate existed, so the rows stored are identical either way.</para>
/// </summary>
public static class XeShredGate
{
Expand Down Expand Up @@ -72,7 +75,8 @@ public static string KeyFor(string? databaseName) =>

/// <summary>
/// The pure decision, unit-testable without a server: shred unless BOTH the current and the prior
/// execution_count are known and EQUAL. Anything else -- an unreadable current count, no prior, a
/// execution_count are known, EQUAL and non-zero. Anything else -- an unreadable current count, a
/// current count of 0 (#6868: a Managed Instance never moves the count off 0), no prior, a
/// count that grew, or one that fell -- shreds, which is the same full read every one of these
/// collectors always ran before this gate existed. The actual gate lives server-side (an IF in the
/// collector's own T-SQL, evaluated on the SAME values this method would be given), because the whole
Expand All @@ -81,6 +85,7 @@ public static string KeyFor(string? databaseName) =>
/// </summary>
public static bool ShouldShred(long? currentExecutionCount, long? lastExecutionCount) =>
currentExecutionCount is null
|| currentExecutionCount.Value == 0
|| lastExecutionCount is null
|| currentExecutionCount.Value != lastExecutionCount.Value;

Expand Down
6 changes: 4 additions & 2 deletions install/22_collect_blocked_processes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,10 @@ BEGIN
AND xet.target_name = N''ring_buffer''
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down Expand Up @@ -463,9 +464,10 @@ BEGIN
AND xet.target_name = N''ring_buffer''
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down
6 changes: 4 additions & 2 deletions install/24_collect_deadlock_xml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ BEGIN
AND xet.target_name = N''ring_buffer''
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down Expand Up @@ -420,9 +421,10 @@ BEGIN
AND xet.target_name = N''ring_buffer''
OPTION(RECOMPILE);

/* #6868: a count of 0 shreds like NULL. A Managed Instance never moves it off 0, so 0 = 0 would skip forever. */
SET @shred_needed =
CASE
WHEN @execution_count IS NULL THEN 1
WHEN @execution_count IS NULL OR @execution_count = 0 THEN 1
WHEN @last_execution_count IS NULL THEN 1
WHEN @execution_count <> @last_execution_count THEN 1
ELSE 0
Expand Down
Loading