Skip to content

Commit

Permalink
Remove Milvus.Client dependency. (dotnet#6435)
Browse files Browse the repository at this point in the history
* Remove Milvus.Client dependency.

* namespaces!

* Fix random change.
  • Loading branch information
mitchdenny authored Oct 23, 2024
1 parent 0f3bccb commit 90dd896
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 116 deletions.
5 changes: 0 additions & 5 deletions src/Aspire.Hosting.Milvus/Aspire.Hosting.Milvus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<ItemGroup>
<Compile Include="$(SharedDir)StringComparers.cs" Link="Utils\StringComparers.cs" />
<Compile Include="$(SharedDir)VolumeNameGenerator.cs" Link="Utils\VolumeNameGenerator.cs" />
<Compile Include="$(ComponentsDir)Aspire.Milvus.Client\MilvusHealthCheck.cs" Link="MilvusHealthCheck.cs"></Compile>
</ItemGroup>

<ItemGroup>
Expand All @@ -29,8 +28,4 @@
<ItemGroup>
<InternalsVisibleTo Include="Aspire.Hosting.Milvus.Tests"></InternalsVisibleTo>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Milvus.Client" />
</ItemGroup>
</Project>
70 changes: 1 addition & 69 deletions src/Aspire.Hosting.Milvus/MilvusBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data.Common;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Milvus;
using Aspire.Hosting.Utils;
using Aspire.Milvus.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Milvus.Client;

namespace Aspire.Hosting;

Expand Down Expand Up @@ -56,27 +50,6 @@ public static IResourceBuilder<MilvusServerResource> AddMilvus(this IDistributed
ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-key");

var milvus = new MilvusServerResource(name, apiKeyParameter);

MilvusClient? milvusClient = null;

builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(milvus, async (@event, ct) =>
{
var connectionString = await milvus.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false)
?? throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{milvus.Name}' resource but the connection string was null.");
milvusClient = CreateMilvusClient(@event.Services, connectionString);
});

var healthCheckKey = $"{name}_check";
// TODO: Use health check from AspNetCore.Diagnostics.HealthChecks once it's implemented via this issue:
// https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/issues/2214
builder.Services.AddHealthChecks()
.Add(new HealthCheckRegistration(
healthCheckKey,
sp => new MilvusHealthCheck(milvusClient!),
failureStatus: default,
tags: default,
timeout: default));

return builder.AddResource(milvus)
.WithImage(MilvusContainerImageTags.Image, MilvusContainerImageTags.Tag)
.WithImageRegistry(MilvusContainerImageTags.Registry)
Expand All @@ -93,8 +66,7 @@ public static IResourceBuilder<MilvusServerResource> AddMilvus(this IDistributed
{
ctx.EnvironmentVariables["COMMON_SECURITY_DEFAULTROOTPASSWORD"] = milvus.ApiKeyParameter;
})
.WithArgs("milvus", "run", "standalone")
.WithHealthCheck(healthCheckKey);
.WithArgs("milvus", "run", "standalone");
}

/// <summary>
Expand Down Expand Up @@ -220,44 +192,4 @@ private static void ConfigureAttuContainer(EnvironmentCallbackContext context, M
// This will need to be refactored once updated service discovery APIs are available
context.EnvironmentVariables.Add("MILVUS_URL", $"{resource.PrimaryEndpoint.Scheme}://{resource.Name}:{resource.PrimaryEndpoint.TargetPort}");
}
internal static MilvusClient CreateMilvusClient(IServiceProvider sp, string? connectionString)
{
if (connectionString is null)
{
throw new InvalidOperationException("Connection string is unavailable");
}

Uri? endpoint = null;
string? key = null;
string? database = null;

if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri))
{
endpoint = uri;
}
else
{
var connectionBuilder = new DbConnectionStringBuilder
{
ConnectionString = connectionString
};

if (connectionBuilder.ContainsKey("Endpoint") && Uri.TryCreate(connectionBuilder["Endpoint"].ToString(), UriKind.Absolute, out var serviceUri))
{
endpoint = serviceUri;
}

if (connectionBuilder.ContainsKey("Key"))
{
key = connectionBuilder["Key"].ToString();
}

if (connectionBuilder.ContainsKey("Database"))
{
database = connectionBuilder["Database"].ToString();
}
}

return new MilvusClient(endpoint!, apiKey: key!, database: database, loggerFactory: sp.GetRequiredService<ILoggerFactory>());
}
}
42 changes: 0 additions & 42 deletions tests/Aspire.Hosting.Milvus.Tests/MilvusFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Components.Common.Tests;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Tests.Utils;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Milvus.Client;
using Xunit;
Expand Down Expand Up @@ -201,44 +199,4 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
}
}
}

[Fact]
[RequiresDocker]
public async Task VerifyWaitForOnMilvusBlocksDependentResources()
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
using var builder = TestDistributedApplicationBuilder.CreateWithTestContainerRegistry(testOutputHelper);

var healthCheckTcs = new TaskCompletionSource<HealthCheckResult>();
builder.Services.AddHealthChecks().AddAsyncCheck("blocking_check", () =>
{
return healthCheckTcs.Task;
});

var resource = builder.AddMilvus("resource")
.WithHealthCheck("blocking_check");

var dependentResource = builder.AddMilvus("dependentresource")
.WaitFor(resource);

using var app = builder.Build();

var pendingStart = app.StartAsync(cts.Token);

var rns = app.Services.GetRequiredService<ResourceNotificationService>();

await rns.WaitForResourceAsync(resource.Resource.Name, KnownResourceStates.Running, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Waiting, cts.Token);

healthCheckTcs.SetResult(HealthCheckResult.Healthy());

await rns.WaitForResourceHealthyAsync(resource.Resource.Name, cts.Token);

await rns.WaitForResourceAsync(dependentResource.Resource.Name, KnownResourceStates.Running, cts.Token);

await pendingStart;

await app.StopAsync();
}
}

0 comments on commit 90dd896

Please sign in to comment.