-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add Akka.Discovery.Config.Hosting #1758
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d377cc
Add Akka.Discovery.Config.Hosting
Arkatufus 5ec3635
Merge branch 'dev' into Add_Discovery.Config
Aaronontheweb 69b0fa7
Merge branch 'dev' into Add_Discovery.Config
Aaronontheweb b1f608c
Move Discovery.Config into Akka.Management project
Arkatufus File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ery/config/Akka.Discovery.Config.Hosting.Tests/Akka.Discovery.Config.Hosting.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Akka.Cluster.Hosting" Version="$(AkkaHostingVersion)" /> | ||
<PackageReference Include="Akka.Hosting.TestKit" Version="$(AkkaHostingVersion)" /> | ||
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionVersion)" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" /> | ||
<PackageReference Include="xunit" Version="$(XunitVersion)" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVersion)"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="$(CoverletVersion)"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="xunit.runner.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Akka.Discovery.Config.Hosting\Akka.Discovery.Config.Hosting.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
40 changes: 40 additions & 0 deletions
40
src/discovery/config/Akka.Discovery.Config.Hosting.Tests/ConfigServiceConfigSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ConfigServiceConfigSpec.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Akka.Hosting; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Akka.Discovery.Config.Hosting.Tests; | ||
|
||
public class ConfigServiceConfigSpec | ||
{ | ||
[Fact(DisplayName = "ConfigServiceDiscoveryOptions should generate proper HOCON config")] | ||
public void OptionsShouldGenerateHoconConfig() | ||
{ | ||
var builder = new AkkaConfigurationBuilder(new ServiceCollection(), "") | ||
.WithConfigDiscovery(new ConfigServiceDiscoveryOptions | ||
{ | ||
Services = new List<Service> | ||
{ | ||
new Service | ||
{ | ||
Name = "Test", | ||
Endpoints = new[] { "abc:1", "def:2" } | ||
} | ||
} | ||
}); | ||
var systemConfig = builder.Configuration.Value; | ||
var config = systemConfig.GetConfig(ConfigServiceDiscoveryOptions.FullPath); | ||
|
||
Type.GetType(config.GetString("class")).Should().Be(typeof(ConfigServiceDiscovery)); | ||
config.GetString("services-path").Should().Be("akka.discovery.config.services"); | ||
config.GetStringList("services.Test.endpoints").Should().BeEquivalentTo("abc:1", "def:2"); | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
src/discovery/config/Akka.Discovery.Config.Hosting.Tests/End2End/ConfigServiceSpec.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ConfigServiceSpec.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Cluster.Hosting; | ||
using Akka.Hosting; | ||
using Akka.Hosting.TestKit.Internals; | ||
using Akka.Management; | ||
using Akka.Management.Cluster.Bootstrap; | ||
using Akka.Remote.Hosting; | ||
using Akka.Util; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Discovery.Config.Hosting.Tests.End2End; | ||
|
||
public class ConfigServiceSpec: Akka.Hosting.TestKit.TestKit | ||
{ | ||
private const int ClusterNodeCount = 3; | ||
|
||
private readonly AtomicBoolean _clusterFormed = new (); | ||
private readonly int[] _remotingPorts = new int[ClusterNodeCount]; | ||
private readonly int[] _managementPorts = new int[ClusterNodeCount]; | ||
private readonly string[] _managementEndpoints = new string[ClusterNodeCount]; | ||
|
||
private IHost? _host1; | ||
private IHost? _host2; | ||
private ActorSystem? _sys1; | ||
private ActorSystem? _sys2; | ||
|
||
public ConfigServiceSpec(ITestOutputHelper output) : base(nameof(ConfigServiceSpec), output) | ||
{ | ||
var rnd = new Random(); | ||
var port = rnd.Next(30000, 40000); | ||
for (var i = 0; i < ClusterNodeCount; i++) | ||
{ | ||
_remotingPorts[i] = port; | ||
port++; | ||
_managementPorts[i] = port; | ||
_managementEndpoints[i] = $"127.0.0.1:{port}"; | ||
port++; | ||
} | ||
} | ||
|
||
#region Test Setup | ||
|
||
private async Task<IHost> StartAkkaHost(int index) | ||
{ | ||
var hostBuilder = new HostBuilder(); | ||
hostBuilder | ||
.ConfigureLogging(logger => | ||
{ | ||
logger.ClearProviders(); | ||
logger.AddProvider(new XUnitLoggerProvider(Output, LogLevel)); | ||
logger.AddFilter("Akka.*", LogLevel); | ||
}) | ||
.ConfigureServices((_, services) => | ||
{ | ||
services.AddAkka(nameof(ConfigServiceSpec), (builder, _) => | ||
{ | ||
AddConfigDiscovery(builder, index); | ||
}); | ||
}); | ||
var host = hostBuilder.Build(); | ||
await host.StartAsync(); | ||
|
||
return host; | ||
} | ||
|
||
private AkkaConfigurationBuilder AddConfigDiscovery(AkkaConfigurationBuilder builder, int index) | ||
{ | ||
var port = _remotingPorts[index]; | ||
var managementPort = _managementPorts[index]; | ||
|
||
return builder | ||
.WithRemoting(options => | ||
{ | ||
options.Port = port; | ||
options.HostName = "localhost"; | ||
}) | ||
.WithClustering(new ClusterOptions | ||
{ | ||
MinimumNumberOfMembers = 3 | ||
}) | ||
.WithClusterBootstrap( | ||
requiredContactPoints:3, | ||
serviceName:"LocalService", | ||
// NOTE: this is needed to prevent cluster bootstrap from filtering out multiple result from a single domain name | ||
portName:"port") | ||
.WithAkkaManagement( | ||
hostName:"127.0.0.1", | ||
port: managementPort, | ||
bindHostname:"127.0.0.1", | ||
bindPort:managementPort, | ||
autoStart:true) | ||
.WithConfigDiscovery(opt => | ||
{ | ||
opt.Services.Add(new Service | ||
{ | ||
Name = "LocalService", | ||
Endpoints = _managementEndpoints | ||
}); | ||
}); | ||
} | ||
|
||
protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider) | ||
{ | ||
AddConfigDiscovery(builder, 0) | ||
.AddStartup((system, _) => | ||
{ | ||
var cluster = Cluster.Cluster.Get(system); | ||
cluster.RegisterOnMemberUp(() => | ||
{ | ||
_clusterFormed.CompareAndSet(false, true); | ||
}); | ||
}); | ||
} | ||
|
||
protected override async Task BeforeTestStart() | ||
{ | ||
await base.BeforeTestStart(); | ||
_host1 = await StartAkkaHost(1); | ||
_sys1 = _host1.Services.GetRequiredService<ActorSystem>(); | ||
_host2 = await StartAkkaHost(2); | ||
_sys2 = _host2.Services.GetRequiredService<ActorSystem>(); | ||
Output!.WriteLine("Systems started"); | ||
} | ||
|
||
protected override async Task AfterAllAsync() | ||
{ | ||
await base.AfterAllAsync(); | ||
|
||
var tasks = new List<Task>(); | ||
if (_sys1 is not null) | ||
tasks.Add(_sys1.Terminate()); | ||
if(_sys2 is not null) | ||
tasks.Add(_sys2.Terminate()); | ||
await Task.WhenAll(tasks); | ||
|
||
_host1?.Dispose(); | ||
_host2?.Dispose(); | ||
} | ||
|
||
#endregion | ||
|
||
|
||
[Fact(DisplayName = "Cluster should form")] | ||
public async Task ClusterFormingSpec() | ||
{ | ||
await AwaitConditionAsync(() => Task.FromResult(_clusterFormed.Value), max:TimeSpan.FromSeconds(30)); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/discovery/config/Akka.Discovery.Config.Hosting.Tests/xunit.runner.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", | ||
"longRunningTestSeconds": 60, | ||
"parallelizeAssembly": false, | ||
"parallelizeTestCollections": false | ||
} |
12 changes: 12 additions & 0 deletions
12
src/discovery/config/Akka.Discovery.Config.Hosting/Akka.Discovery.Config.Hosting.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\management\Akka.Management\Akka.Management.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
96 changes: 96 additions & 0 deletions
96
src/discovery/config/Akka.Discovery.Config.Hosting/AkkaHostingExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ConfigDiscoveryHostingExtensions.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using Akka.Actor; | ||
using Akka.Hosting; | ||
|
||
namespace Akka.Discovery.Config.Hosting; | ||
|
||
public static class AkkaHostingExtensions | ||
{ | ||
/// <summary> | ||
/// Adds Akka.Discovery.Config.Hosting support to the <see cref="ActorSystem"/>. | ||
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for | ||
/// a complete solution. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The builder instance being configured. | ||
/// </param> | ||
/// <param name="configure"> | ||
/// An action that modifies an <see cref="ConfigServiceDiscoveryOptions"/> instance, used | ||
/// to configure Akka.Discovery.Config.Hosting. | ||
/// </param> | ||
/// <returns> | ||
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in. | ||
/// </returns> | ||
/// <example> | ||
/// <code> | ||
/// services.AddAkka("mySystem", builder => { | ||
/// builder | ||
/// .WithClustering() | ||
/// .WithClusterBootstrap(options => | ||
/// { | ||
/// options.ContactPointDiscovery.ServiceName = "testService"; | ||
/// options.ContactPointDiscovery.RequiredContactPointsNr = 1; | ||
/// }, autoStart: true) | ||
/// .WithConfigDiscovery(options => { | ||
/// options.WithCredentialProvider<AnonymousEc2CredentialProvider>(); | ||
/// options.TagKey = "myTag"; | ||
/// }); | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static AkkaConfigurationBuilder WithConfigDiscovery( | ||
this AkkaConfigurationBuilder builder, | ||
Action<ConfigServiceDiscoveryOptions> configure) | ||
{ | ||
var options = new ConfigServiceDiscoveryOptions(); | ||
configure(options); | ||
return builder.WithConfigDiscovery(options); | ||
} | ||
|
||
/// <summary> | ||
/// Adds Akka.Discovery.Config.Hosting support to the <see cref="ActorSystem"/>. | ||
/// Note that this only adds the discovery plugin, you will still need to add ClusterBootstrap for | ||
/// a complete solution. | ||
/// </summary> | ||
/// <param name="builder"> | ||
/// The builder instance being configured. | ||
/// </param> | ||
/// <param name="options"> | ||
/// The <see cref="ConfigServiceDiscoveryOptions"/> instance used to configure Akka.Discovery.Azure. | ||
/// </param> | ||
/// <returns> | ||
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in. | ||
/// </returns> | ||
/// <example> | ||
/// <code> | ||
/// services.AddAkka("mySystem", builder => { | ||
/// builder | ||
/// .WithClustering() | ||
/// .WithClusterBootstrap(options => | ||
/// { | ||
/// options.ContactPointDiscovery.ServiceName = "testService"; | ||
/// options.ContactPointDiscovery.RequiredContactPointsNr = 1; | ||
/// }, autoStart: true) | ||
/// .WithAwsEc2Discovery(new Ec2ServiceDiscoveryOptions { | ||
/// TagKey = "myTag" | ||
/// }); | ||
/// } | ||
/// </code> | ||
/// </example> | ||
public static AkkaConfigurationBuilder WithConfigDiscovery( | ||
this AkkaConfigurationBuilder builder, | ||
ConfigServiceDiscoveryOptions options) | ||
{ | ||
builder.AddHocon($"akka.discovery.method = {options.ConfigPath}", HoconAddMode.Prepend); | ||
options.Apply(builder); | ||
builder.AddHocon(DiscoveryProvider.DefaultConfiguration(), HoconAddMode.Append); | ||
return builder; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's fold this into the Akka.Management base project - one less NuGet package for users to worry about.