X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fentityownership%2Fselectionstrategy%2FLeastLoadedCandidateSelectionStrategy.java;h=7102cbbbf096be52bcc1656b39e162e6ce7bf796;hb=8f30cbc0ba2676e883faadba10f757881983b2d0;hp=9ebf21cdcec1be10479cc5674219455fae42502b;hpb=1c66a277c9791984fab3b70f8e93d61795b2fe15;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategy.java index 9ebf21cdce..7102cbbbf0 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategy.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/LeastLoadedCandidateSelectionStrategy.java @@ -8,34 +8,57 @@ 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 java.util.Collection; +import java.util.HashMap; import java.util.Map; +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 { - protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis) { - super(selectionDelayInMillis); + private final Map localStatistics = new HashMap<>(); + + protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis, Map initialStatistics) { + super(selectionDelayInMillis, initialStatistics); + + localStatistics.putAll(initialStatistics); } @Override - public String newOwner(Collection viableCandidates, Map statistics) { + public String newOwner(@Nullable String currentOwner, Collection viableCandidates) { + Preconditions.checkArgument(viableCandidates.size() > 0); String leastLoadedCandidate = null; long leastLoadedCount = Long.MAX_VALUE; - for(String candidateName : viableCandidates){ - Long val = statistics.get(candidateName); - if(val != null && 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){ - return viableCandidates.iterator().next(); + if (leastLoadedCandidate == null) { + leastLoadedCandidate = viableCandidates.iterator().next(); } + + localStatistics.put(leastLoadedCandidate, leastLoadedCount + 1); return leastLoadedCandidate; } + + @VisibleForTesting + Map getLocalStatistics() { + return localStatistics; + } }