Skip to content

[dashboard-oop] Allow dashboard to run without resource service - #1795

Closed
Drew Noakes (drewnoakes) wants to merge 7 commits into
microsoft:feature/dashboard-oopfrom
drewnoakes:disconnected-client
Closed

Drew Noakes (drewnoakes) wants to merge 7 commits into
microsoft:feature/dashboard-oopfrom
drewnoakes:disconnected-client

Conversation

@drewnoakes

@drewnoakes Drew Noakes (drewnoakes) commented Jan 24, 2024 •

Copy link
Copy Markdown
Member

Relates to #1556

With the dashboard running OOP, it is configured with the resource server endpoint via the DOTNET_DASHBOARD_GRPC_ENDPOINT_URL environment variable.

The dashboard can actually perform useful work even without this connection, however it requires some code changes to support this.

In this PR, if the environment variable is not defined, then IDashboardClient.IsEnabled will be false, and some code paths become inactive based off the flag.

To get tests passing, we introduce an abstraction for environment variables so we can inject the values we need.

The navigation tree will not display the "Resources" or "Console logs" items when no resource service endpoint is available:

image

Microsoft Reviewers: Open in CodeFlow

And tidy up some code that parsed `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL`. Change that code to only support a single URI for that purpose, as it only makes one connection.
When the `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL` variable is not in the dashboard's environment, the `DashboardClient` disables itself. In this state, it will throw when invoked, though you may query the `IsEnabled` property safely before making other calls.
Makes it possible to unit test environment variables.
Hides the "Resources" and "Console logs" navigation menu items whenever the `DashboardClient` is not enabled (due to `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL` not being available).
@ghost ghost added the area-dashboard label Jan 24, 2024
@drewnoakes

Copy link
Copy Markdown
Member Author

Test failure in Aspire.Azure.Storage.Queues.Tests.ConformanceTests.TracingEnablesTheRightActivitySource which seems unrelated (and passes locally). Kicking off another build.

@drewnoakes

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mitchdenny

Copy link
Copy Markdown
Member

Is the intention to merge this in before going to main, or will you retarget to main once this merges in?

@drewnoakes

Copy link
Copy Markdown
Member Author

I'm ok either way. I'm not sure whether we need this for p3 or not. I assumed that with the dashboard moving OOP, people will start trying to use it in different ways, and this supports that.

David Fowler (@davidfowl) Damian Edwards (@DamianEdwards) for input on scheduling this.

@DamianEdwards

Copy link
Copy Markdown
Member

Given we don't currently think we'll have the container published in preview 3 and that's the way we'll mostly facilitate the dashboard to run outside of the AppHost scenario, I don't think it's important we get this in preview 3, but definitely for preview 4.

@davidfowl

Copy link
Copy Markdown
Collaborator

I'm fine with this if we can get the changes in tomorrow so we can stablize the rest of the week. This is great!

Comment on lines +63 to +64
_channel = null!;
_client = null!;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not a fan of this. But making it clean would involve another nested type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't the dashboard client throw an error when disposed because the channel is null?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe they should be nullable and then there is AssertNotDisabled() in methods that use these types. It would be annotated so that the compiled knows they're not null if it doesn't throw.

@drewnoakes Drew Noakes (drewnoakes) Jan 24, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I experimented with MemberNotNull but didn't like how it looked. For example:

@@ -126,8 +125,28 @@ GrpcChannel CreateChannel()

     public bool IsEnabled => _state is not StateDisabled;

+    [MemberNotNull(nameof(_channel))]
+    [MemberNotNull(nameof(_client))]
+    [MemberNotNull(nameof(_connection))]
+    private void AssertNotDisabled()
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
+    {
+        // If we're in disabled state, we're not changing out of it.
+        // We don't need to worry about concurrency for this read.
+        if (_state is StateDisabled)
+        {
+            throw new InvalidOperationException($"{nameof(DashboardClient)} is disabled. Check the {nameof(IsEnabled)} property before calling this.");
+        }
+    }
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
+
+    [MemberNotNull(nameof(_channel))]
+    [MemberNotNull(nameof(_client))]
+    [MemberNotNull(nameof(_connection))]
     private void EnsureInitialized()
     {
+        AssertNotDisabled();
+
         var priorState = Interlocked.CompareExchange(ref _state, value: StateInitialized, comparand: StateNone);

         if (priorState is StateDisabled)

Or, annotating only EnsureInitialized:

@@ -126,6 +125,9 @@ GrpcChannel CreateChannel()

     public bool IsEnabled => _state is not StateDisabled;

+    [MemberNotNull(nameof(_channel))]
+    [MemberNotNull(nameof(_client))]
+    [MemberNotNull(nameof(_connection))]
     private void EnsureInitialized()
     {
         var priorState = Interlocked.CompareExchange(ref _state, value: StateInitialized, comparand: StateNone);
@@ -138,12 +140,16 @@ private void EnsureInitialized()
         if (priorState is not StateNone)
         {
             ObjectDisposedException.ThrowIf(priorState is StateDisposed, this);
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
             return;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
         }

         _connection = Task.Run(() => ConnectAndWatchResourcesAsync(_cts.Token), _cts.Token);

+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
         return;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.

         async Task ConnectAndWatchResourcesAsync(CancellationToken cancellationToken)
         {
@@ -155,7 +161,7 @@ async Task ConnectAsync()
             {
                 try
                 {
-                    var response = await _client.GetApplicationInformationAsync(new(), cancellationToken: cancellationToken);
+                    var response = await _client!.GetApplicationInformationAsync(new(), cancellationToken: cancellationToken);

                     _applicationName = response.ApplicationName;

@@ -208,7 +214,7 @@ static TimeSpan ExponentialBackOff(int errorCount, double maxSeconds)

                 async Task WatchResourcesAsync()
                 {
-                    var call = _client.WatchResources(new WatchResourcesRequest { IsReconnect = errorCount != 0 }, cancellationToken: cancellationToken);
+                    var call = _client!.WatchResources(new WatchResourcesRequest { IsReconnect = errorCount != 0 }, cancellationToken: cancellationToken);

                     await foreach (var response in call.ResponseStream.ReadAllAsync(cancellationToken: cancellationToken))
                     {

I don't like either of those more than what we have now.

Won't the dashboard client throw an error when disposed because the channel is null?

Yes, thanks I'll fix that. This is what nullability is supposed to catch. I'm going to make the fields nullable, remove two suppressions and add three more, rather than either of the above. If you know a cleaner way, I'd be interested to hear it.

[return: NotNullIfNotNull(nameof(defaultValue))]
public string? GetString(string variableName, string? defaultValue = null)
{
var value = ImmutableInterlocked.GetOrAdd(ref _valueByName, key: variableName, valueFactory: Environment.GetEnvironmentVariable);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need any caching at all? Why not just pass through to Environment.GetEnvironmentVariable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like it allocates and P/Invokes each time:

https://github.com/dotnet/runtime/blob/d56fad46e213f8eb8d36be8fe7d342700c0a253c/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs#L13-L31

I don't know if we care, but we do check env vars a fair bit around the place, and caching takes two lines of code.

@drewnoakes

Copy link
Copy Markdown
Member Author

I will also update the dashboard's README, which currently states there is a default value (no longer true with this PR).

@drewnoakes
Drew Noakes (drewnoakes) deleted the branch microsoft:feature/dashboard-oop January 24, 2024 12:08
@drewnoakes

Copy link
Copy Markdown
Member Author

GitHub closed this when I merged the feature branch, and I can't see a way to retarget it. I think we'll need a new PR. Apologies to reviewers. I'll apply feedback to the new PR before creating it. There are some merge conflicts to fix too.

@drewnoakes

Copy link
Copy Markdown
Member Author

David Fowler (@davidfowl) James Newton-King (@JamesNK) this was closed when the feature branch merged. I've discussed your feedback here and applied changes to the new PR #1815.

@drewnoakes
Drew Noakes (drewnoakes) deleted the disconnected-client branch January 25, 2024 19:19
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants