From 8c800cf6b37aea41e5ac4ad815680b37c09ac31f Mon Sep 17 00:00:00 2001 From: Gregorius Soedharmo Date: Wed, 1 Sep 2021 03:41:12 +0700 Subject: [PATCH] Refactor AdaptiveLoadBalancingRoutingLogic to use IClusterMetricsRoutingLogic instead (#5237) Co-authored-by: Aaron Stannard --- .../Routing/ClusterMetricsRouting.cs | 15 ++++++++++----- ...CoreAPISpec.ApproveClusterMetrics.approved.txt | 8 ++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/contrib/cluster/Akka.Cluster.Metrics/Routing/ClusterMetricsRouting.cs b/src/contrib/cluster/Akka.Cluster.Metrics/Routing/ClusterMetricsRouting.cs index d360d6ce80a..ffe4d5fb256 100644 --- a/src/contrib/cluster/Akka.Cluster.Metrics/Routing/ClusterMetricsRouting.cs +++ b/src/contrib/cluster/Akka.Cluster.Metrics/Routing/ClusterMetricsRouting.cs @@ -22,12 +22,17 @@ namespace Akka.Cluster.Metrics { + public interface IClusterMetricsRoutingLogic + { + void MetricsChanged(ClusterMetricsChanged @event); + } + /// /// Load balancing of messages to cluster nodes based on cluster metric data. /// /// It uses random selection of routees based on probabilities derived from the remaining capacity of corresponding node. /// - public sealed class AdaptiveLoadBalancingRoutingLogic : RoutingLogic + public sealed class AdaptiveLoadBalancingRoutingLogic : RoutingLogic, IClusterMetricsRoutingLogic { private readonly ActorSystem _system; private readonly IMetricsSelector _metricsSelector; @@ -176,7 +181,7 @@ public override Router CreateRouter(ActorSystem system) /// public override Props RoutingLogicController(RoutingLogic routingLogic) { - return Actor.Props.Create(() => new AdaptiveLoadBalancingMetricsListener(routingLogic as AdaptiveLoadBalancingRoutingLogic)); + return Actor.Props.Create(() => new AdaptiveLoadBalancingMetricsListener((IClusterMetricsRoutingLogic)routingLogic)); } /// @@ -328,7 +333,7 @@ public override Router CreateRouter(ActorSystem system) /// public override Props RoutingLogicController(RoutingLogic routingLogic) { - return Actor.Props.Create(() => new AdaptiveLoadBalancingMetricsListener(routingLogic as AdaptiveLoadBalancingRoutingLogic)); + return Actor.Props.Create(() => new AdaptiveLoadBalancingMetricsListener((IClusterMetricsRoutingLogic)routingLogic)); } /// @@ -392,10 +397,10 @@ public ISurrogated FromSurrogate(ActorSystem system) [InternalApi] public class AdaptiveLoadBalancingMetricsListener : ActorBase { - private readonly AdaptiveLoadBalancingRoutingLogic _routingLogic; + private readonly IClusterMetricsRoutingLogic _routingLogic; private readonly ClusterMetrics _extension = ClusterMetrics.Get(Context.System); - public AdaptiveLoadBalancingMetricsListener(AdaptiveLoadBalancingRoutingLogic routingLogic) + public AdaptiveLoadBalancingMetricsListener(IClusterMetricsRoutingLogic routingLogic) { _routingLogic = routingLogic; } diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterMetrics.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterMetrics.approved.txt index c527ba6bf0f..4fab075a254 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterMetrics.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveClusterMetrics.approved.txt @@ -23,7 +23,7 @@ namespace Akka.Cluster.Metrics [Akka.Annotations.InternalApiAttribute()] public class AdaptiveLoadBalancingMetricsListener : Akka.Actor.ActorBase { - public AdaptiveLoadBalancingMetricsListener(Akka.Cluster.Metrics.AdaptiveLoadBalancingRoutingLogic routingLogic) { } + public AdaptiveLoadBalancingMetricsListener(Akka.Cluster.Metrics.IClusterMetricsRoutingLogic routingLogic) { } protected override void PostStop() { } protected override void PreStart() { } protected override bool Receive(object message) { } @@ -52,7 +52,7 @@ namespace Akka.Cluster.Metrics public Akka.Util.ISurrogated FromSurrogate(Akka.Actor.ActorSystem system) { } } } - public sealed class AdaptiveLoadBalancingRoutingLogic : Akka.Routing.RoutingLogic + public sealed class AdaptiveLoadBalancingRoutingLogic : Akka.Routing.RoutingLogic, Akka.Cluster.Metrics.IClusterMetricsRoutingLogic { public AdaptiveLoadBalancingRoutingLogic(Akka.Actor.ActorSystem system, Akka.Cluster.Metrics.IMetricsSelector metricsSelector = null) { } public void MetricsChanged(Akka.Cluster.Metrics.Events.ClusterMetricsChanged @event) { } @@ -120,6 +120,10 @@ namespace Akka.Cluster.Metrics public CpuMetricsSelector() { } public override System.Collections.Immutable.IImmutableDictionary Capacity(System.Collections.Immutable.IImmutableSet nodeMetrics) { } } + public interface IClusterMetricsRoutingLogic + { + void MetricsChanged(Akka.Cluster.Metrics.Events.ClusterMetricsChanged @event); + } public interface IMetricsCollector : System.IDisposable { Akka.Cluster.Metrics.Serialization.NodeMetrics Sample();