Skip to content

Commit

Permalink
Implement MaxCapacityUsageInstanceConstraint soft constraint (#463)
Browse files Browse the repository at this point in the history
The constraint evaluates the score by checking the max used capacity key out of all the capacity
keys.
The higher the maximum usage value for the capacity key, the lower the score will be, implying
that it is that much less desirable to assign anything on the given node.
It is a greedy approach since it evaluates only the most used capacity key.
  • Loading branch information
i3wangyi authored and Jiajun Wang committed Jan 6, 2020
1 parent 0026ce1 commit 94cc6ec
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* 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 normalized score will be within [0, 1]
*/
class InstancePartitionsCountConstraint extends SoftConstraint {
private static final float MAX_SCORE = 1f;
Expand Down
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;

/**
* The constraint evaluates the score by checking the max used capacity key out of all the capacity
* keys.
* The higher the maximum usage value for the capacity key, the lower the score will be, implying
* that it is that much less desirable to assign anything on the given node.
* It is a greedy approach since it evaluates only on the most used capacity key.
*/
class MaxCapacityUsageInstanceConstraint extends SoftConstraint {
private static final float MIN_SCORE = 0;
private static final float MAX_SCORE = 1;

MaxCapacityUsageInstanceConstraint() {
super(MAX_SCORE, MIN_SCORE);
}

@Override
protected float getAssignmentScore(AssignableNode node, AssignableReplica replica,
ClusterContext clusterContext) {
float maxCapacityUsage = node.getHighestCapacityUtilization();
return 1.0f - maxCapacityUsage / 2.0f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import org.apache.helix.controller.rebalancer.waged.model.ClusterContext;

/**
* The "soft" constraint evaluates the optimality of an assignment by giving it a score of a scale of [minScore, maxScore]
* The "soft" constraint evaluates the optimality of an assignment by giving it a score of a scale
* of [minScore, maxScore]
* The higher the score, the better the assignment; Intuitively, the assignment is encouraged.
* The lower score the score, the worse the assignment; Intuitively, the assignment is penalized.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SoftConstraintWeightModel {
}

static {
//TODO update the weight
// TODO update the weight
MODEL = ImmutableMap.<Class, Float> builder().put(InstancePartitionsCountConstraint.class, 1.0f)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ public class TestInstancePartitionsCountConstraint {
@Test
public void testWhenInstanceIsIdle() {
when(_testNode.getAssignedReplicaCount()).thenReturn(0);
float score = _constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext);
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);
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);
float score =
_constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext);
Assert.assertEquals(score, 0.75f);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
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.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestMaxCapacityUsageInstanceConstraint {
private AssignableReplica _testReplica;
private AssignableNode _testNode;
private ClusterContext _clusterContext;
private final SoftConstraint _constraint = new MaxCapacityUsageInstanceConstraint();

@BeforeMethod
public void setUp() {
_testNode = mock(AssignableNode.class, CALLS_REAL_METHODS);
_testReplica = mock(AssignableReplica.class);
_clusterContext = mock(ClusterContext.class);
}

@Test
public void testGetNormalizedScore() {
when(_testNode.getHighestCapacityUtilization()).thenReturn(0.8f);
float score =
_constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
Assert.assertEquals(score, 0.6f);
float normalizedScore =
_constraint.getAssignmentNormalizedScore(_testNode, _testReplica, _clusterContext);
Assert.assertEquals(normalizedScore, 0.6f);
}
}

0 comments on commit 94cc6ec

Please sign in to comment.