provide missing module's name via IllegalArgumentException
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / LeastLoadedCandidateSelectionStrategy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Strings;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Map;
19 import javax.annotation.Nullable;
20
21 /**
22  * The LeastLoadedCandidateSelectionStrategy assigns ownership for an entity to the candidate which owns the least
23  * number of entities.
24  */
25 public class LeastLoadedCandidateSelectionStrategy extends AbstractEntityOwnerSelectionStrategy {
26     private final Map<String, Long> localStatistics = new HashMap<>();
27
28     protected LeastLoadedCandidateSelectionStrategy(long selectionDelayInMillis, Map<String, Long> initialStatistics) {
29         super(selectionDelayInMillis, initialStatistics);
30
31         localStatistics.putAll(initialStatistics);
32     }
33
34     @Override
35     @SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
36             justification = "Unrecognised NullableDecl")
37     public String newOwner(@Nullable String currentOwner, Collection<String> viableCandidates) {
38         Preconditions.checkArgument(viableCandidates.size() > 0);
39         String leastLoadedCandidate = null;
40         long leastLoadedCount = Long.MAX_VALUE;
41
42         if (!Strings.isNullOrEmpty(currentOwner)) {
43             long localVal = MoreObjects.firstNonNull(localStatistics.get(currentOwner), 0L);
44             localStatistics.put(currentOwner, localVal - 1);
45         }
46
47         for (String candidateName : viableCandidates) {
48             long val = MoreObjects.firstNonNull(localStatistics.get(candidateName), 0L);
49             if (val < leastLoadedCount) {
50                 leastLoadedCount = val;
51                 leastLoadedCandidate = candidateName;
52             }
53         }
54
55         if (leastLoadedCandidate == null) {
56             leastLoadedCandidate = viableCandidates.iterator().next();
57         }
58
59         localStatistics.put(leastLoadedCandidate, leastLoadedCount + 1);
60         return leastLoadedCandidate;
61     }
62
63     @VisibleForTesting
64     Map<String, Long> getLocalStatistics() {
65         return localStatistics;
66     }
67 }