1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import java.net.InetSocketAddress;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
31 import org.apache.hadoop.hbase.util.Pair;
32
33
34
35
36 @InterfaceAudience.Private
37 public class FailedServers {
38 private final LinkedList<Pair<Long, String>> failedServers = new
39 LinkedList<Pair<Long, String>>();
40 private final int recheckServersTimeout;
41 private static final Log LOG = LogFactory.getLog(FailedServers.class);
42
43 public FailedServers(Configuration conf) {
44 this.recheckServersTimeout = conf.getInt(
45 RpcClient.FAILED_SERVER_EXPIRY_KEY, RpcClient.FAILED_SERVER_EXPIRY_DEFAULT);
46 }
47
48
49
50
51 public synchronized void addToFailedServers(InetSocketAddress address, Throwable throwable) {
52 final long expiry = EnvironmentEdgeManager.currentTime() + recheckServersTimeout;
53 failedServers.addFirst(new Pair<Long, String>(expiry, address.toString()));
54 if (LOG.isDebugEnabled()) {
55 LOG.debug(
56 "Added failed server with address " + address.toString() + " to list caused by "
57 + throwable.toString());
58 }
59 }
60
61
62
63
64
65
66 public synchronized boolean isFailedServer(final InetSocketAddress address) {
67 if (failedServers.isEmpty()) {
68 return false;
69 }
70
71 final String lookup = address.toString();
72 final long now = EnvironmentEdgeManager.currentTime();
73
74
75 Iterator<Pair<Long, String>> it = failedServers.iterator();
76 while (it.hasNext()) {
77 Pair<Long, String> cur = it.next();
78 if (cur.getFirst() < now) {
79 it.remove();
80 } else {
81 if (lookup.equals(cur.getSecond())) {
82 return true;
83 }
84 }
85 }
86
87 return false;
88 }
89 }