From 29be254e9271d9fea168e76b3c16fee646aa5ba7 Mon Sep 17 00:00:00 2001 From: David Wiseman <33157668+DavidWiseman@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:02:32 +0100 Subject: [PATCH] Query store top queries - command timeout Set command timeout based on message lifetime --- DBADash/Messaging/MessageBase.cs | 4 +++- DBADash/Messaging/QueryStoreTopQueriesMessage.cs | 2 +- DBADashGUI/Messaging/CollectionMessaging.cs | 2 +- DBADashGUI/Performance/QueryStoreTopQueries.cs | 6 ++++-- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/DBADash/Messaging/MessageBase.cs b/DBADash/Messaging/MessageBase.cs index db5f66e5..89f08e19 100644 --- a/DBADash/Messaging/MessageBase.cs +++ b/DBADash/Messaging/MessageBase.cs @@ -18,7 +18,9 @@ public abstract class MessageBase : IMessage public DateTime Created { get; set; } = DateTime.UtcNow; - public bool IsExpired => DateTime.UtcNow - Created > TimeSpan.FromMinutes(5); + public int Lifetime { get; set; } = 300; + + public bool IsExpired => DateTime.UtcNow - Created > TimeSpan.FromSeconds(Lifetime); public DBADashAgent CollectAgent { get; set; } public DBADashAgent ImportAgent { get; set; } diff --git a/DBADash/Messaging/QueryStoreTopQueriesMessage.cs b/DBADash/Messaging/QueryStoreTopQueriesMessage.cs index 8384a24d..fc117032 100644 --- a/DBADash/Messaging/QueryStoreTopQueriesMessage.cs +++ b/DBADash/Messaging/QueryStoreTopQueriesMessage.cs @@ -236,7 +236,7 @@ private async Task GetTopQueriesForDatabase(string connectionString, { await using var cn = new SqlConnection(connectionString); await cn.OpenAsync(); - await using var cmd = new SqlCommand(SqlStrings.QueryStoreTopQueries, cn); + await using var cmd = new SqlCommand(SqlStrings.QueryStoreTopQueries, cn) {CommandTimeout = Lifetime}; cmd.Parameters.AddWithValue("@Database", db); cmd.Parameters.AddWithValue("@FromDate", From); cmd.Parameters.AddWithValue("@ToDate ", To); diff --git a/DBADashGUI/Messaging/CollectionMessaging.cs b/DBADashGUI/Messaging/CollectionMessaging.cs index 410c7af4..83371d32 100644 --- a/DBADashGUI/Messaging/CollectionMessaging.cs +++ b/DBADashGUI/Messaging/CollectionMessaging.cs @@ -75,7 +75,7 @@ public static async Task TriggerCollection(string connectionID, List typ var collectAgent = DBADashAgent.GetDBADashAgent(Common.ConnectionString, collectAgentID); var importAgent = DBADashAgent.GetDBADashAgent(Common.ConnectionString, importAgentID); - var x = new CollectionMessage(types, connectionID) { CollectAgent = collectAgent, ImportAgent = importAgent, DatabaseName = db}; + var x = new CollectionMessage(types, connectionID) { CollectAgent = collectAgent, ImportAgent = importAgent, DatabaseName = db, Lifetime = CollectionDialogLifetime}; var payload = x.Serialize(); var messageGroup = Guid.NewGuid(); diff --git a/DBADashGUI/Performance/QueryStoreTopQueries.cs b/DBADashGUI/Performance/QueryStoreTopQueries.cs index 06d2a470..9e5b0ea7 100644 --- a/DBADashGUI/Performance/QueryStoreTopQueries.cs +++ b/DBADashGUI/Performance/QueryStoreTopQueries.cs @@ -111,6 +111,7 @@ public async void RefreshData() From = new DateTimeOffset(DateRange.FromUTC, TimeSpan.Zero), To = new DateTimeOffset(DateRange.ToUTC, TimeSpan.Zero), IncludeWaits = IncludeWaits, + Lifetime = Config.DefaultCommandTimeout }; lblStatus.InvokeSetStatus(messageSentMessage, string.Empty, DashColors.Information); await SendMessageAndProcessReply(message, dgv); @@ -124,14 +125,14 @@ private async Task SendMessageAndProcessReply(MessageBase message, DataGridView lblStatus.InvokeSetStatus("No Import Agent", string.Empty, DashColors.Fail); return; } - await MessageProcessing.SendMessageToService(message.Serialize(), (int)CurrentContext.ImportAgentID, messageGroup, Common.ConnectionString, Config.DefaultCommandTimeout); + await MessageProcessing.SendMessageToService(message.Serialize(), (int)CurrentContext.ImportAgentID, messageGroup, Common.ConnectionString, message.Lifetime); var completed = false; while (!completed) { ResponseMessage reply; try { - reply = await Messaging.CollectionMessaging.ReceiveReply(messageGroup, Config.DefaultCommandTimeout * 1000); + reply = await Messaging.CollectionMessaging.ReceiveReply(messageGroup, message.Lifetime * 1000); } catch (Exception ex) { @@ -431,6 +432,7 @@ private void QueryDrillDown(DataGridView _dgv, DataGridViewCellEventArgs e) From = new DateTimeOffset(DateRange.FromUTC, TimeSpan.Zero), To = new DateTimeOffset(DateRange.ToUTC, TimeSpan.Zero), IncludeWaits = IncludeWaits, + Lifetime = Config.DefaultCommandTimeout }; Task.Run(() => SendMessageAndProcessReply(message, dgvDrillDown)); }