From e33d8d78b9a315c4bf306c6071967eb7e6c8f6de Mon Sep 17 00:00:00 2001 From: qoo332001 Date: Sat, 27 May 2023 23:14:34 +0800 Subject: [PATCH 1/2] add ClusterBean to MigrationCost --- .../org/astraea/app/web/BalancerHandler.java | 3 +- .../org/astraea/common/balancer/Balancer.java | 34 +++++-------------- .../balancer/algorithms/GreedyBalancer.java | 2 ++ .../algorithms/SingleStepBalancer.java | 2 ++ .../astraea/common/cost/MigrationCost.java | 4 ++- .../common/balancer/BalancerConsoleTest.java | 7 +++- .../gui/tab/health/BalancerNodeTest.java | 2 ++ 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/astraea/app/web/BalancerHandler.java b/app/src/main/java/org/astraea/app/web/BalancerHandler.java index 6cfbc6718b..c3a91e446d 100644 --- a/app/src/main/java/org/astraea/app/web/BalancerHandler.java +++ b/app/src/main/java/org/astraea/app/web/BalancerHandler.java @@ -192,7 +192,8 @@ private PlanExecutionProgress progress(String taskId) { solution -> new PlanReport( changes.apply(solution), - MigrationCost.migrationCosts(contextCluster, solution.proposal()))) + MigrationCost.migrationCosts( + contextCluster, solution.proposal(), solution.clusterBean()))) .orElse(null); var phase = balancerConsole.taskPhase(taskId).orElseThrow(); return new PlanExecutionProgress( diff --git a/common/src/main/java/org/astraea/common/balancer/Balancer.java b/common/src/main/java/org/astraea/common/balancer/Balancer.java index 5854f1da36..3118c76dad 100644 --- a/common/src/main/java/org/astraea/common/balancer/Balancer.java +++ b/common/src/main/java/org/astraea/common/balancer/Balancer.java @@ -24,6 +24,7 @@ import org.astraea.common.balancer.algorithms.GreedyBalancer; import org.astraea.common.balancer.algorithms.SingleStepBalancer; import org.astraea.common.cost.ClusterCost; +import org.astraea.common.metrics.ClusterBean; public interface Balancer { @@ -32,41 +33,24 @@ public interface Balancer { */ Optional offer(AlgorithmConfig config); - class Plan { - private final ClusterInfo initialClusterInfo; - private final ClusterCost initialClusterCost; - - private final ClusterInfo proposal; - private final ClusterCost proposalClusterCost; - - public Plan( - ClusterInfo initialClusterInfo, - ClusterCost initialClusterCost, - ClusterInfo proposal, - ClusterCost proposalClusterCost) { - this.initialClusterInfo = initialClusterInfo; - this.initialClusterCost = initialClusterCost; - this.proposal = proposal; - this.proposalClusterCost = proposalClusterCost; - } - - public ClusterInfo initialClusterInfo() { - return initialClusterInfo; - } + record Plan( + ClusterBean clusterBean, + ClusterInfo initialClusterInfo, + ClusterCost initialClusterCost, + ClusterInfo proposal, + ClusterCost proposalClusterCost) { /** * The {@link ClusterCost} score of the original {@link ClusterInfo} when this plan is start * generating. */ + @Override public ClusterCost initialClusterCost() { return initialClusterCost; } - public ClusterInfo proposal() { - return proposal; - } - /** The {@link ClusterCost} score of the proposed new allocation. */ + @Override public ClusterCost proposalClusterCost() { return proposalClusterCost; } diff --git a/common/src/main/java/org/astraea/common/balancer/algorithms/GreedyBalancer.java b/common/src/main/java/org/astraea/common/balancer/algorithms/GreedyBalancer.java index 26b628f95a..4a20bba22b 100644 --- a/common/src/main/java/org/astraea/common/balancer/algorithms/GreedyBalancer.java +++ b/common/src/main/java/org/astraea/common/balancer/algorithms/GreedyBalancer.java @@ -209,6 +209,7 @@ public Optional offer(AlgorithmConfig config) { .map( newAllocation -> new Plan( + config.clusterBean(), config.clusterInfo(), initialCost, newAllocation, @@ -254,6 +255,7 @@ public Optional offer(AlgorithmConfig config) { .overflow()) { return Optional.of( new Plan( + config.clusterBean(), config.clusterInfo(), config.clusterCostFunction().clusterCost(config.clusterInfo(), clusterBean), currentClusterInfo, diff --git a/common/src/main/java/org/astraea/common/balancer/algorithms/SingleStepBalancer.java b/common/src/main/java/org/astraea/common/balancer/algorithms/SingleStepBalancer.java index 03b8cbfc29..e2acccdf48 100644 --- a/common/src/main/java/org/astraea/common/balancer/algorithms/SingleStepBalancer.java +++ b/common/src/main/java/org/astraea/common/balancer/algorithms/SingleStepBalancer.java @@ -136,6 +136,7 @@ public Optional offer(AlgorithmConfig config) { .map( newAllocation -> new Plan( + config.clusterBean(), config.clusterInfo(), currentCost, newAllocation, @@ -155,6 +156,7 @@ public Optional offer(AlgorithmConfig config) { .overflow()) { return Optional.of( new Plan( + config.clusterBean(), config.clusterInfo(), config.clusterCostFunction().clusterCost(config.clusterInfo(), clusterBean), currentClusterInfo, diff --git a/common/src/main/java/org/astraea/common/cost/MigrationCost.java b/common/src/main/java/org/astraea/common/cost/MigrationCost.java index ef673b40be..c88c1d2488 100644 --- a/common/src/main/java/org/astraea/common/cost/MigrationCost.java +++ b/common/src/main/java/org/astraea/common/cost/MigrationCost.java @@ -25,6 +25,7 @@ import org.astraea.common.admin.Broker; import org.astraea.common.admin.ClusterInfo; import org.astraea.common.admin.Replica; +import org.astraea.common.metrics.ClusterBean; public class MigrationCost { @@ -37,7 +38,8 @@ public class MigrationCost { public static final String REPLICA_LEADERS_TO_REMOVE = "leader number to remove"; public static final String CHANGED_REPLICAS = "changed replicas"; - public static List migrationCosts(ClusterInfo before, ClusterInfo after) { + public static List migrationCosts( + ClusterInfo before, ClusterInfo after, ClusterBean clusterBean) { var migrateInBytes = recordSizeToSync(before, after); var migrateOutBytes = recordSizeToFetch(before, after); var migrateReplicaNum = replicaNumChanged(before, after); diff --git a/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java b/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java index 78b25a664e..67216abb30 100644 --- a/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java +++ b/common/src/test/java/org/astraea/common/balancer/BalancerConsoleTest.java @@ -400,7 +400,12 @@ public Optional offer(AlgorithmConfig config) { costFunction, Duration.ofMillis(sampleTimeMs - (System.currentTimeMillis() - startMs))); return Optional.of( - new Plan(config.clusterInfo(), () -> 0, config.clusterInfo(), () -> 0)); + new Plan( + config.clusterBean(), + config.clusterInfo(), + () -> 0, + config.clusterInfo(), + () -> 0)); } }; diff --git a/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java b/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java index 90ca3ca23b..b9de226662 100644 --- a/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java +++ b/gui/src/test/java/org/astraea/gui/tab/health/BalancerNodeTest.java @@ -30,6 +30,7 @@ import org.astraea.common.admin.Replica; import org.astraea.common.balancer.Balancer; import org.astraea.common.cost.ReplicaLeaderSizeCost; +import org.astraea.common.metrics.ClusterBean; import org.astraea.gui.Context; import org.astraea.gui.pane.Argument; import org.astraea.it.Service; @@ -150,6 +151,7 @@ void testResult() { var results = BalancerNode.assignmentResult( new Balancer.Plan( + ClusterBean.EMPTY, beforeClusterInfo, () -> 1.0D, ClusterInfo.of("fake", allNodes, Map.of(), afterReplicas), From 18d22d34a6b28bbb6900c1aac8784cf23aa7aa74 Mon Sep 17 00:00:00 2001 From: qoo332001 Date: Mon, 29 May 2023 23:43:25 +0800 Subject: [PATCH 2/2] merge main --- .../main/java/org/astraea/gui/tab/health/BalancerNode.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java b/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java index b2ccf6415d..563172af6e 100644 --- a/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java +++ b/gui/src/main/java/org/astraea/gui/tab/health/BalancerNode.java @@ -105,7 +105,9 @@ static List> costResult(Balancer.Plan plan) { }) .put(migrationCost.name, count))); - process.accept(MigrationCost.migrationCosts(plan.initialClusterInfo(), plan.proposal())); + process.accept( + MigrationCost.migrationCosts( + plan.initialClusterInfo(), plan.proposal(), plan.clusterBean())); return List.copyOf(map.values()); }