You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by ji...@apache.org on 2019/10/07 21:12:27 UTC
[helix] 24/37: Add soft constraint:
ResourcetopStateAntiAffinityConstraint (#465)
This is an automated email from the ASF dual-hosted git repository.
jiajunwang pushed a commit to branch wagedRebalancer2
in repository https://gitbox.apache.org/repos/asf/helix.git
commit 153d38596184f9aefaa676ad4ceddde52f2969f3
Author: Yi Wang <i3...@gmail.com>
AuthorDate: Wed Sep 11 19:22:00 2019 -0700
Add soft constraint: ResourcetopStateAntiAffinityConstraint (#465)
Add ResourcetopStateAntiAffinityConstraint
The more total top state partitions assigned to the instance, the lower the score, vice versa.
---
.../ResourceTopStateAntiAffinityConstraint.java | 46 +++++++++++++++
.../rebalancer/waged/model/AssignableNode.java | 6 +-
...TestResourceTopStateAntiAffinityConstraint.java | 65 ++++++++++++++++++++++
3 files changed, 114 insertions(+), 3 deletions(-)
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/constraints/ResourceTopStateAntiAffinityConstraint.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/constraints/ResourceTopStateAntiAffinityConstraint.java
new file mode 100644
index 0000000..b1e64b9
--- /dev/null
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/constraints/ResourceTopStateAntiAffinityConstraint.java
@@ -0,0 +1,46 @@
+package org.apache.helix.controller.rebalancer.waged.constraints;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.helix.controller.rebalancer.waged.model.AssignableNode;
+import org.apache.helix.controller.rebalancer.waged.model.AssignableReplica;
+import org.apache.helix.controller.rebalancer.waged.model.ClusterContext;
+
+/**
+ * Evaluate the proposed assignment according to the top state replication count on the instance.
+ * The higher number the number of top state partitions assigned to the instance, the lower the
+ * score, vice versa.
+ */
+public class ResourceTopStateAntiAffinityConstraint extends SoftConstraint {
+ @Override
+ protected float getAssignmentScore(AssignableNode node, AssignableReplica replica,
+ ClusterContext clusterContext) {
+ if (!replica.isReplicaTopState()) {
+ return (getMaxScore() + getMinScore()) / 2;
+ }
+
+ int curTopPartitionCountForResource = node.getAssignedTopStatePartitionsCount();
+ int doubleMaxTopStateCount = 2 * clusterContext.getEstimatedMaxTopStateCount();
+
+ return Math.max(
+ ((float) doubleMaxTopStateCount - curTopPartitionCountForResource) / doubleMaxTopStateCount,
+ 0);
+ }
+}
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/model/AssignableNode.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/model/AssignableNode.java
index f25c289..6966353 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/model/AssignableNode.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/model/AssignableNode.java
@@ -187,10 +187,10 @@ public class AssignableNode implements Comparable<AssignableNode> {
/**
* @return The total count of assigned top state partitions.
*/
- public long getAssignedTopStatePartitionsCount() {
- return _currentAssignedReplicaMap.values().stream()
+ public int getAssignedTopStatePartitionsCount() {
+ return (int) _currentAssignedReplicaMap.values().stream()
.flatMap(replicaMap -> replicaMap.values().stream())
- .filter(replica -> replica.isReplicaTopState()).count();
+ .filter(AssignableReplica::isReplicaTopState).count();
}
/**
diff --git a/helix-core/src/test/java/org/apache/helix/controller/rebalancer/waged/constraints/TestResourceTopStateAntiAffinityConstraint.java b/helix-core/src/test/java/org/apache/helix/controller/rebalancer/waged/constraints/TestResourceTopStateAntiAffinityConstraint.java
new file mode 100644
index 0000000..06ef537
--- /dev/null
+++ b/helix-core/src/test/java/org/apache/helix/controller/rebalancer/waged/constraints/TestResourceTopStateAntiAffinityConstraint.java
@@ -0,0 +1,65 @@
+package org.apache.helix.controller.rebalancer.waged.constraints;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+import org.apache.helix.controller.rebalancer.waged.model.AssignableNode;
+import org.apache.helix.controller.rebalancer.waged.model.AssignableReplica;
+import org.apache.helix.controller.rebalancer.waged.model.ClusterContext;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestResourceTopStateAntiAffinityConstraint {
+ private final AssignableReplica _testReplica = Mockito.mock(AssignableReplica.class);
+ private final AssignableNode _testNode = Mockito.mock(AssignableNode.class);
+ private final ClusterContext _clusterContext = Mockito.mock(ClusterContext.class);
+
+ private final SoftConstraint _constraint = new ResourceTopStateAntiAffinityConstraint();
+
+ @Test
+ public void testGetAssignmentScoreWhenReplicaNotTopState() {
+ when(_testReplica.isReplicaTopState()).thenReturn(false);
+ float score = _constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
+ Assert.assertEquals(score, (_constraint.getMaxScore() + _constraint.getMinScore()) / 2);
+ verifyZeroInteractions(_testNode);
+ verifyZeroInteractions(_clusterContext);
+ }
+
+ @Test
+ public void testGetAssignmentScoreWhenReplicaIsTopStateHeavyLoad() {
+ when(_testReplica.isReplicaTopState()).thenReturn(true);
+ when(_testNode.getAssignedTopStatePartitionsCount()).thenReturn(20);
+ when(_clusterContext.getEstimatedMaxTopStateCount()).thenReturn(20);
+ float score = _constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
+ Assert.assertEquals(score, 0.5f);
+ }
+
+ @Test
+ public void testGetAssignmentScoreWhenReplicaIsTopStateLightLoad() {
+ when(_testReplica.isReplicaTopState()).thenReturn(true);
+ when(_testNode.getAssignedTopStatePartitionsCount()).thenReturn(0);
+ when(_clusterContext.getEstimatedMaxTopStateCount()).thenReturn(20);
+ float score = _constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
+ Assert.assertEquals(score, 1f);
+ }
+}