-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the interfaces of hard/soft constraints and a central place …
…to keep the softConstraint weights
- Loading branch information
Showing
5 changed files
with
88 additions
and
116 deletions.
There are no files selected for viewing
45 changes: 0 additions & 45 deletions
45
...x-core/src/main/java/org/apache/helix/controller/rebalancer/waged/RebalanceAlgorithm.java
This file was deleted.
Oops, something went wrong.
49 changes: 0 additions & 49 deletions
49
...g/apache/helix/controller/rebalancer/waged/constraints/ConstraintsRebalanceAlgorithm.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
47 changes: 47 additions & 0 deletions
47
...a/org/apache/helix/controller/rebalancer/waged/constraints/SoftConstraintWeightModel.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; | ||
|
||
import java.util.Map; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** | ||
* The class retrieves the offline model that defines the relative importance of soft constraints. | ||
*/ | ||
class SoftConstraintWeightModel { | ||
private float MIN_SCORE = Float.MIN_VALUE; | ||
private float MAX_SCORE = Float.MAX_VALUE; | ||
private static Map<SoftConstraint.Type, Float> MODEL; | ||
|
||
static { | ||
// TODO either define the weights in property files or zookeeper node or static human input | ||
MODEL = ImmutableMap.<SoftConstraint.Type, Float> builder() | ||
.put(SoftConstraint.Type.LEAST_MOVEMENTS, 1.0f) | ||
.put(SoftConstraint.Type.LEAST_PARTITION_COUNT, 1.0f) | ||
.put(SoftConstraint.Type.LEAST_USED_NODE, 1.0f).build(); | ||
} | ||
|
||
interface ScoreScaler { | ||
/** | ||
* Method to scale the origin score to a normalized range | ||
* @param originScore The origin score of a range | ||
* @return The normalized value between 0 - 1 | ||
*/ | ||
float scale(float originScore); | ||
} | ||
|
||
private ScoreScaler MIN_MAX_SCALER = | ||
originScore -> (originScore - MIN_SCORE) / (MAX_SCORE - MIN_SCORE); | ||
|
||
/** | ||
* Given the calculated scores map by soft constraints, get the sum of scores | ||
* @param originScoresMap The origin scores of soft constraints | ||
* @return The sum of double type | ||
*/ | ||
double getSumOfScores(Map<SoftConstraint, Float> originScoresMap) { | ||
return originScoresMap.entrySet().stream().map(constraintToOriginScore -> { | ||
float score = MIN_MAX_SCALER.scale(constraintToOriginScore.getValue()); | ||
float weight = MODEL.get(constraintToOriginScore.getKey().getType()); | ||
return score * weight; | ||
}).mapToDouble(i -> i).sum(); | ||
} | ||
} |