1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import org.apache.hadoop.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.ServerName;
23 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
24 import org.apache.hadoop.hbase.util.Pair;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36
37
38
39 @InterfaceAudience.Private
40 public class DeadServer {
41
42
43
44
45
46
47
48 private final Map<ServerName, Long> deadServers = new HashMap<ServerName, Long>();
49
50
51
52
53 private int numProcessing = 0;
54
55
56
57
58
59
60
61
62
63 public synchronized boolean cleanPreviousInstance(final ServerName newServerName) {
64 Iterator<ServerName> it = deadServers.keySet().iterator();
65 while (it.hasNext()) {
66 ServerName sn = it.next();
67 if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
68 it.remove();
69 return true;
70 }
71 }
72
73 return false;
74 }
75
76
77
78
79
80 public synchronized boolean isDeadServer(final ServerName serverName) {
81 return deadServers.containsKey(serverName);
82 }
83
84
85
86
87
88
89
90
91 public synchronized boolean areDeadServersInProgress() {
92 return numProcessing != 0;
93 }
94
95 public synchronized Set<ServerName> copyServerNames() {
96 Set<ServerName> clone = new HashSet<ServerName>(deadServers.size());
97 clone.addAll(deadServers.keySet());
98 return clone;
99 }
100
101
102
103
104
105 public synchronized void add(ServerName sn) {
106 this.numProcessing++;
107 if (!deadServers.containsKey(sn)){
108 deadServers.put(sn, EnvironmentEdgeManager.currentTimeMillis());
109 }
110 }
111
112 @SuppressWarnings("UnusedParameters")
113 public synchronized void finish(ServerName ignored) {
114 this.numProcessing--;
115 }
116
117 public synchronized int size() {
118 return deadServers.size();
119 }
120
121 public synchronized boolean isEmpty() {
122 return deadServers.isEmpty();
123 }
124
125 public synchronized void cleanAllPreviousInstances(final ServerName newServerName) {
126 Iterator<ServerName> it = deadServers.keySet().iterator();
127 while (it.hasNext()) {
128 ServerName sn = it.next();
129 if (ServerName.isSameHostnameAndPort(sn, newServerName)) {
130 it.remove();
131 }
132 }
133 }
134
135 public synchronized String toString() {
136 StringBuilder sb = new StringBuilder();
137 for (ServerName sn : deadServers.keySet()) {
138 if (sb.length() > 0) {
139 sb.append(", ");
140 }
141 sb.append(sn.toString());
142 }
143 return sb.toString();
144 }
145
146
147
148
149
150
151 public synchronized List<Pair<ServerName, Long>> copyDeadServersSince(long ts){
152 List<Pair<ServerName, Long>> res = new ArrayList<Pair<ServerName, Long>>(size());
153
154 for (Map.Entry<ServerName, Long> entry:deadServers.entrySet()){
155 if (entry.getValue() >= ts){
156 res.add(new Pair<ServerName, Long>(entry.getKey(), entry.getValue()));
157 }
158 }
159
160 Collections.sort(res, ServerNameDeathDateComparator);
161 return res;
162 }
163
164 private static Comparator<Pair<ServerName, Long>> ServerNameDeathDateComparator =
165 new Comparator<Pair<ServerName, Long>>(){
166
167 @Override
168 public int compare(Pair<ServerName, Long> o1, Pair<ServerName, Long> o2) {
169 return o1.getSecond().compareTo(o2.getSecond());
170 }
171 };
172 }