Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for solution to be loaded before initializing remote telemetry #74944

Merged
merged 15 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/EditorFeatures/Core/Remote/SolutionChecksumUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ private void OnActiveDocumentChanged(object? sender, DocumentId? e)
private async ValueTask SynchronizePrimaryWorkspaceAsync(CancellationToken cancellationToken)
{
var solution = _workspace.CurrentSolution;

// Wait for the remote side to actually become available (without being the cause of its creation ourselves). We
// want to wait for some feature to kick this off, then we'll start syncing this data once that has happened.
await RemoteHostClient.WaitForClientCreationAsync(_workspace, cancellationToken).ConfigureAwait(false);

var client = await RemoteHostClient.TryGetClientAsync(_workspace, cancellationToken).ConfigureAwait(false);
if (client == null)
return;
Expand All @@ -181,6 +186,10 @@ private async ValueTask SynchronizeActiveDocumentAsync(CancellationToken cancell
{
var activeDocument = _documentTrackingService.TryGetActiveDocument();

// Wait for the remote side to actually become available (without being the cause of its creation ourselves). We
// want to wait for some feature to kick this off, then we'll start syncing this data once that has happened.
await RemoteHostClient.WaitForClientCreationAsync(_workspace, cancellationToken).ConfigureAwait(false);

var client = await RemoteHostClient.TryGetClientAsync(_workspace, cancellationToken).ConfigureAwait(false);
if (client == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@

namespace Microsoft.VisualStudio.LanguageServices.Remote;

internal sealed class DefaultRemoteHostClientProvider : IRemoteHostClientProvider
[method: Obsolete(MefConstruction.FactoryMethodMessage, error: true)]
internal sealed class DefaultRemoteHostClientProvider() : IRemoteHostClientProvider
{
[Obsolete(MefConstruction.FactoryMethodMessage, error: true)]
public DefaultRemoteHostClientProvider()
{
}

public Task<RemoteHostClient?> TryGetRemoteHostClientAsync(CancellationToken cancellationToken)
=> SpecializedTasks.Null<RemoteHostClient>();

public Task WaitForClientCreationAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.Shell.ServiceBroker;
using Microsoft.VisualStudio.Threading;
using Roslyn.Utilities;
using VSThreading = Microsoft.VisualStudio.Threading;

Expand Down Expand Up @@ -91,6 +92,7 @@ public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
private readonly IVsService<IBrokeredServiceContainer> _brokeredServiceContainer;
private readonly AsynchronousOperationListenerProvider _listenerProvider;
private readonly RemoteServiceCallbackDispatcherRegistry _callbackDispatchers;
private readonly TaskCompletionSource<bool> _clientCreationSource = new();
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved

private VisualStudioRemoteHostClientProvider(
SolutionServices services,
Expand Down Expand Up @@ -133,9 +135,16 @@ private VisualStudioRemoteHostClientProvider(
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(leaving comment here because I can't do it elsewhere)

I like the "wait until launched" mechanism you're adding for uses like the SolutionChecksumUpdater, since it means that as a generic unrelated piece of code can wait. What I don't get though is why we're not just passing the telemetry settings here when we're creating the process, rather than assuming that other thing is going to run?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't get though is why we're not just passing the telemetry settings here when we're creating the process

i guess i don't understand. how would we get those telemetry settings? it seems like we need to be told about them. which is when we go and pusha ll fo this to the OOP side.

return null;
}
finally
{
_clientCreationSource.SetResult(true);
}
}

public Task<RemoteHostClient?> TryGetRemoteHostClientAsync(CancellationToken cancellationToken)
=> _lazyClient.GetValueAsync(cancellationToken);

public Task WaitForClientCreationAsync(CancellationToken cancellationToken)
=> _clientCreationSource.Task.WithCancellation(cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
using System;
using System.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Options;
Expand All @@ -19,20 +20,16 @@
namespace Microsoft.VisualStudio.LanguageServices.Telemetry;

[ExportWorkspaceService(typeof(IWorkspaceTelemetryService)), Shared]
internal sealed class VisualStudioWorkspaceTelemetryService : AbstractWorkspaceTelemetryService
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class VisualStudioWorkspaceTelemetryService(
IThreadingContext threadingContext,
VisualStudioWorkspace workspace,
IGlobalOptionService globalOptions) : AbstractWorkspaceTelemetryService
{
private readonly VisualStudioWorkspace _workspace;
private readonly IGlobalOptionService _globalOptions;

[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualStudioWorkspaceTelemetryService(
VisualStudioWorkspace workspace,
IGlobalOptionService globalOptions)
{
_workspace = workspace;
_globalOptions = globalOptions;
}
private readonly IThreadingContext _threadingContext = threadingContext;
private readonly VisualStudioWorkspace _workspace = workspace;
private readonly IGlobalOptionService _globalOptions = globalOptions;

protected override ILogger CreateLogger(TelemetrySession telemetrySession, bool logDelta)
=> AggregateLogger.Create(
Expand All @@ -44,13 +41,16 @@ protected override ILogger CreateLogger(TelemetrySession telemetrySession, bool

protected override void TelemetrySessionInitialized()
{
var cancellationToken = _threadingContext.DisposalToken;
_ = Task.Run(async () =>
{
var client = await RemoteHostClient.TryGetClientAsync(_workspace, CancellationToken.None).ConfigureAwait(false);
// Wait until the remote host was created by some other party (we don't want to cause it to happen ourselves
// in the call to RemoteHostClient below).
await RemoteHostClient.WaitForClientCreationAsync(_workspace, cancellationToken).ConfigureAwait(false);

var client = await RemoteHostClient.TryGetClientAsync(_workspace, cancellationToken).ConfigureAwait(false);
if (client == null)
{
return;
}

var settings = SerializeCurrentSessionSettings();
Contract.ThrowIfNull(settings);
Expand All @@ -61,7 +61,7 @@ protected override void TelemetrySessionInitialized()
// initialize session in the remote service
_ = await client.TryInvokeAsync<IRemoteProcessTelemetryService>(
(service, cancellationToken) => service.InitializeTelemetrySessionAsync(Process.GetCurrentProcess().Id, settings, logDelta, cancellationToken),
CancellationToken.None).ConfigureAwait(false);
});
cancellationToken).ConfigureAwait(false);
}, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ internal interface IRemoteHostClientProvider : IWorkspaceService
/// Get <see cref="RemoteHostClient"/> to current RemoteHost
/// </summary>
Task<RemoteHostClient?> TryGetRemoteHostClientAsync(CancellationToken cancellationToken);

/// <summary>
/// Allows a caller to wait until the remote host client is is first create, without itself kicking off the work to
/// spawn the remote host and make the client itself.
/// </summary>
Task WaitForClientCreationAsync(CancellationToken cancellationToken);
}
9 changes: 9 additions & 0 deletions src/Workspaces/Core/Portable/Remote/RemoteHostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ internal abstract class RemoteHostClient : IDisposable
{
public abstract void Dispose();

public static Task WaitForClientCreationAsync(Workspace workspace, CancellationToken cancellationToken)
{
var service = workspace.Services.GetService<IRemoteHostClientProvider>();
if (service == null)
return Task.CompletedTask;

return service.WaitForClientCreationAsync(cancellationToken);
}

public static Task<RemoteHostClient?> TryGetClientAsync(Project project, CancellationToken cancellationToken)
{
if (!RemoteSupportedLanguages.IsSupported(project.Language))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.UnitTests.Remote;
using Microsoft.VisualStudio.Threading;
using Roslyn.Test.Utilities;

namespace Microsoft.CodeAnalysis.Remote.Testing
Expand Down Expand Up @@ -62,6 +63,7 @@ private static RemoteWorkspace CreateRemoteWorkspace(
private readonly SolutionServices _services;
private readonly Lazy<WorkspaceManager> _lazyManager;
private readonly Lazy<RemoteHostClient> _lazyClient;
private readonly TaskCompletionSource<bool> _clientCreationSource = new();

public Type[]? AdditionalRemoteParts { get; set; }
public Type[]? ExcludedRemoteParts { get; set; }
Expand All @@ -80,11 +82,21 @@ public InProcRemoteHostClientProvider(SolutionServices services, RemoteServiceCa
AdditionalRemoteParts,
ExcludedRemoteParts));
_lazyClient = new Lazy<RemoteHostClient>(
() => InProcRemoteHostClient.Create(
_services,
callbackDispatchers,
TraceListener,
new RemoteHostTestData(_lazyManager.Value, isInProc: true)));
() =>
{
try
{
return InProcRemoteHostClient.Create(
_services,
callbackDispatchers,
TraceListener,
new RemoteHostTestData(_lazyManager.Value, isInProc: true));
}
finally
{
_clientCreationSource.SetResult(true);
}
});
}

public void Dispose()
Expand All @@ -99,6 +111,9 @@ public void Dispose()

public Task<RemoteHostClient?> TryGetRemoteHostClientAsync(CancellationToken cancellationToken)
=> Task.FromResult<RemoteHostClient?>(_lazyClient.Value);

public Task WaitForClientCreationAsync(CancellationToken cancellationToken)
=> _clientCreationSource.Task.WithCancellation(cancellationToken);
}
#pragma warning restore CA1416 // Validate platform compatibility
}
9 changes: 0 additions & 9 deletions src/Workspaces/Remote/Core/ServiceHubRemoteHostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Telemetry;
using Microsoft.ServiceHub.Client;
using Microsoft.ServiceHub.Framework;
using Microsoft.VisualStudio.Threading;
using Roslyn.Utilities;
using StreamJsonRpc;

namespace Microsoft.CodeAnalysis.Remote
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

namespace Microsoft.CodeAnalysis.Remote;

internal sealed partial class RemoteProcessTelemetryService : BrokeredServiceBase, IRemoteProcessTelemetryService
internal sealed partial class RemoteProcessTelemetryService(
BrokeredServiceBase.ServiceConstructionArguments arguments)
: BrokeredServiceBase(arguments), IRemoteProcessTelemetryService
{
internal sealed class Factory : FactoryBase<IRemoteProcessTelemetryService>
{
Expand All @@ -33,12 +35,6 @@ protected override IRemoteProcessTelemetryService CreateService(in ServiceConstr

#pragma warning disable IDE0052 // Remove unread private members
private PerformanceReporter? _performanceReporter;
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
#pragma warning restore
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved

public RemoteProcessTelemetryService(ServiceConstructionArguments arguments)
: base(arguments)
{
}

/// <summary>
/// Remote API. Initializes ServiceHub process global state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
namespace Microsoft.VisualStudio.LanguageServices.Telemetry;

[ExportWorkspaceService(typeof(IWorkspaceTelemetryService)), Shared]
internal sealed class RemoteWorkspaceTelemetryService : AbstractWorkspaceTelemetryService
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class RemoteWorkspaceTelemetryService() : AbstractWorkspaceTelemetryService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public RemoteWorkspaceTelemetryService()
{
}

protected override ILogger CreateLogger(TelemetrySession telemetrySession, bool logDelta)
=> AggregateLogger.Create(
TelemetryLogger.Create(telemetrySession, logDelta),
Expand Down
Loading