1 package org.gffs.network;
3 import java.util.HashMap;
5 import org.apache.commons.lang3.builder.HashCodeBuilder;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
21 public static class ClientProperties {
22 int timeout = 2 * 60 * 1000;
27 static public ClientProperties
props;
31 static private final int HOW_MANY_DOWNS_ALLOWED = 1;
37 static private final int MAXIMUM_ALLOWABLE_CHECKING_DELAY = 60 * 1000 * 5;
39 public static class HostKey
41 public String hostname;
44 HostKey(String hostname,
int port)
46 this.hostname = hostname;
53 return new HashCodeBuilder(37, 839).
55 append(hostname).append(port).toHashCode();
59 public boolean equals(Object o)
61 if (!(o instanceof HostKey))
63 HostKey realo = (HostKey) o;
64 return realo.hostname.equals(hostname) && (realo.port == port);
68 public String toString()
70 return hostname +
":" + port;
74 static final HashMap<HostKey, RetryInfo> deadHosts =
new HashMap<HostKey, RetryInfo>();
76 public static class RetryInfo
80 public int downCount = 0;
85 delay = initialDelay();
86 nextTime = System.currentTimeMillis() + delay;
91 return props.timeout / 2;
94 boolean isThisHostDead()
96 if (downCount < HOW_MANY_DOWNS_ALLOWED) {
99 if (System.currentTimeMillis() > nextTime) {
101 nextTime = System.currentTimeMillis() + delay;
118 HostKey key =
new HostKey(host, port);
121 synchronized (deadHosts) {
122 if (deadHosts.containsKey(host)) {
123 RetryInfo inf = deadHosts.get(key);
125 _logger.warn(
"logic error: dead hosts list said it had host " + key +
" was listed but we got a null record for it.");
128 return !inf.isThisHostDead();
131 if (_logger.isTraceEnabled())
132 _logger.debug(
"host " + key +
" is fine as far as we know.");
140 HostKey key =
new HostKey(host, port);
142 synchronized (deadHosts) {
143 RetryInfo inf = deadHosts.get(key);
146 inf =
new RetryInfo();
147 deadHosts.put(key, inf);
150 boolean alreadyDead =
false;
151 if (inf.isThisHostDead()) {
153 if (_logger.isDebugEnabled())
154 _logger.warn(
"host " + key +
" is considered dead already; increasing delay.");
156 inf.nextTime = System.currentTimeMillis() + inf.delay;
158 if (inf.delay > MAXIMUM_ALLOWABLE_CHECKING_DELAY) {
159 inf.delay = MAXIMUM_ALLOWABLE_CHECKING_DELAY;
168 if (!inf.isThisHostDead()) {
170 if (_logger.isDebugEnabled())
171 _logger.debug(
"host " + key +
" is not dead yet but suffered a connection problem.");
174 if (!alreadyDead && _logger.isDebugEnabled())
175 _logger.warn(
"host " + key +
" is newly considered dead due to communication problems.");
182 HostKey key =
new HostKey(host, port);
185 synchronized (deadHosts) {
186 if (deadHosts.containsKey(key)) {
187 if (_logger.isDebugEnabled()) {
189 _logger.debug(
"host " + key +
" is being removed from dead host pool.");
192 deadHosts.remove(key);
static void addHostToDeadPool(String host, int port)
static ClientProperties props
static boolean evaluateHostAlive(String host, int port)
static void removeHostFromDeadPool(String host, int port)