Skip to content

Commit

Permalink
Add soft constraint: ResourcetopStateAntiAffinityConstraint (apache#465)
Browse files Browse the repository at this point in the history
Add ResourcetopStateAntiAffinityConstraint

The more total top state partitions assigned to the instance, the lower the score, vice versa.
  • Loading branch information
i3wangyi authored and jiajunwang committed Feb 7, 2020
1 parent 03d0d80 commit 291451a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public Set<String> getAssignedTopStatePartitionsByResource(String resource) {
/**
* @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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 291451a

Please sign in to comment.