com.netflix.fenzo.ScaleDownConstraintExecutor Maven / Gradle / Ivy
/*
* Copyright 2017 Netflix, Inc.
*
* Licensed 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.
*/
package com.netflix.fenzo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* {@link ScaleDownConstraintExecutor} uses {@link ScaleDownOrderEvaluator} and multiple
* {@link ScaleDownConstraintEvaluator}s to determine termination order of candidate VM list. The evaluation process
* includes the following steps:
*
* - {@link ScaleDownOrderEvaluator} splits candidate VMs into ordered list of equivalence groups
* - For each equivalence group multiple {@link ScaleDownOrderEvaluator}s are executed to produce a score for each VM
* - VMs are ordered in each equivalence group with the highest score first
* - The ordered equivalence groups are merged into single list, preserving the partitioning order computed by
* {@link ScaleDownConstraintExecutor}. This list is returned as a result.
*
*
*/
class ScaleDownConstraintExecutor {
private static final Logger logger = LoggerFactory.getLogger(ScaleDownConstraintExecutor.class);
private static final double NOT_REMOVABLE_MARKER = -1;
private final ScaleDownOrderEvaluator orderEvaluator;
private final Map scoringEvaluators;
ScaleDownConstraintExecutor(ScaleDownOrderEvaluator orderEvaluator, Map weightedScoringEvaluators) {
checkArguments(weightedScoringEvaluators);
this.orderEvaluator = orderEvaluator;
this.scoringEvaluators = weightedScoringEvaluators;
}
List evaluate(Collection candidates) {
List> fixedOrder = orderEvaluator.evaluate(candidates);
List scaleDownOrder = scoringEvaluators.isEmpty()
? fixedOrder.stream().flatMap(Set::stream).collect(Collectors.toList())
: fixedOrder.stream().flatMap(this::groupEvaluator).collect(Collectors.toList());
if (logger.isDebugEnabled()) {
List hosts = scaleDownOrder.stream().map(VirtualMachineLease::hostname).collect(Collectors.toList());
logger.debug("Evaluated scale down order: {}", hosts);
}
return scaleDownOrder;
}
private void checkArguments(Map weightedScoringEvaluators) {
List evaluatorsWithInvalidWeights = weightedScoringEvaluators.entrySet().stream()
.filter(e -> e.getValue() <= 0)
.map(e -> e.getKey().getName() + '=' + e.getValue())
.collect(Collectors.toList());
if (!evaluatorsWithInvalidWeights.isEmpty()) {
throw new IllegalArgumentException("Evaluator weighs must be > 0. This invariant is violated by " + evaluatorsWithInvalidWeights);
}
}
private Stream groupEvaluator(Set groupCandidates) {
Map scores = new HashMap<>();
scoringEvaluators.forEach((e, weight) -> {
Optional super Object> optionalContext = Optional.empty();
for (VirtualMachineLease lease : groupCandidates) {
double currentScore = scores.getOrDefault(lease, 0.0);
if (currentScore != NOT_REMOVABLE_MARKER) {
ScaleDownConstraintEvaluator.Result result = e.evaluate(lease, optionalContext);
double newScore = result.getScore() * weight;
if (newScore == 0) {
scores.put(lease, NOT_REMOVABLE_MARKER);
} else {
scores.put(lease, currentScore + newScore);
}
optionalContext = result.getContext();
}
}
});
return scores.entrySet().stream()
.filter(e -> e.getValue() != NOT_REMOVABLE_MARKER)
.sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue())) // Descending order
.map(Map.Entry::getKey);
}
}