From 9cb33b2b13e33e2882a7862640437a608d03fb11 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sun, 7 Sep 2025 17:34:58 +0700 Subject: [PATCH] Update orleans cluster config --- src/Managing.Bootstrap/ApiBootstrap.cs | 39 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Managing.Bootstrap/ApiBootstrap.cs b/src/Managing.Bootstrap/ApiBootstrap.cs index 1405ffc7..2d238aae 100644 --- a/src/Managing.Bootstrap/ApiBootstrap.cs +++ b/src/Managing.Bootstrap/ApiBootstrap.cs @@ -93,8 +93,25 @@ public static class ApiBootstrap var disableOrleansClustering = !string.IsNullOrEmpty(disableOrleansClusteringEnv) && bool.TryParse(disableOrleansClusteringEnv, out var disabled) && disabled; + // Get TASK_SLOT for multi-instance configuration + var taskSlotEnv = Environment.GetEnvironmentVariable("TASK_SLOT"); + var taskSlot = 1; // Default to 1 if not provided + if (!string.IsNullOrEmpty(taskSlotEnv) && int.TryParse(taskSlotEnv, out var parsedTaskSlot)) + { + taskSlot = parsedTaskSlot; + } + + // Calculate unique ports based on task slot + var siloPort = 11111 + (taskSlot - 1) * 10; // 11111, 11121, 11131, etc. + var gatewayPort = 30000 + (taskSlot - 1) * 10; // 30000, 30010, 30020, etc. + var dashboardPort = 9999 + (taskSlot - 1); // 9999, 10000, 10001, etc. + var postgreSqlConnectionString = configuration.GetSection("PostgreSql")["Orleans"]; + Console.WriteLine($"Silo port: {siloPort}"); + Console.WriteLine($"Gateway port: {gatewayPort}"); + Console.WriteLine($"Dashboard port: {dashboardPort}"); + return hostBuilder.UseOrleans(siloBuilder => { // Configure clustering with improved networking or use localhost clustering if disabled @@ -123,33 +140,37 @@ public static class ApiBootstrap }); } - // Configure networking - simplified for Docker compatibility + // Configure networking - simplified for Docker compatibility with unique ports per instance if (disableOrleansClustering) { // Use localhost clustering when clustering is disabled - siloBuilder.ConfigureEndpoints(IPAddress.Loopback, 11111, 30000); + siloBuilder.ConfigureEndpoints(IPAddress.Loopback, siloPort, gatewayPort); } else { // Use localhost for development, proper hostname for production if (isProduction) { - // In production, use all interfaces - siloBuilder.ConfigureEndpoints(IPAddress.Any, 11111, 30000); + // In production, use all interfaces with unique ports per instance + siloBuilder.ConfigureEndpoints(IPAddress.Any, siloPort, gatewayPort); } else { - // In development, use localhost - siloBuilder.ConfigureEndpoints(IPAddress.Loopback, 11111, 30000); + // In development, use localhost with unique ports per instance + siloBuilder.ConfigureEndpoints(IPAddress.Loopback, siloPort, gatewayPort); } } siloBuilder .Configure(options => { - // Configure cluster options with unique identifiers + // Configure cluster options with unique identifiers including task slot options.ServiceId = "ManagingApp"; options.ClusterId = configuration["ASPNETCORE_ENVIRONMENT"] ?? "Development"; + + // Add task slot to silo identity for better tracking + var siloName = $"ManagingApi-{taskSlot}"; + // Orleans will use this for internal identification }) .Configure(options => { @@ -204,8 +225,8 @@ public static class ApiBootstrap { siloBuilder.UseDashboard(options => { - // Configure dashboard with proper shutdown handling - options.Port = 9999; + // Configure dashboard with proper shutdown handling and unique ports per instance + options.Port = dashboardPort; options.HostSelf = true; options.CounterUpdateIntervalMs = 10000; // 10 seconds options.HideTrace = true; // Hide trace to reduce dashboard overhead