package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import javax.annotation.Nullable;
/**
* The LeastLoadedCandidateSelectionStrategy assigns ownership for an entity to the candidate which owns the least
* number of entities.
*/
public class LeastLoadedCandidateSelectionStrategy extends AbstractEntityOwnerSelectionStrategy {
- private static final Logger LOG = LoggerFactory.getLogger(LeastLoadedCandidateSelectionStrategy.class);
+ private final Map<String, Long> localStatistics = new HashMap<>();
- private Map<String, Long> localStatistics = new HashMap<>();
+ protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis, Map<String, Long> initialStatistics) {
+ super(selectionDelayInMillis, initialStatistics);
- protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis) {
- super(selectionDelayInMillis);
+ localStatistics.putAll(initialStatistics);
}
@Override
- public String newOwner(Collection<String> viableCandidates, Map<String, Long> statistics) {
+ @SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
+ justification = "Unrecognised NullableDecl")
+ public String newOwner(@Nullable String currentOwner, Collection<String> viableCandidates) {
+ Preconditions.checkArgument(viableCandidates.size() > 0);
String leastLoadedCandidate = null;
long leastLoadedCount = Long.MAX_VALUE;
- for(String candidateName : viableCandidates){
- long val = MoreObjects.firstNonNull(statistics.get(candidateName), 0L);
- long localVal = MoreObjects.firstNonNull(localStatistics.get(candidateName), 0L);
- if(val < localVal){
- LOG.debug("Local statistic higher - Candidate : {}, local statistic : {}, provided statistic : {}",
- candidateName, localVal, val);
- val = localVal;
- } else {
- LOG.debug("Provided statistic higher - Candidate : {}, local statistic : {}, provided statistic : {}",
- candidateName, localVal, val);
- localStatistics.put(candidateName, val);
- }
- if(val < leastLoadedCount){
+ if (!Strings.isNullOrEmpty(currentOwner)) {
+ long localVal = MoreObjects.firstNonNull(localStatistics.get(currentOwner), 0L);
+ localStatistics.put(currentOwner, localVal - 1);
+ }
+
+ for (String candidateName : viableCandidates) {
+ long val = MoreObjects.firstNonNull(localStatistics.get(candidateName), 0L);
+ if (val < leastLoadedCount) {
leastLoadedCount = val;
leastLoadedCandidate = candidateName;
}
}
- if(leastLoadedCandidate == null){
+ if (leastLoadedCandidate == null) {
leastLoadedCandidate = viableCandidates.iterator().next();
}
localStatistics.put(leastLoadedCandidate, leastLoadedCount + 1);
return leastLoadedCandidate;
}
+
+ @VisibleForTesting
+ Map<String, Long> getLocalStatistics() {
+ return localStatistics;
+ }
}