Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add soft constraint: ResourcetopStateAntiAffinityConstraint #465

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}