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