aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/mr/client/HostSelector.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/dmaap/mr/client/HostSelector.java')
-rw-r--r--src/main/java/org/onap/dmaap/mr/client/HostSelector.java262
1 files changed, 120 insertions, 142 deletions
diff --git a/src/main/java/org/onap/dmaap/mr/client/HostSelector.java b/src/main/java/org/onap/dmaap/mr/client/HostSelector.java
index 9bd73f9..f872745 100644
--- a/src/main/java/org/onap/dmaap/mr/client/HostSelector.java
+++ b/src/main/java/org/onap/dmaap/mr/client/HostSelector.java
@@ -4,11 +4,13 @@
* ================================================================================
* Copyright © 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
+ * Modifications Copyright © 2021 Orange.
+ * ================================================================================
* 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.
@@ -17,182 +19,158 @@
* ============LICENSE_END=========================================================
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
- *
+ *
*******************************************************************************/
+
package org.onap.dmaap.mr.client;
+import java.security.SecureRandom;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
-import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
-import java.util.Vector;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class HostSelector
-{
- private final TreeSet<String> fBaseHosts;
- private final DelayQueue<BlacklistEntry> fBlacklist;
- private String fIdealHost;
- private String fCurrentHost;
- private static final Logger log = LoggerFactory.getLogger(HostSelector.class);
-
- public HostSelector(String hostPart)
- {
- this(makeSet(hostPart), null);
- }
-
- public HostSelector(Collection<String> baseHosts)
- {
- this(baseHosts, null);
- }
-
- public HostSelector(Collection<String> baseHosts, String signature)
- {
- if (baseHosts.isEmpty())
- {
- throw new IllegalArgumentException("At least one host must be provided.");
- }
+public class HostSelector {
+ private final TreeSet<String> baseHosts;
+ private final DelayQueue<BlacklistEntry> blacklist;
+ private String idealHost;
+ private String currentHost;
+ private static final Logger logger = LoggerFactory.getLogger(HostSelector.class);
- this.fBaseHosts = new TreeSet(baseHosts);
- this.fBlacklist = new DelayQueue();
- this.fIdealHost = null;
-
- if (signature == null) {
- return;
- }
- int index = 0 ;
- int value = signature.hashCode();
- if(value!=0) {
- index = Math.abs(value) % baseHosts.size();
- }
- Iterator it = this.fBaseHosts.iterator();
- while (index-- > 0)
- {
- it.next();
- }
- this.fIdealHost = ((String)it.next());
- }
-
- public String selectBaseHost()
- {
- if (this.fCurrentHost == null)
- {
- makeSelection();
+ public HostSelector(String hostPart) {
+ this(makeSet(hostPart), null);
}
- return this.fCurrentHost;
- }
-
- public void reportReachabilityProblem(long blacklistUnit, TimeUnit blacklistTimeUnit)
- {
- if (this.fCurrentHost == null)
- {
- log.warn("Reporting reachability problem, but no host is currently selected.");
+
+ public HostSelector(Collection<String> baseHosts) {
+ this(baseHosts, null);
}
- if (blacklistUnit > 0L)
- {
- for (BlacklistEntry be : this.fBlacklist)
- {
- if (be.getHost().equals(this.fCurrentHost))
- {
- be.expireNow();
+ public HostSelector(Collection<String> baseHosts, String signature) {
+ if (baseHosts.isEmpty()) {
+ throw new IllegalArgumentException("At least one host must be provided.");
}
- }
- LinkedList devNull = new LinkedList();
- this.fBlacklist.drainTo(devNull);
+ this.baseHosts = new TreeSet<>(baseHosts);
+ this.blacklist = new DelayQueue<>();
+ this.idealHost = null;
- if (this.fCurrentHost != null)
- {
- this.fBlacklist.add(new BlacklistEntry(this.fCurrentHost, TimeUnit.MILLISECONDS.convert(blacklistUnit, blacklistTimeUnit)));
- }
- }
- this.fCurrentHost = null;
- }
-
- private String makeSelection()
- {
- TreeSet workingSet = new TreeSet(this.fBaseHosts);
-
- LinkedList devNull = new LinkedList();
- this.fBlacklist.drainTo(devNull);
- for (BlacklistEntry be : this.fBlacklist)
- {
- workingSet.remove(be.getHost());
+ if (signature == null) {
+ return;
+ }
+ int index = 0;
+ int value = signature.hashCode();
+ if (value != 0) {
+ index = Math.abs(value) % baseHosts.size();
+ }
+ Iterator<String> it = this.baseHosts.iterator();
+ while (index-- > 0) {
+ it.next();
+ }
+ this.idealHost = (it.next());
}
- if (workingSet.isEmpty())
- {
- log.warn("All hosts were blacklisted; reverting to full set of hosts.");
- workingSet.addAll(this.fBaseHosts);
- this.fCurrentHost = null;
+ public String selectBaseHost() {
+ if (this.currentHost == null) {
+ makeSelection();
+ }
+ return this.currentHost;
}
- String selection = null;
- if ((this.fCurrentHost != null) && (workingSet.contains(this.fCurrentHost)))
- {
- selection = this.fCurrentHost;
- }
- else if ((this.fIdealHost != null) && (workingSet.contains(this.fIdealHost)))
- {
- selection = this.fIdealHost;
- }
- else
- {
- int index = 0;
- int value = new Random().nextInt();
- Vector v = new Vector(workingSet);
- if(value!=0) {
- index = Math.abs(value) % workingSet.size();
- }
- selection = (String)v.elementAt(index);
+ public void reportReachabilityProblem(long blacklistUnit, TimeUnit blacklistTimeUnit) {
+ if (this.currentHost == null) {
+ logger.warn("Reporting reachability problem, but no host is currently selected.");
+ }
+
+ if (blacklistUnit > 0L) {
+ for (BlacklistEntry be : this.blacklist) {
+ if (be.getHost().equals(this.currentHost)) {
+ be.expireNow();
+ }
+ }
+
+ LinkedList<Delayed> devNull = new LinkedList<>();
+ this.blacklist.drainTo(devNull);
+
+ if (this.currentHost != null) {
+ this.blacklist.add(new BlacklistEntry(this.currentHost,
+ TimeUnit.MILLISECONDS.convert(blacklistUnit, blacklistTimeUnit)));
+ }
+ }
+ this.currentHost = null;
}
- this.fCurrentHost = selection;
- return this.fCurrentHost;
- }
+ private String makeSelection() {
+ TreeSet<String> workingSet = new TreeSet<>(this.baseHosts);
- private static Set<String> makeSet(String s)
- {
- TreeSet set = new TreeSet();
- set.add(s);
- return set; }
+ LinkedList<Delayed> devNull = new LinkedList<>();
+ this.blacklist.drainTo(devNull);
+ for (BlacklistEntry be : this.blacklist) {
+ workingSet.remove(be.getHost());
+ }
- private static class BlacklistEntry implements Delayed {
- private final String fHost;
- private long fExpireAtMs;
+ if (workingSet.isEmpty()) {
+ logger.warn("All hosts were blacklisted; reverting to full set of hosts.");
+ workingSet.addAll(this.baseHosts);
+ this.currentHost = null;
+ }
- public BlacklistEntry(String host, long delayMs) {
- this.fHost = host;
- this.fExpireAtMs = (System.currentTimeMillis() + delayMs);
- }
+ String selection = null;
+ if ((this.currentHost != null) && (workingSet.contains(this.currentHost))) {
+ selection = this.currentHost;
+ } else if ((this.idealHost != null) && (workingSet.contains(this.idealHost))) {
+ selection = this.idealHost;
+ } else {
+ int index = 0;
+ int value = new SecureRandom().nextInt();
+ ArrayList<String> workingArray = new ArrayList<>(workingSet);
+ if (value != 0) {
+ index = Math.abs(value) % workingSet.size();
+ }
+ selection = workingArray.get(index);
+ }
- public void expireNow()
- {
- this.fExpireAtMs = 0L;
+ this.currentHost = selection;
+ return this.currentHost;
}
- public String getHost()
- {
- return this.fHost;
+ private static Set<String> makeSet(String firstTreeElem) {
+ TreeSet<String> set = new TreeSet<>();
+ set.add(firstTreeElem);
+ return set;
}
- public int compareTo(Delayed o)
- {
- Long thisDelay = Long.valueOf(getDelay(TimeUnit.MILLISECONDS));
- return thisDelay.compareTo(Long.valueOf(o.getDelay(TimeUnit.MILLISECONDS)));
- }
+ private static class BlacklistEntry implements Delayed {
+ private final String host;
+ private long expireAtMs;
- public long getDelay(TimeUnit unit)
- {
- long remainingMs = this.fExpireAtMs - System.currentTimeMillis();
- return unit.convert(remainingMs, TimeUnit.MILLISECONDS);
+ public BlacklistEntry(String host, long delayMs) {
+ this.host = host;
+ this.expireAtMs = (System.currentTimeMillis() + delayMs);
+ }
+
+ public void expireNow() {
+ this.expireAtMs = 0L;
+ }
+
+ public String getHost() {
+ return this.host;
+ }
+
+ public int compareTo(Delayed object) {
+ Long thisDelay = getDelay(TimeUnit.MILLISECONDS);
+ return thisDelay.compareTo(object.getDelay(TimeUnit.MILLISECONDS));
+ }
+
+ public long getDelay(TimeUnit unit) {
+ long remainingMs = this.expireAtMs - System.currentTimeMillis();
+ return unit.convert(remainingMs, TimeUnit.MILLISECONDS);
+ }
}
- }
}