forked from apache/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement one of the soft constraints (apache#450)
Implement Instance Partitions Count soft constraint. Evaluate by instance's current partition count versus estimated max partition count. Intuitively, Encourage the assignment if the instance's occupancy rate is below average; Discourage the assignment if the instance's occupancy rate is above average. The final normalized score will be within [0, 1]. The implementation of the class will depend on the cluster current total partitions count as the max score.
- Loading branch information
1 parent
bbae20d
commit 03d0d80
Showing
9 changed files
with
196 additions
and
80 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
47 changes: 47 additions & 0 deletions
47
...ache/helix/controller/rebalancer/waged/constraints/InstancePartitionsCountConstraint.java
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,47 @@ | ||
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 by instance's current partition count versus estimated max partition count | ||
* Intuitively, Encourage the assignment if the instance's occupancy rate is below average; | ||
* Discourage the assignment if the instance's occupancy rate is above average | ||
*/ | ||
class InstancePartitionsCountConstraint extends SoftConstraint { | ||
private static final float MAX_SCORE = 1f; | ||
private static final float MIN_SCORE = 0f; | ||
|
||
InstancePartitionsCountConstraint() { | ||
super(MAX_SCORE, MIN_SCORE); | ||
} | ||
|
||
@Override | ||
protected float getAssignmentScore(AssignableNode node, AssignableReplica replica, | ||
ClusterContext clusterContext) { | ||
float doubleEstimatedMaxPartitionCount = 2 * clusterContext.getEstimatedMaxPartitionCount(); | ||
float currentPartitionCount = node.getAssignedReplicaCount(); | ||
return Math.max((doubleEstimatedMaxPartitionCount - currentPartitionCount) | ||
/ doubleEstimatedMaxPartitionCount, 0); | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
...g/apache/helix/controller/rebalancer/waged/constraints/LeastPartitionCountConstraint.java
This file was deleted.
Oops, something went wrong.
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
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
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
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
60 changes: 60 additions & 0 deletions
60
.../helix/controller/rebalancer/waged/constraints/TestInstancePartitionsCountConstraint.java
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,60 @@ | ||
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.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 TestInstancePartitionsCountConstraint { | ||
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 InstancePartitionsCountConstraint(); | ||
|
||
@Test | ||
public void testWhenInstanceIsIdle() { | ||
when(_testNode.getAssignedReplicaCount()).thenReturn(0); | ||
float score = _constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext); | ||
Assert.assertEquals(score, 1.0f); | ||
} | ||
|
||
@Test | ||
public void testWhenInstanceIsFull() { | ||
when(_testNode.getAssignedReplicaCount()).thenReturn(10); | ||
when(_clusterContext.getEstimatedMaxPartitionCount()).thenReturn(10); | ||
float score = _constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext); | ||
Assert.assertEquals(score, 0.5f); | ||
} | ||
|
||
@Test | ||
public void testWhenInstanceHalfOccupied() { | ||
when(_testNode.getAssignedReplicaCount()).thenReturn(10); | ||
when(_clusterContext.getEstimatedMaxPartitionCount()).thenReturn(20); | ||
float score = _constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext); | ||
Assert.assertEquals(score, 0.75f); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...he/helix/controller/rebalancer/waged/constraints/TestSoftConstraintNormalizeFunction.java
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,47 @@ | ||
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; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class TestSoftConstraintNormalizeFunction { | ||
@Test | ||
public void testDefaultNormalizeFunction() { | ||
int maxScore = 100; | ||
int minScore = 0; | ||
SoftConstraint softConstraint = new SoftConstraint(maxScore, minScore) { | ||
@Override | ||
protected float getAssignmentScore(AssignableNode node, AssignableReplica replica, | ||
ClusterContext clusterContext) { | ||
return 0; | ||
} | ||
}; | ||
|
||
for (int i = minScore; i <= maxScore; i++) { | ||
float normalized = softConstraint.getNormalizeFunction().scale(i); | ||
Assert.assertTrue(normalized <= 1 && normalized >= 0, | ||
String.format("input: %s, output: %s", i, normalized)); | ||
} | ||
} | ||
} |