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: ResourcePartitionAntiAffinityConstraint #464

Merged
merged 2 commits into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 other partitions assignment within the scope of
i3wangyi marked this conversation as resolved.
Show resolved Hide resolved
* the same resource.
* If the resource of the partition overall has a light load on the instance, the score is higher compared to
* case when the resource is heavily loaded on the instance
*/
public class ResourcePartitionAntiAffinityConstraint extends SoftConstraint {

@Override
protected float getAssignmentScore(AssignableNode node, AssignableReplica replica,
ClusterContext clusterContext) {
String resource = replica.getResourceName();
int curPartitionCountForResource = node.getAssignedPartitionsByResource(resource).size();
i3wangyi marked this conversation as resolved.
Show resolved Hide resolved
int doubleMaxPartitionCountForResource =
2 * clusterContext.getEstimatedMaxPartitionByResource(resource);
return Math.max(((float) doubleMaxPartitionCountForResource - curPartitionCountForResource)
i3wangyi marked this conversation as resolved.
Show resolved Hide resolved
/ doubleMaxPartitionCountForResource, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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;

import com.google.common.collect.ImmutableSet;

import java.util.Collections;

public class TestResourcePartitionAntiAffinityConstraint {
i3wangyi marked this conversation as resolved.
Show resolved Hide resolved
private static final String TEST_PARTITION = "TestPartition";
private static final String TEST_RESOURCE = "TestResource";
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 ResourcePartitionAntiAffinityConstraint();

@Test
public void testGetAssignmentScore() {
when(_testReplica.getResourceName()).thenReturn(TEST_RESOURCE);
when(_testNode.getAssignedPartitionsByResource(TEST_RESOURCE)).thenReturn(
ImmutableSet.of(TEST_PARTITION + "1", TEST_PARTITION + "2", TEST_PARTITION + "3"));
when(_clusterContext.getEstimatedMaxPartitionByResource(TEST_RESOURCE)).thenReturn(10);

float score = _constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
Assert.assertEquals(score, 0.85f);
}

@Test
public void testGetAssignmentScoreMaxScore() {
when(_testReplica.getResourceName()).thenReturn(TEST_RESOURCE);
when(_testNode.getAssignedPartitionsByResource(TEST_RESOURCE)).thenReturn(Collections.emptySet());
when(_clusterContext.getEstimatedMaxPartitionByResource(TEST_RESOURCE)).thenReturn(10);

float score = _constraint.getAssignmentScore(_testNode, _testReplica, _clusterContext);
Assert.assertEquals(score, 1f);
}
}