Stopping sessions when handling stop requests (#627)

* Dropping profiler session on stop request

* Changes to IXEventSession to simplify dropping sessions

* Stop sessions instead of dropping, disable flaky tests
This commit is contained in:
Madeline MacDonald
2018-06-05 13:48:55 -07:00
committed by GitHub
parent b41c19bd25
commit 35b19320d4
9 changed files with 235 additions and 161 deletions

View File

@@ -27,6 +27,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// <summary>
/// Stops monitoring a profiler session
/// </summary>
bool StopMonitoringSession(string sessionId);
bool StopMonitoringSession(string sessionId, out ProfilerSession session);
}
}

View File

@@ -14,5 +14,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// Reads XEvent XML from the default session target
/// </summary>
string GetTargetXml();
/// <summary>
/// Stops XEvent session
/// </summary>
void Stop();
}
}

View File

@@ -154,7 +154,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
try
{
monitor.StopMonitoringSession(parameters.OwnerUri);
ProfilerSession session;
monitor.StopMonitoringSession(parameters.OwnerUri, out session);
session.XEventSession.Stop();
await requestContext.SendResult(new StopProfilingResult
{
Succeeded = true
@@ -163,7 +166,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
catch (Exception e)
{
await requestContext.SendError(e);
}
}
}
/// <summary>
@@ -224,9 +227,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
private static Session CreateSession(SqlStoreConnection connection, string sessionName)
{
string createSessionSql =
string createSessionSql =
@"
CREATE EVENT SESSION [Profiler] ON SERVER
CREATE EVENT SESSION [Profiler] ON SERVER
ADD EVENT sqlserver.attention(
ACTION(package0.event_sequence,sqlserver.client_app_name,sqlserver.client_pid,sqlserver.database_id,sqlserver.nt_username,sqlserver.query_hash,sqlserver.server_principal_name,sqlserver.session_id)
WHERE ([package0].[equal_boolean]([sqlserver].[is_system],(0)))),
@@ -251,7 +254,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
connection.ServerConnection.ExecuteNonQuery(createSessionSql);
XEStore store = new XEStore(connection);
return store.Sessions[sessionName];
return store.Sessions[sessionName];
}
/// <summary>
@@ -275,7 +278,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
public void Dispose()
{
if (!disposed)
{
{
disposed = true;
}
}

View File

@@ -72,17 +72,17 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// <summary>
/// Stop monitoring the session specified by the sessionId
/// </summary>
public bool StopMonitoringSession(string sessionId)
public bool StopMonitoringSession(string sessionId, out ProfilerSession session)
{
lock (this.sessionsLock)
{
if (this.monitoredSessions.ContainsKey(sessionId))
{
ProfilerSession session;
return this.monitoredSessions.Remove(sessionId, out session);
}
else
{
session = null;
return false;
}
}

View File

@@ -14,6 +14,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
public Session Session { get; set; }
public void Stop()
{
this.Session.Stop();
}
public string GetTargetXml()
{
if (this.Session == null)

View File

@@ -52,7 +52,8 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
/// <summary>
/// Verify the default "create agent alert" request handler with valid parameters
/// </summary>
[Fact]
// TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/630
// [Fact]
public async Task TestHandleCreateAgentAlertsRequest()
{
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
@@ -84,7 +85,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
// {
// OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
// Alert = alert
// }, deleteContext.Object);
// }, deleteContext.Object);
// }
// }
// }
@@ -123,13 +124,13 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Agent
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
Alert = alert
}, createContext.Object);
await service.HandleUpdateAgentAlertRequest(new UpdateAgentAlertParams()
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
Alert = alert
}, updateContext.Object);
await service.HandleDeleteAgentAlertRequest(new DeleteAgentAlertParams()
{
OwnerUri = connectionResult.ConnectionInfo.OwnerUri,

View File

@@ -89,7 +89,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
[Fact]
public async void VerifyScriptAsCreateTable()
{
string query = @"CREATE TABLE testTable1 (c1 int)
string query = @"CREATE TABLE testTable1 (c1 int)
GO
CREATE CLUSTERED INDEX [ClusteredIndex-1] ON [dbo].[testTable1]
(
@@ -167,17 +167,18 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
await VerifyScriptAsForMultipleObjects(query, scriptingObjects, scriptCreateDrop, expectedScripts);
}
[Fact]
// TODO: Fix flaky test. See https://github.com/Microsoft/sqltoolsservice/issues/631
// [Fact]
public async void VerifyScriptAsExecuteStoredProcedure()
{
string query = @"CREATE PROCEDURE testSp1
@BusinessEntityID [int],
@JobTitle [nvarchar](50),
@HireDate [datetime],
@RateChangeDate [datetime],
@Rate [money],
string query = @"CREATE PROCEDURE testSp1
@BusinessEntityID [int],
@JobTitle [nvarchar](50),
@HireDate [datetime],
@RateChangeDate [datetime],
@Rate [money],
@PayFrequency [tinyint]
AS
AS
BEGIN Select * from sys.all_columns END";
ScriptingOperationType scriptCreateDrop = ScriptingOperationType.Execute;
ScriptingObject scriptingObject = new ScriptingObject
@@ -320,7 +321,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Scripting
{
scriptCreateOperation = $"Script{operation}";
}
scriptingParams.ScriptOptions = new ScriptOptions
{
ScriptCreateDrop = scriptCreateOperation,

View File

@@ -22,7 +22,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
/// Unit tests for ProfilerService
/// </summary>
public class ProfilerServiceTests
{
{
/// <summary>
/// Test starting a profiling session and receiving event callback
/// </summary>
@@ -35,7 +35,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
string testUri = "profiler_uri";
var requestContext = new Mock<RequestContext<StartProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StartProfilingResult>()))
.Returns<StartProfilingResult>((result) =>
.Returns<StartProfilingResult>((result) =>
{
// capture the session id for sending the stop message
sessionId = result.SessionId;
@@ -65,5 +65,62 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
Assert.Equal(sessionListener.PreviousSessionId, sessionId);
Assert.Equal(sessionListener.PreviousEvents.Count, 1);
}
/// <summary>
/// Test stopping a session and receiving event callback
/// </summary>
/// <returns></returns>
[Fact]
public async Task TestStopProfilingRequest()
{
bool success = false;
bool stopped = false;
string testUri = "test_session";
// capture stopping results
var requestContext = new Mock<RequestContext<StopProfilingResult>>();
requestContext.Setup(rc => rc.SendResult(It.IsAny<StopProfilingResult>()))
.Returns<StopProfilingResult>((result) =>
{
success = result.Succeeded;
return Task.FromResult(0);
});
// capture if session was dropped
var mockSession = new Mock<IXEventSession>();
mockSession.Setup(p => p.Stop()).Callback(() =>
{
stopped = true;
});
var sessionListener = new TestSessionListener();
var profilerService = new ProfilerService();
profilerService.SessionMonitor.AddSessionListener(sessionListener);
profilerService.ConnectionServiceInstance = TestObjects.GetTestConnectionService();
ConnectionInfo connectionInfo = TestObjects.GetTestConnectionInfo();
profilerService.ConnectionServiceInstance.OwnerToConnectionMap.Add(testUri, connectionInfo);
profilerService.XEventSessionFactory = new TestXEventSessionFactory();
var requestParams = new StopProfilingParams();
requestParams.OwnerUri = testUri;
ProfilerSession session = new ProfilerSession();
session.XEventSession = mockSession.Object;
session.SessionId = testUri;
profilerService.SessionMonitor.StartMonitoringSession(session);
await profilerService.HandleStopProfilingRequest(requestParams, requestContext.Object);
requestContext.VerifyAll();
// check that session was succesfully stopped and drop was called
Assert.True(success);
Assert.True(stopped);
// should not be able to remove the session, it should already be gone
ProfilerSession ps;
Assert.False(profilerService.SessionMonitor.StopMonitoringSession(testUri, out ps));
}
}
}

View File

@@ -50,148 +50,150 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Profiler
public class TestXEventSession : IXEventSession
{
private string testXEventXml =
"<RingBufferTarget truncated=\"0\" processingTime=\"3\" totalEventsProcessed=\"1\" eventCount=\"1\" droppedCount=\"0\" memoryUsed=\"47996\">" +
" <event name=\"existing_connection\" package=\"sqlserver\" timestamp=\"2017-09-08T07:46:53.579Z\">" +
" <data name=\"session_id\">" +
" <type name=\"int16\" package=\"package0\"></type>" +
" <value>51</value>" +
" </data>" +
" <data name=\"is_dac\">" +
" <type name=\"boolean\" package=\"package0\"></type>" +
" <value>false</value>" +
" </data>" +
" <data name=\"database_id\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>1</value>" +
" </data>" +
" <data name=\"packet_size\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>4096</value>" +
" </data>" +
" <data name=\"transaction_count\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>0</value>" +
" </data>" +
" <data name=\"group_id\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>2</value>" +
" </data>" +
" <data name=\"duration\">" +
" <type name=\"uint64\" package=\"package0\"></type>" +
" <value>191053000</value>" +
" </data>" +
" <data name=\"client_pid\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>4680</value>" +
" </data>" +
" <data name=\"options\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>2000002838f4010000000000</value>" +
" </data>" +
" <data name=\"options_text\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value>" +
" <![CDATA[-- network protocol: LPC" +
"set quoted_identifier on" +
"set arithabort off" +
"set numeric_roundabort off" +
"set ansi_warnings on" +
"set ansi_padding on" +
"set ansi_nulls on" +
"set concat_null_yields_null on" +
"set cursor_close_on_commit off" +
"set implicit_transactions off" +
"set language us_english" +
"set dateformat mdy" +
"set datefirst 7" +
"set transaction isolation level read committed" +
"]]>" +
" </value>" +
" </data>" +
" <data name=\"started_event_session_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Profiler]]></value>" +
" </data>" +
" <data name=\"database_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"client_app_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Microsoft SQL Server Management Studio]]></value>" +
" </data>" +
" <data name=\"client_hostname\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[KARLBURTRAMC189]]></value>" +
" </data>" +
" <data name=\"nt_domain\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"nt_user\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"session_nt_domain\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"session_nt_user\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"server_principal_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </data>" +
" <data name=\"server_principal_sid\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>01</value>" +
" </data>" +
" <data name=\"session_server_principal_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </data>" +
" <data name=\"session_server_principal_sid\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>01</value>" +
" </data>" +
" <action name=\"session_id\" package=\"sqlserver\">" +
" <type name=\"uint16\" package=\"package0\"></type>" +
" <value>56</value>" +
" </action>" +
" <action name=\"server_principal_name\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </action>" +
" <action name=\"nt_username\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </action>" +
" <action name=\"client_pid\" package=\"sqlserver\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>930958063</value>" +
" </action>" +
" <action name=\"client_app_name\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Core .Net SqlClient Data Provider]]></value>" +
" </action>" +
" <action name=\"attach_activity_id_xfer\" package=\"package0\">" +
" <type name=\"activity_id_xfer\" package=\"package0\"></type>" +
" <value>A2873402-C433-4D1F-94C4-9CA99749453E-0</value>" +
" </action>" +
" <action name=\"attach_activity_id\" package=\"package0\">" +
" <type name=\"activity_id\" package=\"package0\"></type>" +
" <value>770C3538-EC3F-4A27-86A9-31A2FC777DBC-1</value>" +
" </action>" +
" </event>" +
private string testXEventXml =
"<RingBufferTarget truncated=\"0\" processingTime=\"3\" totalEventsProcessed=\"1\" eventCount=\"1\" droppedCount=\"0\" memoryUsed=\"47996\">" +
" <event name=\"existing_connection\" package=\"sqlserver\" timestamp=\"2017-09-08T07:46:53.579Z\">" +
" <data name=\"session_id\">" +
" <type name=\"int16\" package=\"package0\"></type>" +
" <value>51</value>" +
" </data>" +
" <data name=\"is_dac\">" +
" <type name=\"boolean\" package=\"package0\"></type>" +
" <value>false</value>" +
" </data>" +
" <data name=\"database_id\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>1</value>" +
" </data>" +
" <data name=\"packet_size\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>4096</value>" +
" </data>" +
" <data name=\"transaction_count\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>0</value>" +
" </data>" +
" <data name=\"group_id\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>2</value>" +
" </data>" +
" <data name=\"duration\">" +
" <type name=\"uint64\" package=\"package0\"></type>" +
" <value>191053000</value>" +
" </data>" +
" <data name=\"client_pid\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>4680</value>" +
" </data>" +
" <data name=\"options\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>2000002838f4010000000000</value>" +
" </data>" +
" <data name=\"options_text\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value>" +
" <![CDATA[-- network protocol: LPC" +
"set quoted_identifier on" +
"set arithabort off" +
"set numeric_roundabort off" +
"set ansi_warnings on" +
"set ansi_padding on" +
"set ansi_nulls on" +
"set concat_null_yields_null on" +
"set cursor_close_on_commit off" +
"set implicit_transactions off" +
"set language us_english" +
"set dateformat mdy" +
"set datefirst 7" +
"set transaction isolation level read committed" +
"]]>" +
" </value>" +
" </data>" +
" <data name=\"started_event_session_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Profiler]]></value>" +
" </data>" +
" <data name=\"database_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"client_app_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Microsoft SQL Server Management Studio]]></value>" +
" </data>" +
" <data name=\"client_hostname\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[KARLBURTRAMC189]]></value>" +
" </data>" +
" <data name=\"nt_domain\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"nt_user\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"session_nt_domain\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"session_nt_user\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </data>" +
" <data name=\"server_principal_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </data>" +
" <data name=\"server_principal_sid\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>01</value>" +
" </data>" +
" <data name=\"session_server_principal_name\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </data>" +
" <data name=\"session_server_principal_sid\">" +
" <type name=\"binary_data\" package=\"package0\"></type>" +
" <value>01</value>" +
" </data>" +
" <action name=\"session_id\" package=\"sqlserver\">" +
" <type name=\"uint16\" package=\"package0\"></type>" +
" <value>56</value>" +
" </action>" +
" <action name=\"server_principal_name\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[sa]]></value>" +
" </action>" +
" <action name=\"nt_username\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[]]></value>" +
" </action>" +
" <action name=\"client_pid\" package=\"sqlserver\">" +
" <type name=\"uint32\" package=\"package0\"></type>" +
" <value>930958063</value>" +
" </action>" +
" <action name=\"client_app_name\" package=\"sqlserver\">" +
" <type name=\"unicode_string\" package=\"package0\"></type>" +
" <value><![CDATA[Core .Net SqlClient Data Provider]]></value>" +
" </action>" +
" <action name=\"attach_activity_id_xfer\" package=\"package0\">" +
" <type name=\"activity_id_xfer\" package=\"package0\"></type>" +
" <value>A2873402-C433-4D1F-94C4-9CA99749453E-0</value>" +
" </action>" +
" <action name=\"attach_activity_id\" package=\"package0\">" +
" <type name=\"activity_id\" package=\"package0\"></type>" +
" <value>770C3538-EC3F-4A27-86A9-31A2FC777DBC-1</value>" +
" </action>" +
" </event>" +
"</RingBufferTarget>";
public string GetTargetXml()
{
return testXEventXml;
}
public void Stop(){}
}
public class TestXEventSessionFactory : IXEventSessionFactory