-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5f9e66
commit f52b86f
Showing
43 changed files
with
2,708 additions
and
106 deletions.
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
14 changes: 14 additions & 0 deletions
14
.../ActivationRebalancing/ActivationRebalancing.Cluster/ActivationRebalancing.Cluster.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<OrleansBuildTimeCodeGen>true</OrleansBuildTimeCodeGen> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Orleans.Server\Orleans.Server.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
170 changes: 170 additions & 0 deletions
170
playground/ActivationRebalancing/ActivationRebalancing.Cluster/Program.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,170 @@ | ||
using System.Diagnostics; | ||
using System.Net; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Orleans.Configuration; | ||
using Orleans.Runtime.Placement; | ||
|
||
#nullable enable | ||
|
||
// Ledjon: The silos will run in the same process so they will have the same memory usage. | ||
// I previously had 4 console apps to run the example, but didn't want to add so many proj into the solution. | ||
// I am sure with something like Aspire that would be easier, but for now I'll leave them like this. | ||
// You (the reader) feel free to run this in different processes for a more realistic example. | ||
|
||
var host0 = await StartSiloHost(0); | ||
var host1 = await StartSiloHost(1); | ||
var host2 = await StartSiloHost(2); | ||
var host3 = await StartSiloHost(3); | ||
IHost? host5 = null; | ||
|
||
Console.WriteLine("All silos have started."); | ||
|
||
var grainFactory = host0.Services.GetRequiredService<IGrainFactory>(); | ||
var mgmtGrain = grainFactory.GetGrain<IManagementGrain>(0); | ||
|
||
var silos = await mgmtGrain.GetHosts(onlyActive: true); | ||
Debug.Assert(silos.Count == 4); | ||
var addresses = silos.Select(x => x.Key).ToArray(); | ||
|
||
var tasks = new List<Task>(); | ||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[0]); | ||
for (var i = 0; i < 300; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[1]); | ||
for (var i = 0; i < 30; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[2]); | ||
for (var i = 0; i < 410; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[3]); | ||
for (var i = 0; i < 120; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
var sessionCount = 0; | ||
while (true) | ||
{ | ||
if (sessionCount == 25) | ||
{ | ||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[0]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[1]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
} | ||
|
||
if (sessionCount == 35) | ||
{ | ||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[1]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[2]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
} | ||
|
||
if (sessionCount == 40) | ||
{ | ||
host5 = await StartSiloHost(4); | ||
} | ||
|
||
if (sessionCount == 45) | ||
{ | ||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[2]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
|
||
RequestContext.Set(IPlacementDirector.PlacementHintKey, addresses[3]); | ||
for (var i = 0; i < 50; i++) | ||
{ | ||
tasks.Add(grainFactory.GetGrain<IRebalancingTestGrain>(Guid.NewGuid()).Ping()); | ||
} | ||
} | ||
|
||
await Task.Delay(5000); // session duration | ||
sessionCount++; | ||
|
||
if (sessionCount > 55) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
Console.WriteLine("Simulation has finished. Press Enter to terminate..."); | ||
Console.ReadLine(); | ||
|
||
await host0.StopAsync(); | ||
await host1.StopAsync(); | ||
await host2.StopAsync(); | ||
await host3.StopAsync(); | ||
|
||
if (host5 != null) | ||
{ | ||
await host5.StopAsync(); | ||
} | ||
|
||
static async Task<IHost> StartSiloHost(int num) | ||
{ | ||
#pragma warning disable ORLEANSEXP002 | ||
var host = Host.CreateDefaultBuilder() | ||
.ConfigureLogging(builder => builder | ||
.AddFilter("", LogLevel.Error) | ||
.AddFilter("Orleans.Runtime.Placement.Rebalancing", LogLevel.Trace) | ||
.AddConsole()) | ||
.UseOrleans(builder => builder | ||
.Configure<ActivationRebalancerOptions>(o => | ||
{ | ||
o.RebalancerDueTime = TimeSpan.FromSeconds(5); | ||
o.SessionCyclePeriod = TimeSpan.FromSeconds(5); | ||
// uncomment these below, if you want higher migration rate | ||
//o.CycleNumberWeight = 1; | ||
//o.SiloNumberWeight = 0; | ||
}) | ||
.UseLocalhostClustering( | ||
siloPort: EndpointOptions.DEFAULT_SILO_PORT + num, | ||
gatewayPort: EndpointOptions.DEFAULT_GATEWAY_PORT + num, | ||
primarySiloEndpoint: new IPEndPoint(IPAddress.Loopback, EndpointOptions.DEFAULT_SILO_PORT)) | ||
.AddActivationRebalancer()) | ||
.Build(); | ||
#pragma warning restore ORLEANSEXP002 | ||
|
||
await host.StartAsync(); | ||
Console.WriteLine($"Silo{num} started."); | ||
|
||
return host; | ||
} | ||
|
||
public interface IRebalancingTestGrain : IGrainWithGuidKey | ||
{ | ||
Task Ping(); | ||
} | ||
|
||
public class RebalancingTestGrain : Grain, IRebalancingTestGrain | ||
{ | ||
public Task Ping() => Task.CompletedTask; | ||
} |
14 changes: 14 additions & 0 deletions
14
...ctivationRebalancing/ActivationRebalancing.Frontend/ActivationRebalancing.Frontend.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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Orleans.Server\Orleans.Server.csproj" /> | ||
<ProjectReference Include="..\ActivationRebalancing.Cluster\ActivationRebalancing.Cluster.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
...round/ActivationRebalancing/ActivationRebalancing.Frontend/Controllers/StatsController.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,36 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Orleans.Runtime; | ||
using Orleans; | ||
|
||
namespace ActivationRebalancing.Frontend.Controllers; | ||
|
||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class StatsController(IClusterClient clusterClient) : ControllerBase | ||
{ | ||
[HttpGet("silos")] | ||
public async Task<IActionResult> GetStats() | ||
{ | ||
var grainStats = await clusterClient | ||
.GetGrain<IManagementGrain>(0) | ||
.GetDetailedGrainStatistics(); | ||
|
||
var siloData = grainStats.GroupBy(stat => stat.SiloAddress) | ||
.Select(g => new SiloData(g.Key.ToString(), g.Count())) | ||
.ToList(); | ||
|
||
if (siloData.Count == 4) | ||
{ | ||
siloData = [.. siloData, new SiloData("x", 0)]; | ||
} | ||
|
||
if (siloData.Count > 5) | ||
{ | ||
throw new NotSupportedException("The frontend cant support more than 6 silos"); | ||
} | ||
|
||
return Ok(siloData); | ||
} | ||
} | ||
|
||
public record SiloData(string Host, int Activations); |
17 changes: 17 additions & 0 deletions
17
playground/ActivationRebalancing/ActivationRebalancing.Frontend/Program.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,17 @@ | ||
using Orleans.Hosting; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.UseOrleansClient(clientBuilder => clientBuilder.UseLocalhostClustering()); | ||
builder.Services.AddControllers(); | ||
|
||
var app = builder.Build(); | ||
|
||
var options = new DefaultFilesOptions(); | ||
options.DefaultFileNames.Clear(); | ||
options.DefaultFileNames.Add("index.html"); | ||
|
||
app.UseDefaultFiles(options); | ||
app.UseStaticFiles(); | ||
app.MapControllers(); | ||
app.Run(); |
14 changes: 14 additions & 0 deletions
14
...round/ActivationRebalancing/ActivationRebalancing.Frontend/Properties/launchSettings.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,14 @@ | ||
{ | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "index.html", | ||
"applicationUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
playground/ActivationRebalancing/ActivationRebalancing.Frontend/appsettings.Development.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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
playground/ActivationRebalancing/ActivationRebalancing.Frontend/appsettings.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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
Oops, something went wrong.