XEvent Profiler initial event handlers (#456)

* Bump SMO to 140.2.5 to pick-up private XEvent binaries

* Pick up SMO binaries from the build lab

* Add ProfilerService class placeholder

* Update SMO nuget package to include DB Scoped XEvents

* Stage changes

* Stage changes

* Update SMO to use RTM dependencies and remove separate SqlScript package

* Stage changes

* Iterate on profiler service

* Fix post-merge break in localization

* More refactoring

* Continue iterating on profiler

* Add test profiler listener

* Address a couple of the code review feedback

* Fix AppVeyor build break

* Use self-cleaning test file
This commit is contained in:
Karl Burtram
2017-09-12 14:08:50 -07:00
committed by GitHub
parent 2677efb6b8
commit 84ea045572
42 changed files with 1448 additions and 115 deletions

View File

@@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -1086,5 +1087,40 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
}
}
}
/// <summary>
/// Create and open a new SqlConnection from a ConnectionInfo object
/// Note: we need to audit all uses of this method to determine why we're
/// bypassing normal ConnectionService connection management
/// </summary>
internal static SqlConnection OpenSqlConnection(ConnectionInfo connInfo)
{
try
{
// increase the connection timeout to at least 30 seconds and and build connection string
// enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
int? originalTimeout = connInfo.ConnectionDetails.ConnectTimeout;
bool? originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
connInfo.ConnectionDetails.ConnectTimeout = Math.Max(30, originalTimeout ?? 0);
connInfo.ConnectionDetails.PersistSecurityInfo = true;
string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
connInfo.ConnectionDetails.ConnectTimeout = originalTimeout;
connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
// open a dedicated binding server connection
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
return sqlConn;
}
catch (Exception ex)
{
string error = string.Format(CultureInfo.InvariantCulture,
"Failed opening a SqlConnection: error:{0} inner:{1} stacktrace:{2}",
ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty, ex.StackTrace);
Logger.Write(LogLevel.Error, error);
}
return null;
}
}
}

View File

@@ -22,8 +22,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
/// <summary>
/// Gets or sets the connection password
/// </summary>
/// <returns></returns>
public string Password {
public string Password
{
get
{
return GetOptionValue<string>("password");

View File

@@ -143,8 +143,8 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
if (connInfo != null)
{
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true);
SqlConnection sqlConn = GetSqlConnection(connInfo);
if ((sqlConn != null) && !connInfo.IsSqlDW && !connInfo.IsAzure)
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
{
BackupConfigInfo backupConfigInfo = this.GetBackupConfigInfo(helper.DataContainer, sqlConn, sqlConn.Database);
backupConfigInfo.DatabaseInfo = AdminService.GetDatabaseInfo(connInfo);
@@ -296,7 +296,7 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
if (supported && connInfo != null)
{
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(connInfo, databaseExists: true);
SqlConnection sqlConn = GetSqlConnection(connInfo);
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
BackupOperation backupOperation = CreateBackupOperation(helper.DataContainer, sqlConn, backupParams.BackupInfo);
SqlTask sqlTask = null;
@@ -320,32 +320,6 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
}
}
internal static SqlConnection GetSqlConnection(ConnectionInfo connInfo)
{
try
{
// increase the connection timeout to at least 30 seconds and and build connection string
// enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
int? originalTimeout = connInfo.ConnectionDetails.ConnectTimeout;
bool? originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
connInfo.ConnectionDetails.ConnectTimeout = Math.Max(30, originalTimeout ?? 0);
connInfo.ConnectionDetails.PersistSecurityInfo = true;
string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
connInfo.ConnectionDetails.ConnectTimeout = originalTimeout;
connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
// open a dedicated binding server connection
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
return sqlConn;
}
catch (Exception)
{
}
return null;
}
private bool IsBackupRestoreOperationSupported(string ownerUri, out ConnectionInfo connectionInfo)
{
SqlConnection sqlConn = null;
@@ -358,8 +332,8 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
if (connInfo != null)
{
sqlConn = GetSqlConnection(connInfo);
if ((sqlConn != null) && !connInfo.IsSqlDW && !connInfo.IsAzure)
sqlConn = ConnectionService.OpenSqlConnection(connInfo);
if (sqlConn != null && !connInfo.IsSqlDW && !connInfo.IsAzure)
{
connectionInfo = connInfo;
return true;

View File

@@ -114,7 +114,7 @@ namespace Microsoft.SqlTools.ServiceLayer.FileBrowser
{
try
{
Task.Run(() => RunFileBrowserOpenTask(fileBrowserParams));
var task = Task.Run(() => RunFileBrowserOpenTask(fileBrowserParams));
await requestContext.SendResult(true);
}
catch
@@ -129,7 +129,7 @@ namespace Microsoft.SqlTools.ServiceLayer.FileBrowser
{
try
{
Task.Run(() => RunFileBrowserExpandTask(fileBrowserParams));
var task = Task.Run(() => RunFileBrowserExpandTask(fileBrowserParams));
await requestContext.SendResult(true);
}
catch
@@ -144,7 +144,7 @@ namespace Microsoft.SqlTools.ServiceLayer.FileBrowser
{
try
{
Task.Run(() => RunFileBrowserValidateTask(fileBrowserParams));
var task = Task.Run(() => RunFileBrowserValidateTask(fileBrowserParams));
await requestContext.SendResult(true);
}
catch

View File

@@ -15,6 +15,7 @@ using Microsoft.SqlTools.ServiceLayer.EditData;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.Metadata;
using Microsoft.SqlTools.ServiceLayer.Profiler;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.Scripting;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
@@ -94,6 +95,9 @@ namespace Microsoft.SqlTools.ServiceLayer
DisasterRecoveryService.Instance.InitializeService(serviceHost);
serviceProvider.RegisterSingleService(DisasterRecoveryService.Instance);
ProfilerService.Instance.InitializeService(serviceHost);
serviceProvider.RegisterSingleService(ProfilerService.Instance);
InitializeHostedServices(serviceProvider, serviceHost);
serviceHost.ServiceProvider = serviceProvider;

View File

@@ -81,20 +81,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
try
{
bindingContext.BindingLock.Reset();
// increase the connection timeout to at least 30 seconds and and build connection string
// enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
int? originalTimeout = connInfo.ConnectionDetails.ConnectTimeout;
bool? originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
connInfo.ConnectionDetails.ConnectTimeout = Math.Max(DefaultMinimumConnectionTimeout, originalTimeout ?? 0);
connInfo.ConnectionDetails.PersistSecurityInfo = true;
string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
connInfo.ConnectionDetails.ConnectTimeout = originalTimeout;
connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
// open a dedicated binding server connection
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
// populate the binding context to work with the SMO metadata provider
ServerConnection serverConn = new ServerConnection(sqlConn);

View File

@@ -3517,6 +3517,14 @@ namespace Microsoft.SqlTools.ServiceLayer
}
}
public static string ProfilerConnectionNotFound
{
get
{
return Keys.GetString(Keys.ProfilerConnectionNotFound);
}
}
public static string ConnectionServiceListDbErrorNotConnected(string uri)
{
return Keys.GetString(Keys.ConnectionServiceListDbErrorNotConnected, uri);
@@ -4945,6 +4953,9 @@ namespace Microsoft.SqlTools.ServiceLayer
public const string InvalidPathError = "InvalidPathError";
public const string ProfilerConnectionNotFound = "ProfilerConnectionNotFound";
private Keys()
{ }

View File

@@ -1931,4 +1931,8 @@
<value>Cannot access the specified path on the server: {0}</value>
<comment></comment>
</data>
<data name="ProfilerConnectionNotFound" xml:space="preserve">
<value>Connection not found</value>
<comment></comment>
</data>
</root>

View File

@@ -848,4 +848,8 @@ ScriptTaskName = scripting
############################################################################
# File Browser Validation Errors
InvalidPathError = Cannot access the specified path on the server: {0}
InvalidPathError = Cannot access the specified path on the server: {0}
############################################################################
# Profiler
ProfilerConnectionNotFound = Connection not found

View File

@@ -2250,6 +2250,11 @@
<target state="new">scripting</target>
<note></note>
</trans-unit>
<trans-unit id="ProfilerConnectionNotFound">
<source>Connection not found</source>
<target state="new">Connection not found</target>
<note></note>
</trans-unit>
<trans-unit id="BackupPathIsFolderError">
<source>The provided path specifies a directory but a file path is required: {0}</source>
<target state="new">The file name specified is also a directory name: {0}</target>

View File

@@ -74,7 +74,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Metadata
var metadata = new List<ObjectMetadata>();
if (connInfo != null)
{
using (SqlConnection sqlConn = OpenMetadataConnection(connInfo))
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo))
{
ReadMetadata(sqlConn, metadata);
}
@@ -129,7 +129,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Metadata
ColumnMetadata[] metadata = null;
if (connInfo != null)
{
SqlConnection sqlConn = OpenMetadataConnection(connInfo);
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(connInfo);
TableMetadata table = new SmoMetadataFactory().GetObjectMetadata(
sqlConn, metadataParams.Schema,
metadataParams.ObjectName, objectType);
@@ -147,35 +147,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Metadata
}
}
/// <summary>
/// Create a SqlConnection to use for querying metadata
/// </summary>
internal static SqlConnection OpenMetadataConnection(ConnectionInfo connInfo)
{
try
{
// increase the connection timeout to at least 30 seconds and and build connection string
// enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
int? originalTimeout = connInfo.ConnectionDetails.ConnectTimeout;
bool? originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
connInfo.ConnectionDetails.ConnectTimeout = Math.Max(30, originalTimeout ?? 0);
connInfo.ConnectionDetails.PersistSecurityInfo = true;
string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
connInfo.ConnectionDetails.ConnectTimeout = originalTimeout;
connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
// open a dedicated binding server connection
SqlConnection sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
return sqlConn;
}
catch (Exception)
{
}
return null;
}
internal static bool IsSystemDatabase(string database)
{
// compare against master for now

View File

@@ -19,8 +19,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.4" />
<PackageReference Include="Microsoft.SqlServer.Management.SqlScriptPublishModel" Version="140.2.4" />
<PackageReference Include="Microsoft.SqlServer.Smo" Version="140.2.5" />
</ItemGroup>
<ItemGroup>
<Compile Include="**\*.cs" />

View File

@@ -0,0 +1,78 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Collections.Generic;
namespace Microsoft.SqlTools.ServiceLayer.Profiler.Contracts
{
/// <summary>
/// Class that contains data for a single profile event
/// </summary>
public class ProfilerEvent
{
/// <summary>
/// Initialize a new ProfilerEvent with required parameters
/// </summary>
public ProfilerEvent(string name, string timestamp)
{
this.Name = name;
this.Timestamp = timestamp;
this.Values = new Dictionary<string, string>();
}
/// <summary>
/// Profiler event name
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Profiler event timestamp
/// </summary>
public string Timestamp { get; private set; }
/// <summary>
/// Profiler event values collection
/// </summary>
public Dictionary<string, string> Values { get; private set; }
/// <summary>
/// Equals method
/// </summary>
public bool Equals(ProfilerEvent p)
{
// if parameter is null return false:
if ((object)p == null)
{
return false;
}
return this.Name == p.Name
&& this.Timestamp == p.Timestamp
&& this.Values.Count == p.Values.Count;
}
/// <summary>
/// GetHashCode method
/// </summary>
public override int GetHashCode()
{
int hashCode = this.GetType().ToString().GetHashCode();
if (this.Name != null)
{
hashCode ^= this.Name.GetHashCode();
}
if (this.Timestamp != null)
{
hashCode ^= this.Timestamp.GetHashCode();
}
hashCode ^= this.Values.Count.GetHashCode();
return hashCode;
}
}
}

View File

@@ -0,0 +1,27 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Collections.Generic;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Profiler.Contracts
{
public class ProfilerEventsAvailableParams
{
public string SessionId { get; set; }
public List<ProfilerEvent> Events { get; set; }
}
public class ProfilerEventsAvailableNotification
{
public static readonly
EventType<ProfilerEventsAvailableParams> Type =
EventType<ProfilerEventsAvailableParams>.Create("profiler/eventsavailable");
}
}

View File

@@ -0,0 +1,55 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Profiler.Contracts
{
/// <summary>
/// Start Profiling request parameters
/// </summary>
public class StartProfilingParams : GeneralRequestDetails
{
public string OwnerUri { get; set; }
public string TemplateName
{
get
{
return GetOptionValue<string>("templateName");
}
set
{
SetOptionValue("templateName", value);
}
}
}
public class StartProfilingResult
{
/// <summary>
/// Session ID that was started
/// </summary>
public string SessionId { get; set; }
public bool Succeeded { get; set; }
public string ErrorMessage { get; set; }
}
/// <summary>
/// Start Profile request type
/// </summary>
public class StartProfilingRequest
{
/// <summary>
/// Request definition
/// </summary>
public static readonly
RequestType<StartProfilingParams, StartProfilingResult> Type =
RequestType<StartProfilingParams, StartProfilingResult>.Create("profiler/start");
}
}

View File

@@ -0,0 +1,38 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Profiler.Contracts
{
/// <summary>
/// Stop Profiling request parameters
/// </summary>
public class StopProfilingParams
{
public string SessionId { get; set; }
}
public class StopProfilingResult
{
public bool Succeeded { get; set; }
public string ErrorMessage { get; set; }
}
/// <summary>
/// Start Profile request type
/// </summary>
public class StopProfilingRequest
{
/// <summary>
/// Request definition
/// </summary>
public static readonly
RequestType<StopProfilingParams, StopProfilingResult> Type =
RequestType<StopProfilingParams, StopProfilingResult>.Create("profiler/stop");
}
}

View File

@@ -0,0 +1,15 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Collections.Generic;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
public interface IProfilerSessionListener
{
void EventsAvailable(string sessionId, List<ProfilerEvent> events);
}
}

View File

@@ -0,0 +1,32 @@
//
// Copyright (c) Microsoft. All rights reserved.
// 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.SqlClient;
using System.Linq;
using System.Xml;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.XEvent;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Profiler session monitor interface
/// </summary>
public interface IProfilerSessionMonitor
{
/// <summary>
/// Starts monitoring a profiler session
/// </summary>
bool StartMonitoringSession(ProfilerSession session);
/// <summary>
/// Stops monitoring a profiler session
/// </summary>
bool StopMonitoringSession(string sessionId);
}
}

View File

@@ -0,0 +1,18 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Main class for Profiler Service functionality
/// </summary>
public interface IXEventSession
{
/// <summary>
/// Reads XEvent XML from the default session target
/// </summary>
string GetTargetXml();
}
}

View File

@@ -0,0 +1,23 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.XEvent;
using Microsoft.SqlTools.ServiceLayer.Connection;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Main class for Profiler Service functionality
/// </summary>
public interface IXEventSessionFactory
{
/// <summary>
/// Create a new XEvent session
/// </summary>
IXEventSession CreateXEventSession(ConnectionInfo connInfo);
}
}

View File

@@ -0,0 +1,246 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.XEvent;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
using Microsoft.SqlTools.Utility;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Main class for Profiler Service functionality
/// </summary>
public sealed class ProfilerService : IDisposable, IXEventSessionFactory, IProfilerSessionListener
{
private bool disposed;
private ConnectionService connectionService = null;
private ProfilerSessionMonitor monitor = new ProfilerSessionMonitor();
private static readonly Lazy<ProfilerService> instance = new Lazy<ProfilerService>(() => new ProfilerService());
/// <summary>
/// Construct a new ProfilerService instance with default parameters
/// </summary>
public ProfilerService()
{
this.XEventSessionFactory = this;
}
/// <summary>
/// Gets the singleton instance object
/// </summary>
public static ProfilerService Instance
{
get { return instance.Value; }
}
/// <summary>
/// Internal for testing purposes only
/// </summary>
internal ConnectionService ConnectionServiceInstance
{
get
{
if (connectionService == null)
{
connectionService = ConnectionService.Instance;
}
return connectionService;
}
set
{
connectionService = value;
}
}
/// <summary>
/// XEvent session factory. Internal to allow mocking in unit tests.
/// </summary>
internal IXEventSessionFactory XEventSessionFactory { get; set; }
/// <summary>
/// Session monitor instance
/// </summary>
internal ProfilerSessionMonitor SessionMonitor
{
get
{
return this.monitor;
}
}
/// <summary>
/// Service host object for sending/receiving requests/events.
/// Internal for testing purposes.
/// </summary>
internal IProtocolEndpoint ServiceHost
{
get;
set;
}
/// <summary>
/// Initializes the Profiler Service instance
/// </summary>
public void InitializeService(ServiceHost serviceHost)
{
this.ServiceHost = serviceHost;
this.ServiceHost.SetRequestHandler(StartProfilingRequest.Type, HandleStartProfilingRequest);
this.ServiceHost.SetRequestHandler(StopProfilingRequest.Type, HandleStopProfilingRequest);
this.SessionMonitor.AddSessionListener(this);
}
/// <summary>
/// Handle request to start a profiling session
/// </summary>
internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext<StartProfilingResult> requestContext)
{
try
{
var result = new StartProfilingResult();
ConnectionInfo connInfo;
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
if (connInfo != null)
{
ProfilerSession session = StartSession(connInfo);
result.SessionId = session.SessionId;
result.Succeeded = true;
}
else
{
result.Succeeded = false;
result.ErrorMessage = SR.ProfilerConnectionNotFound;
}
await requestContext.SendResult(result);
}
catch (Exception e)
{
await requestContext.SendError(e);
}
}
/// <summary>
/// Handle request to stop a profiling session
/// </summary>
internal async Task HandleStopProfilingRequest(StopProfilingParams parameters, RequestContext<StopProfilingResult> requestContext)
{
try
{
monitor.StopMonitoringSession(parameters.SessionId);
await requestContext.SendResult(new StopProfilingResult
{
Succeeded = true
});
}
catch (Exception e)
{
await requestContext.SendError(e);
}
}
/// <summary>
/// Starts a new profiler session for the provided connection
/// </summary>
internal ProfilerSession StartSession(ConnectionInfo connInfo)
{
// create a new XEvent session and Profiler session
var xeSession = this.XEventSessionFactory.CreateXEventSession(connInfo);
var profilerSession = new ProfilerSession()
{
SessionId = Guid.NewGuid().ToString(),
XEventSession = xeSession
};
// start monitoring the profiler session
monitor.StartMonitoringSession(profilerSession);
return profilerSession;
}
/// <summary>
/// Create a new XEvent sessions per the IXEventSessionFactory contract
/// </summary>
public IXEventSession CreateXEventSession(ConnectionInfo connInfo)
{
var sqlConnection = ConnectionService.OpenSqlConnection(connInfo);
SqlStoreConnection connection = new SqlStoreConnection(sqlConnection);
Session session = ProfilerService.GetOrCreateSession(connection, "Profiler");
// create xevent session wrapper
return new XEventSession()
{
Session = session
};
}
/// <summary>
/// Gets an existing XEvent session or creates one if no matching session exists.
/// Also starts the session if it isn't currently running
/// </summary>
private static Session GetOrCreateSession(SqlStoreConnection connection, string sessionName)
{
XEStore store = new XEStore(connection);
Session session = store.Sessions["Profiler"];
// start the session if it isn't already running
if (session != null && !session.IsRunning)
{
session.Start();
}
return session;
}
/// <summary>
/// Callback when profiler events are available
/// </summary>
public void EventsAvailable(string sessionId, List<ProfilerEvent> events)
{
// pass the profiler events on to the client
this.ServiceHost.SendEvent(
ProfilerEventsAvailableNotification.Type,
new ProfilerEventsAvailableParams()
{
SessionId = sessionId,
Events = events
});
}
/// <summary>
/// Disposes the Profiler Service
/// </summary>
public void Dispose()
{
if (!disposed)
{
disposed = true;
}
}
}
}

View File

@@ -0,0 +1,127 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SqlServer.Management.XEvent;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Profiler session class
/// </summary>
public class ProfilerSession
{
private static readonly TimeSpan DefaultPollingDelay = TimeSpan.FromSeconds(1);
private object pollingLock = new object();
private bool isPolling = false;
private DateTime lastPollTime = DateTime.Now.Subtract(DefaultPollingDelay);
private TimeSpan pollingDelay = DefaultPollingDelay;
private ProfilerEvent lastSeenEvent = null;
/// <summary>
/// Unique ID for the session
/// </summary>
public string SessionId { get; set; }
/// <summary>
/// Connection to use for the session
/// </summary>
public ConnectionInfo ConnectionInfo { get; set; }
/// <summary>
/// Underlying XEvent session wrapper
/// </summary>
public IXEventSession XEventSession { get; set; }
/// <summary>
/// Try to set the session into polling mode if criteria is meet
/// </summary>
/// <returns>True if session set to polling mode, False otherwise</returns>
public bool TryEnterPolling()
{
lock (this.pollingLock)
{
if (!this.isPolling && DateTime.Now.Subtract(this.lastPollTime) >= pollingDelay)
{
this.isPolling = true;
this.lastPollTime = DateTime.Now;
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// Is the session currently being polled
/// </summary>
public bool IsPolling
{
get
{
return this.isPolling;
}
set
{
lock (this.pollingLock)
{
this.isPolling = value;
}
}
}
/// <summary>
/// The delay between session polls
/// </summary>
public TimeSpan PollingDelay
{
get
{
return pollingDelay;
}
}
/// <summary>
/// Filter the event list to not include previously seen events
/// </summary>
public List<ProfilerEvent> FilterOldEvents(List<ProfilerEvent> events)
{
if (lastSeenEvent != null)
{
// find the last event we've previously seen
bool foundLastEvent = false;
int idx = events.Count;
while (--idx >= 0)
{
if (events[idx].Equals(lastSeenEvent))
{
foundLastEvent = true;
break;
}
}
// remove all the events we've seen before
if (foundLastEvent)
{
events.RemoveRange(0, idx + 1);
}
}
// save the last event so we know where to clean-up the list from next time
if (events.Count > 0)
{
lastSeenEvent = events.LastOrDefault();
}
return events;
}
}
}

View File

@@ -0,0 +1,201 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.XEvent;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
/// <summary>
/// Classs to monitor active profiler sessions
/// </summary>
public class ProfilerSessionMonitor : IProfilerSessionMonitor
{
private const int PollingLoopDelay = 1000;
private object sessionsLock = new object();
private object listenersLock = new object();
private Task processorThread = null;
private Dictionary<string, ProfilerSession> monitoredSessions = new Dictionary<string, ProfilerSession>();
private List<IProfilerSessionListener> listeners = new List<IProfilerSessionListener>();
/// <summary>
/// Registers a session event listener to receive a callback when events arrive
/// </summary>
public void AddSessionListener(IProfilerSessionListener listener)
{
lock (this.listenersLock)
{
this.listeners.Add(listener);
}
}
/// <summary>
/// Start monitoring the provided sessions
/// </summary>
public bool StartMonitoringSession(ProfilerSession session)
{
lock (this.sessionsLock)
{
// start the monitoring thread
if (this.processorThread == null)
{
this.processorThread = Task.Factory.StartNew(ProcessSessions);;
}
if (!this.monitoredSessions.ContainsKey(session.SessionId))
{
this.monitoredSessions.Add(session.SessionId, session);
}
}
return true;
}
/// <summary>
/// Stop monitoring the session specified by the sessionId
/// </summary>
public bool StopMonitoringSession(string sessionId)
{
lock (this.sessionsLock)
{
if (this.monitoredSessions.ContainsKey(sessionId))
{
ProfilerSession session;
return this.monitoredSessions.Remove(sessionId, out session);
}
else
{
return false;
}
}
}
/// <summary>
/// The core queue processing method
/// </summary>
/// <param name="state"></param>
private void ProcessSessions()
{
while (true)
{
lock (this.sessionsLock)
{
foreach (var session in this.monitoredSessions.Values)
{
ProcessSession(session);
}
}
Thread.Sleep(PollingLoopDelay);
}
}
/// <summary>
/// Process a session for new XEvents if it meets the polling criteria
/// </summary>
private void ProcessSession(ProfilerSession session)
{
if (session.TryEnterPolling())
{
Task.Factory.StartNew(() =>
{
var events = PollSession(session);
if (events.Count > 0)
{
SendEventsToListeners(session.SessionId, events);
}
});
}
}
private List<ProfilerEvent> PollSession(ProfilerSession session)
{
var events = new List<ProfilerEvent>();
try
{
if (session == null || session.XEventSession == null)
{
return events;
}
var targetXml = session.XEventSession.GetTargetXml();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(targetXml);
var nodes = xmlDoc.DocumentElement.GetElementsByTagName("event");
foreach (XmlNode node in nodes)
{
var profilerEvent = ParseProfilerEvent(node);
if (profilerEvent != null)
{
events.Add(profilerEvent);
}
}
}
finally
{
session.IsPolling = false;
}
return session.FilterOldEvents(events);
}
/// <summary>
/// Notify listeners when new profiler events are available
/// </summary>
private void SendEventsToListeners(string sessionId, List<ProfilerEvent> events)
{
lock (listenersLock)
{
foreach (var listener in this.listeners)
{
listener.EventsAvailable(sessionId, events);
}
}
}
/// <summary>
/// Parse a single event node from XEvent XML
/// </summary>
private ProfilerEvent ParseProfilerEvent(XmlNode node)
{
var name = node.Attributes["name"];
var timestamp = node.Attributes["timestamp"];
var profilerEvent = new ProfilerEvent(name.InnerText, timestamp.InnerText);
foreach (XmlNode childNode in node.ChildNodes)
{
var childName = childNode.Attributes["name"];
XmlNode typeNode = childNode.SelectSingleNode("type");
var typeName = typeNode.Attributes["name"];
XmlNode valueNode = childNode.SelectSingleNode("value");
if (!profilerEvent.Values.ContainsKey(childName.InnerText))
{
profilerEvent.Values.Add(childName.InnerText, valueNode.InnerText);
}
}
return profilerEvent;
}
}
}

View File

@@ -0,0 +1,29 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Linq;
using Microsoft.SqlServer.Management.XEvent;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Profiler.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.Profiler
{
public class XEventSession : IXEventSession
{
public Session Session { get; set; }
public string GetTargetXml()
{
if (this.Session == null)
{
return string.Empty;
}
// try to read events from the first target
Target defaultTarget = this.Session.Targets.FirstOrDefault();
return defaultTarget != null ? defaultTarget.GetTargetData() : string.Empty;
}
}
}

View File

@@ -213,12 +213,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Scripting
{
if (!disposed)
{
disposed = true;
foreach (ScriptingScriptOperation operation in this.ActiveOperations.Values)
{
operation.Dispose();
}
disposed = true;
}
}