-
Notifications
You must be signed in to change notification settings - Fork 50
Reduce SQL Server queue length monitoring query volume #5555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ramonsmits
wants to merge
3
commits into
master
Choose a base branch
from
spike/sql-queuelength-configurable-and-single-query
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
src/ServiceControl.Transports.SqlServer.Tests/QueueLengthQueryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| namespace ServiceControl.Transport.Tests; | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using NUnit.Framework; | ||
| using Transports.SqlServer; | ||
|
|
||
| [TestFixture] | ||
| class QueueLengthQueryTests | ||
| { | ||
| [Test] | ||
| public void BuildBulkLengthQuery_emits_a_single_catalog_view_query_for_all_tables() | ||
| { | ||
| var tables = new[] | ||
| { | ||
| SqlTable.Parse("Sales", "dbo"), | ||
| SqlTable.Parse("Billing", "dbo"), | ||
| }; | ||
|
|
||
| var query = SqlTable.BuildBulkLengthQuery(catalog: null, tables); | ||
|
|
||
| // One statement, read from the catalog views — no per-table IF EXISTS, no scan of the queue tables. | ||
| Assert.That(query, Does.Contain("sys.partitions")); | ||
| Assert.That(query, Does.Contain("p.index_id IN (0, 1)")); | ||
| Assert.That(query, Does.Not.Contain("INFORMATION_SCHEMA")); | ||
| Assert.That(query, Does.Not.Contain("RowVersion")); | ||
|
|
||
| // Dirty reads are scoped to this statement via NOLOCK hints, not a connection-scoped SET that | ||
| // would leak READ UNCOMMITTED onto the pooled connection. | ||
| Assert.That(query, Does.Contain("WITH (NOLOCK)")); | ||
| Assert.That(query, Does.Not.Contain("ISOLATION LEVEL")); | ||
|
|
||
| // Both tracked tables are covered by the single query. | ||
| Assert.That(query, Does.Contain("t.name = 'Sales'")); | ||
| Assert.That(query, Does.Contain("t.name = 'Billing'")); | ||
|
|
||
| // Exactly one result-producing statement. | ||
| Assert.That(System.Text.RegularExpressions.Regex.Matches(query, "SELECT").Count, Is.EqualTo(1)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void BuildBulkLengthQuery_with_no_tables_stays_valid_and_returns_no_rows() | ||
| { | ||
| var query = SqlTable.BuildBulkLengthQuery(catalog: null, []); | ||
|
|
||
| // No predicate terms must not produce the invalid `AND ()`; a constant-false predicate keeps the | ||
| // query syntactically valid while matching nothing. | ||
| Assert.That(query, Does.Not.Contain("AND ()")); | ||
| Assert.That(query, Does.Contain("1 = 0")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void BuildBulkLengthQuery_prefixes_the_catalog_when_supplied() | ||
| { | ||
| var tables = new[] { SqlTable.Parse("Sales@dbo@MyCatalog", "dbo") }; | ||
|
|
||
| var query = SqlTable.BuildBulkLengthQuery(catalog: "MyCatalog", tables); | ||
|
|
||
| Assert.That(query, Does.Contain("[MyCatalog].sys.partitions")); | ||
| Assert.That(query, Does.Contain("[MyCatalog].sys.tables")); | ||
| Assert.That(query, Does.Contain("[MyCatalog].sys.schemas")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryDelayInterval_parses_and_strips_the_custom_part() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;Integrated Security=true;QueueLengthQueryDelayInterval=1000"; | ||
|
|
||
| var cleaned = connectionString.RemoveQueueLengthQueryDelayInterval(out var interval); | ||
|
|
||
| Assert.That(interval, Is.EqualTo(TimeSpan.FromSeconds(1))); | ||
| // The custom key must be stripped so SqlConnection never sees the unknown keyword. | ||
| Assert.That(cleaned, Does.Not.Contain("QueueLengthQueryDelayInterval").IgnoreCase); | ||
| Assert.That(cleaned, Does.Contain("Initial Catalog=nsb").IgnoreCase); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryDelayInterval_defaults_to_null_when_absent() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;Integrated Security=true"; | ||
|
|
||
| connectionString.RemoveQueueLengthQueryDelayInterval(out var interval); | ||
|
|
||
| Assert.That(interval, Is.Null); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryDelayInterval_throws_on_non_numeric_value() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;QueueLengthQueryDelayInterval=soon"; | ||
|
|
||
| Assert.That(() => connectionString.RemoveQueueLengthQueryDelayInterval(out _), | ||
| Throws.Exception.With.Message.Contains("QueueLengthQueryDelayInterval")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryDelayInterval_throws_on_negative_value() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;QueueLengthQueryDelayInterval=-5"; | ||
|
|
||
| // A negative interval would flow through to Task.Delay and throw mid-loop; reject it up front. | ||
| Assert.That(() => connectionString.RemoveQueueLengthQueryDelayInterval(out _), | ||
| Throws.Exception.With.Message.Contains("QueueLengthQueryDelayInterval")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryDelayInterval_throws_on_zero_value() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;QueueLengthQueryDelayInterval=0"; | ||
|
|
||
| // Zero would poll with no pacing at all; reject it. | ||
| Assert.That(() => connectionString.RemoveQueueLengthQueryDelayInterval(out _), | ||
| Throws.Exception.With.Message.Contains("QueueLengthQueryDelayInterval")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RemoveQueueLengthQueryMaxDelayInterval_parses_and_strips_the_custom_part() | ||
| { | ||
| const string connectionString = "Data Source=.;Initial Catalog=nsb;QueueLengthQueryMaxDelayInterval=60000"; | ||
|
|
||
| var cleaned = connectionString.RemoveQueueLengthQueryMaxDelayInterval(out var interval); | ||
|
|
||
| Assert.That(interval, Is.EqualTo(TimeSpan.FromSeconds(60))); | ||
| Assert.That(cleaned, Does.Not.Contain("QueueLengthQueryMaxDelayInterval").IgnoreCase); | ||
| } | ||
|
|
||
| static readonly TimeSpan Base = TimeSpan.FromMilliseconds(200); | ||
| static readonly TimeSpan Max = TimeSpan.FromSeconds(60); | ||
|
|
||
| [Test] | ||
| public void NextDelay_snaps_back_to_base_when_any_queue_has_work() | ||
| { | ||
| // Even fully backed off, a single non-empty queue returns to full speed immediately. | ||
| Assert.That(QueueLengthProvider.NextDelay(Max, Base, Max, maxObservedLength: 1), Is.EqualTo(Base)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NextDelay_doubles_while_idle() | ||
| { | ||
| Assert.That(QueueLengthProvider.NextDelay(Base, Base, Max, maxObservedLength: 0), | ||
| Is.EqualTo(TimeSpan.FromMilliseconds(400))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NextDelay_caps_at_max_while_idle() | ||
| { | ||
| Assert.That(QueueLengthProvider.NextDelay(TimeSpan.FromSeconds(40), Base, Max, maxObservedLength: 0), | ||
| Is.EqualTo(Max)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NextDelay_never_drops_below_base() | ||
| { | ||
| Assert.That(QueueLengthProvider.NextDelay(TimeSpan.FromMilliseconds(50), Base, Max, maxObservedLength: 0), | ||
| Is.EqualTo(Base)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NextDelay_with_max_equal_to_base_disables_backoff() | ||
| { | ||
| // Operator did not opt in (max == base) -> cadence is constant at base, even while idle. | ||
| Assert.That(QueueLengthProvider.NextDelay(Base, Base, Base, maxObservedLength: 0), Is.EqualTo(Base)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.