fix for issue 3123 (#729)

* fix for issue 3123

* using seperate thread to handle the requests and fixed a typo
This commit is contained in:
Alan Ren
2018-11-08 14:48:27 -08:00
committed by GitHub
parent 4f148a583b
commit 2cb7f682c5

View File

@@ -124,41 +124,44 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// </summary> /// </summary>
internal async Task HandleCreateXEventSessionRequest(CreateXEventSessionParams parameters, RequestContext<CreateXEventSessionResult> requestContext) internal async Task HandleCreateXEventSessionRequest(CreateXEventSessionParams parameters, RequestContext<CreateXEventSessionResult> requestContext)
{ {
try await Task.Run(async () =>
{ {
ConnectionInfo connInfo; try
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
if (connInfo == null)
{ {
throw new Exception(SR.ProfilerConnectionNotFound); ConnectionInfo connInfo;
} ConnectionServiceInstance.TryFindConnection(
else if (parameters.SessionName == null) parameters.OwnerUri,
{ out connInfo);
throw new ArgumentNullException("SessionName"); if (connInfo == null)
} {
else if (parameters.Template == null) throw new Exception(SR.ProfilerConnectionNotFound);
{ }
throw new ArgumentNullException("Template"); else if (parameters.SessionName == null)
} {
else throw new ArgumentNullException("SessionName");
{ }
// create a new XEvent session and Profiler session else if (parameters.Template == null)
var xeSession = this.XEventSessionFactory.CreateXEventSession(parameters.Template.CreateStatement, parameters.SessionName, connInfo); {
// start monitoring the profiler session throw new ArgumentNullException("Template");
monitor.StartMonitoringSession(parameters.OwnerUri, xeSession); }
else
{
// create a new XEvent session and Profiler session
var xeSession = this.XEventSessionFactory.CreateXEventSession(parameters.Template.CreateStatement, parameters.SessionName, connInfo);
// start monitoring the profiler session
monitor.StartMonitoringSession(parameters.OwnerUri, xeSession);
var result = new CreateXEventSessionResult(); var result = new CreateXEventSessionResult();
await requestContext.SendResult(result); await requestContext.SendResult(result);
SessionCreatedNotification(parameters.OwnerUri, parameters.SessionName, parameters.Template.Name); SessionCreatedNotification(parameters.OwnerUri, parameters.SessionName, parameters.Template.Name);
}
} }
} catch (Exception e)
catch (Exception e) {
{ await requestContext.SendError(new Exception(SR.CreateSessionFailed(e.Message)));
await requestContext.SendError(new Exception(SR.CreateSessionFailed(e.Message))); }
} });
} }
/// <summary> /// <summary>
@@ -166,31 +169,34 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// </summary> /// </summary>
internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext<StartProfilingResult> requestContext) internal async Task HandleStartProfilingRequest(StartProfilingParams parameters, RequestContext<StartProfilingResult> requestContext)
{ {
try await Task.Run(async () =>
{ {
ConnectionInfo connInfo; try
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
if (connInfo != null)
{ {
// create a new XEvent session and Profiler session ConnectionInfo connInfo;
var xeSession = this.XEventSessionFactory.GetXEventSession(parameters.SessionName, connInfo); ConnectionServiceInstance.TryFindConnection(
// start monitoring the profiler session parameters.OwnerUri,
monitor.StartMonitoringSession(parameters.OwnerUri, xeSession); out connInfo);
if (connInfo != null)
{
// create a new XEvent session and Profiler session
var xeSession = this.XEventSessionFactory.GetXEventSession(parameters.SessionName, connInfo);
// start monitoring the profiler session
monitor.StartMonitoringSession(parameters.OwnerUri, xeSession);
var result = new StartProfilingResult(); var result = new StartProfilingResult();
await requestContext.SendResult(result); await requestContext.SendResult(result);
}
else
{
throw new Exception(SR.ProfilerConnectionNotFound);
}
} }
else catch (Exception e)
{ {
throw new Exception(SR.ProfilerConnectionNotFound); await requestContext.SendError(new Exception(SR.StartSessionFailed(e.Message)));
} }
} });
catch (Exception e)
{
await requestContext.SendError(new Exception(SR.StartSessionFailed(e.Message)));
}
} }
/// <summary> /// <summary>
@@ -198,25 +204,47 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// </summary> /// </summary>
internal async Task HandleStopProfilingRequest(StopProfilingParams parameters, RequestContext<StopProfilingResult> requestContext) internal async Task HandleStopProfilingRequest(StopProfilingParams parameters, RequestContext<StopProfilingResult> requestContext)
{ {
try await Task.Run(async () =>
{ {
ProfilerSession session; try
monitor.StopMonitoringSession(parameters.OwnerUri, out session); {
ProfilerSession session;
monitor.StopMonitoringSession(parameters.OwnerUri, out session);
if (session != null) if (session != null)
{ {
session.XEventSession.Stop(); // Occasionally we might see the InvalidOperationException due to a read is
await requestContext.SendResult(new StopProfilingResult{}); // in progress, add the following retry logic will solve the problem.
int remainingAttempts = 3;
while (true)
{
try
{
session.XEventSession.Stop();
await requestContext.SendResult(new StopProfilingResult { });
break;
}
catch (InvalidOperationException)
{
remainingAttempts--;
if (remainingAttempts == 0)
{
throw;
}
Thread.Sleep(500);
}
}
}
else
{
throw new Exception(SR.SessionNotFound);
}
} }
else catch (Exception e)
{ {
throw new Exception(SR.SessionNotFound); await requestContext.SendError(new Exception(SR.StopSessionFailed(e.Message)));
} }
} });
catch (Exception e)
{
await requestContext.SendError(new Exception(SR.StopSessionFailed(e.Message)));
}
} }
/// <summary> /// <summary>
@@ -224,16 +252,19 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// </summary> /// </summary>
internal async Task HandlePauseProfilingRequest(PauseProfilingParams parameters, RequestContext<PauseProfilingResult> requestContext) internal async Task HandlePauseProfilingRequest(PauseProfilingParams parameters, RequestContext<PauseProfilingResult> requestContext)
{ {
try await Task.Run(async () =>
{ {
monitor.PauseViewer(parameters.OwnerUri); try
{
monitor.PauseViewer(parameters.OwnerUri);
await requestContext.SendResult(new PauseProfilingResult{}); await requestContext.SendResult(new PauseProfilingResult { });
} }
catch (Exception e) catch (Exception e)
{ {
await requestContext.SendError(new Exception(SR.PauseSessionFailed(e.Message))); await requestContext.SendError(new Exception(SR.PauseSessionFailed(e.Message)));
} }
});
} }
/// <summary> /// <summary>
@@ -241,28 +272,31 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
/// </summary> /// </summary>
internal async Task HandleGetXEventSessionsRequest(GetXEventSessionsParams parameters, RequestContext<GetXEventSessionsResult> requestContext) internal async Task HandleGetXEventSessionsRequest(GetXEventSessionsParams parameters, RequestContext<GetXEventSessionsResult> requestContext)
{ {
try await Task.Run(async () =>
{ {
var result = new GetXEventSessionsResult(); try
ConnectionInfo connInfo;
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
if (connInfo == null)
{ {
await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); var result = new GetXEventSessionsResult();
ConnectionInfo connInfo;
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
if (connInfo == null)
{
await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound));
}
else
{
List<string> sessions = GetXEventSessionList(parameters.OwnerUri, connInfo);
result.Sessions = sessions;
await requestContext.SendResult(result);
}
} }
else catch (Exception e)
{ {
List<string> sessions = GetXEventSessionList(parameters.OwnerUri, connInfo); await requestContext.SendError(e);
result.Sessions = sessions;
await requestContext.SendResult(result);
} }
} });
catch (Exception e)
{
await requestContext.SendError(e);
}
} }
/// <summary> /// <summary>
@@ -278,10 +312,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
BaseXEStore store = CreateXEventStore(connInfo, connection); BaseXEStore store = CreateXEventStore(connInfo, connection);
// get session names from the session list // get session names from the session list
List<string> results = store.Sessions.Aggregate(new List<string>(), (result, next) => { List<string> results = store.Sessions.Aggregate(new List<string>(), (result, next) =>
{
result.Add(next.Name); result.Add(next.Name);
return result; return result;
} ); });
return results; return results;
} }
@@ -349,11 +384,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Profiler
throw new Exception(SR.SessionAlreadyExists(sessionName)); throw new Exception(SR.SessionAlreadyExists(sessionName));
} }
var statement = createStatement.Replace("{sessionName}",sessionName); var statement = createStatement.Replace("{sessionName}", sessionName);
connection.ServerConnection.ExecuteNonQuery(statement); connection.ServerConnection.ExecuteNonQuery(statement);
store.Refresh(); store.Refresh();
session = store.Sessions[sessionName]; session = store.Sessions[sessionName];
if (session == null){ if (session == null)
{
throw new Exception(SR.SessionNotFound); throw new Exception(SR.SessionNotFound);
} }
if (!session.IsRunning) if (!session.IsRunning)