diff --git a/Darling/Darling.Tests/XeShredGateLiveSqlTests.cs b/Darling/Darling.Tests/XeShredGateLiveSqlTests.cs new file mode 100644 index 0000000000..9530f68921 --- /dev/null +++ b/Darling/Darling.Tests/XeShredGateLiveSqlTests.cs @@ -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; + +/// +/// #6868: on an Azure SQL Managed Instance sys.dm_xe_session_targets.execution_count 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 (shred_gated = 0). +/// +/// Gated on DARLING_TEST_SQL (the SQL Server host; optional DARLING_TEST_SQL_USER and +/// DARLING_TEST_SQL_PASSWORD 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. +/// DARLING_TEST_SQL_READONLY=1 skips the class: it creates a session, and a server that holds real monitoring data +/// must not get one. +/// +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 state) + => new() + { + ServerId = 1, + ServerName = "live-sql", + CollectionTime = DateTime.UtcNow, + Deltas = null!, + State = state, + AlwaysOnSessionName = sessionName, + Watermark = DateTime.UtcNow.AddMinutes(-10), + }; + + /// The shred_gated measurement one real run of the collector's batch and its own ReadAsync produced. + private static async Task RunAndReadShredGatedAsync( + string connectionString, CollectorDefinitionBase collector, string measurementLabel, string sessionName, string? priorCount, CancellationToken ct) + { + var state = priorCount is null + ? CollectorContext.NoState + : new Dictionary { [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); + } + } + } +} diff --git a/Lite.Tests/XeShredGateTests.cs b/Lite.Tests/XeShredGateTests.cs index 2cce2f5d2b..857d663d98 100644 --- a/Lite.Tests/XeShredGateTests.cs +++ b/Lite.Tests/XeShredGateTests.cs @@ -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); diff --git a/PerformanceMonitor.Collectors/BlockedProcessReportCollector.cs b/PerformanceMonitor.Collectors/BlockedProcessReportCollector.cs index 3df4cde28e..b1bb7990dd 100644 --- a/PerformanceMonitor.Collectors/BlockedProcessReportCollector.cs +++ b/PerformanceMonitor.Collectors/BlockedProcessReportCollector.cs @@ -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 diff --git a/PerformanceMonitor.Collectors/DeadlocksCollector.cs b/PerformanceMonitor.Collectors/DeadlocksCollector.cs index 998773bace..cddcc04af7 100644 --- a/PerformanceMonitor.Collectors/DeadlocksCollector.cs +++ b/PerformanceMonitor.Collectors/DeadlocksCollector.cs @@ -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 @@ -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 diff --git a/PerformanceMonitor.Collectors/XeShredGate.cs b/PerformanceMonitor.Collectors/XeShredGate.cs index b182d739f1..a3a5caa203 100644 --- a/PerformanceMonitor.Collectors/XeShredGate.cs +++ b/PerformanceMonitor.Collectors/XeShredGate.cs @@ -27,14 +27,17 @@ namespace PerformanceMonitor.Collectors; /// Both collectors read it in BuildQuery, BEFORE the cast, in the same batch, and skip the cast+shred /// entirely when it has not moved. /// -/// The three shapes that always shred -- the conservative default, the same rule +/// The four shapes that always shred -- the conservative default, the same rule /// 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. +/// no row); the counter reads 0 (#6868: an Azure SQL Managed Instance never moves +/// execution_count 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. /// public static class XeShredGate { @@ -72,7 +75,8 @@ public static string KeyFor(string? databaseName) => /// /// 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 @@ -81,6 +85,7 @@ public static string KeyFor(string? databaseName) => /// public static bool ShouldShred(long? currentExecutionCount, long? lastExecutionCount) => currentExecutionCount is null + || currentExecutionCount.Value == 0 || lastExecutionCount is null || currentExecutionCount.Value != lastExecutionCount.Value; diff --git a/install/22_collect_blocked_processes.sql b/install/22_collect_blocked_processes.sql index 51a152a003..bbd9bfea21 100644 --- a/install/22_collect_blocked_processes.sql +++ b/install/22_collect_blocked_processes.sql @@ -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 @@ -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 diff --git a/install/24_collect_deadlock_xml.sql b/install/24_collect_deadlock_xml.sql index 251e28616d..a6be0fdc6c 100644 --- a/install/24_collect_deadlock_xml.sql +++ b/install/24_collect_deadlock_xml.sql @@ -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 @@ -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