Skip to content

Commit

Permalink
Query store top queries - command timeout
Browse files Browse the repository at this point in the history
Set command timeout based on message lifetime
  • Loading branch information
DavidWiseman committed Jul 5, 2024
1 parent c52d25e commit 29be254
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion DBADash/Messaging/MessageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion DBADash/Messaging/QueryStoreTopQueriesMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private async Task<DataTable> 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);
Expand Down
2 changes: 1 addition & 1 deletion DBADashGUI/Messaging/CollectionMessaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static async Task TriggerCollection(string connectionID, string type, int

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();
Expand Down
6 changes: 4 additions & 2 deletions DBADashGUI/Performance/QueryStoreTopQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 29be254

Please sign in to comment.